summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Melcher <fltk@matthiasm.com>2012-07-24 19:49:37 +0000
committerMatthias Melcher <fltk@matthiasm.com>2012-07-24 19:49:37 +0000
commit4e0bc497136b38220e8be19a89a3d22e618387d1 (patch)
treeb5c851a56fbc6cf6f0a2d09a937b621c7bbfc87a
parent398acd9c2a73d0d5c69bb51f7f3497e229fb367c (diff)
Fixed fl_utf_strncasecmp etc.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@9639 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
-rw-r--r--CHANGES1
-rw-r--r--src/fl_utf8.cxx93
2 files changed, 31 insertions, 63 deletions
diff --git a/CHANGES b/CHANGES
index aa6279ac9..cf4b0202c 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,6 @@
CHANGES IN FLTK 1.3.2
+ - Fixed utf_strncasecmp and utf_strcasecmp
- Moved all inline constructors into source file to avoid bad DLLs
- Fixed Fl_Widget::copy_label() and Fl_Window::copy_label() when
called with the old label() (STR #2836)
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);
}
/**