]> git.kaiwu.me - njs.git/commitdiff
Fixed String.prototype.toBytes() for ASCII strings.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 31 May 2019 12:11:39 +0000 (15:11 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 31 May 2019 12:11:39 +0000 (15:11 +0300)
This closes #155 issue on Github.

njs/njs_string.c
njs/test/njs_unit_test.c

index 04acad78e21ee2f4fef0890b7a1b5497f970a5aa..a7d2645e8df697527fcae4e8af1f1f342c310419 100644 (file)
@@ -1048,7 +1048,7 @@ njs_string_prototype_to_bytes(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 
     if (nxt_fast_path(p != NULL)) {
 
-        if (string.length != 0) {
+        if (string.length != string.size) {
             /* UTF-8 string. */
             end = string.start + string.size;
 
index c6e6f4f4e27d050ec7efbaa2d72088e73de4adb2..92aab42188ba4a62287d73d5289e68d78ad0506a 100644 (file)
@@ -4644,6 +4644,18 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("'A'.repeat(16).toBytes() === 'A'.repeat(16)"),
       nxt_string("true") },
 
+    { nxt_string("'A'.repeat(38).toBytes(-5) === 'AAAAA'"),
+      nxt_string("true") },
+
+    { nxt_string("('α' + 'A'.repeat(32)).toBytes()"),
+      nxt_string("null") },
+
+    { nxt_string("('α' + 'A'.repeat(32)).toBytes(1) === 'A'.repeat(32)"),
+      nxt_string("true") },
+
+    { nxt_string("('α' + 'A'.repeat(40)).toBytes(-3,-1)"),
+      nxt_string("AA") },
+
     { nxt_string("var s = 'x'.repeat(2**10).repeat(2**14);"
                  "var a = Array(200).fill(s);"
                  "String.prototype.concat.apply(s, a.slice(1))"),