@@ -1106,12 +1106,12 @@ ecma_string_copy_to_utf8_buffer (const ecma_string_t *string_p, /**< ecma-string
11061106 }
11071107 }
11081108
1109- bool is_ascii ;
1110- const lit_utf8_byte_t * chars_p = ecma_string_get_chars (string_p , & size , & is_ascii );
1109+ uint8_t flags = ECMA_STRING_FLAG_IS_ASCII ;
1110+ const lit_utf8_byte_t * chars_p = ecma_string_get_chars (string_p , & size , & flags );
11111111
11121112 JERRY_ASSERT (chars_p != NULL );
11131113
1114- if (is_ascii )
1114+ if (flags & ECMA_STRING_FLAG_IS_ASCII )
11151115 {
11161116 JERRY_ASSERT (size <= buffer_size );
11171117 memcpy (buffer_p , chars_p , size );
@@ -1123,6 +1123,11 @@ ecma_string_copy_to_utf8_buffer (const ecma_string_t *string_p, /**< ecma-string
11231123 buffer_p ,
11241124 buffer_size );
11251125
1126+ if (flags & ECMA_STRING_FLAG_MUST_BE_FREED )
1127+ {
1128+ jmem_heap_free_block ((void * ) chars_p , size );
1129+ }
1130+
11261131 JERRY_ASSERT (size <= buffer_size );
11271132 return size ;
11281133} /* ecma_string_copy_to_utf8_buffer */
@@ -1392,18 +1397,19 @@ ecma_string_get_uint32_size (const uint32_t uint32_number) /**< number in the st
13921397 * Returns with the cesu8 character array of a string.
13931398 *
13941399 * Note:
1395- * This function returns with NULL for uint32 strings.
1396- * The buffer size is rounded up to 8 in this case.
1400+ * - This function returns with a newly allocated buffer for uint32 strings,
1401+ * which must be freed.
1402+ * - The ASCII check only happens if the flags parameter gets
1403+ * 'ECMA_STRING_FLAG_IS_ASCII' as an input.
13971404 *
1398- * @return NULL - for uint32 strings
1399- * start of cesu8 characters - otherwise
1405+ * @return start of cesu8 characters
14001406 */
14011407const lit_utf8_byte_t *
14021408ecma_string_get_chars (const ecma_string_t * string_p , /**< ecma-string */
14031409 lit_utf8_size_t * size_p , /**< [out] size of the ecma string */
1404- bool * is_ascii_p ) /**< [out] true, if the string is an ascii
1405- * character sequence (size == length)
1406- * false, otherwise */
1410+ uint8_t * flags_p ) /**< [in, out] flags: ECMA_STRING_FLAG_EMPTY,
1411+ ECMA_STRING_FLAG_IS_ASCII,
1412+ ECMA_STRING_FLAG_MUST_BE_FREED */
14071413{
14081414 ecma_length_t length ;
14091415 lit_utf8_size_t size ;
@@ -1431,11 +1437,10 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
14311437 uint32_t uint32_number = (uint32_t ) ECMA_GET_DIRECT_STRING_VALUE (string_p );
14321438 size = (lit_utf8_size_t ) ecma_string_get_uint32_size (uint32_number );
14331439
1434- /* All numbers must be ascii strings. */
1435- JERRY_ASSERT (ecma_string_get_length (string_p ) == size );
1436-
1437- length = size ;
1438- result_p = NULL ;
1440+ result_p = (const lit_utf8_byte_t * ) jmem_heap_alloc_block (size );
1441+ length = ecma_uint32_to_utf8_string (uint32_number , (lit_utf8_byte_t * ) result_p , size );
1442+ JERRY_ASSERT (length == size );
1443+ * flags_p |= ECMA_STRING_FLAG_MUST_BE_FREED ;
14391444 break ;
14401445 }
14411446 default :
@@ -1447,10 +1452,9 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
14471452 size = lit_get_magic_string_ex_size (id );
14481453 length = 0 ;
14491454
1450- if (unlikely (is_ascii_p != NULL ))
1455+ if (unlikely (* flags_p & ECMA_STRING_FLAG_IS_ASCII ))
14511456 {
1452- length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p -> u .magic_string_ex_id ),
1453- lit_get_magic_string_ex_size (string_p -> u .magic_string_ex_id ));
1457+ length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p -> u .magic_string_ex_id ), size );
14541458 }
14551459
14561460 result_p = lit_get_magic_string_ex_utf8 (id );
@@ -1483,12 +1487,12 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
14831487 {
14841488 size = (lit_utf8_size_t ) ecma_string_get_uint32_size (string_p -> u .uint32_number );
14851489
1486- /* All numbers must be ascii strings. */
1487- JERRY_ASSERT (ecma_string_get_length (string_p ) == size );
1488-
1489- length = size ;
1490- result_p = NULL ;
1490+ result_p = (const lit_utf8_byte_t * ) jmem_heap_alloc_block (size );
1491+ length = ecma_uint32_to_utf8_string (string_p -> u .uint32_number , (lit_utf8_byte_t * ) result_p , size );
1492+ JERRY_ASSERT (length == size );
1493+ * flags_p |= ECMA_STRING_FLAG_MUST_BE_FREED ;
14911494 break ;
1495+
14921496 }
14931497 default :
14941498 {
@@ -1497,10 +1501,9 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
14971501 size = lit_get_magic_string_ex_size (string_p -> u .magic_string_ex_id );
14981502 length = 0 ;
14991503
1500- if (unlikely (is_ascii_p != NULL ))
1504+ if (unlikely (* flags_p & ECMA_STRING_FLAG_IS_ASCII ))
15011505 {
1502- length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p -> u .magic_string_ex_id ),
1503- lit_get_magic_string_ex_size (string_p -> u .magic_string_ex_id ));
1506+ length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p -> u .magic_string_ex_id ), size );
15041507 }
15051508
15061509 result_p = lit_get_magic_string_ex_utf8 (string_p -> u .magic_string_ex_id );
@@ -1510,11 +1513,14 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
15101513 }
15111514
15121515 * size_p = size ;
1513-
1514- if (is_ascii_p != NULL )
1516+ if (* flags_p & ECMA_STRING_FLAG_IS_ASCII )
15151517 {
1516- * is_ascii_p = (length == size );
1518+ if (length != size )
1519+ {
1520+ * flags_p &= (uint8_t ) ~ECMA_STRING_FLAG_IS_ASCII ;
1521+ }
15171522 }
1523+
15181524 return result_p ;
15191525} /* ecma_string_get_chars */
15201526
@@ -2126,31 +2132,23 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */
21262132 JERRY_ASSERT (index < ecma_string_get_length (string_p ));
21272133
21282134 lit_utf8_size_t buffer_size ;
2129- bool is_ascii ;
2130- const lit_utf8_byte_t * chars_p = ecma_string_get_chars (string_p , & buffer_size , & is_ascii );
2135+ uint8_t flags = ECMA_STRING_FLAG_IS_ASCII ;
2136+ const lit_utf8_byte_t * chars_p = ecma_string_get_chars (string_p , & buffer_size , & flags );
21312137
2132- if (chars_p != NULL )
2138+ ecma_char_t ch ;
2139+ if (flags & ECMA_STRING_FLAG_IS_ASCII )
21332140 {
2134- if (is_ascii )
2135- {
2136- return chars_p [index ];
2137- }
2138-
2139- return lit_utf8_string_code_unit_at (chars_p , buffer_size , index );
2141+ ch = chars_p [index ];
2142+ }
2143+ else
2144+ {
2145+ ch = lit_utf8_string_code_unit_at (chars_p , buffer_size , index );
21402146 }
21412147
2142- ecma_char_t ch ;
2143-
2144- JMEM_DEFINE_LOCAL_ARRAY (utf8_str_p , buffer_size , lit_utf8_byte_t );
2145-
2146- ecma_string_to_utf8_bytes (string_p , utf8_str_p , buffer_size );
2147-
2148- /* Uint32 must be an ascii string. */
2149- JERRY_ASSERT (is_ascii );
2150-
2151- ch = utf8_str_p [index ];
2152-
2153- JMEM_FINALIZE_LOCAL_ARRAY (utf8_str_p );
2148+ if (flags & ECMA_STRING_FLAG_MUST_BE_FREED )
2149+ {
2150+ jmem_heap_free_block ((void * ) chars_p , buffer_size );
2151+ }
21542152
21552153 return ch ;
21562154} /* ecma_string_get_char_at_pos */
0 commit comments