static int str_utf8_prefix (const char *text, const char *prefix) { char *t = str_utf8_normalize (text); char *p = str_utf8_normalize (prefix); const char *nt = t; const char *np = p; const char *nnt = t; const char *nnp = p; int result; while (nt[0] != '\0' && np[0] != '\0') { str_utf8_cnext_char_safe (&nnt); str_utf8_cnext_char_safe (&nnp); if (nnt - nt != nnp - np) break; if (strncmp (nt, np, nnt - nt) != 0) break; nt = nnt; np = nnp; } result = np - p; g_free (t); g_free (p); return result; }
static int str_utf8_ncompare (const char *t1, const char *t2) { char *n1, *n2; int result; n1 = str_utf8_normalize (t1); n2 = str_utf8_normalize (t2); result = strncmp (n1, n2, min (strlen (n1), strlen (n2))); g_free (n1); g_free (n2); return result; }
static char * str_utf8_create_key_gen (const char *text, int case_sen, gchar * (*keygen) (const gchar *, gssize size)) { char *result; if (case_sen) { result = str_utf8_normalize (text); } else { const char *start, *end; char *fold, *key; GString *fixed = g_string_new (""); start = text; while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') { if (start != end) { fold = g_utf8_casefold (start, end - start); key = keygen (fold, -1); g_string_append (fixed, key); g_free (key); g_free (fold); } g_string_append_c (fixed, end[0]); start = end + 1; } if (start == text) { fold = g_utf8_casefold (text, -1); result = keygen (fold, -1); g_free (fold); } else { if (start[0] != '\0' && start != end) { fold = g_utf8_casefold (start, end - start); key = keygen (fold, -1); g_string_append (fixed, key); g_free (key); g_free (fold); } result = g_strdup (fixed->str); } g_string_free (fixed, TRUE); } return result; }
static char * str_utf8_create_key_gen (const char *text, int case_sen, gchar * (*keygen) (const gchar * text, gssize size)) { char *result; if (case_sen) { result = str_utf8_normalize (text); } else { gboolean dot; GString *fixed; const char *start, *end; char *fold, *key; dot = text[0] == '.'; fixed = g_string_sized_new (16); if (!dot) start = text; else { start = text + 1; g_string_append_c (fixed, '.'); } while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') { if (start != end) { fold = g_utf8_casefold (start, end - start); key = keygen (fold, -1); g_string_append (fixed, key); g_free (key); g_free (fold); } g_string_append_c (fixed, end[0]); start = end + 1; } if (start == text) { fold = g_utf8_casefold (start, -1); result = keygen (fold, -1); g_free (fold); g_string_free (fixed, TRUE); } else if (dot && (start == text + 1)) { fold = g_utf8_casefold (start, -1); key = keygen (fold, -1); g_string_append (fixed, key); g_free (key); g_free (fold); result = g_string_free (fixed, FALSE); } else { if (start[0] != '\0' && start != end) { fold = g_utf8_casefold (start, end - start); key = keygen (fold, -1); g_string_append (fixed, key); g_free (key); g_free (fold); } result = g_string_free (fixed, FALSE); } } return result; }