From 6f20a1215e5f4aea6628e76c65f3c78a42df88c1 Mon Sep 17 00:00:00 2001 From: Laszlo Vidacs Date: Tue, 21 Jul 2015 16:17:02 +0200 Subject: [PATCH] Implement String.prototype.indexOf() JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com --- .../ecma-builtin-string-prototype.cpp | 117 +++++++++++++++- tests/jerry/string-prototype-indexof.js | 128 ++++++++++++++++++ 2 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 tests/jerry/string-prototype-indexof.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp index 0803c37f55..a38c28684f 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp @@ -307,7 +307,122 @@ ecma_builtin_string_prototype_object_index_of (ecma_value_t this_arg, /**< this ecma_value_t arg1, /**< routine's first argument */ ecma_value_t arg2) /**< routine's second argument */ { - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg1, arg2); + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); + + /* 1 */ + ECMA_TRY_CATCH (check_coercible_val, + ecma_op_check_object_coercible (this_arg), + ret_value); + + /* 2 */ + ECMA_TRY_CATCH (to_str_val, + ecma_op_to_string (this_arg), + ret_value); + + /* 3 */ + ECMA_TRY_CATCH (search_str_val, + ecma_op_to_string (arg1), + ret_value); + + /* 4 */ + ECMA_OP_TO_NUMBER_TRY_CATCH (pos_num, + arg2, + ret_value); + + /* 5 */ + ecma_string_t *original_str_p = ecma_get_string_from_value (to_str_val); + const ecma_length_t original_len = ecma_string_get_length (original_str_p); + const lit_utf8_size_t original_size = ecma_string_get_size (original_str_p); + + /* 4b, 6 */ + ecma_length_t start = ecma_builtin_helper_string_index_normalize (pos_num, original_len); + + /* 7 */ + ecma_string_t *search_str_p = ecma_get_string_from_value (search_str_val); + const ecma_length_t search_len = ecma_string_get_length (search_str_p); + const lit_utf8_size_t search_size = ecma_string_get_size (search_str_p); + + ecma_number_t *ret_num_p = ecma_alloc_number (); + *ret_num_p = ecma_int32_to_number (-1); + + /* 8 */ + if (search_len <= original_len) + { + if (!search_len) + { + *ret_num_p = ecma_uint32_to_number (0); + } + else + { + /* create utf8 string from original string and advance to start position */ + MEM_DEFINE_LOCAL_ARRAY (original_str_utf8_p, + original_size, + lit_utf8_byte_t); + + ecma_string_to_utf8_string (original_str_p, + original_str_utf8_p, + (ssize_t) (original_size)); + + lit_utf8_iterator_t original_it = lit_utf8_iterator_create (original_str_utf8_p, original_size); + + ecma_length_t index = start; + lit_utf8_iterator_advance (&original_it, index); + + /* create utf8 string from search string */ + MEM_DEFINE_LOCAL_ARRAY (search_str_utf8_p, + search_size, + lit_utf8_byte_t); + + ecma_string_to_utf8_string (search_str_p, + search_str_utf8_p, + (ssize_t) (search_size)); + + lit_utf8_iterator_t search_it = lit_utf8_iterator_create (search_str_utf8_p, search_size); + + /* iterate original string and try to match at each position */ + bool found = false; + + while (!found && index <= original_len - search_len) + { + ecma_length_t match_len = 0; + lit_utf8_iterator_pos_t stored_original_pos = lit_utf8_iterator_get_pos (&original_it); + + while (match_len < search_len && + lit_utf8_iterator_read_next (&original_it) == lit_utf8_iterator_read_next (&search_it)) + { + match_len++; + } + + /* Check for match */ + if (match_len == search_len) + { + *ret_num_p = ecma_uint32_to_number (index); + found = true; + } + else + { + /* reset iterators */ + lit_utf8_iterator_seek_bos (&search_it); + lit_utf8_iterator_seek (&original_it, stored_original_pos); + lit_utf8_iterator_incr (&original_it); + } + index++; + } + + MEM_FINALIZE_LOCAL_ARRAY (search_str_utf8_p); + MEM_FINALIZE_LOCAL_ARRAY (original_str_utf8_p); + } + } + + ecma_value_t new_value = ecma_make_number_value (ret_num_p); + ret_value = ecma_make_normal_completion_value (new_value); + + ECMA_OP_TO_NUMBER_FINALIZE (pos_num); + ECMA_FINALIZE (search_str_val); + ECMA_FINALIZE (to_str_val); + ECMA_FINALIZE (check_coercible_val); + + return ret_value; } /* ecma_builtin_string_prototype_object_index_of */ /** diff --git a/tests/jerry/string-prototype-indexof.js b/tests/jerry/string-prototype-indexof.js new file mode 100644 index 0000000000..96f6a94cf8 --- /dev/null +++ b/tests/jerry/string-prototype-indexof.js @@ -0,0 +1,128 @@ +// Copyright 2015 Samsung Electronics Co., Ltd. +// Copyright 2015 University of Szeged. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// check properties +assert(Object.getOwnPropertyDescriptor(String.prototype.indexOf, 'length').configurable === false); + +assert(Object.getOwnPropertyDescriptor(String.prototype.indexOf, 'length').enumerable === false); + +assert(Object.getOwnPropertyDescriptor(String.prototype.indexOf, 'length').writable === false); + +assert(String.prototype.indexOf.length === 1); + +assert("Hello world, welcome to the universe.".indexOf("welcome") === 13); + +assert("Hello world, welcome to the universe.".indexOf("Hello world, welcome to the universe.") === 0); + +assert("Hello world, welcome to the universe.".indexOf("welcome",10) == 13); + +assert("Hello world, welcome to the universe.".indexOf("welcome",-100) == 13); + +assert("Hello world, welcome to the universe.".indexOf("welcome", 15) === -1); + +assert("Hello world, welcome to the universe.".indexOf("o", 15) === 17); + +// check utf8 strings +assert("\uFFA2".indexOf("\uFFA2") === 0); + +assert("\uFFA2".indexOf("A") === -1); + +assert("w2\uFFA2A".indexOf("A") === 3); + +assert("w2\u1D306A".indexOf("A") === 4); + +// check surrogate pairs +assert("\uD834\uDF06".indexOf("\uDF06") === 1); + +assert("\uD834\uDF06w2\u1D306D".indexOf("D") === 6); + +assert("\ud800\dc00".indexOf("\dc00") === 1); + +// check prefix search +assert("aaaabaaa".indexOf("aaaba") === 1); + +// check empty string +assert(String.prototype.indexOf.call(new String()) === -1); + +assert(String.prototype.indexOf.call("","") === 0); + +// check NaN +assert("Hello world, welcome to the universe.".indexOf(NaN) === -1); + +assert("Hello world, welcome to the universe.".indexOf("welcome",NaN) === 13); + +// check Object +assert(String.prototype.indexOf.call({}) === -1); + +// check +-Inf +assert("hello world!".indexOf("world", -Infinity) === 6); + +assert("hello world!".indexOf("world", Infinity) === -1); + +// check numbers +assert("hello world!".indexOf(-1) === -1); + +assert("hello 0 world!".indexOf(-0) === 6); + +// check undefined +assert("hello world!".indexOf(undefined) === -1); + +var undefined_var; +assert("Hello world, welcome to the universe.".indexOf("welcome", undefined_var) === 13); + +// check booleans +assert("true".indexOf(true, false) === 0); + +// check this is undefined +try { + String.prototype.indexOf.call(undefined); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +// check this is null +try { + String.prototype.indexOf.call(null); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +// check coercible - undefined +try { + assert(true.indexOf()); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +// check coercible - null +try { + assert(isNaN(String.prototype.indexOf.call(null, 0))); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +// check coercible - Boolean +assert(String.prototype.indexOf.call(true, "e") === 3); + +// check coercible - Object +var test_object = {firstName:"John", lastName:"Doe"}; +assert(String.prototype.indexOf.call(test_object, "Obj") === 8); + +// check coercible - Number +assert(String.prototype.indexOf.call(123, "2") === 1);