@@ -1106,12 +1106,12 @@ ecma_string_copy_to_utf8_buffer (const ecma_string_t *string_p, /**< ecma-string
1106
1106
}
1107
1107
}
1108
1108
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 );
1111
1111
1112
1112
JERRY_ASSERT (chars_p != NULL );
1113
1113
1114
- if (is_ascii )
1114
+ if (flags & ECMA_STRING_FLAG_IS_ASCII )
1115
1115
{
1116
1116
JERRY_ASSERT (size <= buffer_size );
1117
1117
memcpy (buffer_p , chars_p , size );
@@ -1123,6 +1123,11 @@ ecma_string_copy_to_utf8_buffer (const ecma_string_t *string_p, /**< ecma-string
1123
1123
buffer_p ,
1124
1124
buffer_size );
1125
1125
1126
+ if (flags & ECMA_STRING_FLAG_MUST_BE_FREED )
1127
+ {
1128
+ jmem_heap_free_block ((void * ) chars_p , size );
1129
+ }
1130
+
1126
1131
JERRY_ASSERT (size <= buffer_size );
1127
1132
return size ;
1128
1133
} /* ecma_string_copy_to_utf8_buffer */
@@ -1392,18 +1397,19 @@ ecma_string_get_uint32_size (const uint32_t uint32_number) /**< number in the st
1392
1397
* Returns with the cesu8 character array of a string.
1393
1398
*
1394
1399
* 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.
1397
1404
*
1398
- * @return NULL - for uint32 strings
1399
- * start of cesu8 characters - otherwise
1405
+ * @return start of cesu8 characters
1400
1406
*/
1401
1407
const lit_utf8_byte_t *
1402
1408
ecma_string_get_chars (const ecma_string_t * string_p , /**< ecma-string */
1403
1409
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 */
1407
1413
{
1408
1414
ecma_length_t length ;
1409
1415
lit_utf8_size_t size ;
@@ -1431,11 +1437,10 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
1431
1437
uint32_t uint32_number = (uint32_t ) ECMA_GET_DIRECT_STRING_VALUE (string_p );
1432
1438
size = (lit_utf8_size_t ) ecma_string_get_uint32_size (uint32_number );
1433
1439
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 ;
1439
1444
break ;
1440
1445
}
1441
1446
default :
@@ -1447,10 +1452,9 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
1447
1452
size = lit_get_magic_string_ex_size (id );
1448
1453
length = 0 ;
1449
1454
1450
- if (unlikely (is_ascii_p != NULL ))
1455
+ if (unlikely (* flags_p & ECMA_STRING_FLAG_IS_ASCII ))
1451
1456
{
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 );
1454
1458
}
1455
1459
1456
1460
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 */
1483
1487
{
1484
1488
size = (lit_utf8_size_t ) ecma_string_get_uint32_size (string_p -> u .uint32_number );
1485
1489
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 ;
1491
1494
break ;
1495
+
1492
1496
}
1493
1497
default :
1494
1498
{
@@ -1497,10 +1501,9 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
1497
1501
size = lit_get_magic_string_ex_size (string_p -> u .magic_string_ex_id );
1498
1502
length = 0 ;
1499
1503
1500
- if (unlikely (is_ascii_p != NULL ))
1504
+ if (unlikely (* flags_p & ECMA_STRING_FLAG_IS_ASCII ))
1501
1505
{
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 );
1504
1507
}
1505
1508
1506
1509
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 */
1510
1513
}
1511
1514
1512
1515
* size_p = size ;
1513
-
1514
- if (is_ascii_p != NULL )
1516
+ if (* flags_p & ECMA_STRING_FLAG_IS_ASCII )
1515
1517
{
1516
- * is_ascii_p = (length == size );
1518
+ if (length != size )
1519
+ {
1520
+ * flags_p = (uint8_t ) (* flags_p & ~ECMA_STRING_FLAG_IS_ASCII );
1521
+ }
1517
1522
}
1523
+
1518
1524
return result_p ;
1519
1525
} /* ecma_string_get_chars */
1520
1526
@@ -2126,31 +2132,23 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */
2126
2132
JERRY_ASSERT (index < ecma_string_get_length (string_p ));
2127
2133
2128
2134
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 );
2131
2137
2132
- if (chars_p != NULL )
2138
+ ecma_char_t ch ;
2139
+ if (flags & ECMA_STRING_FLAG_IS_ASCII )
2133
2140
{
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 );
2140
2146
}
2141
2147
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
+ }
2154
2152
2155
2153
return ch ;
2156
2154
} /* ecma_string_get_char_at_pos */
0 commit comments