Refactor the ecma_string_substr function
JerryScript-DCO-1.0-Signed-off-by: Robert Sipka
@@ -1779,57 +1779,21 @@ ecma_string_substr (const ecma_string_t *string_p, /**< pointer to an ecma strin
ecma_length_t start_pos, /**< start position, should be less or equal than string length */
ecma_length_t end_pos) /**< end position, should be less or equal than string length */
{
-#ifndef JERRY_NDEBUG
const ecma_length_t string_length = ecma_string_get_length (string_p);
JERRY_ASSERT (start_pos <= string_length);
JERRY_ASSERT (end_pos <= string_length);
-#endif /* !JERRY_NDEBUG */
- if (start_pos < end_pos)
+ if (start_pos >= end_pos)
{
- lit_utf8_size_t buffer_size;
- bool is_ascii;
- const lit_utf8_byte_t *start_p = ecma_string_raw_chars (string_p, &buffer_size, &is_ascii);
-
- end_pos -= start_pos;
-
- if (start_p != NULL)
- {
- if (is_ascii)
- {
- return ecma_new_ecma_string_from_utf8 (start_p + start_pos,
- (lit_utf8_size_t) end_pos);
+ return ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
}
- while (start_pos--)
- {
- start_p += lit_get_unicode_char_size_by_utf8_first_byte (*start_p);
- }
-
- const lit_utf8_byte_t *end_p = start_p;
- while (end_pos--)
- {
- end_p += lit_get_unicode_char_size_by_utf8_first_byte (*end_p);
- }
-
- return ecma_new_ecma_string_from_utf8 (start_p, (lit_utf8_size_t) (end_p - start_p));
- }
-
- /**
- * I. Dump original string to plain buffer
- */
- ecma_string_t *ecma_string_p;
-
- JMEM_DEFINE_LOCAL_ARRAY (utf8_str_p, buffer_size, lit_utf8_byte_t);
-
- ecma_string_to_utf8_bytes (string_p, utf8_str_p, buffer_size);
+ ecma_string_t *ecma_string_p = NULL;
+ end_pos -= start_pos;
- /**
- * II. Extract substring
- */
- start_p = utf8_str_p;
+ ECMA_STRING_TO_UTF8_STRING (string_p, start_p, buffer_size);
- if (is_ascii)
+ if (string_length == buffer_size)
{
ecma_string_p = ecma_new_ecma_string_from_utf8 (start_p + start_pos,
(lit_utf8_size_t) end_pos);
@@ -1850,16 +1814,9 @@ ecma_string_substr (const ecma_string_t *string_p, /**< pointer to an ecma strin
ecma_string_p = ecma_new_ecma_string_from_utf8 (start_p, (lit_utf8_size_t) (end_p - start_p));
}
- JMEM_FINALIZE_LOCAL_ARRAY (utf8_str_p);
+ ECMA_FINALIZE_UTF8_STRING (start_p, buffer_size);
return ecma_string_p;
- }
- else
- {
- return ecma_new_ecma_string_from_utf8 (NULL, 0);
- }
-
- JERRY_UNREACHABLE ();
} /* ecma_string_substr */
/**
