45 return g_utf8_collate(da, db);
60 #define UTF8_COMPUTE(Char, Mask, Len) \ 66 else if ((Char & 0xe0) == 0xc0) \ 71 else if ((Char & 0xf0) == 0xe0) \ 76 else if ((Char & 0xf8) == 0xf0) \ 81 else if ((Char & 0xfc) == 0xf8) \ 86 else if ((Char & 0xfe) == 0xfc) \ 94 #define UTF8_LENGTH(Char) \ 95 ((Char) < 0x80 ? 1 : \ 96 ((Char) < 0x800 ? 2 : \ 97 ((Char) < 0x10000 ? 3 : \ 98 ((Char) < 0x200000 ? 4 : \ 99 ((Char) < 0x4000000 ? 5 : 6))))) 102 #define UTF8_GET(Result, Chars, Count, Mask, Len) \ 103 (Result) = (Chars)[0] & (Mask); \ 104 for ((Count) = 1; (Count) < (Len); ++(Count)) \ 106 if (((Chars)[(Count)] & 0xc0) != 0x80) \ 112 (Result) |= ((Chars)[(Count)] & 0x3f); \ 115 #define UNICODE_VALID(Char) \ 116 ((Char) < 0x110000 && \ 117 (((Char) & 0xFFFFF800) != 0xD800) && \ 118 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \ 119 ((Char) >= 0x20 || (Char) == 0x09 || (Char) == 0x0A || (Char) == 0x0D) && \ 120 ((Char) & 0xFFFE) != 0xFFFE) 130 g_return_val_if_fail (str != NULL, FALSE);
137 while ((max_len < 0 || (p - str) < max_len) && *p)
139 int i, mask = 0, len;
141 unsigned char c = (
unsigned char) * p;
143 UTF8_COMPUTE (c, mask, len);
150 ((max_len - (p - str)) < len))
153 UTF8_GET (result, p, i, mask, len);
155 if (UTF8_LENGTH (result) != len)
158 if (result == (gunichar) - 1)
161 if (!UNICODE_VALID (result))
174 p != (str + max_len))
176 else if (max_len < 0 &&
189 g_return_if_fail(str);
194 g_warning(
"Invalid utf8 string: %s", str);
198 memmove(end, end + 1, len);
206 gchar *result = g_strdup (str);
215 const gchar *controls =
"\b\f\n\r\t\v";
216 g_return_if_fail (str != NULL && strlen (str) > 0);
218 for(c = str + strlen (str) - 1; c != str; --c)
220 gboolean line_control = ((
unsigned char)(*c) < 0x20);
221 if (line_control || strchr(controls, *c) != NULL)
230 gsize bytes_written = 0;
234 locale_str = g_locale_from_utf8(str, -1, NULL, &bytes_written, &err);
237 g_warning(
"g_locale_from_utf8 failed: %s", err->message);
248 gsize bytes_written = 0;
252 utf8_str = g_locale_to_utf8(str, -1, NULL, &bytes_written, &err);
255 g_warning(
"g_locale_to_utf8 failed: %s", err->message);
266 for (; list != NULL; list = list->next)
268 rtn = g_list_prepend (rtn, (*fn)(list->data, user_data));
270 return g_list_reverse (rtn);
276 if (list == NULL || *list == NULL)
280 if (cut_point->prev == NULL)
286 cut_point->prev->next = NULL;
287 cut_point->prev = NULL;
294 gint seplen = sep ? strlen(sep) : 0;
295 gint length = -seplen;
298 for (GList *n = list_of_strings; n; n = n->next)
300 gchar *str = n->data;
302 length += strlen (str) + seplen;
308 p = retval = (gchar*) g_malloc0 (length *
sizeof (gchar) + 1);
309 for (GList *n = list_of_strings; n; n = n->next)
311 gchar *str = n->data;
314 if (sep && (p != retval))
315 p = g_stpcpy (p, sep);
316 p = g_stpcpy (p, str);
325 for (GList *lst = (GList*) list;; lst = g_list_next (lst), len--)
327 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.
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_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...