Fix position calculations of String.startsWith (#3734)

Fixes two bugs in the index calculations that affect the result
of the `String.startsWith` function:

- Fixes the implementation of ECMA-262 v6, 21.1.3.18, step 14:
  "If searchLength+start is greater than len, return false".
- Fixes the return value of the helper function
  `ecma_builtin_helper_string_find_index`. If it is called with a
  starting search position, the returned index should be
  not less than that.

These changes fix the following test cases in the test262 suite:

- built-ins/String/prototype/startsWith/return-true-if-searchstring-is-empty.js
- built-ins/String/prototype/startsWith/searchstring-found-with-position.js

JerryScript-DCO-1.0-Signed-off-by: Mátyás Mustoha mmatyas@inf.u-szeged.hu
This commit is contained in:
Mátyás Mustoha 2020-05-15 22:18:42 +02:00 committed by GitHub
parent b51157d3c5
commit f13e8b602e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -585,7 +585,9 @@ ecma_builtin_helper_string_prototype_object_index_of (ecma_string_t *original_st
#if ENABLED (JERRY_ES2015)
case ECMA_STRING_STARTS_WITH:
{
if (pos_num + start > original_len)
const ecma_length_t search_len = ecma_string_get_length (search_str_p);
if (search_len + start > original_len)
{
break;
}
@ -687,7 +689,7 @@ ecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, /**< index
if (!search_len)
{
match_found = true;
*ret_index_p = first_index ? 0 : original_len;
*ret_index_p = first_index ? start_pos : original_len;
}
else
{

View File

@ -15,7 +15,10 @@
var x = "My cat is awesome";
assert (x.startsWith ("My"));
assert (x.startsWith ("cat", 3));
assert (x.startsWith ("awesome", 10));
assert (x.startsWith (""));
assert (x.startsWith ("", 1));
assert (x.startsWith ("", 17));
assert (x.startsWith ([]));
assert (x.startsWith ("doggo") === false);