Skip to content

Commit 7f153c7

Browse files
committed
Improve empty checks of ecma strings
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 2324617 commit 7f153c7

File tree

8 files changed

+29
-17
lines changed

8 files changed

+29
-17
lines changed

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

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

834+
/**
835+
* Checks whether ecma string is empty or not
836+
*
837+
* @return true - if empty
838+
* false - otherwise
839+
*/
840+
bool
841+
ecma_string_is_empty (const ecma_string_t *str_p) /**< ecma-string */
842+
{
843+
return (ECMA_STRING_GET_CONTAINER (str_p) == ECMA_STRING_CONTAINER_MAGIC_STRING
844+
&& str_p->u.magic_string_id == LIT_MAGIC_STRING__EMPTY);
845+
} /* ecma_string_is_empty */
846+
834847
/**
835848
* Long path part of ecma-string to ecma-string comparison routine
836849
*

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 @@ ecma_string_copy_to_utf8_buffer (const ecma_string_t *, lit_utf8_byte_t *, lit_u
179179
extern void ecma_string_to_utf8_bytes (const ecma_string_t *, lit_utf8_byte_t *, lit_utf8_size_t);
180180
extern const lit_utf8_byte_t *ecma_string_raw_chars (const ecma_string_t *, lit_utf8_size_t *, bool *);
181181
extern void ecma_init_ecma_string_from_uint32 (ecma_string_t *, uint32_t);
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,12 +123,12 @@ 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 = msg_string_p;
129129
ecma_ref_ecma_string (ret_str_p);
130130
}
131-
else if (ecma_string_get_length (msg_string_p) == 0)
131+
else if (ecma_string_is_empty (msg_string_p))
132132
{
133133
ret_str_p = name_string_p;
134134
ecma_ref_ecma_string (ret_str_p);

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

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

14971497
/* 8.b.iii */
1498-
bool is_gap_empty = (ecma_string_get_length (context_p->gap_str_p) == 0);
1499-
1500-
if (!is_gap_empty)
1498+
if (!ecma_string_is_empty (context_p->gap_str_p))
15011499
{
15021500
ecma_string_t *space_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_SPACE_CHAR);
15031501

@@ -1548,10 +1546,8 @@ ecma_builtin_json_object (ecma_object_t *obj_p, /**< the object*/
15481546
/* 10. */
15491547
else
15501548
{
1551-
bool is_gap_empty = (ecma_string_get_length (context_p->gap_str_p) == 0);
1552-
15531549
/* 10.a */
1554-
if (is_gap_empty)
1550+
if (ecma_string_is_empty (context_p->gap_str_p))
15551551
{
15561552
ecma_string_t *left_brace_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_BRACE_CHAR);
15571553
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*/
16881684
/* 10. */
16891685
else
16901686
{
1691-
bool is_gap_empty = (ecma_string_get_length (context_p->gap_str_p) == 0);
16921687
/* 10.a */
1693-
if (is_gap_empty)
1688+
if (ecma_string_is_empty (context_p->gap_str_p))
16941689
{
16951690
ecma_string_t *left_square_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LEFT_SQUARE_CHAR);
16961691
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,9 +1670,6 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_arg, /**< this arg
16701670
}
16711671
else /* if (!ecma_is_value_undefined (arg1)) */
16721672
{
1673-
/* 6. */
1674-
const ecma_length_t string_length = ecma_string_get_length (ecma_get_string_from_value (this_to_string_val));
1675-
16761673
/* 8. */
16771674
ecma_value_t separator = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
16781675

@@ -1692,8 +1689,11 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_arg, /**< this arg
16921689
ECMA_FINALIZE (separator_to_string_val);
16931690
}
16941691

1692+
const ecma_string_t *this_to_string_p = ecma_get_string_from_value (this_to_string_val);
1693+
16951694
/* 11. */
1696-
if (string_length == 0 && ecma_is_value_empty (ret_value))
1695+
if (ecma_string_is_empty (this_to_string_p)
1696+
&& ecma_is_value_empty (ret_value))
16971697
{
16981698
/* 11.a */
16991699
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
17411741

17421742
bool separator_is_empty = false;
17431743

1744+
/* 6. */
1745+
const ecma_length_t string_length = ecma_string_get_length (this_to_string_p);
1746+
17441747
/* 13. */
17451748
while (curr_pos < string_length && !should_return && ecma_is_value_empty (ret_value))
17461749
{

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)