[njs] String.prototypes.includes() method.

Igor Sysoev igor at sysoev.ru
Thu Oct 27 09:51:55 UTC 2016


details:   http://hg.nginx.org/njs/rev/86fd59307356
branches:  
changeset: 224:86fd59307356
user:      Igor Sysoev <igor at sysoev.ru>
date:      Thu Oct 27 11:14:46 2016 +0300
description:
String.prototypes.includes() method.

diffstat:

 njs/njs_string.c         |  78 ++++++++++++++++++++++++++++++++++++++++++++++++
 njs/test/njs_unit_test.c |  18 +++++++++++
 2 files changed, 96 insertions(+), 0 deletions(-)

diffs (123 lines):

diff -r da63e61ecef4 -r 86fd59307356 njs/njs_string.c
--- a/njs/njs_string.c	Thu Oct 27 10:56:54 2016 +0300
+++ b/njs/njs_string.c	Thu Oct 27 11:14:46 2016 +0300
@@ -1369,6 +1369,76 @@ done:
 }
 
 
+static njs_ret_t
+njs_string_prototype_includes(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    ssize_t            index, length, search_length;
+    const u_char       *p, *end;
+    const njs_value_t  *retval;
+    njs_string_prop_t  string, search;
+
+    retval = &njs_value_true;
+
+    if (nargs > 1) {
+        search_length = njs_string_prop(&search, &args[1]);
+
+        if (search_length == 0) {
+            goto done;
+        }
+
+        length = njs_string_prop(&string, &args[0]);
+
+        if (length < search_length) {
+            goto small;
+        }
+
+        index = 0;
+
+        if (nargs > 2) {
+            index = args[2].data.u.number;
+
+            if (index < 0) {
+                index = 0;
+            }
+        }
+
+        if (index < length) {
+            end = string.start + string.size;
+
+            if (string.size == (size_t) length) {
+                /* Byte or ASCII string. */
+                p = string.start + index;
+
+            } else {
+                /* UTF-8 string. */
+                p = njs_string_offset(string.start, end, index);
+            }
+
+            end -= search.size - 1;
+
+            while (p < end) {
+                if (memcmp(p, search.start, search.size) == 0) {
+                    goto done;
+                }
+
+                p++;
+            }
+        }
+    }
+
+small:
+
+    retval = &njs_value_false;
+
+done:
+
+    vm->retval = *retval;
+
+    return NXT_OK;
+}
+
+
 /*
  * njs_string_offset() assumes that index is correct
  * and the optional offset map has been initialized.
@@ -2812,6 +2882,14 @@ static const njs_object_prop_t  njs_stri
                      NJS_STRING_OBJECT_ARG, NJS_STRING_ARG, NJS_INTEGER_ARG),
     },
 
+    /* ES6. */
+    {
+        .type = NJS_METHOD,
+        .name = njs_string("includes"),
+        .value = njs_native_function(njs_string_prototype_includes, 0,
+                     NJS_STRING_OBJECT_ARG, NJS_STRING_ARG, NJS_INTEGER_ARG),
+    },
+
     {
         .type = NJS_METHOD,
         .name = njs_string("toLowerCase"),
diff -r da63e61ecef4 -r 86fd59307356 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Oct 27 10:56:54 2016 +0300
+++ b/njs/test/njs_unit_test.c	Thu Oct 27 11:14:46 2016 +0300
@@ -3272,6 +3272,24 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("''.lastIndexOf('')"),
       nxt_string("0") },
 
+    { nxt_string("''.includes('')"),
+      nxt_string("true") },
+
+    { nxt_string("'12345'.includes()"),
+      nxt_string("false") },
+
+    { nxt_string("'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'.includes('лмно', 10)"),
+      nxt_string("true") },
+
+    { nxt_string("'абв абв абвгдежз'.includes('абвгд', 7)"),
+      nxt_string("true") },
+
+    { nxt_string("'абв абв абвгдежз'.includes('абвгд', 8)"),
+      nxt_string("true") },
+
+    { nxt_string("'абв абв абвгдежз'.includes('абвгд', 9)"),
+      nxt_string("false") },
+
     { nxt_string("'ABC'.toLowerCase()"),
       nxt_string("abc") },
 


More information about the nginx-devel mailing list