Skip to content

Commit 91f43b9

Browse files
committed
Improve empty checks of ecma strings
JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
1 parent 308bb3c commit 91f43b9

File tree

8 files changed

+32
-19
lines changed

8 files changed

+32
-19
lines changed

jerry-core/ecma/base/ecma-helpers-string.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,19 @@ ecma_string_raw_chars (const ecma_string_t *string_p, /**< ecma-string */
893893
return result_p;
894894
} /* ecma_string_raw_chars */
895895

896+
/**
897+
* Checks whether ecma string is empty or not
898+
*
899+
* @return true - if empty
900+
* false - otherwise
901+
*/
902+
bool
903+
ecma_string_is_empty (const ecma_string_t *str_p) /**< ecma-string */
904+
{
905+
return (ECMA_STRING_GET_CONTAINER (str_p) == ECMA_STRING_CONTAINER_MAGIC_STRING
906+
&& str_p->u.magic_string_id == LIT_MAGIC_STRING__EMPTY);
907+
} /* ecma_string_is_empty */
908+
896909
/**
897910
* Long path part of ecma-string to ecma-string comparison routine
898911
*

jerry-core/ecma/base/ecma-helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ extern bool ecma_string_get_array_index (const ecma_string_t *, uint32_t *);
179179
extern lit_utf8_size_t __attr_return_value_should_be_checked___
180180
ecma_string_to_utf8_string (const ecma_string_t *, lit_utf8_byte_t *, lit_utf8_size_t);
181181
extern const lit_utf8_byte_t *ecma_string_raw_chars (const ecma_string_t *, lit_utf8_size_t *, bool *);
182+
extern bool ecma_string_is_empty (const ecma_string_t *);
182183

183184
extern bool ecma_compare_ecma_strings_equal_hashes (const ecma_string_t *, const ecma_string_t *);
184185
extern bool ecma_compare_ecma_strings (const ecma_string_t *, const ecma_string_t *);

jerry-core/ecma/builtin-objects/ecma-builtin-error-prototype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this
123123

124124
ecma_string_t *ret_str_p;
125125

126-
if (ecma_string_get_length (name_string_p) == 0)
126+
if (ecma_string_is_empty (name_string_p))
127127
{
128128
ret_str_p = ecma_copy_or_ref_ecma_string (msg_string_p);
129129
}
130-
else if (ecma_string_get_length (msg_string_p) == 0)
130+
else if (ecma_string_is_empty (msg_string_p))
131131
{
132132
ret_str_p = ecma_copy_or_ref_ecma_string (name_string_p);
133133
}

jerry-core/ecma/builtin-objects/ecma-builtin-json.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,9 +1491,7 @@ ecma_builtin_json_object (ecma_object_t *obj_p, /**< the object*/
14911491
member_str_p = tmp_str_p;
14921492

14931493
/* 8.b.iii */
1494-
bool is_gap_empty = (ecma_string_get_length (context_p->gap_str_p) == 0);
1495-
1496-
if (!is_gap_empty)
1494+
if (!ecma_string_is_empty (context_p->gap_str_p))
14971495
{
14981496
ecma_string_t *space_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_SPACE_CHAR);
14991497

@@ -1544,10 +1542,8 @@ ecma_builtin_json_object (ecma_object_t *obj_p, /**< the object*/
15441542
/* 10. */
15451543
else
15461544
{
1547-
bool is_gap_empty = (ecma_string_get_length (context_p->gap_str_p) == 0);
1548-
15491545
/* 10.a */
1550-
if (is_gap_empty)
1546+
if (ecma_string_is_empty (context_p->gap_str_p))
15511547
{
15521548
ecma_string_t *left_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_BRACE_CHAR);
15531549
ecma_string_t *right_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_RIGHT_BRACE_CHAR);
@@ -1684,9 +1680,8 @@ ecma_builtin_json_array (ecma_object_t *obj_p, /**< the array object*/
16841680
/* 10. */
16851681
else
16861682
{
1687-
bool is_gap_empty = (ecma_string_get_length (context_p->gap_str_p) == 0);
16881683
/* 10.a */
1689-
if (is_gap_empty)
1684+
if (ecma_string_is_empty (context_p->gap_str_p))
16901685
{
16911686
ecma_string_t *left_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_SQUARE_CHAR);
16921687
ecma_string_t *right_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_RIGHT_SQUARE_CHAR);

jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument
162162
ecma_op_to_string (pattern_arg),
163163
ret_value);
164164

165-
if (ecma_string_get_length (ecma_get_string_from_value (regexp_str_value)) == 0)
165+
if (ecma_string_is_empty (ecma_get_string_from_value (regexp_str_value)))
166166
{
167167
pattern_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP);
168168
}

jerry-core/ecma/builtin-objects/ecma-builtin-regexp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*
103103
ecma_op_to_string (pattern_value),
104104
ret_value);
105105

106-
if (ecma_string_get_length (ecma_get_string_from_value (regexp_str_value)) == 0)
106+
if (ecma_string_is_empty (ecma_get_string_from_value (regexp_str_value)))
107107
{
108108
pattern_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP);
109109
}

jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ ecma_builtin_string_prototype_object_char_at (ecma_value_t this_arg, /**< this a
138138

139139
/* 4 */
140140
ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val);
141-
const ecma_length_t len = ecma_string_get_length (original_string_p);
142141

143142
/* 5 */
144-
if (index_num < 0 || index_num >= len || !len)
143+
if (ecma_string_is_empty (original_string_p)
144+
|| index_num >= ecma_string_get_length (original_string_p)
145+
|| index_num < 0)
145146
{
146147
ret_value = ecma_make_string_value (ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY));
147148
}
@@ -1673,9 +1674,6 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_arg, /**< this arg
16731674
}
16741675
else /* if (!ecma_is_value_undefined (arg1)) */
16751676
{
1676-
/* 6. */
1677-
const ecma_length_t string_length = ecma_string_get_length (ecma_get_string_from_value (this_to_string_val));
1678-
16791677
/* 8. */
16801678
ecma_value_t separator = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
16811679

@@ -1695,8 +1693,11 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_arg, /**< this arg
16951693
ECMA_FINALIZE (separator_to_string_val);
16961694
}
16971695

1696+
const ecma_string_t *this_to_string_p = ecma_get_string_from_value (this_to_string_val);
1697+
16981698
/* 11. */
1699-
if (string_length == 0 && ecma_is_value_empty (ret_value))
1699+
if (ecma_string_is_empty (this_to_string_p)
1700+
&& ecma_is_value_empty (ret_value))
17001701
{
17011702
/* 11.a */
17021703
ecma_value_t match_result = ecma_builtin_helper_split_match (this_to_string_val,
@@ -1744,6 +1745,9 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_arg, /**< this arg
17441745

17451746
bool separator_is_empty = false;
17461747

1748+
/* 6. */
1749+
const ecma_length_t string_length = ecma_string_get_length (this_to_string_p);
1750+
17471751
/* 13. */
17481752
while (curr_pos < string_length && !should_return && ecma_is_value_empty (ret_value))
17491753
{

jerry-core/ecma/operations/ecma-conversion.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ ecma_op_to_boolean (ecma_value_t value) /**< ecma value */
222222
{
223223
ecma_string_t *str_p = ecma_get_string_from_value (value);
224224

225-
return ecma_string_get_length (str_p) != 0;
225+
return !ecma_string_is_empty (str_p);
226226
}
227227

228228
JERRY_ASSERT (ecma_is_value_object (value));

0 commit comments

Comments
 (0)