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

Code before:
 *          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  */
{
  uint32_t start = position;
  uint32_t original_length = ecma_string_get_length (original_str_p);

  if (ecma_string_is_empty (search_str_p))
  {
    return start;
  }

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

  const lit_utf8_byte_t *end_p = original_str_utf8_p + original_str_size;
  const lit_utf8_byte_t *end = original_str_utf8_p + original_str_size;
  uint32_t pos = original_length;

  for (ecma_number_t i = pos; i > start; i--)
  {
    lit_utf8_decr (&end_p);
  }

  uint32_t ret_value = UINT32_MAX;

  while (true)
  {
    if (end_p + search_str_size <= end
        && memcmp (end_p, search_str_utf8_p, search_str_size) == 0)
    {
      ret_value = start;
      break;
    }

    if (start == 0)
    {
      break;
    }
    lit_utf8_decr (&end_p);
    start--;
  }

Review comment (suggestion):
One of these should be renamed, you could have something like this:
```c
const lit_utf8_byte_t *end_p = original_str_utf8_p + original_str_size;
const lit_utf8_byte_t *current_p = end_p;
```

And then adjust/iterate `current_p` below.

Code after:
 *          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);
    }

    while (current_p + search_str_size > end_p)
    {
      lit_utf8_decr (&current_p);
      position--;
    }

    while (true)
    {
      if (memcmp (current_p, search_str_utf8_p, search_str_size) == 0)
      {
        ret_value = position;
        break;
      }

      if (position == 0)
      {