diff options
author | Nils Philippsen <nils@tiptoe.de> | 2013-12-10 21:29:24 +0100 |
---|---|---|
committer | Nils Philippsen <nils@tiptoe.de> | 2013-12-10 23:07:47 +0100 |
commit | 96947e2f3a74e5f738c763db7dfa42b1269a3903 (patch) | |
tree | f967f21b3d1b5b0b3e0e923a1b3e4794122457be /libs/ardour/utils.cc | |
parent | e5ae775b42ea98587603ee273ca23394a113d44c (diff) |
add cmp_nocase_utf8()
This is like cmp_nocase(), only that it doesn't use toupper(), tolower()
and therefore is agnostic of the current locale, and attempts to compare
strings in a UTF8-aware way (or falls back to ASCII if one of the
strings isn't UTF8-encoded).
Diffstat (limited to 'libs/ardour/utils.cc')
-rw-r--r-- | libs/ardour/utils.cc | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/libs/ardour/utils.cc b/libs/ardour/utils.cc index 0c98461974..aa06912913 100644 --- a/libs/ardour/utils.cc +++ b/libs/ardour/utils.cc @@ -232,6 +232,41 @@ cmp_nocase (const string& s, const string& s2) return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1; } +int cmp_nocase_utf8 (const string& s1, const string& s2) +{ + const char *cstr1 = s1.c_str(); + const char *cstr2 = s2.c_str(); + gchar *cstr1folded = NULL; + gchar *cstr2folded = NULL; + int retval; + + if (!g_utf8_validate (cstr1, -1, NULL) || + !g_utf8_validate (cstr2, -1, NULL)) { + // fall back to comparing ASCII + return g_ascii_strcasecmp (cstr1, cstr2); + } + + cstr1folded = g_utf8_casefold (cstr1, -1); + cstr2folded = g_utf8_casefold (cstr2, -1); + + if (cstr1folded && cstr2folded) { + retval = strcmp (cstr1folded, cstr2folded); + } else { + // this shouldn't happen, make the best of it + retval = g_ascii_strcasecmp (cstr1, cstr2); + } + + if (cstr1folded) { + g_free (cstr1folded); + } + + if (cstr2folded) { + g_free (cstr2folded); + } + + return retval; +} + int touch_file (string path) { |