From c9afc2259d05d56c958ada024cb29c912640e42f Mon Sep 17 00:00:00 2001 From: Robert Sipka Date: Fri, 13 Jan 2017 09:22:55 +0100 Subject: [PATCH] Refactor the ecma_string_substr function (#1517) JerryScript-DCO-1.0-Signed-off-by: Robert Sipka rsipka.uszeged@partner.samsung.com --- jerry-core/ecma/base/ecma-helpers-string.c | 93 ++++++---------------- 1 file changed, 25 insertions(+), 68 deletions(-) diff --git a/jerry-core/ecma/base/ecma-helpers-string.c b/jerry-core/ecma/base/ecma-helpers-string.c index 7c222a88c..e7c1881b2 100644 --- a/jerry-core/ecma/base/ecma-helpers-string.c +++ b/jerry-core/ecma/base/ecma-helpers-string.c @@ -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 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); + return ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); + } - end_pos -= start_pos; + ecma_string_t *ecma_string_p = NULL; + 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); - } + ECMA_STRING_TO_UTF8_STRING (string_p, start_p, buffer_size); - 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); - - /** - * 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; + if (string_length == buffer_size) + { + ecma_string_p = ecma_new_ecma_string_from_utf8 (start_p + start_pos, + (lit_utf8_size_t) end_pos); } 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 */ /**