]> git.kaiwu.me - njs.git/commitdiff
String.prototype.lastIndexOf() method fix.
authorIgor Sysoev <igor@sysoev.ru>
Thu, 27 Oct 2016 13:38:05 +0000 (16:38 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Thu, 27 Oct 2016 13:38:05 +0000 (16:38 +0300)
In collaboration with Valentin Bartenev.

njs/njs_string.c

index 8a5f3d365b4b3e71825182e448e85fc5770a4d7b..c0e2e195e50490da9f39773a985742b9e580f5fe 100644 (file)
@@ -1290,12 +1290,14 @@ njs_string_prototype_last_index_of(njs_vm_t *vm, njs_value_t *args,
     const u_char       *p, *end;
     njs_string_prop_t  string, search;
 
+    index = -1;
+
     if (nargs > 1) {
         length = njs_string_prop(&string, &args[0]);
         search_length = njs_string_prop(&search, &args[1]);
 
         if (length < search_length) {
-            goto small;
+            goto done;
         }
 
         index = NJS_STRING_MAX_LENGTH;
@@ -1328,10 +1330,10 @@ njs_string_prototype_last_index_of(njs_vm_t *vm, njs_value_t *args,
                     goto done;
                 }
 
-                p--;
                 index--;
+                p--;
 
-            } while (index >= 0);
+            } while (p >= string.start);
 
         } else {
             /* UTF-8 string. */
@@ -1345,22 +1347,22 @@ njs_string_prototype_last_index_of(njs_vm_t *vm, njs_value_t *args,
                 p = nxt_utf8_prev(p);
             }
 
-            do {
+            for ( ;; ) {
                 if (memcmp(p, search.start, search.size) == 0) {
                     goto done;
                 }
 
-                p = nxt_utf8_prev(p);
                 index--;
 
-            } while (index >= 0);
+                if (p <= string.start) {
+                    break;
+                }
+
+                p = nxt_utf8_prev(p);
+            }
         }
     }
 
-small:
-
-    index = -1;
-
 done:
 
     njs_number_set(&vm->retval, index);