Skip to content

Commit a1e0185

Browse files
committed
Implement String.prototype.lastIndexOf()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com
1 parent 39cf5aa commit a1e0185

File tree

4 files changed

+281
-7
lines changed

4 files changed

+281
-7
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-helpers.cpp

Lines changed: 171 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "ecma-builtin-helpers.h"
1818

19+
#include "ecma-alloc.h"
1920
#include "ecma-array-object.h"
2021
#include "ecma-builtins.h"
2122
#include "ecma-conversion.h"
@@ -470,22 +471,33 @@ ecma_builtin_helper_array_concat_value (ecma_object_t *obj_p, /**< array */
470471
* This function clamps the given index to the [0, length] range.
471472
* If the index is negative, 0 value is used.
472473
* If the index is greater than the length of the string, the normalized index will be the length of the string.
474+
* NaN is mapped to zero or length depending on the nan_to_zero parameter.
473475
*
474476
* See also:
475477
* ECMA-262 v5, 15.5.4.15
476478
*
477479
* Used by:
478480
* - The String.prototype.substring routine.
481+
* - The String.prototype.indexOf routine.
482+
* - The ecma_builtin_helper_string_prototype_object_index_of helper routine.
479483
*
480484
* @return uint32_t - the normalized value of the index
481485
*/
482486
uint32_t
483487
ecma_builtin_helper_string_index_normalize (ecma_number_t index, /**< index */
484-
uint32_t length) /**< string's length */
488+
uint32_t length, /**< string's length */
489+
bool nan_to_zero) /**< whether NaN is mapped to zero (t) or length (f) */
485490
{
486491
uint32_t norm_index = 0;
487492

488-
if (!ecma_number_is_nan (index) && !ecma_number_is_negative (index))
493+
if (ecma_number_is_nan (index))
494+
{
495+
if (!nan_to_zero)
496+
{
497+
norm_index = length;
498+
}
499+
}
500+
else if (!ecma_number_is_negative (index))
489501
{
490502
if (ecma_number_is_infinity (index))
491503
{
@@ -505,6 +517,163 @@ ecma_builtin_helper_string_index_normalize (ecma_number_t index, /**< index */
505517
return norm_index;
506518
} /* ecma_builtin_helper_string_index_normalize */
507519

520+
/*
521+
* Helper function for string indexOf and lastIndexOf functions
522+
*
523+
* This function implements string indexOf and lastIndexOf with required checks and conversions.
524+
*
525+
* See also:
526+
* ECMA-262 v5, 15.5.4.7
527+
* ECMA-262 v5, 15.5.4.8
528+
*
529+
* Used by:
530+
* - The String.prototype.indexOf routine.
531+
* - The String.prototype.lastIndexOf routine.
532+
*
533+
* @return uint32_t - (last)index of search string
534+
*/
535+
ecma_completion_value_t
536+
ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**< this argument */
537+
ecma_value_t arg1, /**< routine's first argument */
538+
ecma_value_t arg2, /**< routine's second argument */
539+
bool firstIndex) /**< routine's third argument */
540+
{
541+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
542+
543+
/* 1 */
544+
ECMA_TRY_CATCH (check_coercible_val,
545+
ecma_op_check_object_coercible (this_arg),
546+
ret_value);
547+
548+
/* 2 */
549+
ECMA_TRY_CATCH (to_str_val,
550+
ecma_op_to_string (this_arg),
551+
ret_value);
552+
553+
/* 3 */
554+
ECMA_TRY_CATCH (search_str_val,
555+
ecma_op_to_string (arg1),
556+
ret_value);
557+
558+
/* 4 */
559+
ECMA_OP_TO_NUMBER_TRY_CATCH (pos_num,
560+
arg2,
561+
ret_value);
562+
563+
/* 6 */
564+
ecma_string_t *original_str_p = ecma_get_string_from_value (to_str_val);
565+
const ecma_length_t original_len = ecma_string_get_length (original_str_p);
566+
const lit_utf8_size_t original_size = ecma_string_get_size (original_str_p);
567+
568+
/* 4b, 5, 7 */
569+
ecma_length_t start = ecma_builtin_helper_string_index_normalize (pos_num, original_len, firstIndex);
570+
571+
/* 8 */
572+
ecma_string_t *search_str_p = ecma_get_string_from_value (search_str_val);
573+
const ecma_length_t search_len = ecma_string_get_length (search_str_p);
574+
const lit_utf8_size_t search_size = ecma_string_get_size (search_str_p);
575+
576+
ecma_number_t *ret_num_p = ecma_alloc_number ();
577+
*ret_num_p = ecma_int32_to_number (-1);
578+
579+
/* 9 */
580+
if (search_len <= original_len)
581+
{
582+
if (!search_len)
583+
{
584+
*ret_num_p = ecma_uint32_to_number (firstIndex ? 0 : original_len);
585+
}
586+
else
587+
{
588+
/* create utf8 string from original string and advance to position */
589+
MEM_DEFINE_LOCAL_ARRAY (original_str_utf8_p,
590+
original_size,
591+
lit_utf8_byte_t);
592+
593+
ecma_string_to_utf8_string (original_str_p,
594+
original_str_utf8_p,
595+
(ssize_t) (original_size));
596+
597+
lit_utf8_iterator_t original_it = lit_utf8_iterator_create (original_str_utf8_p, original_size);
598+
599+
ecma_length_t index = start;
600+
lit_utf8_iterator_advance (&original_it, index);
601+
602+
/* create utf8 string from search string */
603+
MEM_DEFINE_LOCAL_ARRAY (search_str_utf8_p,
604+
search_size,
605+
lit_utf8_byte_t);
606+
607+
ecma_string_to_utf8_string (search_str_p,
608+
search_str_utf8_p,
609+
(ssize_t) (search_size));
610+
611+
lit_utf8_iterator_t search_it = lit_utf8_iterator_create (search_str_utf8_p, search_size);
612+
613+
/* iterate original string and try to match at each position */
614+
bool found = false;
615+
bool searching = true;
616+
617+
while (!found && searching)
618+
{
619+
/* match as long as possible */
620+
ecma_length_t match_len = 0;
621+
lit_utf8_iterator_t stored_original_it = original_it;
622+
623+
while (match_len < search_len &&
624+
index + match_len < original_len &&
625+
lit_utf8_iterator_read_next (&original_it) == lit_utf8_iterator_read_next (&search_it))
626+
{
627+
match_len++;
628+
}
629+
630+
/* check for match */
631+
if (match_len == search_len)
632+
{
633+
*ret_num_p = ecma_uint32_to_number (index);
634+
found = true;
635+
}
636+
else
637+
{
638+
/* inc/dec index and update iterators and search condition */
639+
lit_utf8_iterator_seek_bos (&search_it);
640+
original_it = stored_original_it;
641+
642+
if (firstIndex)
643+
{
644+
if ((searching = (index <= original_len - search_len)))
645+
{
646+
lit_utf8_iterator_incr (&original_it);
647+
index++;
648+
}
649+
}
650+
else
651+
{
652+
if ((searching = (index > 0)))
653+
{
654+
lit_utf8_iterator_decr (&original_it);
655+
index--;
656+
}
657+
}
658+
}
659+
}
660+
661+
MEM_FINALIZE_LOCAL_ARRAY (search_str_utf8_p);
662+
MEM_FINALIZE_LOCAL_ARRAY (original_str_utf8_p);
663+
}
664+
}
665+
666+
ecma_value_t new_value = ecma_make_number_value (ret_num_p);
667+
ret_value = ecma_make_normal_completion_value (new_value);
668+
669+
ECMA_OP_TO_NUMBER_FINALIZE (pos_num);
670+
ECMA_FINALIZE (search_str_val);
671+
ECMA_FINALIZE (to_str_val);
672+
ECMA_FINALIZE (check_coercible_val);
673+
674+
return ret_value;
675+
} /* ecma_builtin_helper_string_index_normalize */
676+
508677
/**
509678
* Helper function for using [[DefineOwnProperty]].
510679
*

jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ extern ecma_completion_value_t ecma_builtin_helper_array_concat_value (ecma_obje
3434
uint32_t *length,
3535
ecma_value_t);
3636
extern uint32_t ecma_builtin_helper_array_index_normalize (ecma_number_t index, uint32_t length);
37-
extern uint32_t ecma_builtin_helper_string_index_normalize (ecma_number_t index, uint32_t length);
37+
extern uint32_t ecma_builtin_helper_string_index_normalize (ecma_number_t index, uint32_t length, bool nan_to_zero);
38+
extern ecma_completion_value_t ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg,
39+
ecma_value_t arg1,
40+
ecma_value_t arg2,
41+
bool firstIndex);
3842
extern ecma_completion_value_t ecma_builtin_helper_def_prop (ecma_object_t *obj_p,
3943
ecma_string_t *index_p,
4044
ecma_value_t value,

jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ ecma_builtin_string_prototype_object_index_of (ecma_value_t this_arg, /**< this
335335
const lit_utf8_size_t original_size = ecma_string_get_size (original_str_p);
336336

337337
/* 4b, 6 */
338-
ecma_length_t start = ecma_builtin_helper_string_index_normalize (pos_num, original_len);
338+
ecma_length_t start = ecma_builtin_helper_string_index_normalize (pos_num, original_len, true);
339339

340340
/* 7 */
341341
ecma_string_t *search_str_p = ecma_get_string_from_value (search_str_val);
@@ -439,7 +439,7 @@ ecma_builtin_string_prototype_object_last_index_of (ecma_value_t this_arg, /**<
439439
ecma_value_t arg1, /**< routine's first argument */
440440
ecma_value_t arg2) /**< routine's second argument */
441441
{
442-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg1, arg2);
442+
return ecma_builtin_helper_string_prototype_object_index_of (this_arg, arg1, arg2, false);
443443
} /* ecma_builtin_string_prototype_object_last_index_of */
444444

445445
/**
@@ -906,7 +906,7 @@ ecma_builtin_string_prototype_object_substring (ecma_value_t this_arg, /**< this
906906

907907
ecma_length_t start = 0, end = len;
908908

909-
start = ecma_builtin_helper_string_index_normalize (start_num, len);
909+
start = ecma_builtin_helper_string_index_normalize (start_num, len, true);
910910

911911
/* 5, 7 */
912912
if (ecma_is_value_undefined (arg2))
@@ -919,7 +919,7 @@ ecma_builtin_string_prototype_object_substring (ecma_value_t this_arg, /**< this
919919
arg2,
920920
ret_value);
921921

922-
end = ecma_builtin_helper_string_index_normalize (end_num, len);
922+
end = ecma_builtin_helper_string_index_normalize (end_num, len, true);
923923

924924
ECMA_OP_TO_NUMBER_FINALIZE (end_num);
925925
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
// check properties
17+
assert(Object.getOwnPropertyDescriptor(String.prototype.lastIndexOf, 'length').configurable === false);
18+
19+
assert(Object.getOwnPropertyDescriptor(String.prototype.lastIndexOf, 'length').enumerable === false);
20+
21+
assert(Object.getOwnPropertyDescriptor(String.prototype.lastIndexOf, 'length').writable === false);
22+
23+
assert(String.prototype.lastIndexOf.length === 1);
24+
25+
// simple checks
26+
assert("Hello welcome, welcome to the universe.".lastIndexOf("welcome") === 15);
27+
28+
assert("Hello world, welcome to the universe.".lastIndexOf("Hello world, welcome to the universe.") === 0);
29+
30+
assert("Hello welcome, welcome to the universe.".lastIndexOf("welcome", 5) === -1);
31+
32+
assert("Hello welcome, welcome to the universe.".lastIndexOf("welcome", -100) == -1);
33+
34+
assert("Hello welcome, welcome to the universe.".lastIndexOf("welcome", 15) === 15);
35+
36+
assert("Hello welcome, welcome to the universe o.".lastIndexOf("o", 10) === 10);
37+
38+
assert("Hello welcome, welcome to the universe o.".lastIndexOf("o", 25) === 24);
39+
40+
assert("Helloooo woooorld".lastIndexOf("oooo", 6) === 4);
41+
42+
// check empty string
43+
assert(String.prototype.lastIndexOf.call(new String()) === -1);
44+
45+
assert(String.prototype.lastIndexOf.call("Hello world, welcome to the universe.","") === 37);
46+
47+
assert(String.prototype.lastIndexOf.call("","") === 0);
48+
49+
// check NaN
50+
assert("Hello world, welcome to the universe.".lastIndexOf(NaN) === -1);
51+
52+
assert("Hello world, welcome to the universe.".lastIndexOf("o", NaN) === 22);
53+
54+
// check Object
55+
assert(String.prototype.lastIndexOf.call({}) === -1);
56+
57+
// check +-Inf
58+
assert("hello world!".lastIndexOf("world", -Infinity) === -1);
59+
60+
assert("hello world!".lastIndexOf("world", Infinity) === 6);
61+
62+
// check numbers
63+
assert("hello world!".lastIndexOf(-1) === -1);
64+
65+
assert("hello 0 world!".lastIndexOf(-0) === 6);
66+
67+
// check undefined
68+
assert("hello world!".lastIndexOf(undefined) === -1);
69+
70+
var undefined_var;
71+
assert("Hello world, welcome to the universe.".lastIndexOf("welcome", undefined_var) === 13);
72+
73+
// check booleans
74+
assert("true".lastIndexOf(true, false) === 0);
75+
76+
// check coercible - undefined
77+
try {
78+
assert(String.prototype.lastIndexOf.call(undefined) === -1);
79+
assert(false);
80+
} catch(e) {
81+
assert(e instanceof TypeError);
82+
}
83+
84+
// check coercible - null
85+
try {
86+
assert(String.prototype.lastIndexOf.call(null, 0) === -1);
87+
assert(false);
88+
} catch (e) {
89+
assert(e instanceof TypeError);
90+
}
91+
92+
// check coercible - Boolean
93+
assert(String.prototype.lastIndexOf.call(true, "e") === 3);
94+
assert(String.prototype.lastIndexOf.call(false, "e") === 4);
95+
96+
// check coercible - Object
97+
var test_object = {firstName:"John", lastName:"Doe"};
98+
assert(String.prototype.lastIndexOf.call(test_object, "Obj") === 8);
99+
100+
// check coercible - Number
101+
assert(String.prototype.lastIndexOf.call(123, "2") === 1);

0 commit comments

Comments
 (0)