PATCH: GString overwriting functions
- From: "Samuel Cormier-Iijima" <sciyoshi gmail com>
- To: gtk-devel-list gnome org
- Subject: PATCH: GString overwriting functions
- Date: Tue, 31 Oct 2006 23:03:58 -0500
Hi everyone,
I've cooked up a small patch that adds overwriting functionality to
GStrings. It's explained in more detail in the bug report:
http://bugzilla.gnome.org/show_bug.cgi?id=368686
This is the first patch I've submitted to such a visible project, so
of course your standards are going to be much higher. I've added docs
and test cases in order to try to make your lives easier :-P. Comments
are welcome, I'd like to become more active in the open source world.
Thanks,
Samuel Cormier-Iijima
Index: glib/gstring.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gstring.c,v
retrieving revision 1.49
diff -u -r1.49 gstring.c
--- glib/gstring.c 5 Dec 2005 15:38:52 -0000 1.49
+++ glib/gstring.c 1 Nov 2006 03:32:07 -0000
@@ -731,6 +731,80 @@
return string;
}
+/**
+ * g_string_overwrite:
+ * @string: a #GString
+ * @pos: the position at which to start overwriting
+ * @val: the string that will overwrite the @string starting at @pos
+ *
+ * Overwrites part of a string, lengthening it if necessary.
+ *
+ * Return value: @string
+ **/
+GString *
+g_string_overwrite (GString *string,
+ gssize pos,
+ const gchar *val)
+{
+ gsize len;
+
+ g_return_val_if_fail (string != NULL, NULL);
+ g_return_val_if_fail (val != NULL, string);
+ g_return_val_if_fail (pos >= 0, string);
+ g_return_val_if_fail (pos <= string->len, string);
+
+ len = strlen (val);
+
+ g_string_maybe_expand (string, pos + len - string->len);
+
+ memcpy (string->str + pos, val, len);
+
+ if (pos + len > string->len)
+ string->str[pos + len] = '\0';
+
+ string->len = MAX (string->len, pos + len);
+
+ return string;
+}
+
+/**
+ * g_string_overwrite_len:
+ * @string: a #GString
+ * @pos: the position at which to start overwriting
+ * @val: the string that will overwrite the @string starting at @pos
+ * @len: the number of bytes to write from @val
+ *
+ * Overwrites part of a string, lengthening it if necessary. This function
+ * will work with embedded NULLs.
+ *
+ * Return value: @string
+ **/
+GString *
+g_string_overwrite_len (GString *string,
+ gssize pos,
+ const gchar *val,
+ gsize len)
+{
+ g_return_val_if_fail (string != NULL, NULL);
+ g_return_val_if_fail (val != NULL, string);
+ g_return_val_if_fail (pos >= 0, string);
+ g_return_val_if_fail (pos <= string->len, string);
+
+ if (len < 0)
+ len = strlen (val);
+
+ g_string_maybe_expand (string, pos + len - string->len);
+
+ memcpy (string->str + pos, val, len);
+
+ if (pos + len > string->len)
+ string->str[pos + len] = '\0';
+
+ string->len = MAX (string->len, pos + len);
+
+ return string;
+}
+
GString*
g_string_erase (GString *string,
gssize pos,
Index: glib/gstring.h
===================================================================
RCS file: /cvs/gnome/glib/glib/gstring.h,v
retrieving revision 1.15
diff -u -r1.15 gstring.h
--- glib/gstring.h 2 Dec 2004 01:06:17 -0000 1.15
+++ glib/gstring.h 1 Nov 2006 03:32:07 -0000
@@ -104,6 +104,13 @@
GString* g_string_insert_unichar (GString *string,
gssize pos,
gunichar wc);
+GString* g_string_overwrite (GString *string,
+ gssize pos,
+ const gchar *val);
+GString* g_string_overwrite_len (GString *string,
+ gssize pos,
+ const gchar *val,
+ gsize len);
GString* g_string_erase (GString *string,
gssize pos,
gssize len);
Index: glib/glib.symbols
===================================================================
RCS file: /cvs/gnome/glib/glib/glib.symbols,v
retrieving revision 1.70
diff -u -r1.70 glib.symbols
--- glib/glib.symbols 9 Oct 2006 04:23:57 -0000 1.70
+++ glib/glib.symbols 1 Nov 2006 03:32:08 -0000
@@ -1092,6 +1092,8 @@
g_string_insert_unichar
g_string_new
g_string_new_len
+g_string_overwrite
+g_string_overwrite_len
g_string_prepend
g_string_prepend_c
g_string_prepend_len
Index: tests/string-test.c
===================================================================
RCS file: /cvs/gnome/glib/tests/string-test.c,v
retrieving revision 1.17
diff -u -r1.17 string-test.c
--- tests/string-test.c 18 Aug 2005 09:30:14 -0000 1.17
+++ tests/string-test.c 1 Nov 2006 03:32:08 -0000
@@ -257,6 +257,22 @@
g_assert (g_string_equal(string1, string2));
g_string_free (string1, TRUE);
g_string_free (string2, TRUE);
+
+ /* overwriting functions */
+ string1 = g_string_new ("testing");
+ string2 = g_string_new ("teshi there");
+ g_string_overwrite (string1, 3, "hi there");
+ g_assert (g_string_equal (string1, string2));
+ g_string_overwrite (string1, 4, "53");
+ g_string_free (string2, TRUE);
+ string2 = g_string_new ("tesh53there");
+ g_assert (g_string_equal (string1, string2));
+ g_string_overwrite_len (string1, 8, "blabla", 4);
+ g_string_free (string2, TRUE);
+ string2 = g_string_new ("tesh53thblab");
+ g_assert (g_string_equal (string1, string2));
+ g_string_free (string1, TRUE);
+ g_string_free (string2, TRUE);
/* Check handling of embedded ASCII 0 (NUL) characters in GString. */
string1 = g_string_new ("fiddle");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]