Skip to content

Commit 790d120

Browse files
committed
Implement String.prototype.lastIndexOf()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent e34ab90 commit 790d120

File tree

4 files changed

+278
-7
lines changed

4 files changed

+278
-7
lines changed

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

Lines changed: 168 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"
@@ -481,11 +482,19 @@ ecma_builtin_helper_array_concat_value (ecma_object_t *obj_p, /**< array */
481482
*/
482483
uint32_t
483484
ecma_builtin_helper_string_index_normalize (ecma_number_t index, /**< index */
484-
uint32_t length) /**< string's length */
485+
uint32_t length, /**< string's length */
486+
bool nan_to_zero) /**< whether NaN is mapped to zero (t) or length (f) */
485487
{
486488
uint32_t norm_index = 0;
487489

488-
if (!ecma_number_is_nan (index) && !ecma_number_is_negative (index))
490+
if (ecma_number_is_nan (index))
491+
{
492+
if (!nan_to_zero)
493+
{
494+
norm_index = length;
495+
}
496+
}
497+
else if (!ecma_number_is_negative (index))
489498
{
490499
if (ecma_number_is_infinity (index))
491500
{
@@ -505,6 +514,163 @@ ecma_builtin_helper_string_index_normalize (ecma_number_t index, /**< index */
505514
return norm_index;
506515
} /* ecma_builtin_helper_string_index_normalize */
507516

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

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

3943
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
4044

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)