Skip to content

Commit 6f20a12

Browse files
committed
Implement String.prototype.indexOf()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com
1 parent 11c3103 commit 6f20a12

File tree

2 files changed

+244
-1
lines changed

2 files changed

+244
-1
lines changed

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

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,122 @@ ecma_builtin_string_prototype_object_index_of (ecma_value_t this_arg, /**< this
307307
ecma_value_t arg1, /**< routine's first argument */
308308
ecma_value_t arg2) /**< routine's second argument */
309309
{
310-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg1, arg2);
310+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
311+
312+
/* 1 */
313+
ECMA_TRY_CATCH (check_coercible_val,
314+
ecma_op_check_object_coercible (this_arg),
315+
ret_value);
316+
317+
/* 2 */
318+
ECMA_TRY_CATCH (to_str_val,
319+
ecma_op_to_string (this_arg),
320+
ret_value);
321+
322+
/* 3 */
323+
ECMA_TRY_CATCH (search_str_val,
324+
ecma_op_to_string (arg1),
325+
ret_value);
326+
327+
/* 4 */
328+
ECMA_OP_TO_NUMBER_TRY_CATCH (pos_num,
329+
arg2,
330+
ret_value);
331+
332+
/* 5 */
333+
ecma_string_t *original_str_p = ecma_get_string_from_value (to_str_val);
334+
const ecma_length_t original_len = ecma_string_get_length (original_str_p);
335+
const lit_utf8_size_t original_size = ecma_string_get_size (original_str_p);
336+
337+
/* 4b, 6 */
338+
ecma_length_t start = ecma_builtin_helper_string_index_normalize (pos_num, original_len);
339+
340+
/* 7 */
341+
ecma_string_t *search_str_p = ecma_get_string_from_value (search_str_val);
342+
const ecma_length_t search_len = ecma_string_get_length (search_str_p);
343+
const lit_utf8_size_t search_size = ecma_string_get_size (search_str_p);
344+
345+
ecma_number_t *ret_num_p = ecma_alloc_number ();
346+
*ret_num_p = ecma_int32_to_number (-1);
347+
348+
/* 8 */
349+
if (search_len <= original_len)
350+
{
351+
if (!search_len)
352+
{
353+
*ret_num_p = ecma_uint32_to_number (0);
354+
}
355+
else
356+
{
357+
/* create utf8 string from original string and advance to start position */
358+
MEM_DEFINE_LOCAL_ARRAY (original_str_utf8_p,
359+
original_size,
360+
lit_utf8_byte_t);
361+
362+
ecma_string_to_utf8_string (original_str_p,
363+
original_str_utf8_p,
364+
(ssize_t) (original_size));
365+
366+
lit_utf8_iterator_t original_it = lit_utf8_iterator_create (original_str_utf8_p, original_size);
367+
368+
ecma_length_t index = start;
369+
lit_utf8_iterator_advance (&original_it, index);
370+
371+
/* create utf8 string from search string */
372+
MEM_DEFINE_LOCAL_ARRAY (search_str_utf8_p,
373+
search_size,
374+
lit_utf8_byte_t);
375+
376+
ecma_string_to_utf8_string (search_str_p,
377+
search_str_utf8_p,
378+
(ssize_t) (search_size));
379+
380+
lit_utf8_iterator_t search_it = lit_utf8_iterator_create (search_str_utf8_p, search_size);
381+
382+
/* iterate original string and try to match at each position */
383+
bool found = false;
384+
385+
while (!found && index <= original_len - search_len)
386+
{
387+
ecma_length_t match_len = 0;
388+
lit_utf8_iterator_pos_t stored_original_pos = lit_utf8_iterator_get_pos (&original_it);
389+
390+
while (match_len < search_len &&
391+
lit_utf8_iterator_read_next (&original_it) == lit_utf8_iterator_read_next (&search_it))
392+
{
393+
match_len++;
394+
}
395+
396+
/* Check for match */
397+
if (match_len == search_len)
398+
{
399+
*ret_num_p = ecma_uint32_to_number (index);
400+
found = true;
401+
}
402+
else
403+
{
404+
/* reset iterators */
405+
lit_utf8_iterator_seek_bos (&search_it);
406+
lit_utf8_iterator_seek (&original_it, stored_original_pos);
407+
lit_utf8_iterator_incr (&original_it);
408+
}
409+
index++;
410+
}
411+
412+
MEM_FINALIZE_LOCAL_ARRAY (search_str_utf8_p);
413+
MEM_FINALIZE_LOCAL_ARRAY (original_str_utf8_p);
414+
}
415+
}
416+
417+
ecma_value_t new_value = ecma_make_number_value (ret_num_p);
418+
ret_value = ecma_make_normal_completion_value (new_value);
419+
420+
ECMA_OP_TO_NUMBER_FINALIZE (pos_num);
421+
ECMA_FINALIZE (search_str_val);
422+
ECMA_FINALIZE (to_str_val);
423+
ECMA_FINALIZE (check_coercible_val);
424+
425+
return ret_value;
311426
} /* ecma_builtin_string_prototype_object_index_of */
312427

313428
/**
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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.indexOf, 'length').configurable === false);
18+
19+
assert(Object.getOwnPropertyDescriptor(String.prototype.indexOf, 'length').enumerable === false);
20+
21+
assert(Object.getOwnPropertyDescriptor(String.prototype.indexOf, 'length').writable === false);
22+
23+
assert(String.prototype.indexOf.length === 1);
24+
25+
assert("Hello world, welcome to the universe.".indexOf("welcome") === 13);
26+
27+
assert("Hello world, welcome to the universe.".indexOf("Hello world, welcome to the universe.") === 0);
28+
29+
assert("Hello world, welcome to the universe.".indexOf("welcome",10) == 13);
30+
31+
assert("Hello world, welcome to the universe.".indexOf("welcome",-100) == 13);
32+
33+
assert("Hello world, welcome to the universe.".indexOf("welcome", 15) === -1);
34+
35+
assert("Hello world, welcome to the universe.".indexOf("o", 15) === 17);
36+
37+
// check utf8 strings
38+
assert("\uFFA2".indexOf("\uFFA2") === 0);
39+
40+
assert("\uFFA2".indexOf("A") === -1);
41+
42+
assert("w2\uFFA2A".indexOf("A") === 3);
43+
44+
assert("w2\u1D306A".indexOf("A") === 4);
45+
46+
// check surrogate pairs
47+
assert("\uD834\uDF06".indexOf("\uDF06") === 1);
48+
49+
assert("\uD834\uDF06w2\u1D306D".indexOf("D") === 6);
50+
51+
assert("\ud800\dc00".indexOf("\dc00") === 1);
52+
53+
// check prefix search
54+
assert("aaaabaaa".indexOf("aaaba") === 1);
55+
56+
// check empty string
57+
assert(String.prototype.indexOf.call(new String()) === -1);
58+
59+
assert(String.prototype.indexOf.call("","") === 0);
60+
61+
// check NaN
62+
assert("Hello world, welcome to the universe.".indexOf(NaN) === -1);
63+
64+
assert("Hello world, welcome to the universe.".indexOf("welcome",NaN) === 13);
65+
66+
// check Object
67+
assert(String.prototype.indexOf.call({}) === -1);
68+
69+
// check +-Inf
70+
assert("hello world!".indexOf("world", -Infinity) === 6);
71+
72+
assert("hello world!".indexOf("world", Infinity) === -1);
73+
74+
// check numbers
75+
assert("hello world!".indexOf(-1) === -1);
76+
77+
assert("hello 0 world!".indexOf(-0) === 6);
78+
79+
// check undefined
80+
assert("hello world!".indexOf(undefined) === -1);
81+
82+
var undefined_var;
83+
assert("Hello world, welcome to the universe.".indexOf("welcome", undefined_var) === 13);
84+
85+
// check booleans
86+
assert("true".indexOf(true, false) === 0);
87+
88+
// check this is undefined
89+
try {
90+
String.prototype.indexOf.call(undefined);
91+
assert(false);
92+
} catch(e) {
93+
assert(e instanceof TypeError);
94+
}
95+
96+
// check this is null
97+
try {
98+
String.prototype.indexOf.call(null);
99+
assert(false);
100+
} catch(e) {
101+
assert(e instanceof TypeError);
102+
}
103+
104+
// check coercible - undefined
105+
try {
106+
assert(true.indexOf());
107+
assert(false);
108+
} catch (e) {
109+
assert(e instanceof TypeError);
110+
}
111+
112+
// check coercible - null
113+
try {
114+
assert(isNaN(String.prototype.indexOf.call(null, 0)));
115+
assert(false);
116+
} catch (e) {
117+
assert(e instanceof TypeError);
118+
}
119+
120+
// check coercible - Boolean
121+
assert(String.prototype.indexOf.call(true, "e") === 3);
122+
123+
// check coercible - Object
124+
var test_object = {firstName:"John", lastName:"Doe"};
125+
assert(String.prototype.indexOf.call(test_object, "Obj") === 8);
126+
127+
// check coercible - Number
128+
assert(String.prototype.indexOf.call(123, "2") === 1);

0 commit comments

Comments
 (0)