After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 585816 - GString doesn't have a g_string_replace() function
GString doesn't have a g_string_replace() function
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: gstring
unspecified
Other All
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2009-06-15 07:50 UTC by hcq0411
Modified: 2018-05-24 11:53 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement


Attachments
patch to gstring.c in glib-2.20.0 (1.60 KB, patch)
2009-06-15 08:08 UTC, hcq0411
none Details | Review
patch to gstring.h in glib-2.20.0 (520 bytes, patch)
2009-06-15 08:09 UTC, hcq0411
none Details | Review

Description hcq0411 2009-06-15 07:50:10 UTC
eg: replace all "12" in "125671289312345" with "ab" or "a" or "abcd"
my patches: (one to gstring.h, one to gstring.c in glib-2.20.0, code tested.)



--- gstring.h	2009-03-13 12:09:59.000000000 +0800
+++ gstring.h2	2009-06-15 15:40:48.000000000 +0800
@@ -119,6 +119,9 @@
 GString*     g_string_erase	        (GString	 *string,
 					 gssize		  pos,
 					 gssize		  len);
+GString*     g_string_replace 		(GString     	 *string,
+		  			 const gchar 	 *sub,
+		 			 const gchar 	 *repl);
 GString*     g_string_ascii_down        (GString	 *string);
 GString*     g_string_ascii_up          (GString	 *string);
 void         g_string_vprintf           (GString	 *string,



--- gstring.c	2009-06-15 15:41:25.000000000 +0800
+++ gstring.c2	2009-06-15 15:41:00.000000000 +0800
@@ -1219,6 +1219,69 @@
 }
 


 /**
+ * g_string_replace:
+ * @string: a #GString
+ * @sub: the substring target in #GString, to be replaced.
+ * @repl: the third string to replace sub.
+ *
+ * Replaces all instances of @sub from a #GString with @repl.
+ * if @sub is empty or no instance found in #GString, nothing will happen.
+ * if @repl is empty, it will erase all @sub (s) in #GString.
+ *
+ * Returns: @string
+ */
+GString*
+g_string_replace (GString     *string,
+		  const gchar *sub,
+		  const gchar *repl)
+{
+  gsize ls, lr;
+  gchar *q, *t;
+
+  g_return_val_if_fail (string != NULL, NULL);
+  g_return_val_if_fail (sub != NULL, NULL);
+  g_return_val_if_fail (repl != NULL, NULL);
+
+  q = string->str;
+  ls = strlen(sub);
+  lr = strlen(repl);
+
+  if (!ls)
+	  goto end;
+
+  if(ls > lr) {
+	 while(*q != '\0' && (t = strstr(q,sub)) != NULL) {
+		memcpy(t, repl, lr);
+		strcpy(t+lr, t+ls);
+		q = t + lr;
+		string->len -= ls-lr;
+	  } 
+  } else if(ls == lr) {
+	 while(*q != '\0' && (t = strstr(q,sub)) != NULL) {
+		memcpy(t, repl, lr);
+		q = t + lr;
+	  }
+  } else {
+	 while(*q != '\0' && (t = strstr(q,sub)) != NULL) {
+  		ptrdiff_t offt, offq;
+
+		offq = q - string->str;
+		offt = t - string->str;
+		g_string_maybe_expand(string, lr-ls);
+  		q = string->str + offq;
+		t = string->str + offt;
+		g_memmove(t+lr, t+ls, strlen(t+ls)+1);
+		memcpy(t, repl, lr);
+		q = t + lr;
+		string->len += lr-ls;
+  	  }
+  }
+	
+end:
+  return string;
+}
+
+/**
  * g_string_ascii_down:
  * @string: a GString
  *
Comment 1 hcq0411 2009-06-15 08:08:14 UTC
Created attachment 136607 [details] [review]
patch to gstring.c in glib-2.20.0
Comment 2 hcq0411 2009-06-15 08:09:15 UTC
Created attachment 136608 [details] [review]
patch to gstring.h in glib-2.20.0
Comment 3 Matthias Clasen 2011-12-21 16:06:09 UTC
I'm reluctant to agree with this kind of additions, since string functions are like lemmings... there's so many of them, and they are all very similar. Its a slippery slope; once we add this, surely people will want a regex replace version...
Comment 4 GNOME Infrastructure Team 2018-05-24 11:53:18 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/225.