46 return g_utf8_collate(da, db);
65 gchar *a = g_utf8_collate_key_for_filename(da ? da :
"", -1);
66 gchar *b = g_utf8_collate_key_for_filename(db ? db :
"", -1);
68 int result = strcmp(a, b);
90 #define UTF8_COMPUTE(Char, Mask, Len) \ 96 else if ((Char & 0xe0) == 0xc0) \ 101 else if ((Char & 0xf0) == 0xe0) \ 106 else if ((Char & 0xf8) == 0xf0) \ 111 else if ((Char & 0xfc) == 0xf8) \ 116 else if ((Char & 0xfe) == 0xfc) \ 124 #define UTF8_LENGTH(Char) \ 125 ((Char) < 0x80 ? 1 : \ 126 ((Char) < 0x800 ? 2 : \ 127 ((Char) < 0x10000 ? 3 : \ 128 ((Char) < 0x200000 ? 4 : \ 129 ((Char) < 0x4000000 ? 5 : 6))))) 132 #define UTF8_GET(Result, Chars, Count, Mask, Len) \ 133 (Result) = (Chars)[0] & (Mask); \ 134 for ((Count) = 1; (Count) < (Len); ++(Count)) \ 136 if (((Chars)[(Count)] & 0xc0) != 0x80) \ 142 (Result) |= ((Chars)[(Count)] & 0x3f); \ 145 #define UNICODE_VALID(Char) \ 146 ((Char) < 0x110000 && \ 147 (((Char) & 0xFFFFF800) != 0xD800) && \ 148 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \ 149 ((Char) >= 0x20 || (Char) == 0x09 || (Char) == 0x0A || (Char) == 0x0D) && \ 150 ((Char) & 0xFFFE) != 0xFFFE) 160 g_return_val_if_fail (str != NULL, FALSE);
167 while ((max_len < 0 || (p - str) < max_len) && *p)
169 int i, mask = 0, len;
171 unsigned char c = (
unsigned char) * p;
173 UTF8_COMPUTE (c, mask, len);
180 ((max_len - (p - str)) < len))
183 UTF8_GET (result, p, i, mask, len);
185 if (UTF8_LENGTH (result) != len)
188 if (result == (gunichar) - 1)
191 if (!UNICODE_VALID (result))
204 p != (str + max_len))
206 else if (max_len < 0 &&
219 g_return_if_fail(str);
224 g_warning(
"Invalid utf8 string: %s", str);
228 memmove(end, end + 1, len);
236 gchar *result = g_strdup (str);
245 const gchar *controls =
"\b\f\n\r\t\v";
246 g_return_if_fail (str != NULL && strlen (str) > 0);
248 for(c = str + strlen (str) - 1; c != str; --c)
250 gboolean line_control = ((
unsigned char)(*c) < 0x20);
251 if (line_control || strchr(controls, *c) != NULL)
260 gsize bytes_written = 0;
264 locale_str = g_locale_from_utf8(str, -1, NULL, &bytes_written, &err);
267 g_warning(
"g_locale_from_utf8 failed: %s", err->message);
278 gsize bytes_written = 0;
282 utf8_str = g_locale_to_utf8(str, -1, NULL, &bytes_written, &err);
285 g_warning(
"g_locale_to_utf8 failed: %s", err->message);
296 for (; list != NULL; list = list->next)
298 rtn = g_list_prepend (rtn, (*fn)(list->data, user_data));
300 return g_list_reverse (rtn);
306 if (list == NULL || *list == NULL)
310 if (cut_point->prev == NULL)
316 cut_point->prev->next = NULL;
317 cut_point->prev = NULL;
321 utf8_strstr(
char **needle,
char *haystack)
323 char *tmp = g_utf8_normalize (*needle, -1, G_NORMALIZE_NFC);
324 if (haystack && *haystack)
326 char *place = strstr(haystack, tmp);
338 gnc_g_list_stringjoin_internal (GList *list_of_strings,
const gchar *sep,
bool testdups)
340 gint seplen = sep ? strlen(sep) : 0;
341 gint length = -seplen;
344 for (GList *n = list_of_strings; n; n = n->next)
346 gchar *str = n->data;
348 length += strlen (str) + seplen;
354 p = retval = (gchar*) g_malloc0 (length *
sizeof (gchar) + 1);
355 for (GList *n = list_of_strings; n; n = n->next)
357 gchar *str = n->data;
360 if (!testdups || utf8_strstr (&str, retval))
362 if (sep && (p != retval))
363 p = g_stpcpy (p, sep);
364 p = g_stpcpy (p, str);
376 return gnc_g_list_stringjoin_internal (list_of_strings, sep,
false);
382 return gnc_g_list_stringjoin_internal (list_of_strings, sep,
true);
388 for (GList *lst = (GList*) list;; lst = g_list_next (lst), len--)
390 if (!lst)
return (len ? -1 : 0);
GList * gnc_g_list_map(GList *list, GncGMapFunc fn, gpointer user_data)
gchar * gnc_g_list_stringjoin(GList *list_of_strings, const gchar *sep)
Return a string joining a GList whose elements are gchar* strings.
int safe_utf8_collate(const char *da, const char *db)
Collate two UTF-8 strings.
void gnc_utf8_strip_invalid_and_controls(gchar *str)
Strip any non-utf8 characters and any control characters (everything < 0x20, , , ...
gchar * gnc_locale_from_utf8(const gchar *str)
Converts a string from UTF-8 to the encoding used for strings in the current locale.
gboolean gnc_utf8_validate(const gchar *str, gssize max_len, const gchar **end)
Validates UTF-8 encoded text for use in GnuCash.
int safe_utf8_collate_natural(const char *da, const char *db)
Collate two UTF-8 strings naturally.
void gnc_g_list_cut(GList **list, GList *cut_point)
Cut a GList into two parts; the cut_point is the beginning of the new list; list may need to be modif...
void gnc_utf8_strip_invalid(gchar *str)
Strip any non-UTF-8 characters from a string.
gchar * gnc_g_list_stringjoin_nodups(GList *list_of_strings, const gchar *sep)
Like stringjoin but ensures that the string to be added isn't already part of the return string...
gchar * gnc_locale_to_utf8(const gchar *str)
Converts a string to UTF-8 from the encoding used for strings in the current locale.
gchar * gnc_utf8_strip_invalid_strdup(const gchar *str)
Returns a newly allocated copy of the given string but with any non-UTF-8 character stripped from it...
gint gnc_list_length_cmp(const GList *list, size_t len)
Scans the GList elements the minimum number of iterations required to test it against a specified siz...