[Xfce4-commits] <exo:master> Improve exo_str_replace() a bit and make it handle NULL strings better.
Nick Schermer
nick at xfce.org
Wed Aug 19 18:16:04 CEST 2009
Updating branch refs/heads/master
to ffe5750f096bfcfa2d88da01b341a631f712cea9 (commit)
from c8acc832c7635fd1bd29a5eb42f0d6f498d9d65d (commit)
commit ffe5750f096bfcfa2d88da01b341a631f712cea9
Author: Nick Schermer <nick at xfce.org>
Date: Wed Aug 19 18:13:14 2009 +0200
Improve exo_str_replace() a bit and make it handle NULL strings better.
exo/exo-string.c | 23 ++++++++++++-----------
1 files changed, 12 insertions(+), 11 deletions(-)
diff --git a/exo/exo-string.c b/exo/exo-string.c
index 1ab9818..480d49d 100644
--- a/exo/exo-string.c
+++ b/exo/exo-string.c
@@ -118,11 +118,13 @@ exo_str_is_equal (const gchar *a,
* g_free() when no longer needed.
*
* Note that @pattern and @replacement don't need to be of the
- * same size.
+ * same size. If @replacement is %NULL, the pattern will be
+ * removed from the string.
*
* Return value: a newly allocated copy of @str where all
* occurances of @pattern are replaced with
- * @replacement.
+ * @replacement. Or %NULL if @str and/or @pattern
+ * is %NULL.
*
* Since: 0.3.1.1
**/
@@ -134,16 +136,14 @@ exo_str_replace (const gchar *str,
const gchar *s, *p;
GString *result;
- g_return_val_if_fail (str != NULL, NULL);
- g_return_val_if_fail (pattern != NULL, NULL);
- g_return_val_if_fail (replacement != NULL, NULL);
-
- /* empty patterns are kinda useless, so we just return a copy of str */
- if (G_UNLIKELY (*pattern == '\0'))
+ /* an empty string or pattern is useless, so just
+ * return a copy of str */
+ if (G_UNLIKELY (exo_str_is_empty (str)
+ || exo_str_is_empty (pattern)))
return g_strdup (str);
/* allocate the result string */
- result = g_string_new (NULL);
+ result = g_string_sized_new (strlen (str));
/* process the input string */
while (*str != '\0')
@@ -155,10 +155,11 @@ exo_str_replace (const gchar *str,
if (*p == '\0' || *s == '\0')
break;
- /* check if the pattern matches */
+ /* check if the pattern fully matched */
if (G_LIKELY (*p == '\0'))
{
- g_string_append (result, replacement);
+ if (G_LIKELY (!exo_str_is_empty (replacement)))
+ g_string_append (result, replacement);
str = s;
continue;
}
More information about the Xfce4-commits
mailing list