Welcome! Log In Create A New Profile

Advanced

[njs] String.prototype.indexOf() method fixes.

October 27, 2016 05:54AM
details: http://hg.nginx.org/njs/rev/74785cebd8df
branches:
changeset: 222:74785cebd8df
user: Igor Sysoev <igor@sysoev.ru>
date: Thu Oct 27 10:47:48 2016 +0300
description:
String.prototype.indexOf() method fixes.

diffstat:

njs/njs_string.c | 66 +++++++++++++++++++++++++++++++++++++++++------
njs/test/njs_unit_test.c | 6 ++++
2 files changed, 63 insertions(+), 9 deletions(-)

diffs (104 lines):

diff -r f21747d5ac83 -r 74785cebd8df njs/njs_string.c
--- a/njs/njs_string.c Tue Oct 25 17:52:18 2016 +0300
+++ b/njs/njs_string.c Thu Oct 27 10:47:48 2016 +0300
@@ -1212,24 +1212,72 @@ static njs_ret_t
njs_string_prototype_index_of(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
njs_index_t unused)
{
- ssize_t start, index;
-
- index = -1;
+ ssize_t index, length, search_length;
+ const u_char *p, *end;
+ njs_string_prop_t string, search;

if (nargs > 1) {
- start = 0;
+ length = njs_string_prop(&string, &args[0]);
+ search_length = njs_string_prop(&search, &args[1]);
+
+ if (length < search_length) {
+ goto small;
+ }
+
+ index = 0;

if (nargs > 2) {
- start = args[2].data.u.number;
-
- if (start < 0) {
- start = 0;
+ index = args[2].data.u.number;
+
+ if (index < 0) {
+ index = 0;
}
}

- index = njs_string_index_of(vm, &args[0], &args[1], start);
+ if (index < length) {
+ end = string.start + string.size;
+
+ if (string.size == (size_t) length) {
+ /* Byte or ASCII string. */
+
+ end -= (search.size - 1);
+
+ for (p = string.start + index; p < end; p++) {
+ if (memcmp(p, search.start, search.size) == 0) {
+ goto done;
+ }
+
+ index++;
+ }
+
+ } else {
+ /* UTF-8 string. */
+
+ p = njs_string_offset(string.start, end, index);
+ end -= search.size - 1;
+
+ while (p < end) {
+ if (memcmp(p, search.start, search.size) == 0) {
+ goto done;
+ }
+
+ index++;
+ p = nxt_utf8_next(p, end);
+ }
+ }
+
+ } else if (search.size == 0) {
+ index = length;
+ goto done;
+ }
}

+small:
+
+ index = -1;
+
+done:
+
njs_number_set(&vm->retval, index);

return NXT_OK;
diff -r f21747d5ac83 -r 74785cebd8df njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Tue Oct 25 17:52:18 2016 +0300
+++ b/njs/test/njs_unit_test.c Thu Oct 27 10:47:48 2016 +0300
@@ -3212,9 +3212,15 @@ static njs_unit_test_t njs_test[] =
{ nxt_string("'12345'.indexOf()"),
nxt_string("-1") },

+ { nxt_string("''.indexOf('')"),
+ nxt_string("0") },
+
{ nxt_string("'12345'.indexOf(45, '0')"),
nxt_string("3") },

+ { nxt_string("'12'.indexOf('12345')"),
+ nxt_string("-1") },
+
{ nxt_string("''.indexOf.call(12345, 45, '0')"),
nxt_string("3") },


_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] String.prototype.indexOf() method fixes.

Igor Sysoev 705 October 27, 2016 05:54AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 123
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready