Added e_util_utf8_normalize()
authorTristan Van Berkom <tristanvb@openismus.com>
Mon, 19 Nov 2012 11:19:43 +0000 (20:19 +0900)
committerTristan Van Berkom <tristanvb@openismus.com>
Thu, 22 Nov 2012 03:54:20 +0000 (12:54 +0900)
A utility function to lowercase/remove accents from a utf8 string

libedataserver/e-data-server-util.c
libedataserver/e-data-server-util.h

index 70fad17..ab252b5 100644 (file)
@@ -526,6 +526,40 @@ e_util_utf8_data_make_valid (const gchar *data,
 }
 
 /**
+ * e_util_utf8_normalize:
+ * @str: a UTF-8 string
+ *
+ * Normalizes @str by making it all lower case and removing any accents from it.
+ *
+ * Returns: The normalized version of @str, or %NULL if @str was not valid UTF-8
+ *
+ * Since: 3.8
+ */
+gchar *
+e_util_utf8_normalize (const gchar *str)
+{
+       gchar *valid = NULL;
+       gchar *normal, *casefolded = NULL;
+
+       if (str == NULL)
+               return NULL;
+
+       if (!g_utf8_validate (str, -1, NULL)) {
+               valid = e_util_utf8_make_valid (str);
+               str = valid;
+       }
+
+       normal = e_util_utf8_remove_accents (str);
+       if (normal)
+               casefolded = g_utf8_casefold (normal, -1);
+
+       g_free (valid);
+       g_free (normal);
+
+       return casefolded;
+}
+
+/**
  * e_util_ensure_gdbus_string:
  * @str: a possibly invalid UTF-8 string, or %NULL
  * @gdbus_str: return location for the corrected string
index 911c77d..2687e6e 100644 (file)
@@ -52,6 +52,7 @@ gchar *               e_util_utf8_remove_accents      (const gchar *str);
 gchar *                e_util_utf8_make_valid          (const gchar *str);
 gchar *                e_util_utf8_data_make_valid     (const gchar *data,
                                                 gsize data_bytes);
+gchar *         e_util_utf8_normalize           (const gchar *str);
 const gchar *   e_util_ensure_gdbus_string     (const gchar *str,
                                                 gchar **gdbus_str);
 guint64                e_util_gthread_id               (GThread *thread);