Skip to content

Commit b548eae

Browse files
zherczegyichoi
authored andcommitted
Improve get characters of a string function.
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 0d04c80 commit b548eae

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

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

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,28 +1270,6 @@ ecma_string_to_utf8_bytes (const ecma_string_t *string_desc_p, /**< ecma-string
12701270
JERRY_ASSERT (size == buffer_size);
12711271
} /* ecma_string_to_utf8_bytes */
12721272

1273-
/**
1274-
* Lengths for numeric string values
1275-
*/
1276-
static const uint32_t nums_with_ascending_length[] =
1277-
{
1278-
1u,
1279-
10u,
1280-
100u,
1281-
1000u,
1282-
10000u,
1283-
100000u,
1284-
1000000u,
1285-
10000000u,
1286-
100000000u,
1287-
1000000000u
1288-
};
1289-
1290-
/**
1291-
* Maximum length of numeric strings
1292-
*/
1293-
static const uint32_t max_uint32_len = (uint32_t) (sizeof (nums_with_ascending_length) / sizeof (uint32_t));
1294-
12951273
/**
12961274
* Get size of the number stored locally in the string's descriptor
12971275
*
@@ -1302,12 +1280,24 @@ static const uint32_t max_uint32_len = (uint32_t) (sizeof (nums_with_ascending_l
13021280
static inline ecma_length_t __attr_always_inline___
13031281
ecma_string_get_number_in_desc_size (const uint32_t uint32_number) /**< number in the string-descriptor */
13041282
{
1283+
uint32_t prev_number = 1;
1284+
uint32_t next_number = 100;
13051285
ecma_length_t size = 1;
13061286

1307-
while (size < max_uint32_len && uint32_number >= nums_with_ascending_length[size])
1287+
const uint32_t max_size = 9;
1288+
1289+
while (size < max_size && uint32_number >= next_number)
1290+
{
1291+
prev_number = next_number;
1292+
next_number *= 100;
1293+
size += 2;
1294+
}
1295+
1296+
if (uint32_number >= prev_number * 10)
13081297
{
13091298
size++;
13101299
}
1300+
13111301
return size;
13121302
} /* ecma_string_get_number_in_desc_size */
13131303

@@ -1317,13 +1307,17 @@ ecma_string_get_number_in_desc_size (const uint32_t uint32_number) /**< number i
13171307
#define ECMA_STRING_IS_ASCII(char_p, size) ((size) == lit_utf8_string_length ((char_p), (size)))
13181308

13191309
/**
1320-
* Returns with the raw byte array of the string, if it is available.
1310+
* Returns with the cesu8 character array of a string.
13211311
*
1322-
* @return byte array start - if the byte array of a string is available
1323-
* NULL - otherwise
1312+
* Note:
1313+
* This function returns with NULL for uint32 strings.
1314+
* The buffer size is rounded up to 8 in this case.
1315+
*
1316+
* @return NULL - for uint32 strings
1317+
* start of cesu8 characters - otherwise
13241318
*/
13251319
const lit_utf8_byte_t *
1326-
ecma_string_raw_chars (const ecma_string_t *string_p, /**< ecma-string */
1320+
ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */
13271321
lit_utf8_size_t *size_p, /**< [out] size of the ecma string */
13281322
bool *is_ascii_p) /**< [out] true, if the string is an ascii
13291323
* character sequence (size == length)
@@ -1377,18 +1371,27 @@ ecma_string_raw_chars (const ecma_string_t *string_p, /**< ecma-string */
13771371
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_MAGIC_STRING_EX);
13781372

13791373
size = 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),
1381-
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+
}
13821381

13831382
result_p = lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id);
13841383
break;
13851384
}
13861385
}
13871386

13881387
*size_p = size;
1389-
*is_ascii_p = (length == size);
1388+
1389+
if (is_ascii_p != NULL)
1390+
{
1391+
*is_ascii_p = (length == size);
1392+
}
13901393
return result_p;
1391-
} /* ecma_string_raw_chars */
1394+
} /* ecma_string_get_chars */
13921395

13931396
/**
13941397
* Checks whether the string equals to the magic string id.
@@ -1977,7 +1980,7 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */
19771980

19781981
lit_utf8_size_t buffer_size;
19791982
bool is_ascii;
1980-
const lit_utf8_byte_t *chars_p = ecma_string_raw_chars (string_p, &buffer_size, &is_ascii);
1983+
const lit_utf8_byte_t *chars_p = ecma_string_get_chars (string_p, &buffer_size, &is_ascii);
19811984

19821985
if (chars_p != NULL)
19831986
{
@@ -1996,7 +1999,7 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */
19961999
ecma_string_to_utf8_bytes (string_p, utf8_str_p, buffer_size);
19972000

19982001
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_UINT32_IN_DESC);
1999-
/* Both above must be ascii strings. */
2002+
/* Uint32 must be an ascii string. */
20002003
JERRY_ASSERT (is_ascii);
20012004

20022005
ch = utf8_str_p[index];

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@
5757
utf8_ptr, /**< [out] output buffer pointer */ \
5858
utf8_str_size) /**< [out] output buffer size */ \
5959
lit_utf8_size_t utf8_str_size; \
60-
bool utf8_ptr ## must_be_freed; \
61-
const lit_utf8_byte_t *utf8_ptr = ecma_string_raw_chars (ecma_str_ptr, &utf8_str_size, &utf8_ptr ## must_be_freed); \
62-
utf8_ptr ## must_be_freed = false; /* it was used as 'is_ascii' in 'ecma_string_raw_chars', so we must reset it */ \
60+
const lit_utf8_byte_t *utf8_ptr = ecma_string_get_chars (ecma_str_ptr, &utf8_str_size, NULL); \
61+
bool utf8_ptr ## must_be_freed = false; \
6362
\
6463
if (utf8_ptr == NULL) \
6564
{ \
@@ -232,7 +231,7 @@ ecma_substring_copy_to_utf8_buffer (const ecma_string_t *string_desc_p,
232231
lit_utf8_size_t buffer_size);
233232
void ecma_string_to_utf8_bytes (const ecma_string_t *string_desc_p, lit_utf8_byte_t *buffer_p,
234233
lit_utf8_size_t buffer_size);
235-
const lit_utf8_byte_t *ecma_string_raw_chars (const ecma_string_t *string_p, lit_utf8_size_t *size_p, bool *is_ascii_p);
234+
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);
236235
void ecma_init_ecma_string_from_uint32 (ecma_string_t *string_desc_p, uint32_t uint32_number);
237236
void ecma_init_ecma_magic_string (ecma_string_t *string_desc_p, lit_magic_string_id_t id);
238237
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)