Author: matt
Date: 2012-07-24 12:49:37 -0700 (Tue, 24 Jul 2012)
New Revision: 9639
Log:
Fixed fl_utf_strncasecmp etc.

Modified:
   branches/branch-1.3/CHANGES
   branches/branch-1.3/src/fl_utf8.cxx

Modified: branches/branch-1.3/CHANGES
===================================================================
--- branches/branch-1.3/CHANGES 2012-07-24 04:41:38 UTC (rev 9638)
+++ branches/branch-1.3/CHANGES 2012-07-24 19:49:37 UTC (rev 9639)
@@ -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)

Modified: branches/branch-1.3/src/fl_utf8.cxx
===================================================================
--- branches/branch-1.3/src/fl_utf8.cxx 2012-07-24 04:41:38 UTC (rev 9638)
+++ branches/branch-1.3/src/fl_utf8.cxx 2012-07-24 19:49:37 UTC (rev 9639)
@@ -178,79 +178,46 @@
        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);
 }
 
 /**

_______________________________________________
fltk-commit mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-commit

Reply via email to