misc/libphysfs/physfs_unicode.c
changeset 12218 bb5522e88ab2
parent 8524 a65e9bcf0a03
--- a/misc/libphysfs/physfs_unicode.c	Mon Apr 10 09:05:16 2017 -0400
+++ b/misc/libphysfs/physfs_unicode.c	Mon Apr 10 12:06:43 2017 -0400
@@ -4,7 +4,7 @@
 
 /*
  * From rfc3629, the UTF-8 spec:
- *  http://www.ietf.org/rfc/rfc3629.txt
+ *  https://www.ietf.org/rfc/rfc3629.txt
  *
  *   Char. number range  |        UTF-8 octet sequence
  *      (hexadecimal)    |              (binary)
@@ -447,11 +447,27 @@
 static int utf8codepointcmp(const PHYSFS_uint32 cp1, const PHYSFS_uint32 cp2)
 {
     PHYSFS_uint32 folded1[3], folded2[3];
+
+    if (cp1 == cp2)
+        return 0;  /* obviously matches. */
+
     locate_case_fold_mapping(cp1, folded1);
     locate_case_fold_mapping(cp2, folded2);
-    return ( (folded1[0] == folded2[0]) &&
-             (folded1[1] == folded2[1]) &&
-             (folded1[2] == folded2[2]) );
+
+    if (folded1[0] < folded2[0])
+        return -1;
+    else if (folded1[0] > folded2[0])
+        return 1;
+    else if (folded1[1] < folded2[1])
+        return -1;
+    else if (folded1[1] > folded2[1])
+        return 1;
+    else if (folded1[2] < folded2[2])
+        return -1;
+    else if (folded1[2] > folded2[2])
+        return 1;
+
+    return 0;  /* complete match. */
 } /* utf8codepointcmp */
 
 
@@ -461,8 +477,11 @@
     {
         const PHYSFS_uint32 cp1 = utf8codepoint(&str1);
         const PHYSFS_uint32 cp2 = utf8codepoint(&str2);
-        if (!utf8codepointcmp(cp1, cp2)) break;
-        if (cp1 == 0) return 1;
+        const int rc = utf8codepointcmp(cp1, cp2);
+        if (rc != 0)
+            return rc;
+        else if (cp1 == 0)
+            break;  /* complete match. */
     } /* while */
 
     return 0;
@@ -475,12 +494,15 @@
     {
         const PHYSFS_uint32 cp1 = utf8codepoint(&str1);
         const PHYSFS_uint32 cp2 = utf8codepoint(&str2);
-        if (!utf8codepointcmp(cp1, cp2)) return 0;
-        if (cp1 == 0) return 1;
+        const int rc = utf8codepointcmp(cp1, cp2);
+        if (rc != 0)
+            return rc;
+        else if (cp1 == 0)
+            return 0;
         n--;
     } /* while */
 
-    return 1;  /* matched to n chars. */
+    return 0;  /* matched to n chars. */
 } /* __PHYSFS_utf8strnicmp */