]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
cmpForward: assume (and assert) p2 is upper-case
authorJustin Viiret <justin.viiret@intel.com>
Thu, 21 Apr 2016 03:39:16 +0000 (13:39 +1000)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 18 May 2016 06:22:28 +0000 (16:22 +1000)
src/util/compare.h

index 11c01f08ed5bdfb3260a3d8ed983a8551d32645a..eaa717a4c2978e021e2ac94c7a57905c98527536 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2016, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -97,9 +97,10 @@ u64a theirtoupper64(const u64a x) {
 
 static really_inline
 int cmpNocaseNaive(const u8 *p1, const u8 *p2, size_t len) {
-    const u8 *pEnd = (const u8 *)p1 + len;
+    const u8 *pEnd = p1 + len;
     for (; p1 < pEnd; p1++, p2++) {
-        if (mytolower(*p1) != mytolower(*p2)) {
+        assert(!ourisalpha(*p2) || myisupper(*p2)); // Already upper-case.
+        if ((u8)mytoupper(*p1) != *p2) {
             return 1;
         }
     }
@@ -108,7 +109,7 @@ int cmpNocaseNaive(const u8 *p1, const u8 *p2, size_t len) {
 
 static really_inline
 int cmpCaseNaive(const u8 *p1, const u8 *p2, size_t len) {
-    const u8 *pEnd = (const u8 *)p1 + len;
+    const u8 *pEnd = p1 + len;
     for (; p1 < pEnd; p1++, p2++) {
         if (*p1 != *p2) {
             return 1;
@@ -129,6 +130,11 @@ int cmpCaseNaive(const u8 *p1, const u8 *p2, size_t len) {
 
 #define CMP_SIZE sizeof(CMP_T)
 
+/**
+ * \brief Compare two strings, optionally caselessly.
+ *
+ * Note: If nocase is true, p2 is assumed to be already upper-case.
+ */
 #if defined(ARCH_IA32)
 static UNUSED never_inline
 #else
@@ -145,11 +151,13 @@ int cmpForward(const u8 *p1, const u8 *p2, size_t len, char nocase) {
 
     if (nocase) { // Case-insensitive version.
         for (; p1 < p1_end; p1 += CMP_SIZE, p2 += CMP_SIZE) {
-            if (TOUPPER(ULOAD(p1)) != TOUPPER(ULOAD(p2))) {
+            assert(ULOAD(p2) == TOUPPER(ULOAD(p2))); // Already upper-case.
+            if (TOUPPER(ULOAD(p1)) != ULOAD(p2)) {
                 return 1;
             }
         }
-        if (TOUPPER(ULOAD(p1_end)) != TOUPPER(ULOAD(p2_end))) {
+        assert(ULOAD(p2_end) == TOUPPER(ULOAD(p2_end))); // Already upper-case.
+        if (TOUPPER(ULOAD(p1_end)) != ULOAD(p2_end)) {
             return 1;
         }
     } else { // Case-sensitive version.