diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/fl_utf8.cxx | 93 |
1 files changed, 30 insertions, 63 deletions
diff --git a/src/fl_utf8.cxx b/src/fl_utf8.cxx index cf43556dd..8aead90c4 100644 --- a/src/fl_utf8.cxx +++ b/src/fl_utf8.cxx @@ -178,79 +178,46 @@ fl_utf_nb_char( return nbc; } -/* - * compare only the first n bytes - * return 0 if the strings are equal; - * return 1 if s1 is greater than s2 - * return -1 if s1 is less than s2 - */ -/** - UTF-8 aware strncasecmp - converts to lower case Unicode and tests. - \todo Correct the incorrect logic where length of strings tested - \todo Clarify whether n means number of bytes, or characters. +/** + UTF-8 aware strncasecmp - converts to lower case Unicode and tests. + + \param s1, s2 the utf8 strings to compare + \param n the maximum number of utf8 characters to compare + \return 0 if the strings are equal + \return >0 if s1 is greater than s2 + \return <0 if s1 is less than s2 */ int fl_utf_strncasecmp(const char *s1, const char *s2, int n) { - int i; - int s1_l; - int s2_l; - char *e1, *e2; // string end pointers - - s1_l = 0; - while (s1_l < n && s1[s1_l]) s1_l++; - s2_l = 0; - while (s2_l < n && s2[s2_l]) s2_l++; - - if (s1_l < s2_l) { - return -1; - } else if (s1_l > s2_l) { - return 1; - } - e1 = (char *)&s1[s1_l]; // last char to test - e2 = (char *)&s2[s2_l]; - for (i = 0; i < n;) { - int l1, l2; - unsigned int u1, u2; - int res; - -// l1 = fl_utf2ucs((unsigned char*)s1 + i, n - i, &u1); - u1 = fl_utf8decode(s1 + i, e1, &l1); -// l2 = fl_utf2ucs((unsigned char*)s2 + i, n - i, &u2); - u2 = fl_utf8decode(s2 + i, e2, &l2); - if (l1 - l2 != 0) return l1 - l2; - res = XUtf8Tolower(u1) - XUtf8Tolower(u2); - if (res != 0) return res; - if (l1 < 1) { - i += 1; - } else { - i += l1; - } - } - return 0; + int i; + for (i = 0; i < n; i++) { + int l1, l2; + unsigned int u1, u2; + + if (*s1==0 && *s2==0) return 0; // all compared equal, return 0 + + u1 = fl_utf8decode(s1, 0, &l1); + u2 = fl_utf8decode(s2, 0, &l2); + int res = XUtf8Tolower(u1) - XUtf8Tolower(u2); + if (res) return res; + s1 += l1; + s2 += l2; + } + return 0; } -/* - * return 0 if the strings are equal; - * return 1 if s1 is greater than s2 - * return -1 if s1 is less than s2 - */ -/** - UTF-8 aware strcasecmp - converts to Unicode and tests. - \todo Correct the incorrect logic where length of strings tested +/** + UTF-8 aware strcasecmp - converts to Unicode and tests. + + \return 0 if the strings are equal + \return 1 if s1 is greater than s2 + \return -1 if s1 is less than s2 */ int fl_utf_strcasecmp(const char *s1, const char *s2) { - int s1_l = (int) strlen(s1); - int s2_l = (int) strlen(s2); - - if (s1_l < s2_l) { - return -1; - } else if (s1_l > s2_l) { - return 1; - } - return fl_utf_strncasecmp(s1, s2, s1_l); + return fl_utf_strncasecmp(s1, s2, 0x7fffffff); } /** |
