Skip to content

Commit c5c5db8

Browse files
committed
[WIP] Another try
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent f73d287 commit c5c5db8

File tree

2 files changed

+36
-74
lines changed

2 files changed

+36
-74
lines changed

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

Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,9 +1319,7 @@ ecma_string_get_number_in_desc_size (const uint32_t uint32_number) /**< number i
13191319
const lit_utf8_byte_t *
13201320
ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
13211321
lit_utf8_size_t *size_p, /**< [out] size of the ecma string */
1322-
bool *is_ascii_p) /**< [out] true, if the string is an ascii
1323-
* character sequence (size == length)
1324-
* false, otherwise */
1322+
uint8_t *flags_p) /**< [out] TODO */
13251323
{
13261324
ecma_length_t length;
13271325
lit_utf8_size_t size;
@@ -1351,8 +1349,10 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
13511349
/* All numbers must be ascii strings. */
13521350
JERRY_ASSERT (ecma_string_get_length (string_p) == size);
13531351

1354-
length = size;
1355-
result_p = NULL;
1352+
result_p = (const lit_utf8_byte_t *) jmem_heap_alloc_block (size);
1353+
length = ecma_uint32_to_utf8_string (string_p->u.uint32_number, (lit_utf8_byte_t *) result_p, size);
1354+
JERRY_ASSERT(length == size);
1355+
*flags_p |= 2;
13561356
break;
13571357
}
13581358
case ECMA_STRING_CONTAINER_MAGIC_STRING:
@@ -1371,13 +1371,8 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
13711371
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_MAGIC_STRING_EX);
13721372

13731373
size = lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id);
1374-
length = 0;
1375-
1376-
if (is_ascii_p != NULL)
1377-
{
1378-
length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id),
1379-
lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id));
1380-
}
1374+
length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id),
1375+
lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id));
13811376

13821377
result_p = lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id);
13831378
break;
@@ -1386,9 +1381,9 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
13861381

13871382
*size_p = size;
13881383

1389-
if (is_ascii_p != NULL)
1384+
if (length == size)
13901385
{
1391-
*is_ascii_p = (length == size);
1386+
*flags_p |= 1;
13921387
}
13931388
return result_p;
13941389
} /* ecma_string_get_chars */
@@ -1979,32 +1974,23 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */
19791974
JERRY_ASSERT (index < ecma_string_get_length (string_p));
19801975

19811976
lit_utf8_size_t buffer_size;
1982-
bool is_ascii;
1983-
const lit_utf8_byte_t *chars_p = ecma_string_get_chars (string_p, &buffer_size, &is_ascii);
1977+
uint8_t flags = 0;
1978+
const lit_utf8_byte_t *chars_p = ecma_string_get_chars (string_p, &buffer_size, &flags);
19841979

1985-
if (chars_p != NULL)
1980+
ecma_char_t ch;
1981+
if (flags & 1)
19861982
{
1987-
if (is_ascii)
1988-
{
1989-
return chars_p[index];
1990-
}
1991-
1992-
return lit_utf8_string_code_unit_at (chars_p, buffer_size, index);
1983+
ch = chars_p[index];
1984+
}
1985+
else
1986+
{
1987+
ch = lit_utf8_string_code_unit_at (chars_p, buffer_size, index);
19931988
}
19941989

1995-
ecma_char_t ch;
1996-
1997-
JMEM_DEFINE_LOCAL_ARRAY (utf8_str_p, buffer_size, lit_utf8_byte_t);
1998-
1999-
ecma_string_to_utf8_bytes (string_p, utf8_str_p, buffer_size);
2000-
2001-
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_UINT32_IN_DESC);
2002-
/* Uint32 must be an ascii string. */
2003-
JERRY_ASSERT (is_ascii);
2004-
2005-
ch = utf8_str_p[index];
2006-
2007-
JMEM_FINALIZE_LOCAL_ARRAY (utf8_str_p);
1990+
if (flags & 2)
1991+
{
1992+
jmem_heap_free_block ((void *) chars_p, buffer_size);
1993+
}
20081994

20091995
return ch;
20101996
} /* ecma_string_get_char_at_pos */
@@ -2181,30 +2167,6 @@ ecma_string_trim (const ecma_string_t *string_p) /**< pointer to an ecma string
21812167
return ret_string_p;
21822168
} /* ecma_string_trim */
21832169

2184-
/**
2185-
* Helper function for ecma_string conversion. It is used in
2186-
* ECMA_STRING_TO_UTF8_STRING macro. Returns true if the output
2187-
* buffer must be freed.
2188-
*
2189-
* @return true - if the output buffer must be freed.
2190-
* false - otherwise
2191-
*/
2192-
bool ecma_string_to_utf8_string (const ecma_string_t *ecma_str_ptr, /**< ecma string pointer */
2193-
const lit_utf8_byte_t **utf8_ptr, /**< [out] output buffer pointer */
2194-
lit_utf8_size_t *utf8_str_size) /**< [out] output buffer size */
2195-
{
2196-
*utf8_ptr = ecma_string_get_chars (ecma_str_ptr, utf8_str_size, NULL);
2197-
2198-
if (*utf8_ptr == NULL)
2199-
{
2200-
*utf8_ptr = (const lit_utf8_byte_t *) jmem_heap_alloc_block (*utf8_str_size);
2201-
ecma_string_to_utf8_bytes (ecma_str_ptr, (lit_utf8_byte_t *) *utf8_ptr, *utf8_str_size);
2202-
return true;
2203-
}
2204-
2205-
return false;
2206-
} /* ecma_string_to_utf8_string*/
2207-
22082170
/**
22092171
* @}
22102172
* @}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,19 @@ bool ecma_string_to_utf8_string (const ecma_string_t *ecma_str_ptr,
6161
utf8_ptr, /**< [out] output buffer pointer */ \
6262
utf8_str_size) /**< [out] output buffer size */ \
6363
lit_utf8_size_t utf8_str_size; \
64-
const lit_utf8_byte_t *utf8_ptr = NULL; \
65-
bool utf8_ptr ## must_be_freed = ecma_string_to_utf8_string (ecma_str_ptr, &utf8_ptr, &utf8_str_size);
64+
uint8_t utf8_ptr ## flags = 0; \
65+
const lit_utf8_byte_t *utf8_ptr = ecma_string_get_chars (ecma_str_ptr, &utf8_str_size, &utf8_ptr ## flags);
66+
67+
/**
68+
* Free the cesu-8 string buffer allocated by 'ECMA_STRING_TO_UTF8_STRING'
69+
*/
70+
#define ECMA_FINALIZE_UTF8_STRING(utf8_ptr, /**< pointer to character buffer */ \
71+
utf8_str_size) /**< buffer size */ \
72+
if (utf8_ptr ## flags & 2) \
73+
{ \
74+
JERRY_ASSERT (utf8_ptr != NULL); \
75+
jmem_heap_free_block ((void *) utf8_ptr, utf8_str_size); \
76+
}
6677

6778
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
6879

@@ -118,17 +129,6 @@ bool ecma_string_to_utf8_string (const ecma_string_t *ecma_str_ptr,
118129

119130
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
120131

121-
/**
122-
* Free the cesu-8 string buffer allocated by 'ECMA_STRING_TO_UTF8_STRING'
123-
*/
124-
#define ECMA_FINALIZE_UTF8_STRING(utf8_ptr, /**< pointer to character buffer */ \
125-
utf8_str_size) /**< buffer size */ \
126-
if (utf8_ptr ## must_be_freed) \
127-
{ \
128-
JERRY_ASSERT (utf8_ptr != NULL); \
129-
jmem_heap_free_block ((void *) utf8_ptr, utf8_str_size); \
130-
}
131-
132132
/**
133133
* Convert boolean to bitfield value.
134134
*/
@@ -229,7 +229,7 @@ ecma_substring_copy_to_utf8_buffer (const ecma_string_t *string_desc_p,
229229
lit_utf8_size_t buffer_size);
230230
void ecma_string_to_utf8_bytes (const ecma_string_t *string_desc_p, lit_utf8_byte_t *buffer_p,
231231
lit_utf8_size_t buffer_size);
232-
const lit_utf8_byte_t *ecma_string_get_chars (const ecma_string_t *string_p, lit_utf8_size_t *size_p, bool *is_ascii_p);
232+
const lit_utf8_byte_t *ecma_string_get_chars (const ecma_string_t *string_p, lit_utf8_size_t *size_p, uint8_t *flags_p);
233233
void ecma_init_ecma_string_from_uint32 (ecma_string_t *string_desc_p, uint32_t uint32_number);
234234
void ecma_init_ecma_magic_string (ecma_string_t *string_desc_p, lit_magic_string_id_t id);
235235
bool ecma_compare_ecma_string_to_magic_id (const ecma_string_t *string_p, lit_magic_string_id_t id);

0 commit comments

Comments
 (0)