@@ -1285,6 +1285,64 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma-
12851285 return is_first_less_than_second;
12861286} /* ecma_compare_ecma_strings_relational */
12871287
1288+ /* *
1289+ * Lengths for numeric string values
1290+ */
1291+ static const uint32_t nums_with_ascending_length[] =
1292+ {
1293+ 1u ,
1294+ 10u ,
1295+ 100u ,
1296+ 1000u ,
1297+ 10000u ,
1298+ 100000u ,
1299+ 1000000u ,
1300+ 10000000u ,
1301+ 100000000u ,
1302+ 1000000000u
1303+ };
1304+
1305+ /* *
1306+ * Maximum length of numeric strings
1307+ */
1308+ static const uint32_t max_uint32_len = sizeof (nums_with_ascending_length) / sizeof (uint32_t );
1309+
1310+ /* *
1311+ * Get size of container heap number of ecma-string
1312+ *
1313+ * Note: the number size and length are equal
1314+ *
1315+ * @return number of bytes in the buffer
1316+ */
1317+ static lit_utf8_size_t
1318+ ecma_string_get_heap_number_size (mem_cpointer_t number_cp) /* *< ecma-string */
1319+ {
1320+ const ecma_number_t *num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t , number_cp);
1321+ lit_utf8_byte_t buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
1322+
1323+ return ecma_number_to_utf8_string (*num_p, buffer, sizeof (buffer));
1324+ } /* ecma_string_get_heap_number_size */
1325+
1326+
1327+ /* *
1328+ * Get size of the number stored locally in the string's descriptor
1329+ *
1330+ * Note: the represented number size and length are equal
1331+ *
1332+ * @return size in bytes
1333+ */
1334+ static ecma_length_t
1335+ ecma_string_get_number_in_desc_size (const uint32_t uint32_number)
1336+ {
1337+ ecma_length_t size = 1 ;
1338+
1339+ while (size < max_uint32_len && uint32_number >= nums_with_ascending_length[size])
1340+ {
1341+ size++;
1342+ }
1343+ return size;
1344+ } /* ecma_string_get_number_in_desc_size */
1345+
12881346/* *
12891347 * Get length of ecma-string
12901348 *
@@ -1293,75 +1351,46 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma-
12931351ecma_length_t
12941352ecma_string_get_length (const ecma_string_t *string_p) /* *< ecma-string */
12951353{
1296- ecma_string_container_t container = (ecma_string_container_t ) string_p->container ;
1297-
1298- if (container == ECMA_STRING_CONTAINER_LIT_TABLE)
1299- {
1300- lit_literal_t lit = lit_get_literal_by_cp (string_p->u .lit_cp );
1301- JERRY_ASSERT (RCS_RECORD_IS_CHARSET (lit));
1302- return lit_charset_literal_get_length (lit);
1303- }
1304- else if (container == ECMA_STRING_CONTAINER_MAGIC_STRING)
1305- {
1306- TODO (" Cache magic string lengths" )
1307- return lit_utf8_string_length (lit_get_magic_string_utf8 (string_p->u .magic_string_id ),
1308- lit_get_magic_string_size (string_p->u .magic_string_id ));
1309- }
1310- else if (container == ECMA_STRING_CONTAINER_MAGIC_STRING_EX)
1311- {
1312- TODO (" Cache magic string lengths" )
1313- return lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u .magic_string_ex_id ),
1314- lit_get_magic_string_ex_size (string_p->u .magic_string_ex_id ));
1315- }
1316- else if (container == ECMA_STRING_CONTAINER_UINT32_IN_DESC)
1354+ switch ((ecma_string_container_t ) string_p->container )
13171355 {
1318- const uint32_t uint32_number = string_p->u .uint32_number ;
1319- const uint32_t max_uint32_len = 10 ;
1320- const uint32_t nums_with_ascending_length[10 ] =
1356+ case ECMA_STRING_CONTAINER_LIT_TABLE:
13211357 {
1322- 1u ,
1323- 10u ,
1324- 100u ,
1325- 1000u ,
1326- 10000u ,
1327- 100000u ,
1328- 1000000u ,
1329- 10000000u ,
1330- 100000000u ,
1331- 1000000000u
1332- };
1333-
1334- ecma_length_t length = 1 ;
1335-
1336- while (length < max_uint32_len
1337- && uint32_number >= nums_with_ascending_length[length])
1358+ lit_literal_t lit = lit_get_literal_by_cp (string_p->u .lit_cp );
1359+ JERRY_ASSERT (RCS_RECORD_IS_CHARSET (lit));
1360+ return lit_charset_literal_get_length (lit);
1361+ }
1362+ case ECMA_STRING_CONTAINER_MAGIC_STRING:
13381363 {
1339- length++;
1364+ TODO (" Cache magic string lengths" )
1365+ return lit_utf8_string_length (lit_get_magic_string_utf8 (string_p->u .magic_string_id ),
1366+ lit_get_magic_string_size (string_p->u .magic_string_id ));
13401367 }
1368+ case ECMA_STRING_CONTAINER_MAGIC_STRING_EX:
1369+ {
1370+ TODO (" Cache magic string lengths" )
1371+ return lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u .magic_string_ex_id ),
1372+ lit_get_magic_string_ex_size (string_p->u .magic_string_ex_id ));
1373+ }
1374+ case ECMA_STRING_CONTAINER_UINT32_IN_DESC:
1375+ {
1376+ return ecma_string_get_number_in_desc_size (string_p->u .uint32_number );
1377+ }
1378+ case ECMA_STRING_CONTAINER_HEAP_NUMBER:
1379+ {
1380+ return (ecma_length_t ) ecma_string_get_heap_number_size (string_p->u .number_cp );
1381+ }
1382+ default :
1383+ {
1384+ JERRY_ASSERT ((ecma_string_container_t ) string_p->container == ECMA_STRING_CONTAINER_HEAP_CHUNKS);
13411385
1342- return length;
1343- }
1344- else if (container == ECMA_STRING_CONTAINER_HEAP_NUMBER)
1345- {
1346- const ecma_number_t *num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t ,
1347- string_p->u .number_cp );
1348-
1349- lit_utf8_byte_t buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
1350-
1351- return (ecma_length_t ) ecma_number_to_utf8_string (*num_p, buffer, sizeof (buffer));
1352- }
1353- else
1354- {
1355- JERRY_ASSERT (container == ECMA_STRING_CONTAINER_HEAP_CHUNKS);
1356-
1357- const ecma_collection_header_t *collection_header_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t ,
1358- string_p->u .collection_cp );
1386+ const ecma_collection_header_t *collection_header_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t ,
1387+ string_p->u .collection_cp );
13591388
1360- return ecma_get_chars_collection_length (collection_header_p);
1389+ return ecma_get_chars_collection_length (collection_header_p);
1390+ }
13611391 }
13621392} /* ecma_string_get_length */
13631393
1364-
13651394/* *
13661395 * Get size of ecma-string
13671396 *
@@ -1389,39 +1418,11 @@ ecma_string_get_size (const ecma_string_t *string_p) /**< ecma-string */
13891418 }
13901419 case ECMA_STRING_CONTAINER_UINT32_IN_DESC:
13911420 {
1392- const uint32_t uint32_number = string_p->u .uint32_number ;
1393- const int32_t max_uint32_len = 10 ;
1394- const uint32_t nums_with_ascending_length[max_uint32_len] =
1395- {
1396- 1u ,
1397- 10u ,
1398- 100u ,
1399- 1000u ,
1400- 10000u ,
1401- 100000u ,
1402- 1000000u ,
1403- 10000000u ,
1404- 100000000u ,
1405- 1000000000u
1406- };
1407-
1408- int32_t size = 1 ;
1409- while (size < max_uint32_len
1410- && uint32_number >= nums_with_ascending_length[size])
1411- {
1412- size++;
1413- }
1414- return (lit_utf8_size_t ) size;
1421+ return (lit_utf8_size_t ) ecma_string_get_number_in_desc_size (string_p->u .uint32_number );
14151422 }
14161423 case ECMA_STRING_CONTAINER_HEAP_NUMBER:
14171424 {
1418- const ecma_number_t *num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t ,
1419- string_p->u .number_cp );
1420- lit_utf8_byte_t buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
1421-
1422- return ecma_number_to_utf8_string (*num_p,
1423- buffer,
1424- sizeof (buffer));
1425+ return ecma_string_get_heap_number_size (string_p->u .number_cp );
14251426 }
14261427 default :
14271428 {
0 commit comments