From: Igor Sysoev Date: Wed, 28 Feb 2018 13:20:11 +0000 (+0300) Subject: Fixed String.prototype.toUTF8() function. X-Git-Tag: 0.2.0~30 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=bd21362fa0473b053514dd6e0518c3f87a0ed42e;p=njs.git Fixed String.prototype.toUTF8() function. A byte string returned by String.prototype.toUTF8() had length equal to its size so the string can be processed later as an ASCII string. --- diff --git a/njs/njs_string.c b/njs/njs_string.c index b1690ac5..544a3548 100644 --- a/njs/njs_string.c +++ b/njs/njs_string.c @@ -1051,6 +1051,11 @@ njs_string_slice(njs_vm_t *vm, njs_value_t *dst, start += slice->start; size = slice->length; + if (string->length == 0) { + /* Byte string. */ + length = 0; + } + } else { /* UTF-8 string. */ end = start + string->size; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index eecb57d8..1fd0f9e1 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -3529,6 +3529,15 @@ static njs_unit_test_t njs_test[] = { nxt_string("'α'.toUTF8()[0]"), nxt_string("\xCE") }, + { nxt_string("/^\\x80$/.test('\\x80'.toBytes())"), + nxt_string("true") }, + + { nxt_string("/^\\xC2\\x80$/.test('\\x80'.toUTF8())"), + nxt_string("true") }, + + { nxt_string("'α'.toUTF8().toBytes()"), + nxt_string("α") }, + { nxt_string("var a = 'a'.toBytes() + 'α'; a + a.length"), nxt_string("aα3") },