[njs] String.prototype.repeat() method.

Valentin Bartenev vbart at nginx.com
Fri Oct 28 11:44:02 UTC 2016


details:   http://hg.nginx.org/njs/rev/2cc08001fc78
branches:  
changeset: 227:2cc08001fc78
user:      Valentin Bartenev <vbart at nginx.com>
date:      Fri Oct 28 14:20:23 2016 +0300
description:
String.prototype.repeat() method.

diffstat:

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

diffs (100 lines):

diff -r 5de122a4031a -r 2cc08001fc78 njs/njs_string.c
--- a/njs/njs_string.c	Fri Oct 28 13:32:33 2016 +0300
+++ b/njs/njs_string.c	Fri Oct 28 14:20:23 2016 +0300
@@ -1743,6 +1743,53 @@ done:
 }
 
 
+static njs_ret_t
+njs_string_prototype_repeat(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    u_char             *p, *start;
+    int32_t            n, max;
+    uint32_t           size, length;
+    njs_string_prop_t  string;
+
+    n = 0;
+
+    (void) njs_string_prop(&string, &args[0]);
+
+    if (nargs > 1) {
+        max = NJS_STRING_MAX_LENGTH / string.size;
+        n = args[1].data.u.number;
+
+        if (nxt_slow_path(n < 0 || n > max)) {
+            vm->exception = &njs_exception_range_error;
+            return NXT_ERROR;
+        }
+    }
+
+    size = string.size * n;
+    length = string.length * n;
+
+    start = njs_string_alloc(vm, &vm->retval, size, length);
+    if (nxt_slow_path(start == NULL)) {
+        return NXT_ERROR;
+    }
+
+    p = start;
+
+    while (n != 0) {
+        p = memcpy(p, string.start, string.size);
+        p += string.size;
+        n--;
+    }
+
+    if (length >= NJS_STRING_MAP_OFFSET && size != length) {
+        njs_string_offset_map_init(start, size);
+    }
+
+    return NXT_OK;
+}
+
+
 /*
  * String.search([regexp])
  */
@@ -3042,6 +3089,14 @@ static const njs_object_prop_t  njs_stri
                      NJS_STRING_OBJECT_ARG),
     },
 
+    /* ES6. */
+    {
+        .type = NJS_METHOD,
+        .name = njs_string("repeat"),
+        .value = njs_native_function(njs_string_prototype_repeat, 0,
+                     NJS_STRING_OBJECT_ARG, NJS_INTEGER_ARG),
+    },
+
     {
         .type = NJS_METHOD,
         .name = njs_string("search"),
diff -r 5de122a4031a -r 2cc08001fc78 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Fri Oct 28 13:32:33 2016 +0300
+++ b/njs/test/njs_unit_test.c	Fri Oct 28 14:20:23 2016 +0300
@@ -3611,6 +3611,24 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("'0123456789'.split('').reverse().join('')"),
       nxt_string("9876543210") },
 
+    { nxt_string("'abc'.repeat(3)"),
+      nxt_string("abcabcabc") },
+
+    { nxt_string("'абв'.repeat(3)"),
+      nxt_string("абвабвабв") },
+
+    { nxt_string("'abc'.repeat(0)"),
+      nxt_string("") },
+
+    { nxt_string("'abc'.repeat(NaN)"),
+      nxt_string("") },
+
+    { nxt_string("'abc'.repeat(Infinity)"),
+      nxt_string("RangeError") },
+
+    { nxt_string("'abc'.repeat(-1)"),
+      nxt_string("RangeError") },
+
     { nxt_string("encodeURI()"),
       nxt_string("undefined")},
 


More information about the nginx-devel mailing list