[glib: 2/3] gdatetime: Avoid unnecessary conversions from ASCII to UTF-8



commit 8bfa45817e1025d3710304c785b84890b048acd0
Author: Tomasz Miąsko <tomasz miasko gmail com>
Date:   Fri Nov 30 00:00:00 2018 +0000

    gdatetime: Avoid unnecessary conversions from ASCII to UTF-8

 glib/gdatetime.c       | 11 +++++++++--
 glib/tests/gdatetime.c |  6 ++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
---
diff --git a/glib/gdatetime.c b/glib/gdatetime.c
index 6afe14438..02f526828 100644
--- a/glib/gdatetime.c
+++ b/glib/gdatetime.c
@@ -3363,7 +3363,13 @@ g_date_time_format (GDateTime   *datetime,
                     const gchar *format)
 {
   GString  *outstr;
-  gboolean locale_is_utf8 = g_get_charset (NULL);
+  const gchar *charset;
+  /* Avoid conversions from locale charset to UTF-8 if charset is compatible
+   * with UTF-8 already. Check for UTF-8 and synonymous canonical names of
+   * ASCII. */
+  gboolean locale_is_utf8_compatible = g_get_charset (&charset) ||
+    g_strcmp0 ("ASCII", charset) == 0 ||
+    g_strcmp0 ("ANSI_X3.4-1968", charset) == 0;
 
   g_return_val_if_fail (datetime != NULL, NULL);
   g_return_val_if_fail (format != NULL, NULL);
@@ -3371,7 +3377,8 @@ g_date_time_format (GDateTime   *datetime,
 
   outstr = g_string_sized_new (strlen (format) * 2);
 
-  if (!g_date_time_format_utf8 (datetime, format, outstr, locale_is_utf8))
+  if (!g_date_time_format_utf8 (datetime, format, outstr,
+                                locale_is_utf8_compatible))
     {
       g_string_free (outstr, TRUE);
       return NULL;
diff --git a/glib/tests/gdatetime.c b/glib/tests/gdatetime.c
index 1a46cf55a..00e22181e 100644
--- a/glib/tests/gdatetime.c
+++ b/glib/tests/gdatetime.c
@@ -1538,6 +1538,12 @@ test_format_unrepresentable (void)
   /* We are using Unicode ratio symbol here, which is outside ASCII. */
   TEST_PRINTF_TIME (23, 15, 0, "%H∶%M", "23∶15");
 
+  /* Test again, this time in locale with non ASCII charset. */
+  if (setlocale (LC_ALL, "pl_PL.ISO-8859-2") != NULL)
+    TEST_PRINTF_TIME (23, 15, 0, "%H∶%M", "23∶15");
+  else
+    g_test_skip ("locale pl_PL.ISO-8859-2 not available, skipping test");
+
   setlocale (LC_ALL, oldlocale);
   g_free (oldlocale);
 }


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