From d7493e38a465649d0e56b3e3c86f3b9085372d61 Mon Sep 17 00:00:00 2001 From: wwp Date: Fri, 23 May 2014 00:50:29 +0200 Subject: [PATCH] Add g_utf8_substring function for compiling with GLIB < 2.30. --- src/common/utils.c | 38 ++++++++++++++++++++++++++++++++++++++ src/common/utils.h | 12 +++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/common/utils.c b/src/common/utils.c index 104c691c2..a592ce984 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -14,6 +14,10 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * The code of the g_utf8_substring function below is owned by + * Matthias Clasen / + * and is got from GLIB 2.30 * */ @@ -5504,3 +5508,37 @@ int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name) { slist_free_strings_full(canonical_parts); return 0; } + +#if !GLIB_CHECK_VERSION(2, 30, 0) +/** + * g_utf8_substring: + * @str: a UTF-8 encoded string + * @start_pos: a character offset within @str + * @end_pos: another character offset within @str + * + * Copies a substring out of a UTF-8 encoded string. + * The substring will contain @end_pos - @start_pos + * characters. + * + * Returns: a newly allocated copy of the requested + * substring. Free with g_free() when no longer needed. + * + * Since: GLIB 2.30 + */ +gchar * +g_utf8_substring (const gchar *str, + glong start_pos, + glong end_pos) +{ + gchar *start, *end, *out; + + start = g_utf8_offset_to_pointer (str, start_pos); + end = g_utf8_offset_to_pointer (start, end_pos - start_pos); + + out = g_malloc (end - start + 1); + memcpy (out, start, end - start); + out[end - start] = 0; + + return out; +} +#endif diff --git a/src/common/utils.h b/src/common/utils.h index 434b684c6..1a1f6c496 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -14,7 +14,11 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * + * + * The code of the g_utf8_substring function below is owned by + * Matthias Clasen / + * and is got from GLIB 2.30 + * */ #ifndef __UTILS_H__ @@ -599,6 +603,12 @@ void cm_mutex_free(GMutex *mutex); int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name); +#if !GLIB_CHECK_VERSION(2, 30, 0) +gchar *g_utf8_substring (const gchar *p, + glong start_pos, + glong end_pos) G_GNUC_MALLOC; +#endif + #ifdef __cplusplus } #endif -- 2.25.1