@@ -1310,18 +1310,19 @@ ecma_string_get_number_in_desc_size (const uint32_t uint32_number) /**< number i
1310
1310
* Returns with the cesu8 character array of a string.
1311
1311
*
1312
1312
* Note:
1313
- * This function returns with NULL for uint32 strings.
1314
- * The buffer size is rounded up to 8 in this case.
1313
+ * - This function returns with a newly allocated buffer for uint32 strings,
1314
+ * which must be freed.
1315
+ * - The ASCII check only happens if the flags parameter gets
1316
+ * 'ECMA_STRING_FLAG_IS_ASCII' as an input.
1315
1317
*
1316
- * @return NULL - for uint32 strings
1317
- * start of cesu8 characters - otherwise
1318
+ * @return start of cesu8 characters
1318
1319
*/
1319
1320
const lit_utf8_byte_t *
1320
1321
ecma_string_get_chars (const ecma_string_t * string_p , /**< ecma-string */
1321
1322
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 */
1323
+ uint8_t * flags_p ) /**< [in, out] flags: ECMA_STRING_FLAG_EMPTY,
1324
+ ECMA_STRING_FLAG_IS_ASCII,
1325
+ ECMA_STRING_FLAG_MUST_BE_FREED */
1325
1326
{
1326
1327
ecma_length_t length ;
1327
1328
lit_utf8_size_t size ;
@@ -1351,8 +1352,10 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
1351
1352
/* All numbers must be ascii strings. */
1352
1353
JERRY_ASSERT (ecma_string_get_length (string_p ) == size );
1353
1354
1354
- length = size ;
1355
- result_p = NULL ;
1355
+ result_p = (const lit_utf8_byte_t * ) jmem_heap_alloc_block (size );
1356
+ length = ecma_uint32_to_utf8_string (string_p -> u .uint32_number , (lit_utf8_byte_t * ) result_p , size );
1357
+ JERRY_ASSERT (length == size );
1358
+ * flags_p |= ECMA_STRING_FLAG_MUST_BE_FREED ;
1356
1359
break ;
1357
1360
}
1358
1361
case ECMA_STRING_CONTAINER_MAGIC_STRING :
@@ -1369,14 +1372,12 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
1369
1372
default :
1370
1373
{
1371
1374
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p ) == ECMA_STRING_CONTAINER_MAGIC_STRING_EX );
1372
-
1373
- size = lit_get_magic_string_ex_size (string_p -> u .magic_string_ex_id );
1374
1375
length = 0 ;
1375
1376
1376
- if (is_ascii_p != NULL )
1377
+ size = lit_get_magic_string_ex_size (string_p -> u .magic_string_ex_id );
1378
+ if (* flags_p & ECMA_STRING_FLAG_IS_ASCII )
1377
1379
{
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
+ length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p -> u .magic_string_ex_id ), size );
1380
1381
}
1381
1382
1382
1383
result_p = lit_get_magic_string_ex_utf8 (string_p -> u .magic_string_ex_id );
@@ -1385,11 +1386,13 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
1385
1386
}
1386
1387
1387
1388
* size_p = size ;
1388
-
1389
- if (is_ascii_p != NULL )
1389
+ if (* flags_p & ECMA_STRING_FLAG_IS_ASCII )
1390
1390
{
1391
- * is_ascii_p = (length == size );
1391
+ if (length != size ) {
1392
+ * flags_p &= (uint8_t ) ~ECMA_STRING_FLAG_IS_ASCII ;
1393
+ }
1392
1394
}
1395
+
1393
1396
return result_p ;
1394
1397
} /* ecma_string_get_chars */
1395
1398
@@ -1979,32 +1982,23 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */
1979
1982
JERRY_ASSERT (index < ecma_string_get_length (string_p ));
1980
1983
1981
1984
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 );
1985
+ uint8_t flags = ECMA_STRING_FLAG_IS_ASCII ;
1986
+ const lit_utf8_byte_t * chars_p = ecma_string_get_chars (string_p , & buffer_size , & flags );
1984
1987
1985
- if (chars_p != NULL )
1988
+ ecma_char_t ch ;
1989
+ if (flags & ECMA_STRING_FLAG_IS_ASCII )
1986
1990
{
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 );
1991
+ ch = chars_p [index ];
1992
+ }
1993
+ else
1994
+ {
1995
+ ch = lit_utf8_string_code_unit_at (chars_p , buffer_size , index );
1993
1996
}
1994
1997
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 );
1998
+ if (flags & ECMA_STRING_FLAG_MUST_BE_FREED )
1999
+ {
2000
+ jmem_heap_free_block ((void * ) chars_p , buffer_size );
2001
+ }
2008
2002
2009
2003
return ch ;
2010
2004
} /* ecma_string_get_char_at_pos */
0 commit comments