[njs] String.prototype.lastIndexOf() method fix.

Igor Sysoev igor at sysoev.ru
Thu Oct 27 13:50:15 UTC 2016


details:   http://hg.nginx.org/njs/rev/d84e34c2dbde
branches:  
changeset: 225:d84e34c2dbde
user:      Igor Sysoev <igor at sysoev.ru>
date:      Thu Oct 27 16:38:05 2016 +0300
description:
String.prototype.lastIndexOf() method fix.

In collaboration with Valentin Bartenev.

diffstat:

 njs/njs_string.c |  24 +++++++++++++-----------
 1 files changed, 13 insertions(+), 11 deletions(-)

diffs (63 lines):

diff -r 86fd59307356 -r d84e34c2dbde njs/njs_string.c
--- a/njs/njs_string.c	Thu Oct 27 11:14:46 2016 +0300
+++ b/njs/njs_string.c	Thu Oct 27 16:38:05 2016 +0300
@@ -1290,12 +1290,14 @@ njs_string_prototype_last_index_of(njs_v
     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_v
                     goto done;
                 }
 
-                p--;
                 index--;
-
-            } while (index >= 0);
+                p--;
+
+            } while (p >= string.start);
 
         } else {
             /* UTF-8 string. */
@@ -1345,22 +1347,22 @@ njs_string_prototype_last_index_of(njs_v
                 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);



More information about the nginx-devel mailing list