[glib] datetime: use g_utf8_next_char() to walk utf8 string



commit 6bb89501cf7bfb5e4365d8dd3ef045a0c096af0c
Author: Christian Hergert <chris dronelabs com>
Date:   Tue Aug 31 09:27:58 2010 -0700

    datetime: use g_utf8_next_char() to walk utf8 string
    
    Previously, the format string was iterated many times by
    walking to the given offset in the string repeatedly.
    This patch instead walks the string using g_utf8_next_char().
    
    Additionally, the character for lookups was a char and could
    loose content.  This uses gunichar instead.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=50076

 glib/gdatetime.c |   29 +++++++++++++----------------
 1 files changed, 13 insertions(+), 16 deletions(-)
---
diff --git a/glib/gdatetime.c b/glib/gdatetime.c
index 31d9c09..d3aa2eb 100644
--- a/glib/gdatetime.c
+++ b/glib/gdatetime.c
@@ -1842,13 +1842,11 @@ gchar *
 g_date_time_printf (const GDateTime *datetime,
                     const gchar     *format)
 {
-  GString     *outstr;
-  const gchar *tmp;
-  gchar       *tmp2,
-               c;
-  glong        utf8len;
-  gint         i;
-  gboolean     in_mod;
+  GString  *outstr;
+  gchar    *tmp;
+  gunichar  c;
+  glong     utf8len;
+  gboolean  in_mod;
 
   g_return_val_if_fail (datetime != NULL, NULL);
   g_return_val_if_fail (format != NULL, NULL);
@@ -1858,10 +1856,9 @@ g_date_time_printf (const GDateTime *datetime,
   utf8len = g_utf8_strlen (format, -1);
   in_mod = FALSE;
 
-  for (i = 0; i < utf8len; i++)
+  for (; *format; format = g_utf8_next_char(format))
     {
-      tmp = g_utf8_offset_to_pointer (format, i);
-      c = g_utf8_get_char (tmp);
+      c = g_utf8_get_char (format);
 
       switch (c)
         {
@@ -1974,16 +1971,16 @@ g_date_time_printf (const GDateTime *datetime,
                   break;
                 case 'x':
                   {
-                    tmp2 = GET_PREFERRED_DATE (datetime);
-                    g_string_append (outstr, tmp2);
-                    g_free (tmp2);
+                    tmp = GET_PREFERRED_DATE (datetime);
+                    g_string_append (outstr, tmp);
+                    g_free (tmp);
                   }
                   break;
                 case 'X':
                   {
-                    tmp2 = GET_PREFERRED_TIME (datetime);
-                    g_string_append (outstr, tmp2);
-                    g_free (tmp2);
+                    tmp = GET_PREFERRED_TIME (datetime);
+                    g_string_append (outstr, tmp);
+                    g_free (tmp);
                   }
                   break;
                 case 'y':



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