From 7f153c799acff742316123b2b2214b84a9355652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Lang=C3=B3?= Date: Tue, 31 May 2016 14:38:36 +0200 Subject: [PATCH] Improve empty checks of ecma strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com --- jerry-core/ecma/base/ecma-helpers-string.c | 13 +++++++++++++ jerry-core/ecma/base/ecma-helpers.h | 1 + .../builtin-objects/ecma-builtin-error-prototype.c | 4 ++-- jerry-core/ecma/builtin-objects/ecma-builtin-json.c | 11 +++-------- .../builtin-objects/ecma-builtin-regexp-prototype.c | 2 +- .../ecma/builtin-objects/ecma-builtin-regexp.c | 2 +- .../builtin-objects/ecma-builtin-string-prototype.c | 11 +++++++---- jerry-core/ecma/operations/ecma-conversion.c | 2 +- 8 files changed, 29 insertions(+), 17 deletions(-) diff --git a/jerry-core/ecma/base/ecma-helpers-string.c b/jerry-core/ecma/base/ecma-helpers-string.c index 7ca0c2818..655426e70 100644 --- a/jerry-core/ecma/base/ecma-helpers-string.c +++ b/jerry-core/ecma/base/ecma-helpers-string.c @@ -831,6 +831,19 @@ ecma_string_raw_chars (const ecma_string_t *string_p, /**< ecma-string */ return result_p; } /* ecma_string_raw_chars */ +/** + * Checks whether ecma string is empty or not + * + * @return true - if empty + * false - otherwise + */ +bool +ecma_string_is_empty (const ecma_string_t *str_p) /**< ecma-string */ +{ + return (ECMA_STRING_GET_CONTAINER (str_p) == ECMA_STRING_CONTAINER_MAGIC_STRING + && str_p->u.magic_string_id == LIT_MAGIC_STRING__EMPTY); +} /* ecma_string_is_empty */ + /** * Long path part of ecma-string to ecma-string comparison routine * diff --git a/jerry-core/ecma/base/ecma-helpers.h b/jerry-core/ecma/base/ecma-helpers.h index f2880c21b..3a066487b 100644 --- a/jerry-core/ecma/base/ecma-helpers.h +++ b/jerry-core/ecma/base/ecma-helpers.h @@ -179,6 +179,7 @@ ecma_string_copy_to_utf8_buffer (const ecma_string_t *, lit_utf8_byte_t *, lit_u extern void ecma_string_to_utf8_bytes (const ecma_string_t *, lit_utf8_byte_t *, lit_utf8_size_t); extern const lit_utf8_byte_t *ecma_string_raw_chars (const ecma_string_t *, lit_utf8_size_t *, bool *); extern void ecma_init_ecma_string_from_uint32 (ecma_string_t *, uint32_t); +extern bool ecma_string_is_empty (const ecma_string_t *); extern bool ecma_compare_ecma_strings_equal_hashes (const ecma_string_t *, const ecma_string_t *); extern bool ecma_compare_ecma_strings (const ecma_string_t *, const ecma_string_t *); diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-error-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-error-prototype.c index 002c6ef70..5cfb8a8ce 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-error-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-error-prototype.c @@ -123,12 +123,12 @@ ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this ecma_string_t *ret_str_p; - if (ecma_string_get_length (name_string_p) == 0) + if (ecma_string_is_empty (name_string_p)) { ret_str_p = msg_string_p; ecma_ref_ecma_string (ret_str_p); } - else if (ecma_string_get_length (msg_string_p) == 0) + else if (ecma_string_is_empty (msg_string_p)) { ret_str_p = name_string_p; ecma_ref_ecma_string (ret_str_p); diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-json.c b/jerry-core/ecma/builtin-objects/ecma-builtin-json.c index 1e8939ea7..7bc811fd9 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-json.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-json.c @@ -1495,9 +1495,7 @@ ecma_builtin_json_object (ecma_object_t *obj_p, /**< the object*/ member_str_p = tmp_str_p; /* 8.b.iii */ - bool is_gap_empty = (ecma_string_get_length (context_p->gap_str_p) == 0); - - if (!is_gap_empty) + if (!ecma_string_is_empty (context_p->gap_str_p)) { ecma_string_t *space_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_SPACE_CHAR); @@ -1548,10 +1546,8 @@ ecma_builtin_json_object (ecma_object_t *obj_p, /**< the object*/ /* 10. */ else { - bool is_gap_empty = (ecma_string_get_length (context_p->gap_str_p) == 0); - /* 10.a */ - if (is_gap_empty) + if (ecma_string_is_empty (context_p->gap_str_p)) { ecma_string_t *left_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_BRACE_CHAR); ecma_string_t *right_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_RIGHT_BRACE_CHAR); @@ -1688,9 +1684,8 @@ ecma_builtin_json_array (ecma_object_t *obj_p, /**< the array object*/ /* 10. */ else { - bool is_gap_empty = (ecma_string_get_length (context_p->gap_str_p) == 0); /* 10.a */ - if (is_gap_empty) + if (ecma_string_is_empty (context_p->gap_str_p)) { ecma_string_t *left_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_SQUARE_CHAR); ecma_string_t *right_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_RIGHT_SQUARE_CHAR); diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c index 3ed0cd608..b87775b16 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c @@ -162,7 +162,7 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument ecma_op_to_string (pattern_arg), ret_value); - if (ecma_string_get_length (ecma_get_string_from_value (regexp_str_value)) == 0) + if (ecma_string_is_empty (ecma_get_string_from_value (regexp_str_value))) { pattern_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP); } diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-regexp.c b/jerry-core/ecma/builtin-objects/ecma-builtin-regexp.c index 7cf88563d..05bd8b7d5 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-regexp.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-regexp.c @@ -103,7 +103,7 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /* ecma_op_to_string (pattern_value), ret_value); - if (ecma_string_get_length (ecma_get_string_from_value (regexp_str_value)) == 0) + if (ecma_string_is_empty (ecma_get_string_from_value (regexp_str_value))) { pattern_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP); } diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c index c39c55790..bebffbec9 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c @@ -1670,9 +1670,6 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_arg, /**< this arg } else /* if (!ecma_is_value_undefined (arg1)) */ { - /* 6. */ - const ecma_length_t string_length = ecma_string_get_length (ecma_get_string_from_value (this_to_string_val)); - /* 8. */ ecma_value_t separator = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); @@ -1692,8 +1689,11 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_arg, /**< this arg ECMA_FINALIZE (separator_to_string_val); } + const ecma_string_t *this_to_string_p = ecma_get_string_from_value (this_to_string_val); + /* 11. */ - if (string_length == 0 && ecma_is_value_empty (ret_value)) + if (ecma_string_is_empty (this_to_string_p) + && ecma_is_value_empty (ret_value)) { /* 11.a */ ecma_value_t match_result = ecma_builtin_helper_split_match (this_to_string_val, @@ -1741,6 +1741,9 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_arg, /**< this arg bool separator_is_empty = false; + /* 6. */ + const ecma_length_t string_length = ecma_string_get_length (this_to_string_p); + /* 13. */ while (curr_pos < string_length && !should_return && ecma_is_value_empty (ret_value)) { diff --git a/jerry-core/ecma/operations/ecma-conversion.c b/jerry-core/ecma/operations/ecma-conversion.c index cb1e8a401..18bbced53 100644 --- a/jerry-core/ecma/operations/ecma-conversion.c +++ b/jerry-core/ecma/operations/ecma-conversion.c @@ -222,7 +222,7 @@ ecma_op_to_boolean (ecma_value_t value) /**< ecma value */ { ecma_string_t *str_p = ecma_get_string_from_value (value); - return ecma_string_get_length (str_p) != 0; + return !ecma_string_is_empty (str_p); } JERRY_ASSERT (ecma_is_value_object (value));