gdatetime: Avoid unnecessary conversions from ASCII to UTF-8

This commit is contained in:
Tomasz Miąsko 2018-11-30 00:00:00 +00:00
parent bc59892b1a
commit 8bfa45817e
2 changed files with 15 additions and 2 deletions

View File

@ -3363,7 +3363,13 @@ g_date_time_format (GDateTime *datetime,
const gchar *format) const gchar *format)
{ {
GString *outstr; 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 (datetime != NULL, NULL);
g_return_val_if_fail (format != 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); 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); g_string_free (outstr, TRUE);
return NULL; return NULL;

View File

@ -1538,6 +1538,12 @@ test_format_unrepresentable (void)
/* We are using Unicode ratio symbol here, which is outside ASCII. */ /* We are using Unicode ratio symbol here, which is outside ASCII. */
TEST_PRINTF_TIME (23, 15, 0, "%H%M", "2315"); TEST_PRINTF_TIME (23, 15, 0, "%H%M", "2315");
/* 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", "2315");
else
g_test_skip ("locale pl_PL.ISO-8859-2 not available, skipping test");
setlocale (LC_ALL, oldlocale); setlocale (LC_ALL, oldlocale);
g_free (oldlocale); g_free (oldlocale);
} }