]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
lib/unicode: optimize/simplify Unicode_FindLastSubstrInRange
authorVMware, Inc <>
Mon, 20 Dec 2010 21:57:01 +0000 (13:57 -0800)
committerMarcelo Vanzin <mvanzin@vmware.com>
Mon, 20 Dec 2010 21:57:01 +0000 (13:57 -0800)
It's a bit complex. Make the code faster and simpler.

Signed-off-by: Marcelo Vanzin <mvanzin@vmware.com>
open-vm-tools/lib/unicode/unicodeSimpleOperations.c

index bfd0693f813866472b891f72f1a3660a63f43b30..5e52ffb11dbd73a41b0e1b315ce6c433ae10d1d4 100644 (file)
@@ -323,8 +323,8 @@ Unicode_FindSubstrInRange(ConstUnicode str,              // IN:
    for (index = strStart;
         index <= (strStart + strLength - strToFindLength);
         index++) {
-      Bool match = FALSE;
       UnicodeIndex i;
+      Bool match = FALSE;
       UnicodeIndex indexSrc = index;
       UnicodeIndex indexSrch = strToFindStart;
 
@@ -391,7 +391,6 @@ Unicode_FindLastSubstrInRange(ConstUnicode str,              // IN:
                               UnicodeIndex strToFindLength)  // IN:
 {
    UnicodeIndex index;
-   UnicodeIndex strToFindEnd;
    uint32 *utf32Source = NULL;
    uint32 *utf32Search = NULL;
 
@@ -446,27 +445,25 @@ Unicode_FindLastSubstrInRange(ConstUnicode str,              // IN:
     * to be searched.
     */
 
-   strToFindEnd = strToFindStart + strToFindLength - 1;
-
-   for (index = strStart + strLength - 1; index >= strStart; index--) {
-      if (utf32Source[index] == utf32Search[strToFindEnd]) {
-         UnicodeIndex strSubOffset = index;
-         UnicodeIndex strToFindSubOffset = strToFindEnd;
-
-         while (TRUE) {
-            if (strToFindSubOffset == strToFindStart) {
-               index = strSubOffset;  // Found the substring.
-               goto bail;
-            }
+   for (index = strStart + strLength - strToFindLength;
+        index >= strStart;
+        index--) {
+      UnicodeIndex i;
+      Bool match = FALSE;
+      UnicodeIndex indexSrc = index;
+      UnicodeIndex indexSrch = strToFindStart;
 
-            strToFindSubOffset--;
-            strSubOffset--;
+      for (i = 0; i < strToFindLength; i++) {
+         match = (utf32Source[indexSrc++] == utf32Search[indexSrch++]);
 
-            if (utf32Source[strSubOffset] != utf32Search[strToFindSubOffset]) {
-               break;
-            }
+         if (!match) {
+            break;
          }
       }
+
+      if (match) {
+         goto bail;
+      }
    }
 
    index = UNICODE_INDEX_NOT_FOUND;