Skip to content

Commit

Permalink
Rearrange String.prototype.indexOf, lastIndexOf and Replace helpers
Browse files Browse the repository at this point in the history
Fixes issue jerryscript-project#515

JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
  • Loading branch information
lvidacs committed Sep 29, 2015
1 parent b7c93f1 commit 73e9c21
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 277 deletions.
166 changes: 106 additions & 60 deletions jerry-core/ecma/builtin-objects/ecma-builtin-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ ecma_builtin_helper_array_concat_value (ecma_object_t *obj_p, /**< array */
*
* Used by:
* - The String.prototype.substring routine.
* - The String.prototype.indexOf routine.
* - The ecma_builtin_helper_string_prototype_object_index_of helper routine.
*
* @return uint32_t - the normalized value of the index
Expand Down Expand Up @@ -520,71 +519,44 @@ ecma_builtin_helper_string_index_normalize (ecma_number_t index, /**< index */
return norm_index;
} /* ecma_builtin_helper_string_index_normalize */

/*
* Helper function for string indexOf and lastIndexOf functions
/**
* Helper function for finding index of a search string
*
* This function implements string indexOf and lastIndexOf with required checks and conversions.
* This function clamps the given index to the [0, length] range.
* If the index is negative, 0 value is used.
* If the index is greater than the length of the string, the normalized index will be the length of the string.
* NaN is mapped to zero or length depending on the nan_to_zero parameter.
*
* See also:
* ECMA-262 v5, 15.5.4.7
* ECMA-262 v5, 15.5.4.8
* ECMA-262 v5, 15.5.4.7,8,11
*
* Used by:
* - The String.prototype.indexOf routine.
* - The String.prototype.lastIndexOf routine.
* - The ecma_builtin_helper_string_prototype_object_index_of helper routine.
* - The ecma_builtin_string_prototype_object_replace_match helper routine.
*
* @return uint32_t - (last)index of search string
* @return uint32_t - the normalized value of the index
*/
ecma_completion_value_t
ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg1, /**< routine's first argument */
ecma_value_t arg2, /**< routine's second argument */
bool firstIndex) /**< routine's third argument */
bool
ecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, /**< index */
ecma_string_t *search_str_p, /**< string's length */
bool first_index,
ecma_length_t start_pos,
ecma_length_t *ret_index_p)
{
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);
bool match_found = false;

/* 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);

/* 6 */
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, 5, 7 */
ecma_length_t start = ecma_builtin_helper_string_index_normalize (pos_num, original_len, firstIndex);

/* 8 */
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);

/* 9 */
if (search_len <= original_len)
{
if (!search_len)
{
*ret_num_p = ecma_uint32_to_number (firstIndex ? 0 : original_len);
match_found = true;
*ret_index_p = first_index ? 0 : original_len;
}
else
{
Expand All @@ -598,11 +570,6 @@ ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**
(ssize_t) (original_size));
JERRY_ASSERT (sz >= 0);

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,
Expand All @@ -613,37 +580,51 @@ ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**
(ssize_t) (search_size));
JERRY_ASSERT (sz >= 0);

lit_utf8_iterator_t original_it = lit_utf8_iterator_create (original_str_utf8_p, original_size);
lit_utf8_iterator_t search_it = lit_utf8_iterator_create (search_str_utf8_p, search_size);

ecma_length_t index = start_pos;
lit_utf8_iterator_advance (&original_it, index);

/* iterate original string and try to match at each position */
bool searching = true;
ecma_char_t first_char = lit_utf8_iterator_read_next (&search_it);

while (searching)
{
/* match as long as possible */
ecma_length_t match_len = 0;
lit_utf8_iterator_t stored_original_it = original_it;

while (match_len < search_len &&
if (match_len < search_len &&
index + match_len < original_len &&
lit_utf8_iterator_read_next (&original_it) == lit_utf8_iterator_read_next (&search_it))
lit_utf8_iterator_read_next (&original_it) == first_char)
{
lit_utf8_iterator_t nested_search_it = search_it;
match_len++;

while (match_len < search_len &&
index + match_len < original_len &&
lit_utf8_iterator_read_next (&original_it) == lit_utf8_iterator_read_next (&nested_search_it))
{
match_len++;
}
}

/* check for match */
if (match_len == search_len)
{
*ret_num_p = ecma_uint32_to_number (index);
match_found = true;
*ret_index_p = index;

break;
}
else
{
/* inc/dec index and update iterators and search condition */
lit_utf8_iterator_seek_bos (&search_it);
original_it = stored_original_it;

if (firstIndex)
if (first_index)
{
if ((searching = (index <= original_len - search_len)))
{
Expand All @@ -667,16 +648,81 @@ ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**
}
}

ecma_value_t new_value = ecma_make_number_value (ret_num_p);
ret_value = ecma_make_normal_completion_value (new_value);
return match_found;
} /* ecma_builtin_helper_string_find_index */

/*
* Helper function for string indexOf and lastIndexOf functions
*
* This function implements string indexOf and lastIndexOf with required checks and conversions.
*
* See also:
* ECMA-262 v5, 15.5.4.7
* ECMA-262 v5, 15.5.4.8
*
* Used by:
* - The String.prototype.indexOf routine.
* - The String.prototype.lastIndexOf routine.
*
* @return uint32_t - (last)index of search string
*/
ecma_completion_value_t
ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg1, /**< routine's first argument */
ecma_value_t arg2, /**< routine's second argument */
bool first_index) /**< routine's third argument */
{
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 (indexOf) -- 6 (lastIndexOf) */
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);

/* 4b, 6 (indexOf) - 4b, 5, 7 (lastIndexOf) */
ecma_length_t start = ecma_builtin_helper_string_index_normalize (pos_num, original_len, first_index);

/* 7 (indexOf) -- 8 (lastIndexOf) */
ecma_string_t *search_str_p = ecma_get_string_from_value (search_str_val);

ecma_number_t *ret_num_p = ecma_alloc_number ();
*ret_num_p = ecma_int32_to_number (-1);

/* 8 (indexOf) -- 9 (lastIndexOf) */
ecma_length_t index_of;
if (ecma_builtin_helper_string_find_index (original_str_p, search_str_p, first_index, start, &index_of))
{
*ret_num_p = ecma_uint32_to_number (index_of);
}

ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));

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_helper_string_index_normalize */
} /* ecma_builtin_helper_string_prototype_object_index_of */

/**
* Helper function for using [[DefineOwnProperty]].
Expand Down
2 changes: 2 additions & 0 deletions jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extern uint32_t
ecma_builtin_helper_array_index_normalize (ecma_number_t, uint32_t);
extern uint32_t
ecma_builtin_helper_string_index_normalize (ecma_number_t, uint32_t, bool);
extern bool
ecma_builtin_helper_string_find_index (ecma_string_t *, ecma_string_t *, bool, ecma_length_t, ecma_length_t *);
extern ecma_completion_value_t
ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t, ecma_value_t,
ecma_value_t, bool);
Expand Down
Loading

0 comments on commit 73e9c21

Please sign in to comment.