Repository: jerryscript-project/jerryscript
File: jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c (C)

Code before:
      {
        norm_index = length;
      }
    }
  }

  return norm_index;
} /* ecma_builtin_helper_string_index_normalize */

/**
 * Helper function for finding lastindex of a search string
 *
 * 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.
 *
 * See also:
 *          ECMA-262 v6, 21.1.3.9
 *
 * Used by:
 *         - The ecma_builtin_helper_string_prototype_object_index_of helper routine.
 *         - The ecma_builtin_string_prototype_object_replace_match helper routine.
 *
 * @return uint32_t - whether there is a match for the search string
 */
static uint32_t
ecma_builtin_helper_string_find_last_index (ecma_string_t *original_str_p, /**< index */
                                            ecma_string_t *search_str_p, /**< string's length */
                                            ecma_value_t position) /**< start_position  */
{
  ecma_number_t num_pos;

  if (ECMA_IS_VALUE_ERROR (ecma_get_number (position, &num_pos)))
  {
    return ECMA_VALUE_ERROR;
  }

  if (ecma_number_is_nan (num_pos))
  {
    num_pos = ecma_number_make_infinity (false);
  }
  else
  {
    ecma_value_t num_value = ecma_make_number_value (num_pos);
    ecma_value_t conv_result = ecma_op_to_integer (num_value, &num_pos);
    ecma_free_value (num_value);
    JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (conv_result));
  }

  uint32_t original_length = ecma_string_get_length (original_str_p);
  uint32_t start = ecma_builtin_helper_string_index_normalize (num_pos, original_length, true);

Review comment (suggestion):
The return type is `uint32_t`, yet there are multiple paces where ecma_value_t-s are returned.

Code after:
      {
        norm_index = length;
      }
    }
  }

  return norm_index;
} /* ecma_builtin_helper_string_index_normalize */

/**
 * Helper function for finding lastindex of a search string
 *
 * 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.
 *
 * See also:
 *          ECMA-262 v6, 21.1.3.9
 *
 * Used by:
 *         - The ecma_builtin_helper_string_prototype_object_index_of helper routine.
 *         - The ecma_builtin_string_prototype_object_replace_match helper routine.
 *
 * @return uint32_t - whether there is a match for the search string
 */
static uint32_t
ecma_builtin_helper_string_find_last_index (ecma_string_t *original_str_p, /**< original string */
                                            ecma_string_t *search_str_p, /**< search string */
                                            uint32_t position) /**< start_position */
{
  if (ecma_string_is_empty (search_str_p))
  {
    return position;
  }

  uint32_t original_length = ecma_string_get_length (original_str_p);

  ECMA_STRING_TO_UTF8_STRING (search_str_p, search_str_utf8_p, search_str_size);
  ECMA_STRING_TO_UTF8_STRING (original_str_p, original_str_utf8_p, original_str_size);

  uint32_t ret_value = UINT32_MAX;

  if (original_str_size >= search_str_size)
  {
    const lit_utf8_byte_t *end_p = original_str_utf8_p + original_str_size;
    const lit_utf8_byte_t *current_p = end_p;

    for (ecma_number_t i = original_length; i > position; i--)
    {
      lit_utf8_decr (&current_p);
    }