mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-06-12 23:53:46 +02:00
Make g_strerror() do less work
Store the (translated, UTF-8-encoded) error strings in a hash table to avoid doing translation and (possibly) g_locale_to_utf8() in every g_strerror() call. https://bugzilla.gnome.org/show_bug.cgi?id=754788
This commit is contained in:
parent
19eb511ba4
commit
96675446c5
@ -1253,11 +1253,23 @@ g_ascii_strtoll (const gchar *nptr,
|
|||||||
const gchar *
|
const gchar *
|
||||||
g_strerror (gint errnum)
|
g_strerror (gint errnum)
|
||||||
{
|
{
|
||||||
gchar buf[1024];
|
static GHashTable *errors;
|
||||||
gchar *msg;
|
G_LOCK_DEFINE_STATIC (errors);
|
||||||
gchar *tofree = NULL;
|
const gchar *msg;
|
||||||
const gchar *ret;
|
|
||||||
gint saved_errno = errno;
|
gint saved_errno = errno;
|
||||||
|
|
||||||
|
G_LOCK (errors);
|
||||||
|
if (errors)
|
||||||
|
msg = g_hash_table_lookup (errors, GINT_TO_POINTER (errnum));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
errors = g_hash_table_new (NULL, NULL);
|
||||||
|
msg = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!msg)
|
||||||
|
{
|
||||||
|
gchar buf[1024];
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
|
|
||||||
#if defined(G_OS_WIN32)
|
#if defined(G_OS_WIN32)
|
||||||
@ -1277,16 +1289,17 @@ g_strerror (gint errnum)
|
|||||||
#endif
|
#endif
|
||||||
if (!g_get_charset (NULL))
|
if (!g_get_charset (NULL))
|
||||||
{
|
{
|
||||||
msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, &error);
|
msg = g_locale_to_utf8 (msg, -1, NULL, NULL, &error);
|
||||||
if (error)
|
if (error)
|
||||||
g_print ("%s\n", error->message);
|
g_print ("%s\n", error->message);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = g_intern_string (msg);
|
g_hash_table_insert (errors, GINT_TO_POINTER (errnum), (char *) msg);
|
||||||
g_free (tofree);
|
}
|
||||||
|
G_UNLOCK (errors);
|
||||||
|
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
return ret;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user