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

Code before:
      {
        break;
      }
      if (ecma_builtin_helper_string_find_index (original_str_p, search_str_p,
                                                 (ecma_length_t) start_ends_with, &index_of))
      {
        ret_value = ecma_make_boolean_value (index_of == (ecma_length_t) start_ends_with);
      }
      break;
    }
#endif /* ENABLED (JERRY_ESNEXT) */

    case ECMA_STRING_INDEX_OF:
    {
      /* 8 (indexOf) -- 9 (lastIndexOf) */
      if (ecma_builtin_helper_string_find_index (original_str_p, search_str_p, start, &index_of))
      {
        ret_num = ((ecma_number_t) index_of);
      }
      ret_value = ecma_make_number_value (ret_num);
      break;
    }
    case ECMA_STRING_LAST_INDEX_OF:
    {

    }
    default:
    {
      JERRY_UNREACHABLE ();
    }
  }

  ecma_deref_ecma_string (search_str_p);

  return ret_value;
} /* ecma_builtin_helper_string_prototype_object_index_of */

/**
 * Helper function for finding index 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.
 * NaN is mapped to zero or length depending on the nan_to_zero parameter.
 *
 * See also:
 *          ECMA-262 v6, 21.1.3.8
 *
 * Used by:
 *         - The ecma_builtin_helper_string_prototype_object_index_of helper routine.
 *         - The ecma_builtin_string_prototype_object_replace_match helper routine.

Review comment (suggestion):
I suggest to use the return value as uint32_t with the matching index and `UIN32T_MAX` for not matching.

Code after:
uint32_t
ecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, /**< index */
                                       ecma_string_t *search_str_p, /**< string's length */
                                       uint32_t start_pos) /**< start position */
{
  uint32_t match_found = UINT32_MAX;

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

  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 *str_current_p = original_str_utf8_p;

  for (ecma_number_t i = 0; i < start_pos; i++)
  {
    lit_utf8_incr (&str_current_p);
  }

  const lit_utf8_byte_t *original_end_p = original_str_utf8_p + original_str_size;

  while (!((size_t) (original_end_p - str_current_p) < search_str_size))
  {
    if (memcmp (str_current_p , search_str_utf8_p, search_str_size) == 0)
    {
      match_found = start_pos;
      break;
    }

    lit_utf8_incr (&str_current_p);
    start_pos++;
  }

  ECMA_FINALIZE_UTF8_STRING (original_str_utf8_p, original_str_size);
  ECMA_FINALIZE_UTF8_STRING (search_str_utf8_p, search_str_size);

  return match_found;
} /* ecma_builtin_helper_string_find_index */

/**
 * Helper function for using [[DefineOwnProperty]] specialized for indexed property names
 *
 * Note: this method falls back to the general ecma_builtin_helper_def_prop
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
ecma_value_t