[njs] Fixed String.prototype.toBytes() for ASCII strings.

Dmitry Volyntsev xeioex at nginx.com
Fri May 31 12:12:06 UTC 2019


details:   https://hg.nginx.org/njs/rev/1041e3241457
branches:  
changeset: 996:1041e3241457
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri May 31 15:11:39 2019 +0300
description:
Fixed String.prototype.toBytes() for ASCII strings.

This closes #155 issue on Github.

diffstat:

 njs/njs_string.c         |   2 +-
 njs/test/njs_unit_test.c |  12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletions(-)

diffs (34 lines):

diff -r cc9a99f09b51 -r 1041e3241457 njs/njs_string.c
--- a/njs/njs_string.c	Thu May 30 16:46:26 2019 +0300
+++ b/njs/njs_string.c	Fri May 31 15:11:39 2019 +0300
@@ -1048,7 +1048,7 @@ njs_string_prototype_to_bytes(njs_vm_t *
 
     if (nxt_fast_path(p != NULL)) {
 
-        if (string.length != 0) {
+        if (string.length != string.size) {
             /* UTF-8 string. */
             end = string.start + string.size;
 
diff -r cc9a99f09b51 -r 1041e3241457 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu May 30 16:46:26 2019 +0300
+++ b/njs/test/njs_unit_test.c	Fri May 31 15:11:39 2019 +0300
@@ -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))"),


More information about the nginx-devel mailing list