From 70af0de0ef26cd157cdf012023ebf1c95f874c51 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 31 Aug 2005 14:25:45 +0000 Subject: [PATCH] Add functions to intern strings. 2005-08-31 Matthias Clasen * glib/glib.symbols: * glib/gquark.h: * glib/gdataset.c: Add functions to intern strings. --- ChangeLog | 6 +++++ ChangeLog.pre-2-10 | 6 +++++ ChangeLog.pre-2-12 | 6 +++++ docs/reference/ChangeLog | 5 ++++ docs/reference/glib/glib-sections.txt | 3 ++- docs/reference/glib/tmpl/quarks.sgml | 7 +++++ glib/gdataset.c | 38 +++++++++++++++++++++++++++ glib/glib.symbols | 7 +++++ glib/gquark.h | 4 +++ 9 files changed, 81 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 15caea52..f5c482a5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2005-08-31 Matthias Clasen + + * glib/glib.symbols: + * glib/gquark.h: + * glib/gdataset.c: + 2005-08-28 Matthias Clasen * glib/giochannel.c: Unify some near-duplicate strings. (#314654, diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 15caea52..f5c482a5 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,9 @@ +2005-08-31 Matthias Clasen + + * glib/glib.symbols: + * glib/gquark.h: + * glib/gdataset.c: + 2005-08-28 Matthias Clasen * glib/giochannel.c: Unify some near-duplicate strings. (#314654, diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 15caea52..f5c482a5 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,9 @@ +2005-08-31 Matthias Clasen + + * glib/glib.symbols: + * glib/gquark.h: + * glib/gdataset.c: + 2005-08-28 Matthias Clasen * glib/giochannel.c: Unify some near-duplicate strings. (#314654, diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index 2d14ed2b..5ffbd31a 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,8 @@ +2005-08-31 Matthias Clasen + + * glib/tmpl/quarks.sgml: + * glib/glib-sections.txt: Add string interning functions. + 2005-08-30 Matthias Clasen * glib/tmpl/arrays.sgml: diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 79ed4781..80cd93da 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -1958,7 +1958,8 @@ g_quark_from_string g_quark_from_static_string g_quark_to_string g_quark_try_string - +g_intern_string +g_intern_static_string
diff --git a/docs/reference/glib/tmpl/quarks.sgml b/docs/reference/glib/tmpl/quarks.sgml index 7a8159ed..72bc1a7a 100644 --- a/docs/reference/glib/tmpl/quarks.sgml +++ b/docs/reference/glib/tmpl/quarks.sgml @@ -25,6 +25,13 @@ To find the string corresponding to a given #GQuark, use g_quark_to_string(). To find the #GQuark corresponding to a given string, use g_quark_try_string(). + +Another use for the string pool maintained for the quark functions is string +interning, using g_intern_string() or g_intern_static_string(). An interned string +is a canonical representation for a string. One important advantage of interned strings +is that they can be compared for equality by a simple pointer comparision, rather than +using strcmp(). + diff --git a/glib/gdataset.c b/glib/gdataset.c index 45d3ba66..37c3466f 100644 --- a/glib/gdataset.c +++ b/glib/gdataset.c @@ -711,5 +711,43 @@ g_quark_new (gchar *string) return quark; } +/** + * g_intern_string: + * @string: a string + * + * Returns a canonical representation for @string. Interned strings can + * be compared for equality by comparing the pointers, instead of using strcmp(). + * + * Returns: a canonical representation for the string + * + * Since: 2.10 + */ +G_CONST_RETURN gchar* +g_intern_string (const gchar *string) +{ + return string ? g_quark_to_string (g_quark_from_string (string)) : NULL; +} + +/** + * g_intern_static_string: + * @string: a static string + * + * Returns a canonical representation for @string. Interned strings can + * be compared for equality by comparing the pointers, instead of using strcmp(). + * g_intern_static_string() does not copy the string, therefore @string must + * not be freed or modified. + * + * Returns: a canonical representation for the string + * + * Since: 2.10 + */ +G_CONST_RETURN gchar* +g_intern_static_string (const gchar *string) +{ + return string ? g_quark_to_string (g_quark_from_static_string (string)) : NULL; +} + + + #define __G_DATASET_C__ #include "galiasdef.c" diff --git a/glib/glib.symbols b/glib/glib.symbols index bb205c6e..8170aaff 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -165,10 +165,17 @@ g_dataset_foreach g_dataset_id_get_data g_dataset_id_remove_no_notify g_dataset_id_set_data_full +#endif +#endif + +#if IN_HEADER(__G_QUARK_H__) +#if IN_FILE(__G_DATASET_C__) g_quark_from_static_string g_quark_from_string g_quark_to_string G_GNUC_CONST g_quark_try_string +g_intern_string +g_intern_static_string #endif #endif diff --git a/glib/gquark.h b/glib/gquark.h index 6b96dcf6..fc3006ff 100644 --- a/glib/gquark.h +++ b/glib/gquark.h @@ -40,6 +40,10 @@ GQuark g_quark_from_static_string (const gchar *string); GQuark g_quark_from_string (const gchar *string); G_CONST_RETURN gchar* g_quark_to_string (GQuark quark) G_GNUC_CONST; +G_CONST_RETURN gchar* g_intern_string (const gchar *string); +G_CONST_RETURN gchar* g_intern_static_string (const gchar *string); + + G_END_DECLS #endif /* __G_QUARK_H__ */ -- 2.34.1