From: Dmitry Volyntsev Date: Fri, 31 May 2019 12:11:39 +0000 (+0300) Subject: Fixed String.prototype.toBytes() for ASCII strings. X-Git-Tag: 0.3.3~20 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/%7B@url%7D?a=commitdiff_plain;h=3515292d7791e706795f0957ed270472715907f0;p=njs.git Fixed String.prototype.toBytes() for ASCII strings. This closes #155 issue on Github. --- diff --git a/njs/njs_string.c b/njs/njs_string.c index 04acad78..a7d2645e 100644 --- a/njs/njs_string.c +++ b/njs/njs_string.c @@ -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; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index c6e6f4f4..92aab421 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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))"),