Refactor the ecma_string_substr function (#1517)

JerryScript-DCO-1.0-Signed-off-by: Robert Sipka rsipka.uszeged@partner.samsung.com
This commit is contained in:
Robert Sipka
2017-01-13 09:22:55 +01:00
committed by GitHub
parent f1ed5715e3
commit c9afc2259d
+25 -68
View File
@@ -1779,87 +1779,44 @@ 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 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 */ 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); const ecma_length_t string_length = ecma_string_get_length (string_p);
JERRY_ASSERT (start_pos <= string_length); JERRY_ASSERT (start_pos <= string_length);
JERRY_ASSERT (end_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; return ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
bool is_ascii; }
const lit_utf8_byte_t *start_p = ecma_string_raw_chars (string_p, &buffer_size, &is_ascii);
end_pos -= start_pos; ecma_string_t *ecma_string_p = NULL;
end_pos -= start_pos;
if (start_p != NULL) ECMA_STRING_TO_UTF8_STRING (string_p, start_p, buffer_size);
{
if (is_ascii)
{
return ecma_new_ecma_string_from_utf8 (start_p + start_pos,
(lit_utf8_size_t) end_pos);
}
while (start_pos--) if (string_length == buffer_size)
{ {
start_p += lit_get_unicode_char_size_by_utf8_first_byte (*start_p); ecma_string_p = ecma_new_ecma_string_from_utf8 (start_p + start_pos,
} (lit_utf8_size_t) end_pos);
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);
/**
* II. Extract substring
*/
start_p = utf8_str_p;
if (is_ascii)
{
ecma_string_p = ecma_new_ecma_string_from_utf8 (start_p + start_pos,
(lit_utf8_size_t) end_pos);
}
else
{
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);
}
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);
return ecma_string_p;
} }
else else
{ {
return ecma_new_ecma_string_from_utf8 (NULL, 0); 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);
}
ecma_string_p = ecma_new_ecma_string_from_utf8 (start_p, (lit_utf8_size_t) (end_p - start_p));
} }
JERRY_UNREACHABLE (); ECMA_FINALIZE_UTF8_STRING (start_p, buffer_size);
return ecma_string_p;
} /* ecma_string_substr */ } /* ecma_string_substr */
/** /**