[njs] Fix in String.indexOf() and String.lastIndexOf() functions.

Igor Sysoev igor at sysoev.ru
Wed Jun 1 12:50:35 UTC 2016


details:   http://hg.nginx.org/njs/rev/1c96b24bcdd2
branches:  
changeset: 111:1c96b24bcdd2
user:      Igor Sysoev <igor at sysoev.ru>
date:      Fri Apr 29 17:02:52 2016 +0300
description:
Fix in String.indexOf() and String.lastIndexOf() functions.

diffstat:

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

diffs (53 lines):

diff -r 169958fd3f70 -r 1c96b24bcdd2 njs/njs_string.c
--- a/njs/njs_string.c	Mon Apr 25 19:30:27 2016 +0300
+++ b/njs/njs_string.c	Fri Apr 29 17:02:52 2016 +0300
@@ -1192,25 +1192,36 @@ njs_string_index_of(njs_vm_t *vm, njs_va
 
     if (index < length) {
 
-        p = string.start;
-        end = p + string.size;
-
         if (string.size == length) {
             /* Byte or ASCII string. */
-            p += index;
+            p = string.start + index;
+            end = (string.start + string.size) - (search.size - 1);
+
+            while (p < end) {
+                if (memcmp(p, search.start, search.size) == 0) {
+                    return index;
+                }
+
+                index++;
+                p++;
+            }
 
         } else {
             /* UTF-8 string. */
-            p = njs_string_offset(p, end, index);
-        }
-
-        while (p < end) {
-            if (memcmp(p, search.start, search.size) == 0) {
-                return index;
+            end = string.start + string.size;
+
+            p = njs_string_offset(string.start, end, index);
+
+            end -= search.size - 1;
+
+            while (p < end) {
+                if (memcmp(p, search.start, search.size) == 0) {
+                    return index;
+                }
+
+                index++;
+                p = nxt_utf8_next(p, end);
             }
-
-            index++;
-            p = nxt_utf8_next(p, end);
         }
 
     } else if (search.size == 0) {



More information about the nginx-devel mailing list