Refactor builtins to handle CESU-8 encoded strings.

JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com
This commit is contained in:
Dániel Bátyai
2015-09-09 14:27:17 +02:00
parent dcd610b305
commit 579b1edaa5
17 changed files with 517 additions and 696 deletions
@@ -48,27 +48,22 @@
* @return NaN if cannot read from string, ToNumber() otherwise
*/
static ecma_number_t
ecma_date_parse_date_chars (lit_utf8_iterator_t *iter, /**< iterator of the utf8 string */
ecma_date_parse_date_chars (lit_utf8_byte_t **str_p, /**< pointer to the cesu8 string */
const lit_utf8_byte_t *str_end_p, /**< pointer to the end of the string */
uint32_t num_of_chars) /**< number of characters to read and convert */
{
JERRY_ASSERT (num_of_chars > 0);
lit_utf8_size_t copy_size = 0;
const lit_utf8_byte_t *str_start_p = iter->buf_p + iter->buf_pos.offset;
const lit_utf8_byte_t *str_start_p = *str_p;
while (num_of_chars--)
{
if (lit_utf8_iterator_is_eos (iter)
|| !lit_char_is_decimal_digit (lit_utf8_iterator_peek_next (iter)))
if (*str_p >= str_end_p || !lit_char_is_decimal_digit (lit_utf8_read_next (str_p)))
{
return ecma_number_make_nan ();
}
copy_size += lit_get_unicode_char_size_by_utf8_first_byte (*(iter->buf_p + iter->buf_pos.offset));
lit_utf8_iterator_incr (iter);
}
return ecma_utf8_string_to_number (str_start_p, copy_size);
return ecma_utf8_string_to_number (str_start_p, (lit_utf8_size_t) (*str_p - str_start_p));
} /* ecma_date_parse_date_chars */
/**
@@ -211,10 +206,11 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
ssize_t sz = ecma_string_to_utf8_string (date_str_p, date_start_p, (ssize_t) date_str_size);
JERRY_ASSERT (sz >= 0);
lit_utf8_iterator_t iter = lit_utf8_iterator_create (date_start_p, date_str_size);
lit_utf8_byte_t *date_str_curr_p = date_start_p;
const lit_utf8_byte_t *date_str_end_p = date_start_p + date_str_size;
/* 1. read year */
ecma_number_t year = ecma_date_parse_date_chars (&iter, 4);
ecma_number_t year = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 4);
if (!ecma_number_is_nan (year)
&& year >= 0)
@@ -224,12 +220,12 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
ecma_number_t time = ECMA_NUMBER_ZERO;
/* 2. read month if any */
if (!lit_utf8_iterator_is_eos (&iter)
&& lit_utf8_iterator_peek_next (&iter) == '-')
if (date_str_curr_p < date_str_end_p
&& *date_str_curr_p == '-')
{
/* eat up '-' */
lit_utf8_iterator_incr (&iter);
month = ecma_date_parse_date_chars (&iter, 2);
date_str_curr_p++;
month = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 2);
if (month > 12 || month < 1)
{
@@ -238,12 +234,12 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
}
/* 3. read day if any */
if (!lit_utf8_iterator_is_eos (&iter)
&& lit_utf8_iterator_peek_next (&iter) == '-')
if (date_str_curr_p < date_str_end_p
&& *date_str_curr_p == '-')
{
/* eat up '-' */
lit_utf8_iterator_incr (&iter);
day = ecma_date_parse_date_chars (&iter, 2);
date_str_curr_p++;
day = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 2);
if (day < 1 || day > 31)
{
@@ -252,24 +248,24 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
}
/* 4. read time if any */
if (!lit_utf8_iterator_is_eos (&iter)
&& lit_utf8_iterator_peek_next (&iter) == 'T')
if (date_str_curr_p < date_str_end_p
&& *date_str_curr_p == 'T')
{
/* eat up 'T' */
date_str_curr_p++;
ecma_number_t hours = ECMA_NUMBER_ZERO;
ecma_number_t minutes = ECMA_NUMBER_ZERO;
ecma_number_t seconds = ECMA_NUMBER_ZERO;
ecma_number_t milliseconds = ECMA_NUMBER_ZERO;
ecma_length_t num_of_visited_chars = lit_utf8_iterator_get_index (&iter);
ecma_length_t date_str_len = lit_utf8_string_length (iter.buf_p, iter.buf_size) - 1;
ecma_length_t remaining_length = lit_utf8_string_length (date_str_curr_p,
(lit_utf8_size_t) (date_str_end_p - date_str_curr_p));
if ((date_str_len - num_of_visited_chars) >= 5)
if (remaining_length >= 5)
{
/* eat up 'T' */
lit_utf8_iterator_incr (&iter);
/* 4.1 read hours and minutes */
hours = ecma_date_parse_date_chars (&iter, 2);
hours = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 2);
if (hours < 0 || hours > 24)
{
@@ -281,9 +277,9 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
}
/* eat up ':' */
lit_utf8_iterator_incr (&iter);
date_str_curr_p++;
minutes = ecma_date_parse_date_chars (&iter, 2);
minutes = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 2);
if (minutes < 0 || minutes > 59)
{
@@ -291,11 +287,12 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
}
/* 4.2 read seconds if any */
if (!lit_utf8_iterator_is_eos (&iter) && lit_utf8_iterator_peek_next (&iter) == ':')
if (date_str_curr_p < date_str_end_p
&& *date_str_curr_p == ':')
{
/* eat up ':' */
lit_utf8_iterator_incr (&iter);
seconds = ecma_date_parse_date_chars (&iter, 2);
date_str_curr_p++;
seconds = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 2);
if (seconds < 0 || seconds > 59)
{
@@ -303,11 +300,12 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
}
/* 4.3 read milliseconds if any */
if (!lit_utf8_iterator_is_eos (&iter) && lit_utf8_iterator_peek_next (&iter) == '.')
if (date_str_curr_p < date_str_end_p
&& *date_str_curr_p == '.')
{
/* eat up '.' */
lit_utf8_iterator_incr (&iter);
milliseconds = ecma_date_parse_date_chars (&iter, 3);
date_str_curr_p++;
milliseconds = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 3);
if (milliseconds < 0)
{
@@ -324,34 +322,34 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
}
/* 4.4 read timezone if any */
if (!lit_utf8_iterator_is_eos (&iter)
&& lit_utf8_iterator_peek_next (&iter) == 'Z'
if (date_str_curr_p < date_str_end_p
&& *date_str_curr_p == 'Z'
&& !ecma_number_is_nan (time))
{
lit_utf8_iterator_incr (&iter);
date_str_curr_p++;
time = ecma_date_make_time (hours, minutes, seconds, milliseconds);
}
else if (!lit_utf8_iterator_is_eos (&iter)
&& (lit_utf8_iterator_peek_next (&iter) == '+'
|| lit_utf8_iterator_peek_next (&iter) == '-'))
else if (date_str_curr_p < date_str_end_p
&& (*date_str_curr_p == '+' || *date_str_curr_p == '-'))
{
ecma_length_t num_of_visited_chars = lit_utf8_iterator_get_index (&iter);
ecma_length_t date_str_len = lit_utf8_string_length (iter.buf_p, iter.buf_size) - 1;
ecma_length_t remaining_length;
remaining_length = lit_utf8_string_length (date_str_curr_p,
(lit_utf8_size_t) (date_str_end_p - date_str_curr_p)) - 1;
if ((date_str_len - num_of_visited_chars) == 5)
if (remaining_length == 5)
{
bool is_negative = false;
if (lit_utf8_iterator_peek_next (&iter) == '-')
if (*date_str_curr_p == '-')
{
is_negative = true;
}
/* eat up '+/-' */
lit_utf8_iterator_incr (&iter);
date_str_curr_p++;
/* read hours and minutes */
hours = ecma_date_parse_date_chars (&iter, 2);
hours = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 2);
if (hours < 0 || hours > 24)
{
@@ -363,9 +361,9 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
}
/* eat up ':' */
lit_utf8_iterator_incr (&iter);
date_str_curr_p++;
minutes = ecma_date_parse_date_chars (&iter, 2);
minutes = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 2);
if (minutes < 0 || minutes > 59)
{
@@ -384,7 +382,7 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
}
}
if (lit_utf8_iterator_is_eos (&iter))
if (date_str_curr_p >= date_str_end_p)
{
ecma_number_t date = ecma_date_make_day (year, month - 1, day);
*date_num_p = ecma_date_make_date (date, time);
@@ -90,11 +90,12 @@ ecma_builtin_global_object_print (ecma_value_t this_arg __attr_unused___, /**< t
ssize_t actual_sz = ecma_string_to_utf8_string (str_p, utf8_str_p, (ssize_t) utf8_str_size);
JERRY_ASSERT (actual_sz == (ssize_t) utf8_str_size);
lit_utf8_iterator_t str_iter = lit_utf8_iterator_create (utf8_str_p, utf8_str_size);
lit_utf8_byte_t *utf8_str_curr_p = utf8_str_p;
const lit_utf8_byte_t *utf8_str_end_p = utf8_str_p + utf8_str_size;
while (!lit_utf8_iterator_is_eos (&str_iter))
while (utf8_str_curr_p < utf8_str_end_p)
{
ecma_char_t code_point = lit_utf8_iterator_read_next (&str_iter);
ecma_char_t code_point = lit_utf8_read_next (&utf8_str_curr_p);
if (code_point == LIT_CHAR_NULL)
{
@@ -207,42 +208,40 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
if (str_size > 0)
{
MEM_DEFINE_LOCAL_ARRAY (utf8_string_buff, str_size, lit_utf8_byte_t);
MEM_DEFINE_LOCAL_ARRAY (string_buff, str_size, lit_utf8_byte_t);
ssize_t bytes_copied = ecma_string_to_utf8_string (number_str_p,
utf8_string_buff,
string_buff,
(ssize_t) str_size);
JERRY_ASSERT (bytes_copied >= 0);
lit_utf8_iterator_t iter = lit_utf8_iterator_create (utf8_string_buff, str_size);
lit_utf8_byte_t *string_curr_p = string_buff;
lit_utf8_byte_t *string_end_p = string_buff + str_size;
/* 2. Remove leading whitespace. */
lit_utf8_iterator_seek_eos (&iter);
lit_utf8_iterator_pos_t start = lit_utf8_iterator_get_pos (&iter);
lit_utf8_iterator_pos_t end = lit_utf8_iterator_get_pos (&iter);
lit_utf8_byte_t *start_p = string_end_p;
lit_utf8_byte_t *end_p = string_end_p;
lit_utf8_iterator_seek_bos (&iter);
while (!lit_utf8_iterator_is_eos (&iter))
while (string_curr_p < string_end_p)
{
ecma_char_t current_char = lit_utf8_iterator_read_next (&iter);
ecma_char_t current_char = lit_utf8_read_next (&string_curr_p);
if (!lit_char_is_white_space (current_char)
&& !lit_char_is_line_terminator (current_char))
{
lit_utf8_iterator_read_prev (&iter);
start = lit_utf8_iterator_get_pos (&iter);
lit_utf8_decr (&string_curr_p);
start_p = string_curr_p;
break;
}
}
if (!lit_utf8_iterator_is_eos (&iter))
if (string_curr_p < string_end_p)
{
/* 3. */
int sign = 1;
/* 4. */
ecma_char_t current = lit_utf8_iterator_read_next (&iter);
ecma_char_t current = lit_utf8_read_next (&string_curr_p);
if (current == LIT_CHAR_MINUS)
{
sign = -1;
@@ -251,10 +250,10 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
/* 5. */
if (current == LIT_CHAR_MINUS || current == LIT_CHAR_PLUS)
{
start = lit_utf8_iterator_get_pos (&iter);
if (!lit_utf8_iterator_is_eos (&iter))
start_p = string_curr_p;
if (string_curr_p < string_end_p)
{
current = lit_utf8_iterator_read_next (&iter);
current = lit_utf8_read_next (&string_curr_p);
}
}
@@ -292,25 +291,23 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
/* 10. */
if (strip_prefix)
{
if (end.offset - start.offset >= 2 && current == LIT_CHAR_0)
if (end_p - start_p >= 2 && current == LIT_CHAR_0)
{
ecma_char_t next = lit_utf8_iterator_peek_next (&iter);
ecma_char_t next = *string_curr_p;
if (next == LIT_CHAR_LOWERCASE_X || next == LIT_CHAR_UPPERCASE_X)
{
/* Skip the 'x' or 'X' characters. */
lit_utf8_iterator_incr (&iter);
start = lit_utf8_iterator_get_pos (&iter);
start_p = ++string_curr_p;
rad = 16;
}
}
}
/* 11. Check if characters are in [0, Radix - 1]. We also convert them to number values in the process. */
lit_utf8_iterator_seek (&iter, start);
while (!lit_utf8_iterator_is_eos (&iter))
string_curr_p = start_p;
while (string_curr_p < string_end_p)
{
ecma_char_t current_char = lit_utf8_iterator_read_next (&iter);
ecma_char_t current_char = *string_curr_p++;
int32_t current_number;
if ((current_char >= LIT_CHAR_LOWERCASE_A && current_char <= LIT_CHAR_LOWERCASE_Z))
@@ -333,14 +330,13 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
if (!(current_number < rad))
{
lit_utf8_iterator_decr (&iter);
end = lit_utf8_iterator_get_pos (&iter);
end_p = --string_curr_p;
break;
}
}
/* 12. */
if (end.offset - start.offset == 0)
if (end_p == start_p)
{
ecma_number_t *ret_num_p = ecma_alloc_number ();
*ret_num_p = ecma_number_make_nan ();
@@ -355,17 +351,11 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
ecma_number_t multiplier = 1.0f;
/* 13. and 14. */
if (end.offset < str_size)
string_curr_p = end_p;
while (string_curr_p > start_p)
{
lit_utf8_iterator_seek (&iter, end);
}
else
{
lit_utf8_iterator_seek_eos (&iter);
}
while (lit_utf8_iterator_get_pos (&iter).offset > start.offset)
{
ecma_char_t current_char = lit_utf8_iterator_read_prev (&iter);
ecma_char_t current_char = *(--string_curr_p);
ecma_number_t current_number;
if ((current_char >= LIT_CHAR_LOWERCASE_A && current_char <= LIT_CHAR_LOWERCASE_Z))
@@ -407,7 +397,7 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
}
MEM_FINALIZE_LOCAL_ARRAY (utf8_string_buff);
MEM_FINALIZE_LOCAL_ARRAY (string_buff);
}
else
{
@@ -443,32 +433,29 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
if (str_size > 0)
{
MEM_DEFINE_LOCAL_ARRAY (utf8_string_buff, str_size, lit_utf8_byte_t);
MEM_DEFINE_LOCAL_ARRAY (string_buff, str_size, lit_utf8_byte_t);
ssize_t bytes_copied = ecma_string_to_utf8_string (number_str_p,
utf8_string_buff,
string_buff,
(ssize_t) str_size);
JERRY_ASSERT (bytes_copied >= 0);
lit_utf8_iterator_t iter = lit_utf8_iterator_create (utf8_string_buff, str_size);
lit_utf8_iterator_seek_eos (&iter);
lit_utf8_iterator_pos_t start = lit_utf8_iterator_get_pos (&iter);
lit_utf8_iterator_pos_t end = lit_utf8_iterator_get_pos (&iter);
lit_utf8_iterator_seek_bos (&iter);
lit_utf8_byte_t *str_curr_p = string_buff;
lit_utf8_byte_t *str_end_p = string_buff + str_size;
lit_utf8_byte_t *start_p = str_end_p;
lit_utf8_byte_t *end_p = str_end_p;
/* 2. Find first non whitespace char and set starting position. */
while (!lit_utf8_iterator_is_eos (&iter))
while (str_curr_p < str_end_p)
{
ecma_char_t current_char = lit_utf8_iterator_read_next (&iter);
ecma_char_t current_char = lit_utf8_read_next (&str_curr_p);
if (!lit_char_is_white_space (current_char)
&& !lit_char_is_line_terminator (current_char))
{
lit_utf8_iterator_decr (&iter);
start = lit_utf8_iterator_get_pos (&iter);
lit_utf8_decr (&str_curr_p);
start_p = str_curr_p;
break;
}
}
@@ -476,10 +463,10 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
bool sign = false;
ecma_char_t current;
if (!lit_utf8_iterator_is_eos (&iter))
if (str_curr_p < str_end_p)
{
/* Check if sign is present. */
current = lit_utf8_iterator_read_next (&iter);
current = *str_curr_p;
if (current == LIT_CHAR_MINUS)
{
sign = true;
@@ -488,27 +475,21 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
if (current == LIT_CHAR_MINUS || current == LIT_CHAR_PLUS)
{
/* Set starting position to be after the sign character. */
start = lit_utf8_iterator_get_pos (&iter);
}
else
{
lit_utf8_iterator_decr (&iter);
start_p = ++str_curr_p;
}
}
ecma_number_t *ret_num_p = ecma_alloc_number ();
const lit_utf8_byte_t *infinity_utf8_str_p = lit_get_magic_string_utf8 (LIT_MAGIC_STRING_INFINITY_UL);
lit_utf8_iterator_t infinity_iter = lit_utf8_iterator_create (infinity_utf8_str_p,
sizeof (*infinity_utf8_str_p));
JERRY_ASSERT (!lit_utf8_iterator_is_eos (&infinity_iter));
const lit_utf8_byte_t *infinity_str_p = lit_get_magic_string_utf8 (LIT_MAGIC_STRING_INFINITY_UL);
lit_utf8_byte_t *infinity_str_curr_p = (lit_utf8_byte_t *) infinity_str_p;
lit_utf8_byte_t *infinity_str_end_p = infinity_str_curr_p + sizeof (*infinity_str_p);
/* Check if string is equal to "Infinity". */
while (!lit_utf8_iterator_is_eos (&iter)
&& (lit_utf8_iterator_read_next (&iter) == lit_utf8_iterator_read_next (&infinity_iter)))
while (str_curr_p < str_end_p
&& *str_curr_p++ == *infinity_str_curr_p++)
{
if (lit_utf8_iterator_is_eos (&infinity_iter))
if (infinity_str_curr_p == infinity_str_end_p)
{
/* String matched Infinity. */
*ret_num_p = ecma_number_make_infinity (sign);
@@ -518,11 +499,11 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
}
/* Reset to starting position. */
lit_utf8_iterator_seek (&iter, start);
str_curr_p = start_p;
if (ecma_is_completion_value_empty (ret_value) && !lit_utf8_iterator_is_eos (&iter))
if (ecma_is_completion_value_empty (ret_value) && str_curr_p < str_end_p)
{
current = lit_utf8_iterator_read_next (&iter);
current = *str_curr_p;
bool has_whole_part = false;
bool has_fraction_part = false;
@@ -531,105 +512,93 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
if (lit_char_is_decimal_digit (current))
{
has_whole_part = true;
str_curr_p++;
while (!lit_utf8_iterator_is_eos (&iter))
while (str_curr_p < str_end_p)
{
current = lit_utf8_iterator_read_next (&iter);
current = *str_curr_p++;
if (!lit_char_is_decimal_digit (current))
{
lit_utf8_iterator_decr (&iter);
str_curr_p--;
break;
}
}
}
else
{
lit_utf8_iterator_decr (&iter);
}
/* Set end position to the end of whole part. */
end = lit_utf8_iterator_get_pos (&iter);
if (!lit_utf8_iterator_is_eos (&iter))
end_p = str_curr_p;
if (str_curr_p < str_end_p)
{
current = lit_utf8_iterator_read_next (&iter);
current = *str_curr_p;
}
/* Check decimal point. */
if (current == LIT_CHAR_DOT && !lit_utf8_iterator_is_eos (&iter))
if (current == LIT_CHAR_DOT && str_curr_p < str_end_p)
{
current = lit_utf8_iterator_read_next (&iter);
current = *(++str_curr_p);
if (lit_char_is_decimal_digit (current))
{
has_fraction_part = true;
str_curr_p++;
/* Check digits of fractional part. */
while (!lit_utf8_iterator_is_eos (&iter))
while (str_curr_p < str_end_p)
{
current = lit_utf8_iterator_read_next (&iter);
current = *str_curr_p++;
if (!lit_char_is_decimal_digit (current))
{
lit_utf8_iterator_decr (&iter);
str_curr_p--;
break;
}
}
/* Set end position to end of fraction part. */
end = lit_utf8_iterator_get_pos (&iter);
end_p = str_curr_p;
}
else
{
lit_utf8_iterator_decr (&iter);
}
}
else
{
lit_utf8_iterator_decr (&iter);
}
if (!lit_utf8_iterator_is_eos (&iter))
if (str_curr_p < str_end_p)
{
current = lit_utf8_iterator_read_next (&iter);
current = *str_curr_p++;
}
/* Check exponent. */
if ((current == LIT_CHAR_LOWERCASE_E || current == LIT_CHAR_UPPERCASE_E)
&& (has_whole_part || has_fraction_part)
&& !lit_utf8_iterator_is_eos (&iter))
&& str_curr_p < str_end_p)
{
current = lit_utf8_iterator_read_next (&iter);
current = *str_curr_p++;
/* Check sign of exponent. */
if ((current == LIT_CHAR_PLUS || current == LIT_CHAR_MINUS)
&& !lit_utf8_iterator_is_eos (&iter))
&& str_curr_p < str_end_p)
{
current = lit_utf8_iterator_read_next (&iter);
current = *str_curr_p++;
}
if (lit_char_is_decimal_digit (current))
{
/* Check digits of exponent part. */
while (!lit_utf8_iterator_is_eos (&iter))
while (str_curr_p < str_end_p)
{
current = lit_utf8_iterator_read_next (&iter);
current = *str_curr_p++;
if (!lit_char_is_decimal_digit (current))
{
lit_utf8_iterator_decr (&iter);
str_curr_p--;
break;
}
}
/* Set end position to end of exponent part. */
end = lit_utf8_iterator_get_pos (&iter);
end_p = str_curr_p;
}
}
else
{
lit_utf8_iterator_decr (&iter);
}
/* String did not contain a valid number. */
if (start.offset == end.offset)
if (start_p == end_p)
{
*ret_num_p = ecma_number_make_nan ();
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
@@ -637,8 +606,8 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
else
{
/* 5. */
*ret_num_p = ecma_utf8_string_to_number (utf8_string_buff + start.offset,
(lit_utf8_size_t) (end.offset - start.offset));
*ret_num_p = ecma_utf8_string_to_number (start_p,
(lit_utf8_size_t) (end_p - start_p));
if (sign)
{
@@ -654,7 +623,7 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
*ret_num_p = ecma_number_make_nan ();
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
}
MEM_FINALIZE_LOCAL_ARRAY (utf8_string_buff);
MEM_FINALIZE_LOCAL_ARRAY (string_buff);
}
/* String length is zero. */
else
@@ -1301,12 +1270,13 @@ ecma_builtin_global_object_escape (ecma_value_t this_arg __attr_unused___, /**<
* The escape routine has two major phases: first we compute
* the length of the output, then we encode the input.
*/
lit_utf8_iterator_t iterator = lit_utf8_iterator_create (input_start_p, input_size);
lit_utf8_byte_t *input_curr_p = input_start_p;
lit_utf8_byte_t *input_end_p = input_start_p + input_size;
lit_utf8_size_t output_length = 0;
while (!lit_utf8_iterator_is_eos (&iterator))
while (input_curr_p < input_end_p)
{
ecma_char_t chr = lit_utf8_iterator_read_next (&iterator);
ecma_char_t chr = lit_utf8_read_next (&input_curr_p);
if (chr <= LIT_UTF8_1_BYTE_CODE_POINT_MAX)
{
@@ -1335,11 +1305,11 @@ ecma_builtin_global_object_escape (ecma_value_t this_arg __attr_unused___, /**<
lit_utf8_byte_t *output_char_p = output_start_p;
lit_utf8_iterator_seek_bos (&iterator);
input_curr_p = input_start_p;
while (!lit_utf8_iterator_is_eos (&iterator))
while (input_curr_p < input_end_p)
{
ecma_char_t chr = lit_utf8_iterator_read_next (&iterator);
ecma_char_t chr = lit_utf8_read_next (&input_curr_p);
if (chr <= LIT_UTF8_1_BYTE_CODE_POINT_MAX)
{
@@ -598,10 +598,9 @@ ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**
(ssize_t) (original_size));
JERRY_ASSERT (sz >= 0);
lit_utf8_iterator_t original_it = lit_utf8_iterator_create (original_str_utf8_p, original_size);
ecma_length_t index = start;
lit_utf8_iterator_advance (&original_it, index);
lit_utf8_byte_t *original_str_curr_p = original_str_utf8_p + index;
/* create utf8 string from search string */
MEM_DEFINE_LOCAL_ARRAY (search_str_utf8_p,
@@ -613,7 +612,7 @@ ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**
(ssize_t) (search_size));
JERRY_ASSERT (sz >= 0);
lit_utf8_iterator_t search_it = lit_utf8_iterator_create (search_str_utf8_p, search_size);
lit_utf8_byte_t *search_str_curr_p = search_str_utf8_p;
/* iterate original string and try to match at each position */
bool searching = true;
@@ -622,11 +621,11 @@ ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**
{
/* match as long as possible */
ecma_length_t match_len = 0;
lit_utf8_iterator_t stored_original_it = original_it;
lit_utf8_byte_t *stored_original_str_curr_p = original_str_curr_p;
while (match_len < search_len &&
index + match_len < original_len &&
lit_utf8_iterator_read_next (&original_it) == lit_utf8_iterator_read_next (&search_it))
lit_utf8_read_next (&original_str_curr_p) == lit_utf8_read_next (&search_str_curr_p))
{
match_len++;
}
@@ -640,14 +639,14 @@ ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**
else
{
/* inc/dec index and update iterators and search condition */
lit_utf8_iterator_seek_bos (&search_it);
original_it = stored_original_it;
search_str_curr_p = search_str_utf8_p;
original_str_curr_p = stored_original_str_curr_p;
if (firstIndex)
{
if ((searching = (index <= original_len - search_len)))
{
lit_utf8_iterator_incr (&original_it);
lit_utf8_incr (&original_str_curr_p);
index++;
}
}
@@ -655,7 +654,7 @@ ecma_builtin_helper_string_prototype_object_index_of (ecma_value_t this_arg, /**
{
if ((searching = (index > 0)))
{
lit_utf8_iterator_decr (&original_it);
lit_utf8_decr (&original_str_curr_p);
index--;
}
}
@@ -1145,11 +1145,12 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
JERRY_ASSERT (bytes_copied > 0 || !string_size);
lit_utf8_iterator_t iter = lit_utf8_iterator_create (string_buff, string_size);
lit_utf8_byte_t *str_p = string_buff;
const lit_utf8_byte_t *str_end_p = str_p + string_size;
while (!lit_utf8_iterator_is_eos (&iter))
while (str_p < str_end_p)
{
ecma_char_t current_char = lit_utf8_iterator_read_next (&iter);
ecma_char_t current_char = lit_utf8_read_next (&str_p);
/* 2.a */
if (current_char == LIT_CHAR_BACKSLASH || current_char == LIT_CHAR_DOUBLE_QUOTE)
@@ -369,10 +369,9 @@ ecma_builtin_string_prototype_object_index_of (ecma_value_t this_arg, /**< this
(ssize_t) (original_size));
JERRY_ASSERT (sz >= 0);
lit_utf8_iterator_t original_it = lit_utf8_iterator_create (original_str_utf8_p, original_size);
ecma_length_t index = start;
lit_utf8_iterator_advance (&original_it, index);
lit_utf8_byte_t *original_str_curr_p = original_str_utf8_p + index;
/* create utf8 string from search string */
MEM_DEFINE_LOCAL_ARRAY (search_str_utf8_p,
@@ -384,7 +383,7 @@ ecma_builtin_string_prototype_object_index_of (ecma_value_t this_arg, /**< this
(ssize_t) (search_size));
JERRY_ASSERT (sz >= 0);
lit_utf8_iterator_t search_it = lit_utf8_iterator_create (search_str_utf8_p, search_size);
lit_utf8_byte_t *search_str_curr_p = search_str_utf8_p;
/* iterate original string and try to match at each position */
bool found = false;
@@ -392,10 +391,10 @@ ecma_builtin_string_prototype_object_index_of (ecma_value_t this_arg, /**< this
while (!found && index <= original_len - search_len)
{
ecma_length_t match_len = 0;
lit_utf8_iterator_pos_t stored_original_pos = lit_utf8_iterator_get_pos (&original_it);
lit_utf8_byte_t *stored_original_str_curr_p = original_str_curr_p;
while (match_len < search_len &&
lit_utf8_iterator_read_next (&original_it) == lit_utf8_iterator_read_next (&search_it))
lit_utf8_read_next (&original_str_curr_p) == lit_utf8_read_next (&search_str_curr_p))
{
match_len++;
}
@@ -409,9 +408,9 @@ ecma_builtin_string_prototype_object_index_of (ecma_value_t this_arg, /**< this
else
{
/* reset iterators */
lit_utf8_iterator_seek_bos (&search_it);
lit_utf8_iterator_seek (&original_it, stored_original_pos);
lit_utf8_iterator_incr (&original_it);
search_str_curr_p = search_str_utf8_p;
original_str_curr_p = stored_original_str_curr_p;
lit_utf8_incr (&original_str_curr_p);
}
index++;
}
@@ -765,7 +764,7 @@ typedef struct
/* Replace value string part. */
ecma_string_t *replace_string_p; /**< replace string */
lit_utf8_iterator_t replace_iterator; /**< replace string iterator */
lit_utf8_byte_t *replace_str_curr_p; /**< replace string iterator */
} ecma_builtin_replace_search_ctx_t;
/**
@@ -907,42 +906,44 @@ ecma_builtin_string_prototype_object_replace_match (ecma_builtin_replace_search_
(ssize_t) (input_size));
JERRY_ASSERT (sz >= 0);
lit_utf8_iterator_t search_iterator = lit_utf8_iterator_create (search_start_p, search_size);
lit_utf8_iterator_t input_iterator = lit_utf8_iterator_create (input_start_p, input_size);
lit_utf8_byte_t *search_str_curr_p = search_start_p;
lit_utf8_byte_t *input_str_curr_p = input_start_p;
ecma_length_t match_start = 0;
ecma_length_t match_end = 0;
bool match_found = false;
if (lit_utf8_iterator_is_eos (&search_iterator))
if (!search_size)
{
/* Empty string, always matches. */
match_found = true;
}
else
{
ecma_char_t first_char = lit_utf8_iterator_read_next (&search_iterator);
const lit_utf8_byte_t *input_str_end_p = input_start_p + input_size;
const lit_utf8_byte_t *search_str_end_p = search_start_p + search_size;
ecma_char_t first_char = lit_utf8_read_next (&search_str_curr_p);
while (!lit_utf8_iterator_is_eos (&input_iterator))
while (input_str_curr_p < input_str_end_p)
{
if (lit_utf8_iterator_read_next (&input_iterator) == first_char)
if (lit_utf8_read_next (&input_str_curr_p) == first_char)
{
/* Local copy to preserve the original value of the iterators. */
lit_utf8_iterator_t nested_search_iterator = search_iterator;
lit_utf8_iterator_t nested_input_iterator = input_iterator;
/* Local copy to preserve the original value. */
lit_utf8_byte_t *nested_search_str_curr_p = search_str_curr_p;
lit_utf8_byte_t *nested_input_str_curr_p = input_str_curr_p;
match_end = match_start + 1;
match_found = true;
while (!lit_utf8_iterator_is_eos (&nested_search_iterator))
while (nested_search_str_curr_p < search_str_end_p)
{
if (lit_utf8_iterator_is_eos (&nested_input_iterator))
if (nested_input_str_curr_p >= input_str_end_p)
{
match_found = false;
break;
}
ecma_char_t search_character = lit_utf8_iterator_read_next (&nested_search_iterator);
ecma_char_t input_character = lit_utf8_iterator_read_next (&nested_input_iterator);
ecma_char_t search_character = lit_utf8_read_next (&nested_search_str_curr_p);
ecma_char_t input_character = lit_utf8_read_next (&nested_input_str_curr_p);
if (search_character != input_character)
{
@@ -1095,19 +1096,18 @@ ecma_builtin_string_prototype_object_replace_get_string (ecma_builtin_replace_se
ecma_length_t previous_start = 0;
ecma_length_t current_position = 0;
lit_utf8_iterator_t replace_iterator = context_p->replace_iterator;
lit_utf8_byte_t *replace_str_curr_p = context_p->replace_str_curr_p;
lit_utf8_byte_t *replace_str_end_p = replace_str_curr_p + ecma_string_get_size (context_p->replace_string_p);
JERRY_ASSERT (lit_utf8_iterator_is_bos (&replace_iterator));
while (!lit_utf8_iterator_is_eos (&replace_iterator))
while (replace_str_curr_p < replace_str_end_p)
{
ecma_char_t action = LIT_CHAR_NULL;
if (lit_utf8_iterator_read_next (&replace_iterator) == LIT_CHAR_DOLLAR_SIGN)
if (*replace_str_curr_p++ == LIT_CHAR_DOLLAR_SIGN)
{
if (!lit_utf8_iterator_is_eos (&replace_iterator))
if (replace_str_curr_p < replace_str_end_p)
{
action = lit_utf8_iterator_peek_next (&replace_iterator);
action = *replace_str_curr_p;
if (action == LIT_CHAR_DOLLAR_SIGN)
{
@@ -1125,9 +1125,9 @@ ecma_builtin_string_prototype_object_replace_get_string (ecma_builtin_replace_se
}
else if (index == 0 || match_length > 10)
{
lit_utf8_iterator_incr (&replace_iterator);
replace_str_curr_p++;
ecma_char_t next_character = lit_utf8_iterator_peek_next (&replace_iterator);
ecma_char_t next_character = *replace_str_curr_p;
if (next_character >= LIT_CHAR_0 && next_character <= LIT_CHAR_9)
{
@@ -1138,7 +1138,7 @@ ecma_builtin_string_prototype_object_replace_get_string (ecma_builtin_replace_se
}
}
lit_utf8_iterator_decr (&replace_iterator);
replace_str_curr_p--;
if (index == 0)
{
@@ -1162,7 +1162,7 @@ ecma_builtin_string_prototype_object_replace_get_string (ecma_builtin_replace_se
previous_start,
current_position,
true);
lit_utf8_iterator_incr (&replace_iterator);
replace_str_curr_p++;
if (action == LIT_CHAR_DOLLAR_SIGN)
{
@@ -1198,16 +1198,16 @@ ecma_builtin_string_prototype_object_replace_get_string (ecma_builtin_replace_se
index = (uint32_t) (action - LIT_CHAR_0);
if ((match_length > 10 || index == 0)
&& !lit_utf8_iterator_is_eos (&replace_iterator))
&& replace_str_curr_p < replace_str_end_p)
{
action = lit_utf8_iterator_peek_next (&replace_iterator);
action = *replace_str_curr_p;
if (action >= LIT_CHAR_0 && action <= LIT_CHAR_9)
{
uint32_t full_index = index * 10 + (uint32_t) (action - LIT_CHAR_0);
if (full_index < match_length)
{
index = full_index;
lit_utf8_iterator_incr (&replace_iterator);
replace_str_curr_p++;
current_position++;
}
}
@@ -1419,7 +1419,7 @@ ecma_builtin_string_prototype_object_replace_main (ecma_builtin_replace_search_c
JERRY_ASSERT (sz >= 0);
context_p->replace_string_p = replace_string_p;
context_p->replace_iterator = lit_utf8_iterator_create (replace_start_p, replace_size);
context_p->replace_str_curr_p = replace_start_p;
ret_value = ecma_builtin_string_prototype_object_replace_loop (context_p);
@@ -2300,11 +2300,12 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
*/
lit_utf8_size_t output_length = 0;
lit_utf8_iterator_t input_iterator = lit_utf8_iterator_create (input_start_p, input_size);
lit_utf8_byte_t *input_str_curr_p = input_start_p;
const lit_utf8_byte_t *input_str_end_p = input_start_p + input_size;
while (!lit_utf8_iterator_is_eos (&input_iterator))
while (input_str_curr_p < input_str_end_p)
{
ecma_char_t character = lit_utf8_iterator_read_next (&input_iterator);
ecma_char_t character = lit_utf8_read_next (&input_str_curr_p);
ecma_char_t character_buffer[LIT_MAXIMUM_OTHER_CASE_LENGTH];
lit_utf8_byte_t utf8_byte_buffer[LIT_CESU8_MAX_BYTES_IN_CODE_POINT];
lit_utf8_size_t character_length;
@@ -2339,11 +2340,11 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
lit_utf8_byte_t *output_char_p = output_start_p;
/* Encoding the output. */
lit_utf8_iterator_seek_bos (&input_iterator);
input_str_curr_p = input_start_p;
while (!lit_utf8_iterator_is_eos (&input_iterator))
while (input_str_curr_p < input_str_end_p)
{
ecma_char_t character = lit_utf8_iterator_read_next (&input_iterator);
ecma_char_t character = lit_utf8_read_next (&input_str_curr_p);
ecma_char_t character_buffer[LIT_MAXIMUM_OTHER_CASE_LENGTH];
lit_utf8_size_t character_length;