has_prefix and has_suffix proposal for glib 2.2



I propose this API addition for glib 2.2. I've had to write this a few 
times, and the version in eel is used a lot in nautilus.

Index: glib/gstrfuncs.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gstrfuncs.c,v
retrieving revision 1.82
diff -u -p -r1.82 gstrfuncs.c
--- glib/gstrfuncs.c	28 Mar 2002 23:24:21 -0000	1.82
+++ glib/gstrfuncs.c	3 May 2002 21:11:38 -0000
@@ -2270,3 +2270,59 @@ g_strrstr_len (const gchar *haystack,
 }
 
 
+/**
+ * g_str_has_suffix:
+ * @str: a nul-terminated string.
+ * @suffix: the nul-terminated suffix to look for.
+ *
+ * Looks whether the string @str ends with @suffix.
+ *
+ * Return value: TRUE if @str end with @suffix, FALSE otherwise.
+ **/
+gboolean
+g_str_has_suffix (const gchar  *str,
+		  const gchar  *suffix)
+{
+  int str_len;
+  int suffix_len;
+  
+  g_return_val_if_fail (str != NULL, FALSE);
+  g_return_val_if_fail (suffix != NULL, FALSE);
+
+  str_len = strlen (str);
+  suffix_len = strlen (suffix);
+
+  if (str_len < suffix_len)
+    return FALSE;
+
+  return strcmp (str + str_len - suffix_len, suffix) == 0;
+}
+
+/**
+ * g_str_has_prefix:
+ * @str: a nul-terminated string.
+ * @prefix: the nul-terminated prefix to look for.
+ *
+ * Looks whether the string @str begins with @prefix.
+ *
+ * Return value: TRUE if @str begins with @prefix, FALSE otherwise.
+ **/
+gboolean
+g_str_has_prefix (const gchar  *str,
+		  const gchar  *prefix)
+{
+  int str_len;
+  int prefix_len;
+  
+  g_return_val_if_fail (str != NULL, FALSE);
+  g_return_val_if_fail (prefix != NULL, FALSE);
+
+  str_len = strlen (str);
+  prefix_len = strlen (prefix);
+
+  if (str_len < prefix_len)
+    return FALSE;
+  
+  return strncmp (str, prefix, prefix_len) == 0;
+}
+
Index: glib/gstrfuncs.h
===================================================================
RCS file: /cvs/gnome/glib/glib/gstrfuncs.h,v
retrieving revision 1.17
diff -u -p -r1.17 gstrfuncs.h
--- glib/gstrfuncs.h	24 Oct 2001 18:00:10 -0000	1.17
+++ glib/gstrfuncs.h	3 May 2002 21:11:38 -0000
@@ -116,6 +116,11 @@ gchar *               g_strrstr_len    (
 					gssize        haystack_len,
 					const gchar  *needle);
 
+gboolean              g_str_has_suffix (const gchar  *str,
+					const gchar  *suffix);
+gboolean              g_str_has_prefix (const gchar  *str,
+					const gchar  *prefix);
+
 /* String to/from double conversion functions */
 
 gdouble	              g_strtod         (const gchar  *nptr,
Index: tests/string-test.c
===================================================================
RCS file: /cvs/gnome/glib/tests/string-test.c,v
retrieving revision 1.11
diff -u -p -r1.11 string-test.c
--- tests/string-test.c	19 Jul 2001 20:07:41 -0000	1.11
+++ tests/string-test.c	3 May 2002 21:11:38 -0000
@@ -210,6 +210,22 @@ main (int   argc,
   g_string_free (string1, TRUE);
   g_string_free (string2, TRUE);
   
+  g_assert (g_str_has_prefix("foobar", "gazonk") == FALSE);
+  g_assert (g_str_has_prefix("xyzzy", "xyzzy") == TRUE);
+  g_assert (g_str_has_prefix("xyzzy", "xy") == TRUE);
+  g_assert (g_str_has_prefix("xyzzy", "") == TRUE);
+  g_assert (g_str_has_prefix("xyz", "xyzzy") == FALSE);
+  g_assert (g_str_has_prefix("", "xyzzy") == FALSE);
+  g_assert (g_str_has_prefix("", "") == TRUE);
+
+  g_assert (g_str_has_suffix("foobar", "gazonk") == FALSE);
+  g_assert (g_str_has_suffix("xyzzy", "xyzzy") == TRUE);
+  g_assert (g_str_has_suffix("xyzzy", "zy") == TRUE);
+  g_assert (g_str_has_suffix("xyzzy", "") == TRUE);
+  g_assert (g_str_has_suffix("zzy", "xyzzy") == FALSE);
+  g_assert (g_str_has_suffix("", "xyzzy") == FALSE);
+  g_assert (g_str_has_suffix("", "") == TRUE);
+
   return 0;
 }
 

-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 Alexander Larsson                                            Red Hat, Inc 
                   alexl redhat com    alla lysator liu se 
He's a scarfaced misogynist inventor who knows the secret of the alien 
invasion. She's a psychotic belly-dancing soap star looking for love in all 
the wrong places. They fight crime! 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]