diff --git a/jerry-core/ecma/base/ecma-helpers-conversion.cpp b/jerry-core/ecma/base/ecma-helpers-conversion.cpp index ce344dd703..34405ca121 100644 --- a/jerry-core/ecma/base/ecma-helpers-conversion.cpp +++ b/jerry-core/ecma/base/ecma-helpers-conversion.cpp @@ -354,15 +354,16 @@ ecma_utf8_string_to_number (const lit_utf8_byte_t *str_p, /**< utf-8 string */ return ECMA_NUMBER_ZERO; } - lit_utf8_iterator_t iter = lit_utf8_iterator_create (str_p, str_size); + lit_utf8_byte_t *str_curr_p = (lit_utf8_byte_t *) str_p; + const lit_utf8_byte_t *str_end_p = str_p + str_size; ecma_char_t code_unit; - while (!lit_utf8_iterator_is_eos (&iter)) + while (str_curr_p < str_end_p) { - code_unit = lit_utf8_iterator_peek_next (&iter); + code_unit = lit_utf8_peek_next (str_curr_p); if (lit_char_is_white_space (code_unit) || lit_char_is_line_terminator (code_unit)) { - lit_utf8_iterator_incr (&iter); + lit_utf8_incr (&str_curr_p); } else { @@ -370,17 +371,15 @@ ecma_utf8_string_to_number (const lit_utf8_byte_t *str_p, /**< utf-8 string */ } } - JERRY_ASSERT (!iter.buf_pos.is_non_bmp_middle); - const lit_utf8_byte_t *begin_p = iter.buf_p + iter.buf_pos.offset; + const lit_utf8_byte_t *begin_p = str_curr_p; + str_curr_p = (lit_utf8_byte_t *) str_end_p; - iter = lit_utf8_iterator_create (iter.buf_p + iter.buf_pos.offset, str_size - iter.buf_pos.offset); - lit_utf8_iterator_seek_eos (&iter); - while (!lit_utf8_iterator_is_bos (&iter)) + while (str_curr_p > str_p) { - code_unit = lit_utf8_iterator_peek_prev (&iter); + code_unit = lit_utf8_peek_prev (str_curr_p); if (lit_char_is_white_space (code_unit) || lit_char_is_line_terminator (code_unit)) { - lit_utf8_iterator_decr (&iter); + lit_utf8_decr (&str_curr_p); } else { @@ -388,8 +387,7 @@ ecma_utf8_string_to_number (const lit_utf8_byte_t *str_p, /**< utf-8 string */ } } - JERRY_ASSERT (!iter.buf_pos.is_non_bmp_middle); - const lit_utf8_byte_t *end_p = iter.buf_p + iter.buf_pos.offset - 1; + const lit_utf8_byte_t *end_p = str_curr_p - 1; if (begin_p > end_p) { diff --git a/jerry-core/ecma/base/ecma-helpers-string.cpp b/jerry-core/ecma/base/ecma-helpers-string.cpp index bbd778a303..053372695c 100644 --- a/jerry-core/ecma/base/ecma-helpers-string.cpp +++ b/jerry-core/ecma/base/ecma-helpers-string.cpp @@ -414,7 +414,7 @@ ecma_new_ecma_string_from_utf8 (const lit_utf8_byte_t *string_p, /**< utf-8 stri lit_utf8_size_t string_size) /**< string size */ { JERRY_ASSERT (string_p != NULL || string_size == 0); - JERRY_ASSERT (lit_is_utf8_string_valid (string_p, string_size)); + JERRY_ASSERT (lit_is_cesu8_string_valid (string_p, string_size)); lit_magic_string_id_t magic_string_id; if (lit_is_utf8_string_magic (string_p, string_size, &magic_string_id)) @@ -444,7 +444,7 @@ ecma_new_ecma_string_from_utf8 (const lit_utf8_byte_t *string_p, /**< utf-8 stri } /* ecma_new_ecma_string_from_utf8 */ /** - * Allocate new ecma-string and fill it with utf-8 character which represents specified code unit + * Allocate new ecma-string and fill it with cesu-8 character which represents specified code unit * * @return pointer to ecma-string descriptor */ @@ -627,14 +627,7 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */ jerry_fatal (ERR_OUT_OF_MEMORY); } - ecma_char_t str1_last_code_unit = ecma_string_get_char_at_pos (string1_p, ecma_string_get_length (string1_p) - 1); - ecma_char_t str2_first_code_unit = ecma_string_get_char_at_pos (string2_p, 0); - - bool is_surrogate_pair_sliced = (lit_is_code_unit_high_surrogate (str1_last_code_unit) - && lit_is_code_unit_low_surrogate (str2_first_code_unit)); - - lit_utf8_size_t buffer_size = str1_size + str2_size - (lit_utf8_size_t) (is_surrogate_pair_sliced ? - LIT_UTF8_CESU8_SURROGATE_SIZE_DIF : 0); + lit_utf8_size_t buffer_size = str1_size + str2_size; lit_utf8_byte_t *str_p = (lit_utf8_byte_t *) mem_heap_alloc_block (buffer_size, MEM_HEAP_ALLOC_SHORT_TERM); @@ -643,23 +636,9 @@ ecma_concat_ecma_strings (ecma_string_t *string1_p, /**< first ecma-string */ bytes_copied1 = ecma_string_to_utf8_string (string1_p, str_p, (ssize_t) str1_size); JERRY_ASSERT (bytes_copied1 > 0); - if (!is_surrogate_pair_sliced) - { - bytes_copied2 = ecma_string_to_utf8_string (string2_p, str_p + str1_size, (ssize_t) str2_size); - JERRY_ASSERT (bytes_copied2 > 0); - } - else - { - bytes_copied2 = ecma_string_to_utf8_string (string2_p, - str_p + str1_size - LIT_UTF8_MAX_BYTES_IN_CODE_UNIT + 1, - (ssize_t) buffer_size - bytes_copied1 - + LIT_UTF8_MAX_BYTES_IN_CODE_UNIT); - JERRY_ASSERT (bytes_copied2 > 0); + bytes_copied2 = ecma_string_to_utf8_string (string2_p, str_p + str1_size, (ssize_t) str2_size); + JERRY_ASSERT (bytes_copied2 > 0); - lit_code_point_t surrogate_code_point = lit_convert_surrogate_pair_to_code_point (str1_last_code_unit, - str2_first_code_unit); - lit_code_point_to_utf8 (surrogate_code_point, str_p + str1_size - LIT_UTF8_MAX_BYTES_IN_CODE_UNIT); - } ecma_string_t *str_concat_p = ecma_new_ecma_string_from_utf8 (str_p, buffer_size); mem_heap_free_block ((void*) str_p); @@ -955,7 +934,7 @@ ecma_string_get_array_index (const ecma_string_t *str_p, /**< ecma-string */ } /* ecma_string_is_array_index */ /** - * Convert ecma-string's contents to a utf-8 string and put it to the buffer. + * Convert ecma-string's contents to a cesu-8 string and put it to the buffer. * * @return number of bytes, actually copied to the buffer - if string's content was copied successfully; * otherwise (in case size of buffer is insufficient) - negative number, which is calculated @@ -1018,7 +997,6 @@ ecma_string_to_utf8_string (const ecma_string_t *string_desc_p, /**< ecma-string break; } - case ECMA_STRING_CONTAINER_MAGIC_STRING: { const lit_magic_string_id_t id = string_desc_p->u.magic_string_id; @@ -1491,7 +1469,7 @@ ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */ ssize_t sz = ecma_string_to_utf8_string (string_p, utf8_str_p, (ssize_t) buffer_size); JERRY_ASSERT (sz > 0); - ch = lit_utf8_string_code_unit_at (utf8_str_p, buffer_size, index);; + ch = lit_utf8_string_code_unit_at (utf8_str_p, buffer_size, index); MEM_FINALIZE_LOCAL_ARRAY (utf8_str_p); @@ -1682,10 +1660,7 @@ ecma_string_substr (const ecma_string_t *string_p, /**< pointer to an ecma strin JERRY_ASSERT (end_pos <= string_length); #endif - const ecma_length_t span = (start_pos > end_pos) ? 0 : end_pos - start_pos; - const lit_utf8_size_t utf8_str_size = LIT_UTF8_MAX_BYTES_IN_CODE_UNIT * span; - - if (utf8_str_size) + if (start_pos < end_pos) { /** * I. Dump original string to plain buffer @@ -1701,20 +1676,22 @@ ecma_string_substr (const ecma_string_t *string_p, /**< pointer to an ecma strin /** * II. Extract substring */ - MEM_DEFINE_LOCAL_ARRAY (utf8_substr_buffer, utf8_str_size, lit_utf8_byte_t); + lit_utf8_byte_t *start_p = utf8_str_p; + end_pos -= start_pos; - lit_utf8_size_t utf8_substr_buffer_offset = 0; - for (ecma_length_t idx = 0; idx < span; idx++) + while (start_pos--) { - ecma_char_t code_unit = lit_utf8_string_code_unit_at (utf8_str_p, buffer_size, start_pos + idx); + start_p += lit_get_unicode_char_size_by_utf8_first_byte (*start_p); + } - JERRY_ASSERT (utf8_str_size >= utf8_substr_buffer_offset + LIT_UTF8_MAX_BYTES_IN_CODE_UNIT); - utf8_substr_buffer_offset += lit_code_unit_to_utf8 (code_unit, utf8_substr_buffer + utf8_substr_buffer_offset); + lit_utf8_byte_t *end_p = start_p; + while (end_pos--) + { + end_p += lit_get_unicode_char_size_by_utf8_first_byte (*end_p); } - ecma_string_p = ecma_new_ecma_string_from_utf8 (utf8_substr_buffer, utf8_substr_buffer_offset); + ecma_string_p = ecma_new_ecma_string_from_utf8 (start_p, (lit_utf8_size_t) (end_p - start_p)); - MEM_FINALIZE_LOCAL_ARRAY (utf8_substr_buffer); MEM_FINALIZE_LOCAL_ARRAY (utf8_str_p); return ecma_string_p; @@ -1746,47 +1723,47 @@ ecma_string_trim (const ecma_string_t *string_p) /**< pointer to an ecma string ssize_t sz = ecma_string_to_utf8_string (string_p, utf8_str_p, (ssize_t) buffer_size); JERRY_ASSERT (sz >= 0); - lit_utf8_iterator_t front = lit_utf8_iterator_create (utf8_str_p, buffer_size); - - lit_utf8_iterator_t back = lit_utf8_iterator_create (utf8_str_p, buffer_size); - lit_utf8_iterator_seek_eos (&back); - - lit_utf8_iterator_pos_t start = lit_utf8_iterator_get_pos (&back); - lit_utf8_iterator_pos_t end = lit_utf8_iterator_get_pos (&front); - - ecma_char_t current; + ecma_char_t ch; + lit_utf8_size_t read_size; + lit_utf8_byte_t *nonws_start_p = utf8_str_p + buffer_size; + lit_utf8_byte_t *current_p = utf8_str_p; /* Trim front. */ - while (!lit_utf8_iterator_is_eos (&front)) + while (current_p < nonws_start_p) { - current = lit_utf8_iterator_read_next (&front); - if (!lit_char_is_white_space (current) - && !lit_char_is_line_terminator (current)) + read_size = lit_read_code_unit_from_utf8 (current_p, &ch); + + if (!lit_char_is_white_space (ch) + && !lit_char_is_line_terminator (ch)) { - lit_utf8_iterator_decr (&front); - start = lit_utf8_iterator_get_pos (&front); + nonws_start_p = current_p; break; } + + current_p += read_size; } + current_p = utf8_str_p + buffer_size; + /* Trim back. */ - while (!lit_utf8_iterator_is_bos (&back)) + while (current_p > utf8_str_p) { - current = lit_utf8_iterator_read_prev (&back); - if (!lit_char_is_white_space (current) - && !lit_char_is_line_terminator (current)) + read_size = lit_read_prev_code_unit_from_utf8 (current_p, &ch); + + if (!lit_char_is_white_space (ch) + && !lit_char_is_line_terminator (ch)) { - lit_utf8_iterator_incr (&back); - end = lit_utf8_iterator_get_pos (&back); break; } + + current_p -= read_size; } /* Construct new string. */ - if (end.offset > start.offset) + if (current_p > nonws_start_p) { - ret_string_p = ecma_new_ecma_string_from_utf8 (utf8_str_p + start.offset, - (lit_utf8_size_t) (end.offset - start.offset)); + ret_string_p = ecma_new_ecma_string_from_utf8 (nonws_start_p, + (lit_utf8_size_t) (current_p - nonws_start_p)); } else { diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-date.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-date.cpp index 8d99beaeb7..0acc7cc7d8 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-date.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-date.cpp @@ -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); diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-function.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-function.cpp index ddb371162a..dcfd583634 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-function.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-function.cpp @@ -97,11 +97,13 @@ ecma_builtin_function_helper_get_arguments (const ecma_value_t *arguments_list_p ssize_t sz = ecma_string_to_utf8_string (str_p, start_p, (ssize_t) str_size); JERRY_ASSERT (sz >= 0); - lit_utf8_iterator_t iter = lit_utf8_iterator_create (start_p, str_size); + lit_utf8_byte_t *current_p = start_p; + const lit_utf8_byte_t *string_end_p = start_p + str_size; - while (!lit_utf8_iterator_is_eos (&iter)) + while (current_p < string_end_p) { - ecma_char_t current_char = lit_utf8_iterator_read_next (&iter); + ecma_char_t current_char; + current_p += lit_read_code_unit_from_utf8 (current_p, ¤t_char); if (current_char == ',') { @@ -197,33 +199,36 @@ ecma_builtin_function_dispatch_construct (const ecma_value_t *arguments_list_p, ssize_t sz = ecma_string_to_utf8_string (arguments_str_p, start_p, (ssize_t) str_size); JERRY_ASSERT (sz >= 0); - lit_utf8_iterator_t iter = lit_utf8_iterator_create (start_p, str_size); - ecma_length_t last_separator = lit_utf8_iterator_get_index (&iter); - ecma_length_t end_position; + lit_utf8_byte_t *current_p = start_p; + lit_utf8_byte_t *last_separator = start_p; + lit_utf8_byte_t *end_position; + const lit_utf8_byte_t *string_end_p = start_p + str_size; ecma_string_t *param_str_p; - while (!lit_utf8_iterator_is_eos (&iter)) + while (current_p < string_end_p) { - ecma_char_t current_char = lit_utf8_iterator_read_next (&iter); + ecma_char_t current_char; + lit_utf8_size_t read_size = lit_read_code_unit_from_utf8 (current_p, ¤t_char); if (current_char == ',') { - lit_utf8_iterator_decr (&iter); - end_position = lit_utf8_iterator_get_index (&iter); + end_position = current_p; - param_str_p = ecma_string_substr (arguments_str_p, last_separator, end_position); + param_str_p = ecma_new_ecma_string_from_utf8 (last_separator, + (lit_utf8_size_t) (end_position - last_separator)); string_params_p[params_count] = ecma_string_trim (param_str_p); ecma_deref_ecma_string (param_str_p); - lit_utf8_iterator_incr (&iter); - last_separator = lit_utf8_iterator_get_index (&iter); - + last_separator = current_p + read_size; params_count++; } + + current_p += read_size; } - end_position = lit_utf8_string_length (start_p, str_size); - param_str_p = ecma_string_substr (arguments_str_p, last_separator, end_position); + end_position = (lit_utf8_byte_t *) string_end_p; + param_str_p = ecma_new_ecma_string_from_utf8 (last_separator, + (lit_utf8_size_t) (end_position - last_separator)); string_params_p[params_count] = ecma_string_trim (param_str_p); ecma_deref_ecma_string (param_str_p); params_count++; diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp index 99a258cdfd..9af6eeffb6 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp @@ -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) - { - lit_utf8_iterator_seek (&iter, end); - } - else - { - lit_utf8_iterator_seek_eos (&iter); - } - while (lit_utf8_iterator_get_pos (&iter).offset > start.offset) + string_curr_p = end_p; + + while (string_curr_p > start_p) { - 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); - } - else - { - lit_utf8_iterator_decr (&iter); + end_p = str_curr_p; } } - 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 @@ -852,6 +821,10 @@ ecma_builtin_global_object_decode_uri_helper (ecma_value_t uri __attr_unused___, output_size++; } } + else if ((decoded_byte & LIT_UTF8_4_BYTE_MASK) == LIT_UTF8_4_BYTE_MARKER) + { + output_size += 3; + } else { output_size++; @@ -861,27 +834,23 @@ ecma_builtin_global_object_decode_uri_helper (ecma_value_t uri __attr_unused___, if (ecma_is_completion_value_empty (ret_value)) { MEM_DEFINE_LOCAL_ARRAY (output_start_p, - output_size * 2, + output_size, lit_utf8_byte_t); input_char_p = input_start_p; lit_utf8_byte_t *output_char_p = output_start_p; - lit_utf8_byte_t *output_type_p = output_start_p + output_size; while (input_char_p < input_end_p) { /* Input decode. */ if (*input_char_p != '%') { - *output_type_p++ = URI_DECODE_ORIGINAL_BYTE; *output_char_p = *input_char_p; output_char_p++; input_char_p++; continue; } - *output_type_p++ = URI_DECODE_DECODED_BYTE; - lit_code_point_t decoded_byte; lit_read_code_point_from_hex (input_char_p + 1, 2, &decoded_byte); @@ -898,68 +867,95 @@ ecma_builtin_global_object_decode_uri_helper (ecma_value_t uri __attr_unused___, } else { - *output_char_p = (lit_utf8_byte_t) decoded_byte; - output_char_p++; + *output_char_p++ = (lit_utf8_byte_t) decoded_byte; } } else { - *output_char_p = (lit_utf8_byte_t) decoded_byte; - output_char_p++; - } - } - - JERRY_ASSERT (output_start_p + output_size == output_char_p); + uint32_t bytes_count; - bool valid_utf8 = lit_is_utf8_string_valid (output_start_p, output_size); - - if (valid_utf8) - { - lit_utf8_iterator_t characters = lit_utf8_iterator_create (output_start_p, output_size); - output_type_p = output_start_p + output_size; - - while (!lit_utf8_iterator_is_eos (&characters)) - { - bool original_byte = output_type_p[characters.buf_pos.offset] == URI_DECODE_ORIGINAL_BYTE; + if ((decoded_byte & LIT_UTF8_2_BYTE_MASK) == LIT_UTF8_2_BYTE_MARKER) + { + bytes_count = 2; + } + else if ((decoded_byte & LIT_UTF8_3_BYTE_MASK) == LIT_UTF8_3_BYTE_MARKER) + { + bytes_count = 3; + } + else if ((decoded_byte & LIT_UTF8_4_BYTE_MASK) == LIT_UTF8_4_BYTE_MARKER) + { + bytes_count = 4; + } + else + { + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_URI)); + break; + } - ecma_char_t character = lit_utf8_iterator_read_next (&characters); + lit_utf8_byte_t octets[LIT_UTF8_MAX_BYTES_IN_CODE_POINT]; + octets[0] = (lit_utf8_byte_t) decoded_byte; + bool is_valid = true; - /* Surrogate fragments are allowed in JS, but not accepted by URI decoding. */ - if (!original_byte) + for (uint32_t i = 1; i < bytes_count; i++) { - if (lit_is_code_unit_high_surrogate (character)) + if (input_char_p >= input_end_p || *input_char_p != '%') { - /* Note: stray high/low surrogate pairs are not allowed in the stream. */ - if (lit_utf8_iterator_is_eos (&characters)) - { - valid_utf8 = false; - break; - } + is_valid = false; + break; + } + else + { + lit_code_point_t cp; + lit_read_code_point_from_hex (input_char_p + 1, 2, &cp); - if (output_type_p[characters.buf_pos.offset] == URI_DECODE_ORIGINAL_BYTE - || !lit_is_code_unit_low_surrogate (lit_utf8_iterator_read_next (&characters))) + if ((cp & LIT_UTF8_EXTRA_BYTE_MASK) != LIT_UTF8_EXTRA_BYTE_MARKER) { - valid_utf8 = false; + is_valid = false; break; } + + octets[i] = (lit_utf8_byte_t) cp; + input_char_p += URI_ENCODED_BYTE_SIZE; } - else if (lit_is_code_unit_low_surrogate (character)) - { - valid_utf8 = false; - break; - } } + + if (!is_valid) + { + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_URI)); + break; + } + + lit_code_point_t cp; + lit_read_code_point_from_utf8 (octets, bytes_count, &cp); + + if ((bytes_count == 2 && cp <= LIT_UTF8_1_BYTE_CODE_POINT_MAX) + || (bytes_count == 3 && cp <= LIT_UTF8_2_BYTE_CODE_POINT_MAX) + || (bytes_count == 4 && cp <= LIT_UTF8_3_BYTE_CODE_POINT_MAX) + || lit_is_code_unit_high_surrogate ((ecma_char_t) cp) + || lit_is_code_unit_low_surrogate ((ecma_char_t) cp) + || cp > LIT_UNICODE_CODE_POINT_MAX) + { + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_URI)); + break; + } + + output_char_p += lit_code_point_to_cesu8 (cp, output_char_p); } } - if (valid_utf8) + if (ecma_is_completion_value_empty (ret_value)) { - ecma_string_t *output_string_p = ecma_new_ecma_string_from_utf8 (output_start_p, output_size); - ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_string_p)); - } - else - { - ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_URI)); + JERRY_ASSERT (output_start_p + output_size == output_char_p); + + if (lit_is_cesu8_string_valid (output_start_p, output_size)) + { + ecma_string_t *output_string_p = ecma_new_ecma_string_from_utf8 (output_start_p, output_size); + ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_string_p)); + } + else + { + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_URI)); + } } MEM_FINALIZE_LOCAL_ARRAY (output_start_p); @@ -1056,50 +1052,65 @@ ecma_builtin_global_object_encode_uri_helper (ecma_value_t uri, /**< uri argumen */ lit_utf8_byte_t *input_char_p = input_start_p; - lit_utf8_byte_t *input_end_p = input_start_p + input_size; + const lit_utf8_byte_t *input_end_p = input_start_p + input_size; lit_utf8_size_t output_length = 0; + lit_code_point_t cp; + ecma_char_t ch; + lit_utf8_byte_t octets[LIT_UTF8_MAX_BYTES_IN_CODE_POINT]; while (input_char_p < input_end_p) { - /* - * We expect that the input is a valid UTF-8 sequence, - * so we only need to reject stray surrogate pairs. - */ + /* Input validation, we need to reject stray surrogates. */ + input_char_p += lit_read_code_unit_from_utf8 (input_char_p, &ch); + + if (lit_is_code_unit_low_surrogate (ch)) + { + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_URI)); + break; + } - /* Input validation. */ - if (*input_char_p <= LIT_UTF8_1_BYTE_CODE_POINT_MAX) + cp = ch; + + if (lit_is_code_unit_high_surrogate (ch)) { - if (ecma_builtin_global_object_character_is_in (*input_char_p, unescaped_uri_bitset_p)) + if (input_char_p == input_end_p) { - output_length++; + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_URI)); + break; + } + + ecma_char_t next_ch; + lit_utf8_size_t read_size = lit_read_code_unit_from_utf8 (input_char_p, &next_ch); + + if (lit_is_code_unit_low_surrogate (next_ch)) + { + cp = lit_convert_surrogate_pair_to_code_point (ch, next_ch); + input_char_p += read_size; } else { - output_length += URI_ENCODED_BYTE_SIZE; + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_URI)); + break; } } - else if (*input_char_p == (LIT_UTF8_3_BYTE_MARKER + (LIT_UTF16_HIGH_SURROGATE_MARKER >> 12))) - { - /* The next character is in the [0xd000, 0xdfff] range. */ - output_length += URI_ENCODED_BYTE_SIZE; - input_char_p++; - JERRY_ASSERT (input_char_p < input_end_p); - JERRY_ASSERT ((*input_char_p & LIT_UTF8_EXTRA_BYTE_MASK) == LIT_UTF8_EXTRA_BYTE_MARKER); - /* If this condition is true, the next character is >= LIT_UTF16_HIGH_SURROGATE_MIN. */ - if (*input_char_p & 0x20) + lit_utf8_size_t utf_size = lit_code_point_to_utf8 (cp, octets); + + if (utf_size == 1) + { + if (ecma_builtin_global_object_character_is_in (octets[0], unescaped_uri_bitset_p)) { - ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_URI)); - break; + output_length++; + } + else + { + output_length += URI_ENCODED_BYTE_SIZE; } - output_length += URI_ENCODED_BYTE_SIZE; } else { - output_length += URI_ENCODED_BYTE_SIZE; + output_length += utf_size * URI_ENCODED_BYTE_SIZE; } - - input_char_p++; } if (ecma_is_completion_value_empty (ret_value)) @@ -1114,26 +1125,43 @@ ecma_builtin_global_object_encode_uri_helper (ecma_value_t uri, /**< uri argumen while (input_char_p < input_end_p) { /* Input decode. */ + input_char_p += lit_read_code_unit_from_utf8 (input_char_p, &ch); + cp = ch; + + if (lit_is_code_unit_high_surrogate (ch)) + { + ecma_char_t next_ch; + lit_utf8_size_t read_size = lit_read_code_unit_from_utf8 (input_char_p, &next_ch); + + if (lit_is_code_unit_low_surrogate (next_ch)) + { + cp = lit_convert_surrogate_pair_to_code_point (ch, next_ch); + input_char_p += read_size; + } + } - if (*input_char_p <= LIT_UTF8_1_BYTE_CODE_POINT_MAX) + lit_utf8_size_t utf_size = lit_code_point_to_utf8 (cp, octets); + + if (utf_size == 1) { - if (ecma_builtin_global_object_character_is_in (*input_char_p, unescaped_uri_bitset_p)) + if (ecma_builtin_global_object_character_is_in (octets[0], unescaped_uri_bitset_p)) { - *output_char_p++ = *input_char_p; + *output_char_p++ = octets[0]; } else { - ecma_builtin_global_object_byte_to_hex (output_char_p, *input_char_p); + ecma_builtin_global_object_byte_to_hex (output_char_p, octets[0]); output_char_p += URI_ENCODED_BYTE_SIZE; } } else { - ecma_builtin_global_object_byte_to_hex (output_char_p, *input_char_p); - output_char_p += URI_ENCODED_BYTE_SIZE; + for (uint32_t i = 0; i < utf_size; i++) + { + ecma_builtin_global_object_byte_to_hex (output_char_p, octets[i]); + output_char_p += URI_ENCODED_BYTE_SIZE; + } } - - input_char_p++; } JERRY_ASSERT (output_start_p + output_length == output_char_p); @@ -1242,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) { @@ -1276,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) { diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.cpp index a1d7f15c41..766b789ed7 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.cpp @@ -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--; } } diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-json.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-json.cpp index 3ef3895456..dbf5b77954 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-json.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-json.cpp @@ -186,7 +186,7 @@ ecma_builtin_json_parse_string (ecma_json_token_t *token_p) /**< token argument } current_p += 5; - write_p += lit_code_point_to_utf8 (code_point, write_p); + write_p += lit_code_point_to_cesu8 (code_point, write_p); continue; /* FALLTHRU */ } @@ -199,57 +199,6 @@ ecma_builtin_json_parse_string (ecma_json_token_t *token_p) /**< token argument *write_p++ = *current_p++; } - /* - * Post processing surrogate pairs. - * - * The general issue is, that surrogate fragments can come from - * the original stream and can be constructed by \u sequences - * as well. We need to construct code points from them. - * - * Example: JSON.parse ('"\\ud801\udc00"') === "\ud801\udc00" - * The first \u is parsed by JSON, the second is by the lexer. - * - * The rewrite happens in-place, since the write pointer is always - * precede the read-pointer. We also cannot create an UTF8 iterator, - * because the lit_is_utf8_string_valid assertion may fail. - */ - - lit_utf8_byte_t *read_p = token_p->u.string.start_p; - lit_utf8_byte_t *read_end_p = write_p; - write_p = read_p; - - while (read_p < read_end_p) - { - lit_code_point_t code_point; - read_p += lit_read_code_point_from_utf8 (read_p, - (lit_utf8_size_t) (read_end_p - read_p), - &code_point); - - /* The lit_is_code_unit_high_surrogate expects ecma_char_t argument - so code_points above maximum UTF16 code unit must not be tested. */ - if (read_p < read_end_p - && code_point <= LIT_UTF16_CODE_UNIT_MAX - && lit_is_code_unit_high_surrogate ((ecma_char_t) code_point)) - { - lit_code_point_t next_code_point; - lit_utf8_size_t next_code_point_size = lit_read_code_point_from_utf8 (read_p, - (lit_utf8_size_t) (read_end_p - read_p), - &next_code_point); - - if (next_code_point <= LIT_UTF16_CODE_UNIT_MAX - && lit_is_code_unit_low_surrogate ((ecma_char_t) next_code_point)) - { - code_point = lit_convert_surrogate_pair_to_code_point ((ecma_char_t) code_point, - (ecma_char_t) next_code_point); - read_p += next_code_point_size; - } - } - write_p += lit_code_point_to_utf8 (code_point, write_p); - } - - JERRY_ASSERT (lit_is_utf8_string_valid (token_p->u.string.start_p, - (lit_utf8_size_t) (write_p - token_p->u.string.start_p))); - token_p->u.string.size = (lit_utf8_size_t) (write_p - token_p->u.string.start_p); token_p->current_p = current_p + 1; token_p->type = string_token; @@ -1196,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) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp index 89bc4793ca..e7dba817d1 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp @@ -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; - - JERRY_ASSERT (lit_utf8_iterator_is_bos (&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); - 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,32 +2300,16 @@ 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_UTF8_MAX_BYTES_IN_CODE_POINT]; + lit_utf8_byte_t utf8_byte_buffer[LIT_CESU8_MAX_BYTES_IN_CODE_POINT]; lit_utf8_size_t character_length; - /* - * We need to keep surrogate pairs. Surrogates are never converted, - * regardless they form a valid pair or not. - */ - if (lit_is_code_unit_high_surrogate (character)) - { - ecma_char_t next_character = lit_utf8_iterator_peek_next (&input_iterator); - - if (lit_is_code_unit_low_surrogate (next_character)) - { - lit_code_point_t surrogate_code_point = lit_convert_surrogate_pair_to_code_point (character, next_character); - output_length += lit_code_point_to_utf8 (surrogate_code_point, utf8_byte_buffer); - lit_utf8_iterator_incr (&input_iterator); - continue; - } - } - if (lower_case) { character_length = lit_char_to_lower_case (character, @@ -2356,31 +2340,14 @@ 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; - /* - * We need to keep surrogate pairs. Surrogates are never converted, - * regardless they form a valid pair or not. - */ - if (lit_is_code_unit_high_surrogate (character)) - { - ecma_char_t next_character = lit_utf8_iterator_peek_next (&input_iterator); - - if (lit_is_code_unit_low_surrogate (next_character)) - { - lit_code_point_t surrogate_code_point = lit_convert_surrogate_pair_to_code_point (character, next_character); - output_char_p += lit_code_point_to_utf8 (surrogate_code_point, output_char_p); - lit_utf8_iterator_incr (&input_iterator); - continue; - } - } - if (lower_case) { character_length = lit_char_to_lower_case (character, @@ -2398,7 +2365,7 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, / for (lit_utf8_size_t i = 0; i < character_length; i++) { - output_char_p += lit_code_point_to_utf8 (character_buffer[i], output_char_p); + output_char_p += lit_code_unit_to_utf8 (character_buffer[i], output_char_p); } } @@ -2503,60 +2470,8 @@ ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argu ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val); - /* 3 */ - const lit_utf8_size_t size = ecma_string_get_size (original_string_p); - - /* Workaround: avoid repeated call of ecma_string_get_char_at_pos() because its overhead */ - lit_utf8_byte_t *original_utf8_str_p = (lit_utf8_byte_t *) mem_heap_alloc_block (size + 1, - MEM_HEAP_ALLOC_SHORT_TERM); - ssize_t sz = ecma_string_to_utf8_string (original_string_p, original_utf8_str_p, (ssize_t) size); - JERRY_ASSERT (sz >= 0); - - const ecma_length_t length = lit_utf8_string_length (original_utf8_str_p, size); - - lit_utf8_iterator_t iter = lit_utf8_iterator_create (original_utf8_str_p, size); - - uint32_t prefix = 0, postfix = 0; - uint32_t new_len = 0; - - while (!lit_utf8_iterator_is_eos (&iter)) - { - ecma_char_t current_char = lit_utf8_iterator_read_next (&iter); - - if (lit_char_is_white_space (current_char) - || lit_char_is_line_terminator (current_char)) - { - prefix++; - } - else - { - break; - } - } - - lit_utf8_iterator_seek_eos (&iter); - while (!lit_utf8_iterator_is_bos (&iter)) - { - ecma_char_t current_char = lit_utf8_iterator_read_prev (&iter); - - if (lit_char_is_white_space (current_char) - || lit_char_is_line_terminator (current_char)) - { - postfix++; - } - else - { - break; - } - } - new_len = prefix < length ? length - prefix - postfix : 0; - - ecma_string_t *new_str_p = ecma_string_substr (original_string_p, prefix, prefix + new_len); - - /* 4 */ - ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_str_p)); - - mem_heap_free_block (original_utf8_str_p); + ecma_string_t *trimmed_string_p = ecma_string_trim (original_string_p); + ret_value = ecma_make_normal_completion_value (ecma_make_string_value (trimmed_string_p)); ECMA_FINALIZE (to_string_val); ECMA_FINALIZE (check_coercible_val); diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-string.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-string.cpp index 397a2f16c9..3fe97346a0 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string.cpp @@ -67,15 +67,13 @@ ecma_builtin_string_object_from_char_code (ecma_value_t this_arg __attr_unused__ } else { - lit_utf8_size_t utf8_buf_size = args_number * LIT_UTF8_MAX_BYTES_IN_CODE_UNIT; + lit_utf8_size_t utf8_buf_size = args_number * LIT_CESU8_MAX_BYTES_IN_CODE_UNIT; MEM_DEFINE_LOCAL_ARRAY (utf8_buf_p, utf8_buf_size, lit_utf8_byte_t); lit_utf8_size_t utf8_buf_used = 0; - lit_utf8_size_t last_code_unit_size = 0; - ecma_char_t high_surrogate = 0; for (ecma_length_t arg_index = 0; arg_index < args_number && ecma_is_completion_value_empty (ret_value); @@ -86,37 +84,10 @@ ecma_builtin_string_object_from_char_code (ecma_value_t this_arg __attr_unused__ uint32_t uint32_char_code = ecma_number_to_uint32 (arg_num); ecma_char_t code_unit = (uint16_t) uint32_char_code; - if (high_surrogate && lit_is_code_unit_low_surrogate (code_unit)) - { - JERRY_ASSERT (last_code_unit_size > 0); - JERRY_ASSERT (utf8_buf_used >= last_code_unit_size); - - utf8_buf_used -= last_code_unit_size; - - JERRY_ASSERT (utf8_buf_used <= utf8_buf_size - LIT_UTF8_MAX_BYTES_IN_CODE_POINT); - - lit_code_point_t code_point = lit_convert_surrogate_pair_to_code_point (high_surrogate, code_unit); - - last_code_unit_size = lit_code_point_to_utf8 (code_point, utf8_buf_p + utf8_buf_used); - } - else - { - JERRY_ASSERT (utf8_buf_used <= utf8_buf_size - LIT_UTF8_MAX_BYTES_IN_CODE_UNIT); - last_code_unit_size = lit_code_unit_to_utf8 (code_unit, utf8_buf_p + utf8_buf_used); - } - - utf8_buf_used += last_code_unit_size; + JERRY_ASSERT (utf8_buf_used <= utf8_buf_size - LIT_UTF8_MAX_BYTES_IN_CODE_UNIT); + utf8_buf_used += lit_code_unit_to_utf8 (code_unit, utf8_buf_p + utf8_buf_used); JERRY_ASSERT (utf8_buf_used <= utf8_buf_size); - if (lit_is_code_unit_high_surrogate (code_unit)) - { - high_surrogate = code_unit; - } - else - { - high_surrogate = 0; - } - ECMA_OP_TO_NUMBER_FINALIZE (arg_num); } diff --git a/jerry-core/ecma/operations/ecma-regexp-object.cpp b/jerry-core/ecma/operations/ecma-regexp-object.cpp index 2de687f5dc..2154ea17e1 100644 --- a/jerry-core/ecma/operations/ecma-regexp-object.cpp +++ b/jerry-core/ecma/operations/ecma-regexp-object.cpp @@ -74,12 +74,13 @@ re_parse_regexp_flags (ecma_string_t *flags_str_p, /**< Input string with flags ssize_t sz = ecma_string_to_utf8_string (flags_str_p, flags_start_p, (ssize_t) flags_str_size); JERRY_ASSERT (sz >= 0); - lit_utf8_iterator_t iter = lit_utf8_iterator_create (flags_start_p, flags_str_size); + lit_utf8_byte_t *flags_str_curr_p = flags_start_p; + const lit_utf8_byte_t *flags_str_end_p = flags_start_p + flags_str_size; - while (!lit_utf8_iterator_is_eos (&iter) + while (flags_str_curr_p < flags_str_end_p && ecma_is_completion_value_empty (ret_value)) { - switch (lit_utf8_iterator_read_next (&iter)) + switch (*flags_str_curr_p++) { case 'g': { @@ -335,12 +336,14 @@ re_canonicalize (ecma_char_t ch, /**< character */ static ecma_completion_value_t re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ re_bytecode_t *bc_p, /**< pointer to the current RegExp bytecode */ - lit_utf8_iterator_t iter, /**< input string iterator */ - lit_utf8_iterator_t *out_iter_p) /**< Output: matching substring iterator */ + lit_utf8_byte_t *str_p, /**< input string pointer */ + lit_utf8_byte_t **out_str_p) /**< Output: matching substring iterator */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); re_opcode_t op; + lit_utf8_byte_t *str_curr_p = str_p; + while ((op = re_get_opcode (&bc_p))) { switch (op) @@ -348,20 +351,20 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ case RE_OP_MATCH: { JERRY_DDLOG ("Execute RE_OP_MATCH: match\n"); - *out_iter_p = iter; + *out_str_p = str_curr_p; ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE); return ret_value; /* match */ } case RE_OP_CHAR: { - if (lit_utf8_iterator_is_eos (&iter)) + if (str_curr_p >= re_ctx_p->input_end_p) { return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ } bool is_ignorecase = re_ctx_p->flags & RE_FLAG_IGNORE_CASE; ecma_char_t ch1 = (ecma_char_t) re_get_value (&bc_p); /* Already canonicalized. */ - ecma_char_t ch2 = re_canonicalize (lit_utf8_iterator_read_next (&iter), is_ignorecase); + ecma_char_t ch2 = re_canonicalize (lit_utf8_read_next (&str_curr_p), is_ignorecase); JERRY_DDLOG ("Character matching %d to %d: ", ch1, ch2); if (ch1 != ch2) @@ -376,12 +379,12 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ } case RE_OP_PERIOD: { - if (lit_utf8_iterator_is_eos (&iter)) + if (str_curr_p >= re_ctx_p->input_end_p) { return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ } - ecma_char_t ch = lit_utf8_iterator_read_next (&iter); + ecma_char_t ch = lit_utf8_read_next (&str_curr_p); JERRY_DDLOG ("Period matching '.' to %d: ", (uint32_t) ch); if (lit_char_is_line_terminator (ch)) @@ -397,7 +400,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ { JERRY_DDLOG ("Execute RE_OP_ASSERT_START: "); - if ((iter.buf_p + iter.buf_pos.offset) <= re_ctx_p->input_start_p) + if (str_curr_p <= re_ctx_p->input_start_p) { JERRY_DDLOG ("match\n"); break; @@ -409,7 +412,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ } - if (lit_char_is_line_terminator (lit_utf8_iterator_peek_prev (&iter))) + if (lit_char_is_line_terminator (lit_utf8_peek_prev (str_curr_p))) { JERRY_DDLOG ("match\n"); break; @@ -422,7 +425,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ { JERRY_DDLOG ("Execute RE_OP_ASSERT_END: "); - if ((iter.buf_p + iter.buf_pos.offset) >= re_ctx_p->input_end_p) + if (str_curr_p >= re_ctx_p->input_end_p) { JERRY_DDLOG ("match\n"); break; /* tail merge */ @@ -434,7 +437,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ } - if (lit_char_is_line_terminator (lit_utf8_iterator_peek_next (&iter))) + if (lit_char_is_line_terminator (lit_utf8_peek_next (str_curr_p))) { JERRY_DDLOG ("match\n"); break; /* tail merge */ @@ -448,22 +451,22 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ { bool is_wordchar_left, is_wordchar_right; - if ((iter.buf_p + iter.buf_pos.offset) <= re_ctx_p->input_start_p) + if (str_curr_p <= re_ctx_p->input_start_p) { is_wordchar_left = false; /* not a wordchar */ } else { - is_wordchar_left = lit_char_is_word_char (lit_utf8_iterator_peek_prev (&iter)); + is_wordchar_left = lit_char_is_word_char (lit_utf8_peek_prev (str_curr_p)); } - if ((iter.buf_p + iter.buf_pos.offset) >= re_ctx_p->input_end_p) + if (str_curr_p >= re_ctx_p->input_end_p) { is_wordchar_right = false; /* not a wordchar */ } else { - is_wordchar_right = lit_char_is_word_char (lit_utf8_iterator_peek_next (&iter)); + is_wordchar_right = lit_char_is_word_char (lit_utf8_peek_next (str_curr_p)); } if (op == RE_OP_ASSERT_WORD_BOUNDARY) @@ -494,21 +497,21 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ case RE_OP_LOOKAHEAD_NEG: { ecma_completion_value_t match_value = ecma_make_empty_completion_value (); - lit_utf8_iterator_t sub_iter = lit_utf8_iterator_create (NULL, 0); + lit_utf8_byte_t *sub_str_p = NULL; uint32_t array_size = re_ctx_p->num_of_captures + re_ctx_p->num_of_non_captures; - MEM_DEFINE_LOCAL_ARRAY (saved_bck_p, array_size, lit_utf8_iterator_t); + MEM_DEFINE_LOCAL_ARRAY (saved_bck_p, array_size, lit_utf8_byte_t *); - size_t size = (size_t) (array_size) * sizeof (lit_utf8_iterator_t); + size_t size = (size_t) (array_size) * sizeof (lit_utf8_byte_t *); memcpy (saved_bck_p, re_ctx_p->saved_p, size); do { uint32_t offset = re_get_value (&bc_p); - if (!sub_iter.buf_p) + if (!sub_str_p) { - match_value = re_match_regexp (re_ctx_p, bc_p, iter, &sub_iter); + match_value = re_match_regexp (re_ctx_p, bc_p, str_curr_p, &sub_str_p); if (ecma_is_completion_value_throw (match_value)) { break; @@ -522,11 +525,11 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ { JERRY_DDLOG ("Execute RE_OP_LOOKAHEAD_POS/NEG: "); ecma_free_completion_value (match_value); - if ((op == RE_OP_LOOKAHEAD_POS && sub_iter.buf_p) - || (op == RE_OP_LOOKAHEAD_NEG && !sub_iter.buf_p)) + if ((op == RE_OP_LOOKAHEAD_POS && sub_str_p) + || (op == RE_OP_LOOKAHEAD_NEG && !sub_str_p)) { JERRY_DDLOG ("match\n"); - match_value = re_match_regexp (re_ctx_p, bc_p, iter, &sub_iter); + match_value = re_match_regexp (re_ctx_p, bc_p, str_curr_p, &sub_str_p); } else { @@ -539,7 +542,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ { if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; } else { @@ -559,14 +562,14 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ bool is_match; JERRY_DDLOG ("Execute RE_OP_CHAR_CLASS/RE_OP_INV_CHAR_CLASS, "); - if (lit_utf8_iterator_is_eos (&iter)) + if (str_curr_p >= re_ctx_p->input_end_p) { JERRY_DDLOG ("fail\n"); return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ } bool is_ignorecase = re_ctx_p->flags & RE_FLAG_IGNORE_CASE; - ecma_char_t curr_ch = re_canonicalize (lit_utf8_iterator_read_next (&iter), is_ignorecase); + ecma_char_t curr_ch = re_canonicalize (lit_utf8_read_next (&str_curr_p), is_ignorecase); num_of_ranges = re_get_value (&bc_p); is_match = false; @@ -615,26 +618,26 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ backref_idx *= 2; /* backref n -> saved indices [n*2, n*2+1] */ JERRY_ASSERT (backref_idx >= 2 && backref_idx + 1 < re_ctx_p->num_of_captures); - if (!re_ctx_p->saved_p[backref_idx].buf_p || !re_ctx_p->saved_p[backref_idx + 1].buf_p) + if (!re_ctx_p->saved_p[backref_idx] || !re_ctx_p->saved_p[backref_idx + 1]) { JERRY_DDLOG ("match\n"); break; /* capture is 'undefined', always matches! */ } - lit_utf8_iterator_t sub_iter = re_ctx_p->saved_p[backref_idx]; + lit_utf8_byte_t *sub_str_p = re_ctx_p->saved_p[backref_idx]; - while (sub_iter.buf_pos.offset < re_ctx_p->saved_p[backref_idx + 1].buf_pos.offset) + while (sub_str_p < re_ctx_p->saved_p[backref_idx + 1]) { ecma_char_t ch1, ch2; - if ((iter.buf_p + iter.buf_pos.offset) >= re_ctx_p->input_end_p) + if (str_curr_p >= re_ctx_p->input_end_p) { JERRY_DDLOG ("fail\n"); return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ } - ch1 = lit_utf8_iterator_read_next (&sub_iter); - ch2 = lit_utf8_iterator_read_next (&iter); + ch1 = lit_utf8_read_next (&sub_str_p); + ch2 = lit_utf8_read_next (&str_curr_p); if (ch1 != ch2) { @@ -650,17 +653,17 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ re_bytecode_t *old_bc_p; JERRY_DDLOG ("Execute RE_OP_SAVE_AT_START\n"); - lit_utf8_iterator_t old_start_p = re_ctx_p->saved_p[RE_GLOBAL_START_IDX]; - re_ctx_p->saved_p[RE_GLOBAL_START_IDX] = iter; + lit_utf8_byte_t *old_start_p = re_ctx_p->saved_p[RE_GLOBAL_START_IDX]; + re_ctx_p->saved_p[RE_GLOBAL_START_IDX] = str_curr_p; do { uint32_t offset = re_get_value (&bc_p); - lit_utf8_iterator_t sub_iter = lit_utf8_iterator_create (NULL, 0); - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, iter, &sub_iter); + lit_utf8_byte_t *sub_str_p = NULL; + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, str_curr_p, &sub_str_p); if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; return match_value; /* match */ } else if (ecma_is_completion_value_throw (match_value)) @@ -679,8 +682,8 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ case RE_OP_SAVE_AND_MATCH: { JERRY_DDLOG ("End of pattern is reached: match\n"); - re_ctx_p->saved_p[RE_GLOBAL_END_IDX] = iter; - *out_iter_p = iter; + re_ctx_p->saved_p[RE_GLOBAL_END_IDX] = str_curr_p; + *out_str_p = str_curr_p; return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE); /* match */ } case RE_OP_ALTERNATIVE: @@ -711,8 +714,8 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ * after the group first, if zero iteration is allowed. */ uint32_t start_idx, iter_idx, offset; - lit_utf8_iterator_t old_start = lit_utf8_iterator_create (NULL, 0); - lit_utf8_iterator_t sub_iter = lit_utf8_iterator_create (NULL, 0); + lit_utf8_byte_t *old_start_p = NULL; + lit_utf8_byte_t *sub_str_p = NULL; re_bytecode_t *old_bc_p; old_bc_p = bc_p; /* save the bytecode start position of the group start */ @@ -725,8 +728,8 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ iter_idx = start_idx - 1; start_idx *= 2; - old_start = re_ctx_p->saved_p[start_idx]; - re_ctx_p->saved_p[start_idx] = iter; + old_start_p = re_ctx_p->saved_p[start_idx]; + re_ctx_p->saved_p[start_idx] = str_curr_p; } else { @@ -740,11 +743,11 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ bc_p += offset; /* Try to match after the close paren if zero is allowed */ - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, iter, &sub_iter); + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, str_curr_p, &sub_str_p); if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; return match_value; /* match */ } else if (ecma_is_completion_value_throw (match_value)) @@ -753,7 +756,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ } if (RE_IS_CAPTURE_GROUP (op)) { - re_ctx_p->saved_p[start_idx] = old_start; + re_ctx_p->saved_p[start_idx] = old_start_p; } bc_p = old_bc_p; @@ -765,7 +768,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ case RE_OP_NON_CAPTURE_GREEDY_ZERO_GROUP_START: { uint32_t start_idx, iter_idx, old_iteration_cnt, offset; - lit_utf8_iterator_t sub_iter = lit_utf8_iterator_create (NULL, 0); + lit_utf8_byte_t *sub_str_p = NULL; re_bytecode_t *old_bc_p; re_bytecode_t *end_bc_p = NULL; start_idx = re_get_value (&bc_p); @@ -790,18 +793,18 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ start_idx += re_ctx_p->num_of_captures; } - lit_utf8_iterator_t old_start = re_ctx_p->saved_p[start_idx]; + lit_utf8_byte_t *old_start_p = re_ctx_p->saved_p[start_idx]; old_iteration_cnt = re_ctx_p->num_of_iterations_p[iter_idx]; - re_ctx_p->saved_p[start_idx] = iter; + re_ctx_p->saved_p[start_idx] = str_curr_p; re_ctx_p->num_of_iterations_p[iter_idx] = 0; do { offset = re_get_value (&bc_p); - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, iter, &sub_iter); + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, str_curr_p, &sub_str_p); if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; return match_value; /* match */ } else if (ecma_is_completion_value_throw (match_value)) @@ -820,11 +823,11 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ || op == RE_OP_NON_CAPTURE_GREEDY_ZERO_GROUP_START) { JERRY_ASSERT (end_bc_p); - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, end_bc_p, iter, &sub_iter); + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, end_bc_p, str_curr_p, &sub_str_p); if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; return match_value; /* match */ } else if (ecma_is_completion_value_throw (match_value)) @@ -833,7 +836,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ } } - re_ctx_p->saved_p[start_idx] = old_start; + re_ctx_p->saved_p[start_idx] = old_start_p; return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ } case RE_OP_CAPTURE_NON_GREEDY_GROUP_END: @@ -870,14 +873,14 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ if (re_ctx_p->num_of_iterations_p[iter_idx] >= min && re_ctx_p->num_of_iterations_p[iter_idx] <= max) { - lit_utf8_iterator_t old_end = re_ctx_p->saved_p[end_idx]; - re_ctx_p->saved_p[end_idx] = iter; + lit_utf8_byte_t *old_end_p = re_ctx_p->saved_p[end_idx]; + re_ctx_p->saved_p[end_idx] = str_curr_p; - lit_utf8_iterator_t sub_iter = lit_utf8_iterator_create (NULL, 0); - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, iter, &sub_iter); + lit_utf8_byte_t *sub_str_p = NULL; + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, str_curr_p, &sub_str_p); if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; return match_value; /* match */ } else if (ecma_is_completion_value_throw (match_value)) @@ -885,7 +888,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ return match_value; } - re_ctx_p->saved_p[end_idx] = old_end; + re_ctx_p->saved_p[end_idx] = old_end_p; } re_ctx_p->num_of_iterations_p[iter_idx]--; bc_p = old_bc_p; @@ -897,9 +900,9 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ case RE_OP_NON_CAPTURE_GREEDY_GROUP_END: { uint32_t start_idx, end_idx, iter_idx, min, max, offset; - lit_utf8_iterator_t old_start = lit_utf8_iterator_create (NULL, 0); - lit_utf8_iterator_t old_end = lit_utf8_iterator_create (NULL, 0); - lit_utf8_iterator_t sub_iter = lit_utf8_iterator_create (NULL, 0); + lit_utf8_byte_t *old_start_p = NULL; + lit_utf8_byte_t *old_end_p = NULL; + lit_utf8_byte_t *sub_str_p = NULL; re_bytecode_t *old_bc_p; end_idx = re_get_value (&bc_p); @@ -924,8 +927,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ /* Check the empty iteration if the minimum number of iterations is reached. */ if (re_ctx_p->num_of_iterations_p[iter_idx] >= min - && iter.buf_p == re_ctx_p->saved_p[start_idx].buf_p - && iter.buf_pos.offset == re_ctx_p->saved_p[start_idx].buf_pos.offset) + && str_curr_p== re_ctx_p->saved_p[start_idx]) { return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ } @@ -933,21 +935,21 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ re_ctx_p->num_of_iterations_p[iter_idx]++; old_bc_p = bc_p; /* Save the bytecode end position of the END opcodes for matching after it. */ - old_end = re_ctx_p->saved_p[end_idx]; - re_ctx_p->saved_p[end_idx] = iter; + old_end_p = re_ctx_p->saved_p[end_idx]; + re_ctx_p->saved_p[end_idx] = str_curr_p; if (re_ctx_p->num_of_iterations_p[iter_idx] < max) { bc_p -= offset; offset = re_get_value (&bc_p); - old_start = re_ctx_p->saved_p[start_idx]; - re_ctx_p->saved_p[start_idx] = iter; - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, iter, &sub_iter); + old_start_p = re_ctx_p->saved_p[start_idx]; + re_ctx_p->saved_p[start_idx] = str_curr_p; + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, str_curr_p, &sub_str_p); if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; return match_value; /* match */ } else if (ecma_is_completion_value_throw (match_value)) @@ -955,7 +957,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ return match_value; } - re_ctx_p->saved_p[start_idx] = old_start; + re_ctx_p->saved_p[start_idx] = old_start_p; /* Try to match alternatives if any. */ bc_p += offset; @@ -964,14 +966,14 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ bc_p++; /* RE_OP_ALTERNATIVE */ offset = re_get_value (&bc_p); - old_start = re_ctx_p->saved_p[start_idx]; - re_ctx_p->saved_p[start_idx] = iter; + old_start_p = re_ctx_p->saved_p[start_idx]; + re_ctx_p->saved_p[start_idx] = str_curr_p; - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, iter, &sub_iter); + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, str_curr_p, &sub_str_p); if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; return match_value; /* match */ } else if (ecma_is_completion_value_throw (match_value)) @@ -979,7 +981,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ return match_value; } - re_ctx_p->saved_p[start_idx] = old_start; + re_ctx_p->saved_p[start_idx] = old_start_p; bc_p += offset; } } @@ -988,11 +990,11 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ && re_ctx_p->num_of_iterations_p[iter_idx] <= max) { /* Try to match the rest of the bytecode. */ - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, old_bc_p, iter, &sub_iter); + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, old_bc_p, str_curr_p, &sub_str_p); if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; return match_value; /* match */ } else if (ecma_is_completion_value_throw (match_value)) @@ -1002,14 +1004,14 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ } /* restore if fails */ - re_ctx_p->saved_p[end_idx] = old_end; + re_ctx_p->saved_p[end_idx] = old_end_p; re_ctx_p->num_of_iterations_p[iter_idx]--; return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ } case RE_OP_NON_GREEDY_ITERATOR: { uint32_t min, max, offset, num_of_iter; - lit_utf8_iterator_t sub_iter = lit_utf8_iterator_create (NULL, 0); + lit_utf8_byte_t *sub_str_p = NULL; min = re_get_value (&bc_p); max = re_get_value (&bc_p); @@ -1023,11 +1025,14 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ { if (num_of_iter >= min) { - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p + offset, iter, &sub_iter); + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, + bc_p + offset, + str_curr_p, + &sub_str_p); if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; return match_value; /* match */ } else if (ecma_is_completion_value_throw (match_value)) @@ -1036,7 +1041,10 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ } } - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, iter, &sub_iter); + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, + bc_p, + str_curr_p, + &sub_str_p); if (!ecma_is_value_true (match_value)) { @@ -1048,7 +1056,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ break; } - iter = sub_iter; + str_curr_p = sub_str_p; num_of_iter++; } return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ @@ -1056,7 +1064,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ case RE_OP_GREEDY_ITERATOR: { uint32_t min, max, offset, num_of_iter; - lit_utf8_iterator_t sub_iter = lit_utf8_iterator_create (NULL, 0); + lit_utf8_byte_t *sub_str_p = NULL; min = re_get_value (&bc_p); max = re_get_value (&bc_p); @@ -1069,7 +1077,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ while (num_of_iter < max) { - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, iter, &sub_iter); + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p, str_curr_p, &sub_str_p); if (!ecma_is_value_true (match_value)) { @@ -1081,17 +1089,20 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ break; } - iter = sub_iter; + str_curr_p = sub_str_p; num_of_iter++; } while (num_of_iter >= min) { - ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, bc_p + offset, iter, &sub_iter); + ecma_completion_value_t match_value = re_match_regexp (re_ctx_p, + bc_p + offset, + str_curr_p, + &sub_str_p); if (ecma_is_value_true (match_value)) { - *out_iter_p = sub_iter; + *out_str_p = sub_str_p; return match_value; /* match */ } else if (ecma_is_completion_value_throw (match_value)) @@ -1104,7 +1115,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */ break; } - lit_utf8_iterator_read_prev (&iter); + lit_utf8_read_prev (&str_curr_p); num_of_iter--; } return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */ @@ -1236,16 +1247,22 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */ ecma_string_t *input_string_p = ecma_get_string_from_value (input_string); lit_utf8_size_t input_string_size = ecma_string_get_size (input_string_p); - MEM_DEFINE_LOCAL_ARRAY (input_utf8_buffer_p, input_string_size, lit_utf8_byte_t); + MEM_DEFINE_LOCAL_ARRAY (input_buffer_p, input_string_size, lit_utf8_byte_t); - ssize_t sz = ecma_string_to_utf8_string (input_string_p, input_utf8_buffer_p, (ssize_t) input_string_size); + ssize_t sz = ecma_string_to_utf8_string (input_string_p, input_buffer_p, (ssize_t) input_string_size); JERRY_ASSERT (sz >= 0); - lit_utf8_iterator_t iterator = lit_utf8_iterator_create (input_utf8_buffer_p, input_string_size); + lit_utf8_byte_t *input_curr_p = input_buffer_p; + + if (!input_string_size) + { + input_curr_p = (lit_utf8_byte_t *) lit_get_magic_string_utf8 (LIT_MAGIC_STRING__EMPTY); + } + lit_utf8_byte_t *input_end_p = input_buffer_p + input_string_size; re_matcher_ctx_t re_ctx; - re_ctx.input_start_p = iterator.buf_p; - re_ctx.input_end_p = iterator.buf_p + iterator.buf_size; + re_ctx.input_start_p = input_buffer_p; + re_ctx.input_end_p = input_buffer_p + input_string_size; /* 1. Read bytecode header and init regexp matcher context. */ re_ctx.flags = (uint8_t) re_get_value (&bc_p); @@ -1264,15 +1281,12 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */ JERRY_ASSERT (re_ctx.num_of_captures % 2 == 0); re_ctx.num_of_non_captures = re_get_value (&bc_p); - /* We create an invalid iterator, that will be used to identify unused result values. */ - lit_utf8_iterator_t unused_iter = lit_utf8_iterator_create (NULL, 0); - unused_iter.buf_p = (lit_utf8_byte_t *) 1; - MEM_DEFINE_LOCAL_ARRAY (saved_p, re_ctx.num_of_captures + re_ctx.num_of_non_captures, lit_utf8_iterator_t); + MEM_DEFINE_LOCAL_ARRAY (saved_p, re_ctx.num_of_captures + re_ctx.num_of_non_captures, lit_utf8_byte_t *); for (uint32_t i = 0; i < re_ctx.num_of_captures + re_ctx.num_of_non_captures; i++) { - saved_p[i] = unused_iter; + saved_p[i] = NULL; } re_ctx.saved_p = saved_p; @@ -1287,9 +1301,9 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */ bool is_match = false; re_ctx.num_of_iterations_p = num_of_iter_p; int32_t index = 0; - ecma_length_t input_str_len = lit_utf8_string_length (iterator.buf_p, iterator.buf_size); + ecma_length_t input_str_len = lit_utf8_string_length (input_buffer_p, input_string_size); - if (iterator.buf_p && (re_ctx.flags & RE_FLAG_GLOBAL)) + if (input_buffer_p && (re_ctx.flags & RE_FLAG_GLOBAL)) { ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL); ecma_property_t *lastindex_prop_p = ecma_op_object_get_property (regexp_object_p, magic_str_p); @@ -1297,19 +1311,21 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */ ECMA_OP_TO_NUMBER_TRY_CATCH (lastindex_num, lastindex_prop_p->u.named_data_property.value, ret_value) index = ecma_number_to_int32 (lastindex_num); - JERRY_ASSERT (iterator.buf_pos.offset == 0 && !iterator.buf_pos.is_non_bmp_middle); - if (!lit_utf8_iterator_is_eos (&iterator) + if (input_curr_p < input_end_p && index <= (int32_t) input_str_len && index > 0) { - lit_utf8_iterator_advance (&iterator, (ecma_length_t) index); + for (int i = 0; i < index; i++) + { + lit_utf8_incr (&input_curr_p); + } } ECMA_OP_TO_NUMBER_FINALIZE (lastindex_num); ecma_deref_ecma_string (magic_str_p); } /* 2. Try to match */ - lit_utf8_iterator_t sub_iter = lit_utf8_iterator_create (NULL, 0); + lit_utf8_byte_t *sub_str_p = NULL; while (ecma_is_completion_value_empty (ret_value)) { @@ -1330,17 +1346,16 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */ } else { - ECMA_TRY_CATCH (match_value, re_match_regexp (&re_ctx, bc_p, iterator, &sub_iter), ret_value); - + ECMA_TRY_CATCH (match_value, re_match_regexp (&re_ctx, bc_p, input_curr_p, &sub_str_p), ret_value); if (ecma_is_value_true (match_value)) { is_match = true; break; } - if (!lit_utf8_iterator_is_eos (&iterator)) + if (input_curr_p < input_end_p) { - lit_utf8_iterator_advance (&iterator, 1); + lit_utf8_incr (&input_curr_p); } index++; @@ -1348,11 +1363,21 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */ } } - if (iterator.buf_p && (re_ctx.flags & RE_FLAG_GLOBAL)) + if (input_curr_p && (re_ctx.flags & RE_FLAG_GLOBAL)) { ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL); ecma_number_t *lastindex_num_p = ecma_alloc_number (); - *lastindex_num_p = sub_iter.buf_pos.offset; + + if (sub_str_p) + { + *lastindex_num_p = lit_utf8_string_length (input_buffer_p, + (lit_utf8_size_t) (sub_str_p - input_buffer_p)); + } + else + { + *lastindex_num_p = ECMA_NUMBER_ZERO; + } + ecma_op_object_put (regexp_object_p, magic_str_p, ecma_make_number_value (lastindex_num_p), true); ecma_dealloc_number (lastindex_num_p); ecma_deref_ecma_string (magic_str_p); @@ -1366,7 +1391,7 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */ ecma_completion_value_t result_array = ecma_op_create_array_object (0, 0, false); ecma_object_t *result_array_obj_p = ecma_get_object_from_completion_value (result_array); - ecma_string_t *input_str_p = ecma_new_ecma_string_from_utf8 (iterator.buf_p, iterator.buf_size); + ecma_string_t *input_str_p = ecma_new_ecma_string_from_utf8 (input_buffer_p, input_string_size); re_set_result_array_properties (result_array_obj_p, input_str_p, re_ctx.num_of_captures / 2, index); ecma_deref_ecma_string (input_str_p); @@ -1374,18 +1399,16 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */ { ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (i / 2); - /* Note: 'iter_p->buf_p == NULL' means the input is empty string */ - if ((re_ctx.saved_p[i].buf_p != unused_iter.buf_p && re_ctx.saved_p[i + 1].buf_p != unused_iter.buf_p) - && re_ctx.saved_p[i + 1].buf_pos.offset >= re_ctx.saved_p[i].buf_pos.offset) + if (((re_ctx.saved_p[i] && re_ctx.saved_p[i + 1]) + && re_ctx.saved_p[i + 1] >= re_ctx.saved_p[i])) { ecma_length_t capture_str_len; - capture_str_len = (ecma_length_t) re_ctx.saved_p[i + 1].buf_pos.offset - re_ctx.saved_p[i].buf_pos.offset; + capture_str_len = (ecma_length_t) (re_ctx.saved_p[i + 1] - re_ctx.saved_p[i]); ecma_string_t *capture_str_p; if (capture_str_len > 0) { - const lit_utf8_byte_t *utf8_str_p = re_ctx.saved_p[i].buf_p + re_ctx.saved_p[i].buf_pos.offset; - capture_str_p = ecma_new_ecma_string_from_utf8 (utf8_str_p, capture_str_len); + capture_str_p = ecma_new_ecma_string_from_utf8 (re_ctx.saved_p[i], capture_str_len); } else { @@ -1413,7 +1436,7 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */ MEM_FINALIZE_LOCAL_ARRAY (num_of_iter_p); MEM_FINALIZE_LOCAL_ARRAY (saved_p); - MEM_FINALIZE_LOCAL_ARRAY (input_utf8_buffer_p); + MEM_FINALIZE_LOCAL_ARRAY (input_buffer_p); return ret_value; } /* ecma_regexp_exec_helper */ diff --git a/jerry-core/ecma/operations/ecma-regexp-object.h b/jerry-core/ecma/operations/ecma-regexp-object.h index 00fa99b681..ce315f535a 100644 --- a/jerry-core/ecma/operations/ecma-regexp-object.h +++ b/jerry-core/ecma/operations/ecma-regexp-object.h @@ -41,7 +41,7 @@ */ typedef struct { - lit_utf8_iterator_t *saved_p; /**< saved result string pointers, ECMA 262 v5, 15.10.2.1, State */ + lit_utf8_byte_t **saved_p; /**< saved result string pointers, ECMA 262 v5, 15.10.2.1, State */ const lit_utf8_byte_t *input_start_p; /**< start of input pattern string */ const lit_utf8_byte_t *input_end_p; /**< end of input pattern string */ uint32_t num_of_captures; /**< number of capture groups */ diff --git a/jerry-core/lit/lit-char-helpers.cpp b/jerry-core/lit/lit-char-helpers.cpp index 0019fa1178..103d7ab12b 100644 --- a/jerry-core/lit/lit-char-helpers.cpp +++ b/jerry-core/lit/lit-char-helpers.cpp @@ -326,7 +326,7 @@ lit_char_hex_to_int (ecma_char_t c) /**< code unit, corresponding to * @return true if decoding was successful, false otherwise */ bool -lit_read_code_point_from_hex (const lit_utf8_byte_t *buf_p, /**< buffer with characters */ +lit_read_code_point_from_hex (lit_utf8_byte_t *buf_p, /**< buffer with characters */ lit_utf8_size_t number_of_characters, /**< number of characters to be read */ lit_code_point_t *out_code_point_p) /**< @out: decoded result */ { @@ -360,6 +360,7 @@ lit_read_code_point_from_hex (const lit_utf8_byte_t *buf_p, /**< buffer with cha buf_p++; } + *out_code_point_p = code_point; return true; } /* lit_read_code_point_from_hex */ diff --git a/jerry-core/lit/lit-char-helpers.h b/jerry-core/lit/lit-char-helpers.h index 3e342d8c35..63e4602810 100644 --- a/jerry-core/lit/lit-char-helpers.h +++ b/jerry-core/lit/lit-char-helpers.h @@ -216,7 +216,7 @@ extern bool lit_char_is_hex_digit (ecma_char_t); extern uint32_t lit_char_hex_to_int (ecma_char_t); /* read a hex encoded code point from a zero terminated buffer */ -bool lit_read_code_point_from_hex (const lit_utf8_byte_t *, lit_utf8_size_t, lit_code_point_t *); +bool lit_read_code_point_from_hex (lit_utf8_byte_t *, lit_utf8_size_t, lit_code_point_t *); /** * Null character diff --git a/jerry-core/lit/lit-globals.h b/jerry-core/lit/lit-globals.h index e35c56e078..d040234c4f 100644 --- a/jerry-core/lit/lit-globals.h +++ b/jerry-core/lit/lit-globals.h @@ -93,6 +93,16 @@ typedef ecma_char_t *ecma_char_ptr_t; */ #define LIT_UTF8_MAX_BYTES_IN_CODE_POINT (4) +/** + * Max bytes needed to represent a code unit (utf-16 char) via cesu-8 encoding + */ +#define LIT_CESU8_MAX_BYTES_IN_CODE_UNIT (3) + +/** + * Max bytes needed to represent a code point (Unicode character) via cesu-8 encoding + */ +#define LIT_CESU8_MAX_BYTES_IN_CODE_POINT (6) + /** * A byte of utf-8 string */ diff --git a/jerry-core/lit/lit-literal.cpp b/jerry-core/lit/lit-literal.cpp index 9684c850f4..eb0c1e9d92 100644 --- a/jerry-core/lit/lit-literal.cpp +++ b/jerry-core/lit/lit-literal.cpp @@ -57,7 +57,7 @@ lit_dump_literals () */ literal_t lit_create_literal_from_utf8_string (const lit_utf8_byte_t *str_p, /**< string to initialize the record, - * could be non-zero-terminated */ + * could be non-zero-terminated */ lit_utf8_size_t str_size) /**< length of the string */ { JERRY_ASSERT (str_p || !str_size); diff --git a/jerry-core/lit/lit-magic-strings.cpp b/jerry-core/lit/lit-magic-strings.cpp index 54d970c4c2..79db9f4d95 100644 --- a/jerry-core/lit/lit-magic-strings.cpp +++ b/jerry-core/lit/lit-magic-strings.cpp @@ -182,7 +182,7 @@ lit_magic_strings_ex_set (const lit_utf8_byte_t **ex_str_items, /**< character a /** - * Check if passed utf-8 string equals to one of magic strings + * Check if passed cesu-8 string equals to one of magic strings * and if equal magic string was found, return it's id in 'out_id_p' argument. * * @return true - if magic string equal to passed string was found, diff --git a/jerry-core/lit/lit-strings.cpp b/jerry-core/lit/lit-strings.cpp index 593c237a6c..24c599bcf9 100644 --- a/jerry-core/lit/lit-strings.cpp +++ b/jerry-core/lit/lit-strings.cpp @@ -152,6 +152,76 @@ lit_is_utf8_string_valid (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 string * return true; } /* lit_is_utf8_string_valid */ +/** + * Validate cesu-8 string + * + * @return true if cesu-8 string is well-formed + * false otherwise + */ +bool +lit_is_cesu8_string_valid (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 string */ + lit_utf8_size_t buf_size) /**< string size */ +{ + lit_utf8_size_t idx = 0; + + while (idx < buf_size) + { + lit_utf8_byte_t c = utf8_buf_p[idx++]; + if ((c & LIT_UTF8_1_BYTE_MASK) == LIT_UTF8_1_BYTE_MARKER) + { + continue; + } + + lit_code_point_t code_point = 0; + lit_code_point_t min_code_point = 0; + lit_utf8_size_t extra_bytes_count; + if ((c & LIT_UTF8_2_BYTE_MASK) == LIT_UTF8_2_BYTE_MARKER) + { + extra_bytes_count = 1; + min_code_point = LIT_UTF8_2_BYTE_CODE_POINT_MIN; + code_point = ((uint32_t) (c & LIT_UTF8_LAST_5_BITS_MASK)); + } + else if ((c & LIT_UTF8_3_BYTE_MASK) == LIT_UTF8_3_BYTE_MARKER) + { + extra_bytes_count = 2; + min_code_point = LIT_UTF8_3_BYTE_CODE_POINT_MIN; + code_point = ((uint32_t) (c & LIT_UTF8_LAST_4_BITS_MASK)); + } + else + { + return false; + } + + if (idx + extra_bytes_count > buf_size) + { + /* cesu-8 string breaks in the middle */ + return false; + } + + for (lit_utf8_size_t offset = 0; offset < extra_bytes_count; ++offset) + { + c = utf8_buf_p[idx + offset]; + if ((c & LIT_UTF8_EXTRA_BYTE_MASK) != LIT_UTF8_EXTRA_BYTE_MARKER) + { + /* invalid continuation byte */ + return false; + } + code_point <<= LIT_UTF8_BITS_IN_EXTRA_BYTES; + code_point |= (c & LIT_UTF8_LAST_6_BITS_MASK); + } + + if (code_point < min_code_point) + { + /* cesu-8 string doesn't encode valid unicode code point */ + return false; + } + + idx += extra_bytes_count; + } + + return true; +} /* lit_is_cesu8_string_valid */ + /** * Check if the code unit type is low surrogate * @@ -206,16 +276,6 @@ lit_utf8_iterator_seek_bos (lit_utf8_iterator_t *iter_p) /**< iterator to reset iter_p->buf_pos.is_non_bmp_middle = false; } /* lit_utf8_iterator_seek_bos */ -/** - * Reset iterator to point to the end of a string - */ -void -lit_utf8_iterator_seek_eos (lit_utf8_iterator_t *iter_p) /**< iterator to reset */ -{ - iter_p->buf_pos.offset = iter_p->buf_size & LIT_ITERATOR_OFFSET_MASK; - iter_p->buf_pos.is_non_bmp_middle = false; -} /* lit_utf8_iterator_seek_eos */ - /** * Save iterator's position to restore it later * @@ -244,17 +304,6 @@ lit_utf8_iterator_seek (lit_utf8_iterator_t *iter_p, /**< utf-8 string iterator iter_p->buf_pos = iter_pos; } /* lit_utf8_iterator_seek */ -/** - * Get offset (in code units) of the iterator - * - * @return current offset of the iterator in code units - */ -ecma_length_t -lit_utf8_iterator_get_index (const lit_utf8_iterator_t *iter_p) -{ - return lit_utf8_string_length (iter_p->buf_p, iter_p->buf_pos.offset) + iter_p->buf_pos.is_non_bmp_middle; -} /* lit_utf8_iterator_get_index */ - /** * Represents code point (>0xFFFF) as surrogate pair and returns its lower part * @@ -286,7 +335,7 @@ convert_code_point_to_high_surrogate (lit_code_point_t code_point) /**< code poi code_unit_bits = (ecma_char_t) ((code_point - LIT_UTF16_FIRST_SURROGATE_CODE_POINT) >> LIT_UTF16_BITS_IN_SURROGATE); return (LIT_UTF16_HIGH_SURROGATE_MARKER | code_unit_bits); -} /* convert_code_point_to_low_surrogate */ +} /* convert_code_point_to_high_surrogate */ /** * Get next code unit form the iterated string @@ -321,50 +370,6 @@ lit_utf8_iterator_peek_next (const lit_utf8_iterator_t *iter_p) /**< @in: utf-8 } } /* lit_utf8_iterator_peek_next */ -/** - * Get previous code unit form the iterated string - * - * @return previous code unit - */ -ecma_char_t -lit_utf8_iterator_peek_prev (const lit_utf8_iterator_t *iter_p) /**< @in: utf-8 string iterator */ -{ - JERRY_ASSERT (!lit_utf8_iterator_is_bos (iter_p)); - - lit_code_point_t code_point; - lit_utf8_size_t offset = iter_p->buf_pos.offset; - - if (iter_p->buf_pos.is_non_bmp_middle) - { - lit_read_code_point_from_utf8 (iter_p->buf_p + iter_p->buf_pos.offset, - iter_p->buf_size - iter_p->buf_pos.offset, - &code_point); - return convert_code_point_to_high_surrogate (code_point); - } - - do - { - JERRY_ASSERT (offset != 0); - offset--; - } - while ((iter_p->buf_p[offset] & LIT_UTF8_EXTRA_BYTE_MASK) == LIT_UTF8_EXTRA_BYTE_MARKER); - - JERRY_ASSERT (iter_p->buf_pos.offset - offset <= LIT_UTF8_MAX_BYTES_IN_CODE_POINT); - - lit_read_code_point_from_utf8 (iter_p->buf_p + offset, - iter_p->buf_size - offset, - &code_point); - - if (code_point <= LIT_UTF16_CODE_UNIT_MAX) - { - return (ecma_char_t) code_point; - } - else - { - return convert_code_point_to_low_surrogate (code_point); - } -} /* lit_utf8_iterator_peek_prev */ - /** * Increment iterator to point to next code unit */ @@ -372,16 +377,7 @@ void lit_utf8_iterator_incr (lit_utf8_iterator_t *iter_p) /**< @in-out: utf-8 string iterator */ { lit_utf8_iterator_read_next (iter_p); -} /* lit_utf8_iterator_read_next */ - -/** - * Decrement iterator to point to previous code unit - */ -void -lit_utf8_iterator_decr (lit_utf8_iterator_t *iter_p) /**< @in-out: utf-8 string iterator */ -{ - lit_utf8_iterator_read_prev (iter_p); -} /* lit_utf8_iterator_decr */ +} /* lit_utf8_iterator_incr */ /** * Skip specified number of code units @@ -433,56 +429,6 @@ lit_utf8_iterator_read_next (lit_utf8_iterator_t *iter_p) /**< @in-out: utf-8 st } } /* lit_utf8_iterator_read_next */ -/** - * Get previous code unit form the iterated string and decrement iterator to point to previous code unit - * - * @return previous code unit - */ -ecma_char_t -lit_utf8_iterator_read_prev (lit_utf8_iterator_t *iter_p) /**< @in-out: utf-8 string iterator */ -{ - JERRY_ASSERT (!lit_utf8_iterator_is_bos (iter_p)); - - lit_code_point_t code_point; - lit_utf8_size_t offset = iter_p->buf_pos.offset; - - if (iter_p->buf_pos.is_non_bmp_middle) - { - lit_read_code_point_from_utf8 (iter_p->buf_p + iter_p->buf_pos.offset, - iter_p->buf_size - iter_p->buf_pos.offset, - &code_point); - - iter_p->buf_pos.is_non_bmp_middle = false; - - return convert_code_point_to_high_surrogate (code_point); - } - - do - { - JERRY_ASSERT (offset != 0); - offset--; - } - while ((iter_p->buf_p[offset] & LIT_UTF8_EXTRA_BYTE_MASK) == LIT_UTF8_EXTRA_BYTE_MARKER); - - JERRY_ASSERT (iter_p->buf_pos.offset - offset <= LIT_UTF8_MAX_BYTES_IN_CODE_POINT); - - iter_p->buf_pos.offset = (offset) & LIT_ITERATOR_OFFSET_MASK; - lit_read_code_point_from_utf8 (iter_p->buf_p + iter_p->buf_pos.offset, - iter_p->buf_size - iter_p->buf_pos.offset, - &code_point); - - if (code_point <= LIT_UTF16_CODE_UNIT_MAX) - { - return (ecma_char_t) code_point; - } - else - { - iter_p->buf_pos.is_non_bmp_middle = true; - - return convert_code_point_to_low_surrogate (code_point); - } -} /* lit_utf8_iterator_read_prev */ - /** * Checks iterator reached end of the string * @@ -497,18 +443,6 @@ lit_utf8_iterator_is_eos (const lit_utf8_iterator_t *iter_p) /**< utf-8 string i return (iter_p->buf_pos.offset == iter_p->buf_size); } /* lit_utf8_iterator_is_eos */ -/** - * Checks iterator reached beginning of the string - * - * @return true - iterator is at the beginning of a string - * false - otherwise - */ -bool -lit_utf8_iterator_is_bos (const lit_utf8_iterator_t *iter_p) -{ - return (iter_p->buf_pos.offset == 0 && iter_p->buf_pos.is_non_bmp_middle == false); -} /* lit_utf8_iterator_is_bos */ - /** * Calculate size of a zero-terminated utf-8 string * @@ -524,7 +458,7 @@ lit_zt_utf8_string_size (const lit_utf8_byte_t *utf8_str_p) /**< zero-terminated } /* lit_zt_utf8_string_size */ /** - * Calculate length of a utf-8 string + * Calculate length of a cesu-8 encoded string * * @return UTF-16 code units count */ @@ -533,13 +467,15 @@ lit_utf8_string_length (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 string */ lit_utf8_size_t utf8_buf_size) /**< string size */ { ecma_length_t length = 0; - lit_utf8_iterator_t buf_iter = lit_utf8_iterator_create (utf8_buf_p, utf8_buf_size); - while (!lit_utf8_iterator_is_eos (&buf_iter)) + lit_utf8_size_t size = 0; + + while (size < utf8_buf_size) { - lit_utf8_iterator_read_next (&buf_iter); + size += lit_get_unicode_char_size_by_utf8_first_byte (*(utf8_buf_p + size)); length++; } - JERRY_ASSERT (lit_utf8_iterator_is_eos (&buf_iter)); + + JERRY_ASSERT (size == utf8_buf_size); return length; } /* lit_utf8_string_length */ @@ -597,6 +533,158 @@ lit_read_code_point_from_utf8 (const lit_utf8_byte_t *buf_p, /**< buffer with ch return bytes_count; } /* lit_read_code_point_from_utf8 */ +/** + * Decodes a unicode code unit from non-empty cesu-8-encoded buffer + * + * @return number of bytes occupied by code point in the string + */ +lit_utf8_size_t +lit_read_code_unit_from_utf8 (const lit_utf8_byte_t *buf_p, /**< buffer with characters */ + ecma_char_t *code_point) /**< @out: code point */ +{ + JERRY_ASSERT (buf_p); + + lit_utf8_byte_t c = buf_p[0]; + if ((c & LIT_UTF8_1_BYTE_MASK) == LIT_UTF8_1_BYTE_MARKER) + { + *code_point = (lit_code_point_t) (c & LIT_UTF8_LAST_7_BITS_MASK); + return 1; + } + + lit_code_point_t ret = LIT_UNICODE_CODE_POINT_NULL; + ecma_length_t bytes_count; + if ((c & LIT_UTF8_2_BYTE_MASK) == LIT_UTF8_2_BYTE_MARKER) + { + bytes_count = 2; + ret = ((lit_code_point_t) (c & LIT_UTF8_LAST_5_BITS_MASK)); + } + else + { + JERRY_ASSERT ((c & LIT_UTF8_3_BYTE_MASK) == LIT_UTF8_3_BYTE_MARKER); + bytes_count = 3; + ret = ((lit_code_point_t) (c & LIT_UTF8_LAST_4_BITS_MASK)); + } + + for (uint32_t i = 1; i < bytes_count; ++i) + { + ret <<= LIT_UTF8_BITS_IN_EXTRA_BYTES; + ret |= (buf_p[i] & LIT_UTF8_LAST_6_BITS_MASK); + } + + JERRY_ASSERT (ret <= LIT_UTF16_CODE_UNIT_MAX); + *code_point = (ecma_char_t) ret; + return bytes_count; +} /* lit_read_code_unit_from_utf8 */ + +/** + * Decodes a unicode code unit from non-empty cesu-8-encoded buffer + * + * @return number of bytes occupied by code point in the string + */ +lit_utf8_size_t +lit_read_prev_code_unit_from_utf8 (const lit_utf8_byte_t *buf_p, /**< buffer with characters */ + ecma_char_t *code_point) /**< @out: code point */ +{ + JERRY_ASSERT (buf_p); + + lit_utf8_byte_t *current_p = (lit_utf8_byte_t *) buf_p; + + lit_utf8_decr (¤t_p); + return lit_read_code_unit_from_utf8 (current_p, code_point); +} /* lit_read_prev_code_unit_from_utf8 */ + +/** + * Decodes a unicode code unit from non-empty cesu-8-encoded buffer + * + * @return next code unit + */ +ecma_char_t +lit_utf8_read_next (lit_utf8_byte_t **buf_p) /**< in-out:buffer with characters */ +{ + JERRY_ASSERT (*buf_p); + ecma_char_t ch; + + *buf_p += lit_read_code_unit_from_utf8 (*buf_p, &ch); + + return ch; +} /* lit_utf8_read_next */ + +/** + * Decodes a unicode code unit from non-empty cesu-8-encoded buffer + * + * @return previous code unit + */ +ecma_char_t +lit_utf8_read_prev (lit_utf8_byte_t **buf_p) /**< in-out:buffer with characters */ +{ + JERRY_ASSERT (*buf_p); + ecma_char_t ch; + + lit_utf8_decr (buf_p); + lit_read_code_unit_from_utf8 (*buf_p, &ch); + + return ch; +} /* lit_utf8_read_prev */ + +/** + * Decodes a unicode code unit from non-empty cesu-8-encoded buffer + * + * @return next code unit + */ +ecma_char_t +lit_utf8_peek_next (lit_utf8_byte_t *buf_p) /**< in-out:buffer with characters */ +{ + JERRY_ASSERT (buf_p); + ecma_char_t ch; + + lit_read_code_unit_from_utf8 (buf_p, &ch); + + return ch; +} /* lit_utf8_peek_next */ + +/** + * Decodes a unicode code unit from non-empty cesu-8-encoded buffer + * + * @return previous code unit + */ +ecma_char_t +lit_utf8_peek_prev (lit_utf8_byte_t *buf_p) /**< in-out:buffer with characters */ +{ + JERRY_ASSERT (buf_p); + ecma_char_t ch; + + lit_read_prev_code_unit_from_utf8 (buf_p, &ch); + + return ch; +} /* lit_utf8_peek_prev */ + +/** + * Increase cesu-8 encoded string pointer by one code unit. + */ +void +lit_utf8_incr (lit_utf8_byte_t **buf_p) /**< in-out:buffer with characters */ +{ + JERRY_ASSERT (*buf_p); + + *buf_p += lit_get_unicode_char_size_by_utf8_first_byte (**buf_p); +} /* lit_utf8_incr */ + +/** + * Decrease cesu-8 encoded string pointer by one code unit. + */ +void +lit_utf8_decr (lit_utf8_byte_t **buf_p) /**< in-out:buffer with characters */ +{ + JERRY_ASSERT (*buf_p); + lit_utf8_byte_t *current_p = *buf_p; + do + { + current_p--; + } + while ((*(current_p) & LIT_UTF8_EXTRA_BYTE_MASK) == LIT_UTF8_EXTRA_BYTE_MARKER); + *buf_p = current_p; +} /* lit_utf8_decr */ + /** * Calc hash using the specified hash_basis. * @@ -653,13 +741,13 @@ lit_utf8_string_code_unit_at (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 stri lit_utf8_size_t utf8_buf_size, /**< string size in bytes */ ecma_length_t code_unit_offset) /**< ofset of a code_unit */ { - lit_utf8_iterator_t iter = lit_utf8_iterator_create (utf8_buf_p, utf8_buf_size); + lit_utf8_byte_t *current_p = (lit_utf8_byte_t *) utf8_buf_p; ecma_char_t code_unit; do { - JERRY_ASSERT (!lit_utf8_iterator_is_eos (&iter)); - code_unit = lit_utf8_iterator_read_next (&iter); + JERRY_ASSERT (current_p < utf8_buf_p + utf8_buf_size); + current_p += lit_read_code_unit_from_utf8 (current_p, &code_unit); } while (code_unit_offset--); @@ -667,12 +755,12 @@ lit_utf8_string_code_unit_at (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 stri } /* lit_utf8_string_code_unit_at */ /** - * Return number of bytes occupied by a unicode character in utf-8 representation + * Get CESU-8 encoded size of character * - * @return size of a unicode character in utf-8 format + * @return number of bytes occupied in CESU-8 */ lit_utf8_size_t -lit_get_unicode_char_size_by_utf8_first_byte (lit_utf8_byte_t first_byte) /**< first byte of a utf-8 byte sequence */ +lit_get_unicode_char_size_by_utf8_first_byte (const lit_utf8_byte_t first_byte) /**< buffer with characters */ { if ((first_byte & LIT_UTF8_1_BYTE_MASK) == LIT_UTF8_1_BYTE_MARKER) { @@ -682,34 +770,86 @@ lit_get_unicode_char_size_by_utf8_first_byte (lit_utf8_byte_t first_byte) /**< f { return 2; } - else if ((first_byte & LIT_UTF8_3_BYTE_MASK) == LIT_UTF8_3_BYTE_MARKER) - { - return 3; - } else { - JERRY_ASSERT ((first_byte & LIT_UTF8_4_BYTE_MASK) == LIT_UTF8_4_BYTE_MARKER); - return 4; + JERRY_ASSERT ((first_byte & LIT_UTF8_3_BYTE_MASK) == LIT_UTF8_3_BYTE_MARKER); + return 3; } } /* lit_get_unicode_char_size_by_utf8_first_byte */ /** - * Convert code_unit to utf-8 representation + * Convert code unit to cesu-8 representation * - * @return bytes count, stored required to represent specified code unit + * @return byte count required to represent the code unit */ lit_utf8_size_t lit_code_unit_to_utf8 (ecma_char_t code_unit, /**< code unit */ lit_utf8_byte_t *buf_p) /**< buffer where to store the result, * its size should be at least MAX_BYTES_IN_CODE_UNIT */ { - return lit_code_point_to_utf8 (code_unit, buf_p); + if (code_unit <= LIT_UTF8_1_BYTE_CODE_POINT_MAX) + { + buf_p[0] = (lit_utf8_byte_t) code_unit; + return 1; + } + else if (code_unit <= LIT_UTF8_2_BYTE_CODE_POINT_MAX) + { + uint32_t code_unit_bits = code_unit; + lit_utf8_byte_t second_byte_bits = (lit_utf8_byte_t) (code_unit_bits & LIT_UTF8_LAST_6_BITS_MASK); + code_unit_bits >>= LIT_UTF8_BITS_IN_EXTRA_BYTES; + + lit_utf8_byte_t first_byte_bits = (lit_utf8_byte_t) (code_unit_bits & LIT_UTF8_LAST_5_BITS_MASK); + JERRY_ASSERT (first_byte_bits == code_unit_bits); + + buf_p[0] = LIT_UTF8_2_BYTE_MARKER | first_byte_bits; + buf_p[1] = LIT_UTF8_EXTRA_BYTE_MARKER | second_byte_bits; + return 2; + } + else + { + uint32_t code_unit_bits = code_unit; + lit_utf8_byte_t third_byte_bits = (lit_utf8_byte_t) (code_unit_bits & LIT_UTF8_LAST_6_BITS_MASK); + code_unit_bits >>= LIT_UTF8_BITS_IN_EXTRA_BYTES; + + lit_utf8_byte_t second_byte_bits = (lit_utf8_byte_t) (code_unit_bits & LIT_UTF8_LAST_6_BITS_MASK); + code_unit_bits >>= LIT_UTF8_BITS_IN_EXTRA_BYTES; + + lit_utf8_byte_t first_byte_bits = (lit_utf8_byte_t) (code_unit_bits & LIT_UTF8_LAST_4_BITS_MASK); + JERRY_ASSERT (first_byte_bits == code_unit_bits); + + buf_p[0] = LIT_UTF8_3_BYTE_MARKER | first_byte_bits; + buf_p[1] = LIT_UTF8_EXTRA_BYTE_MARKER | second_byte_bits; + buf_p[2] = LIT_UTF8_EXTRA_BYTE_MARKER | third_byte_bits; + return 3; + } } /* lit_code_unit_to_utf8 */ +/** + * Convert code point to cesu-8 representation + * + * @return byte count required to represent the code point + */ +lit_utf8_size_t +lit_code_point_to_cesu8 (lit_code_point_t code_point, /**< code point */ + lit_utf8_byte_t *buf) /**< buffer where to store the result, + * its size should be at least 6 bytes */ +{ + if (code_point <= LIT_UTF16_CODE_UNIT_MAX) + { + return lit_code_unit_to_utf8 ((ecma_char_t) code_point, buf); + } + else + { + lit_utf8_size_t offset = lit_code_unit_to_utf8 (convert_code_point_to_high_surrogate (code_point), buf); + offset += lit_code_unit_to_utf8 (convert_code_point_to_low_surrogate (code_point), buf + offset); + return offset; + } +} /* lit_code_point_to_utf8 */ + /** * Convert code point to utf-8 representation * - * @return bytes count, stored required to represent specified code unit + * @return byte count required to represent the code point */ lit_utf8_size_t lit_code_point_to_utf8 (lit_code_point_t code_point, /**< code point */ @@ -796,10 +936,10 @@ lit_convert_surrogate_pair_to_code_point (ecma_char_t high_surrogate, /**< high code_point |= (uint16_t) (low_surrogate - LIT_UTF16_LOW_SURROGATE_MIN); return code_point; -} /* lit_surrogate_pair_to_code_point */ +} /* lit_convert_surrogate_pair_to_code_point */ /** - * Compare utf-8 string to utf-8 string + * Compare cesu-8 string to cesu-8 string * * @return true - if strings are equal; * false - otherwise. @@ -819,7 +959,7 @@ lit_compare_utf8_strings (const lit_utf8_byte_t *string1_p, /**< utf-8 string */ } /* lit_compare_utf8_strings */ /** - * Relational compare of utf-8 strings + * Relational compare of cesu-8 strings * * First string is less than second string if: * - strings are not equal; @@ -833,25 +973,28 @@ bool lit_compare_utf8_strings_relational (const lit_utf8_byte_t *string1_p, /**< const lit_utf8_byte_t *string2_p, /**< utf-8 string */ lit_utf8_size_t string2_size) /**< string size */ { - lit_utf8_iterator_t iter1 = lit_utf8_iterator_create (string1_p, string1_size); - lit_utf8_iterator_t iter2 = lit_utf8_iterator_create (string2_p, string2_size); + lit_utf8_byte_t *string1_pos = (lit_utf8_byte_t *) string1_p; + lit_utf8_byte_t *string2_pos = (lit_utf8_byte_t *) string2_p; + const lit_utf8_byte_t *string1_end_p = string1_p + string1_size; + const lit_utf8_byte_t *string2_end_p = string2_p + string2_size; - while (!lit_utf8_iterator_is_eos (&iter1) - && !lit_utf8_iterator_is_eos (&iter2)) + while (string1_pos < string1_end_p && string2_pos < string2_end_p) { - ecma_char_t code_point1 = lit_utf8_iterator_read_next (&iter1); - ecma_char_t code_point2 = lit_utf8_iterator_read_next (&iter2); - if (code_point1 < code_point2) + ecma_char_t ch1, ch2; + string1_pos += lit_read_code_unit_from_utf8 (string1_pos, &ch1); + string2_pos += lit_read_code_unit_from_utf8 (string2_pos, &ch2); + + if (ch1 < ch2) { return true; } - else if (code_point1 > code_point2) + else if (ch1 > ch2) { return false; } } - return (lit_utf8_iterator_is_eos (&iter1) && !lit_utf8_iterator_is_eos (&iter2)); + return (string1_pos >= string1_end_p && string2_pos < string2_end_p); } /* lit_compare_utf8_strings_relational */ /** diff --git a/jerry-core/lit/lit-strings.h b/jerry-core/lit/lit-strings.h index a411c733f7..65357c1f67 100644 --- a/jerry-core/lit/lit-strings.h +++ b/jerry-core/lit/lit-strings.h @@ -123,6 +123,7 @@ int32_t lit_utf8_iterator_pos_cmp (lit_utf8_iterator_pos_t, lit_utf8_iterator_po /* validation */ bool lit_is_utf8_string_valid (const lit_utf8_byte_t *, lit_utf8_size_t); +bool lit_is_cesu8_string_valid (const lit_utf8_byte_t *, lit_utf8_size_t); /* checks */ bool lit_is_code_unit_low_surrogate (ecma_char_t); @@ -132,25 +133,18 @@ bool lit_is_code_unit_high_surrogate (ecma_char_t); lit_utf8_iterator_t lit_utf8_iterator_create (const lit_utf8_byte_t *, lit_utf8_size_t); void lit_utf8_iterator_seek_bos (lit_utf8_iterator_t *); -void lit_utf8_iterator_seek_eos (lit_utf8_iterator_t *); lit_utf8_iterator_pos_t lit_utf8_iterator_get_pos (const lit_utf8_iterator_t *); void lit_utf8_iterator_seek (lit_utf8_iterator_t *, lit_utf8_iterator_pos_t); -ecma_length_t lit_utf8_iterator_get_index (const lit_utf8_iterator_t *); - ecma_char_t lit_utf8_iterator_peek_next (const lit_utf8_iterator_t *); -ecma_char_t lit_utf8_iterator_peek_prev (const lit_utf8_iterator_t *); void lit_utf8_iterator_incr (lit_utf8_iterator_t *); -void lit_utf8_iterator_decr (lit_utf8_iterator_t *); void lit_utf8_iterator_advance (lit_utf8_iterator_t *, ecma_length_t); ecma_char_t lit_utf8_iterator_read_next (lit_utf8_iterator_t *); -ecma_char_t lit_utf8_iterator_read_prev (lit_utf8_iterator_t *); bool lit_utf8_iterator_is_eos (const lit_utf8_iterator_t *); -bool lit_utf8_iterator_is_bos (const lit_utf8_iterator_t *); /* size */ lit_utf8_size_t lit_zt_utf8_string_size (const lit_utf8_byte_t *); @@ -169,17 +163,31 @@ lit_utf8_size_t lit_get_unicode_char_size_by_utf8_first_byte (lit_utf8_byte_t); /* conversion */ lit_utf8_size_t lit_code_unit_to_utf8 (ecma_char_t, lit_utf8_byte_t *); lit_utf8_size_t lit_code_point_to_utf8 (lit_code_point_t, lit_utf8_byte_t *); +lit_utf8_size_t lit_code_point_to_cesu8 (lit_code_point_t, lit_utf8_byte_t *); lit_code_point_t lit_convert_surrogate_pair_to_code_point (ecma_char_t, ecma_char_t); -/* comparison */ -bool lit_compare_utf8_strings (const lit_utf8_byte_t *, lit_utf8_size_t, const lit_utf8_byte_t *, lit_utf8_size_t); +bool lit_compare_utf8_strings (const lit_utf8_byte_t *, lit_utf8_size_t, + const lit_utf8_byte_t *, lit_utf8_size_t); -bool lit_compare_utf8_strings_relational (const lit_utf8_byte_t *, lit_utf8_size_t, - const lit_utf8_byte_t *, lit_utf8_size_t); +bool lit_compare_utf8_strings_relational (const lit_utf8_byte_t *string1_p, lit_utf8_size_t, + const lit_utf8_byte_t *string2_p, lit_utf8_size_t); /* read code point from buffer */ lit_utf8_size_t lit_read_code_point_from_utf8 (const lit_utf8_byte_t *, lit_utf8_size_t, lit_code_point_t *); +lit_utf8_size_t lit_read_code_unit_from_utf8 (const lit_utf8_byte_t *, + ecma_char_t *); + +lit_utf8_size_t lit_read_prev_code_unit_from_utf8 (const lit_utf8_byte_t *, + ecma_char_t *); + +ecma_char_t lit_utf8_read_next (lit_utf8_byte_t **); +ecma_char_t lit_utf8_read_prev (lit_utf8_byte_t **); +ecma_char_t lit_utf8_peek_next (lit_utf8_byte_t *); +ecma_char_t lit_utf8_peek_prev (lit_utf8_byte_t *); +void lit_utf8_incr (lit_utf8_byte_t **); +void lit_utf8_decr (lit_utf8_byte_t **); + /* print */ void lit_put_ecma_char (ecma_char_t); diff --git a/jerry-core/parser/js/lexer.cpp b/jerry-core/parser/js/lexer.cpp index 9ed4e42770..ad7f658841 100644 --- a/jerry-core/parser/js/lexer.cpp +++ b/jerry-core/parser/js/lexer.cpp @@ -160,17 +160,61 @@ lexer_create_token_for_charset (token_type tt, /**< token type */ { JERRY_ASSERT (charset_p != NULL); - literal_t lit = lit_find_literal_by_utf8_string (charset_p, size); + lit_utf8_iterator_t iter = lit_utf8_iterator_create (charset_p, (lit_utf8_size_t) size); + lit_utf8_size_t new_size = 0; + lit_utf8_size_t new_length = 0; + bool should_convert = false; + + while (!lit_utf8_iterator_is_eos (&iter)) + { + if (iter.buf_pos.is_non_bmp_middle) + { + should_convert = true; + } + lit_utf8_iterator_incr (&iter); + new_size += LIT_CESU8_MAX_BYTES_IN_CODE_UNIT; + } + + lit_utf8_byte_t *converted_str_p; + + if (unlikely (should_convert)) + { + lit_utf8_iterator_seek_bos (&iter); + converted_str_p = (lit_utf8_byte_t *) jsp_mm_alloc (new_size); + + while (!lit_utf8_iterator_is_eos (&iter)) + { + ecma_char_t ch = lit_utf8_iterator_read_next (&iter); + new_length += lit_code_unit_to_utf8 (ch, converted_str_p + new_length); + } + } + else + { + converted_str_p = (lit_utf8_byte_t *) charset_p; + new_length = size; + JERRY_ASSERT (lit_is_cesu8_string_valid (converted_str_p, new_length)); + } + + literal_t lit = lit_find_literal_by_utf8_string (converted_str_p, new_length); if (lit != NULL) { + if (unlikely (should_convert)) + { + jsp_mm_free (converted_str_p); + } + return create_token_from_lit (tt, lit); } - - lit = lit_create_literal_from_utf8_string (charset_p, size); + lit = lit_create_literal_from_utf8_string (converted_str_p, new_length); JERRY_ASSERT (lit->get_type () == LIT_STR_T || lit->get_type () == LIT_MAGIC_STR_T || lit->get_type () == LIT_MAGIC_STR_EX_T); + if (unlikely (should_convert)) + { + jsp_mm_free (converted_str_p); + } + return create_token_from_lit (tt, lit); } /* lexer_create_token_for_charset */ @@ -1550,6 +1594,7 @@ lexer_locus_to_line_and_column (lit_utf8_iterator_pos_t locus, /**< iterator pos size_t *column) /**< @out: column number */ { JERRY_ASSERT ((lit_utf8_size_t) (locus.offset + locus.is_non_bmp_middle) <= buffer_size); + lit_utf8_iterator_t iter = lit_utf8_iterator_create (buffer_start, (lit_utf8_size_t) buffer_size); lit_utf8_iterator_pos_t iter_pos = lit_utf8_iterator_get_pos (&iter); diff --git a/jerry-core/parser/regexp/re-compiler.cpp b/jerry-core/parser/regexp/re-compiler.cpp index 5a65cccf53..c11c62c38a 100644 --- a/jerry-core/parser/regexp/re-compiler.cpp +++ b/jerry-core/parser/regexp/re-compiler.cpp @@ -383,19 +383,15 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); uint32_t alterantive_offset = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p); + bool should_loop = true; - while (true) + while (ecma_is_completion_value_empty (ret_value) && should_loop) { ECMA_TRY_CATCH (empty, re_parse_next_token (re_ctx_p->parser_ctx_p, &(re_ctx_p->current_token)), ret_value); - ECMA_FINALIZE (empty); - if (!ecma_is_completion_value_empty (ret_value)) - { - return ret_value; /* error */ - } uint32_t new_atom_start_offset = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p); switch (re_ctx_p->current_token.type) @@ -411,10 +407,7 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context { re_insert_into_group (re_ctx_p, new_atom_start_offset, idx, true); } - else - { - return ret_value; /* error */ - } + break; } case RE_TOK_START_NON_CAPTURE_GROUP: @@ -428,10 +421,7 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context { re_insert_into_group (re_ctx_p, new_atom_start_offset, idx, false); } - else - { - return ret_value; /* error */ - } + break; } case RE_TOK_CHAR: @@ -506,10 +496,7 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context re_insert_into_group_with_jump (re_ctx_p, new_atom_start_offset, idx, false); } - else - { - return ret_value; /* error */ - } + break; } case RE_TOK_ASSERT_START_NEG_LOOKAHEAD: @@ -526,10 +513,7 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context re_insert_into_group_with_jump (re_ctx_p, new_atom_start_offset, idx, false); } - else - { - return ret_value; /* error */ - } + break; } case RE_TOK_BACKREFERENCE: @@ -580,10 +564,6 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context ECMA_FINALIZE (empty); - if (ecma_is_completion_value_throw (ret_value)) - { - return ret_value; /* error */ - } break; } case RE_TOK_END_GROUP: @@ -597,9 +577,9 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context else { re_insert_u32 (bc_ctx_p, alterantive_offset, re_get_bytecode_length (bc_ctx_p) - alterantive_offset); + should_loop = false; } - - return ret_value; + break; } case RE_TOK_EOF: { @@ -610,19 +590,20 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context else { re_insert_u32 (bc_ctx_p, alterantive_offset, re_get_bytecode_length (bc_ctx_p) - alterantive_offset); + should_loop = false; } - return ret_value; + break; } default: { ret_value = ecma_raise_syntax_error ("Unexpected RegExp token."); - return ret_value; + break; } } + ECMA_FINALIZE (empty); } - JERRY_UNREACHABLE (); return ret_value; } /* re_parse_alternative */ @@ -656,10 +637,10 @@ re_compile_bytecode (re_bytecode_t **out_bytecode_p, /**< out:pointer to bytecod ssize_t sz = ecma_string_to_utf8_string (pattern_str_p, pattern_start_p, (ssize_t) pattern_str_size); JERRY_ASSERT (sz >= 0); - lit_utf8_iterator_t iter = lit_utf8_iterator_create (pattern_start_p, pattern_str_size); - re_parser_ctx_t parser_ctx; - parser_ctx.iter = iter; + parser_ctx.input_start_p = pattern_start_p; + parser_ctx.input_curr_p = pattern_start_p; + parser_ctx.input_end_p = pattern_start_p + pattern_str_size; parser_ctx.num_of_groups = -1; re_ctx.parser_ctx_p = &parser_ctx; diff --git a/jerry-core/parser/regexp/re-parser.cpp b/jerry-core/parser/regexp/re-parser.cpp index b1123b0fae..4806bcba4d 100644 --- a/jerry-core/parser/regexp/re-parser.cpp +++ b/jerry-core/parser/regexp/re-parser.cpp @@ -31,17 +31,17 @@ * false, otherwise */ static bool -re_hex_lookup (lit_utf8_iterator_t iter, /**< input string iterator */ +re_hex_lookup (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context */ uint32_t lookup) /**< size of lookup */ { - ecma_char_t ch; bool is_digit = true; + lit_utf8_byte_t *curr_p = parser_ctx_p->input_curr_p; + for (uint32_t i = 0; is_digit && i < lookup; i++) { - if (!lit_utf8_iterator_is_eos (&iter)) + if (curr_p < parser_ctx_p->input_end_p) { - ch = lit_utf8_iterator_read_next (&iter); - is_digit = lit_char_is_hex_digit (ch); + is_digit = lit_char_is_hex_digit (*curr_p++); } else { @@ -59,11 +59,12 @@ re_hex_lookup (lit_utf8_iterator_t iter, /**< input string iterator */ * false, otherwise */ static bool __attr_always_inline___ -re_parse_non_greedy_char (lit_utf8_iterator_t *iter_p) /**< RegExp pattern */ +re_parse_non_greedy_char (re_parser_ctx_t *parser_ctx_p) /**< RegExp parser context */ { - if (!lit_utf8_iterator_is_eos (iter_p) && lit_utf8_iterator_peek_next (iter_p) == LIT_CHAR_QUESTION) + if (parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p + && *parser_ctx_p->input_curr_p == LIT_CHAR_QUESTION) { - lit_utf8_iterator_advance (iter_p, 1); + parser_ctx_p->input_curr_p++; return true; } @@ -76,16 +77,16 @@ re_parse_non_greedy_char (lit_utf8_iterator_t *iter_p) /**< RegExp pattern */ * @return uint32_t - parsed octal number */ static uint32_t -re_parse_octal (lit_utf8_iterator_t *iter) /**< input string iterator */ +re_parse_octal (re_parser_ctx_t *parser_ctx_p) /**< RegExp parser context */ { uint32_t number = 0; for (int index = 0; index < 3 - && !lit_utf8_iterator_is_eos (iter) - && lit_char_is_octal_digit (lit_utf8_iterator_peek_next (iter)); + && parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p + && lit_char_is_octal_digit (*parser_ctx_p->input_curr_p); index++) { - number = number * 8 + lit_char_hex_to_int (lit_utf8_iterator_read_next (iter)); + number = number * 8 + lit_char_hex_to_int (*parser_ctx_p->input_curr_p++); } return number; @@ -102,59 +103,59 @@ re_parse_iterator (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context */ re_token_t *re_token_p) /**< out: output token */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); - lit_utf8_iterator_t *iter_p = &(parser_ctx_p->iter); + re_token_p->qmin = 1; re_token_p->qmax = 1; re_token_p->greedy = true; - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { return ret_value; } - ecma_char_t ch = lit_utf8_iterator_peek_next (iter_p); + ecma_char_t ch = *parser_ctx_p->input_curr_p; switch (ch) { case LIT_CHAR_QUESTION: { - lit_utf8_iterator_advance (iter_p, 1); + parser_ctx_p->input_curr_p++; re_token_p->qmin = 0; re_token_p->qmax = 1; - re_token_p->greedy = !re_parse_non_greedy_char (iter_p); + re_token_p->greedy = !re_parse_non_greedy_char (parser_ctx_p); break; } case LIT_CHAR_ASTERISK: { - lit_utf8_iterator_advance (iter_p, 1); + parser_ctx_p->input_curr_p++; re_token_p->qmin = 0; re_token_p->qmax = RE_ITERATOR_INFINITE; - re_token_p->greedy = !re_parse_non_greedy_char (iter_p); + re_token_p->greedy = !re_parse_non_greedy_char (parser_ctx_p); break; } case LIT_CHAR_PLUS: { - lit_utf8_iterator_advance (iter_p, 1); + parser_ctx_p->input_curr_p++; re_token_p->qmin = 1; re_token_p->qmax = RE_ITERATOR_INFINITE; - re_token_p->greedy = !re_parse_non_greedy_char (iter_p); + re_token_p->greedy = !re_parse_non_greedy_char (parser_ctx_p); break; } case LIT_CHAR_LEFT_BRACE: { - lit_utf8_iterator_advance (iter_p, 1); + parser_ctx_p->input_curr_p++; uint32_t qmin = 0; uint32_t qmax = RE_ITERATOR_INFINITE; uint32_t digits = 0; while (true) { - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { return ecma_raise_syntax_error ("invalid quantifier"); } - ch = lit_utf8_iterator_read_next (iter_p); + ch = *parser_ctx_p->input_curr_p++; if (lit_char_is_decimal_digit (ch)) { @@ -172,19 +173,19 @@ re_parse_iterator (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context */ return ecma_raise_syntax_error ("RegExp quantifier error: double comma."); } - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { return ecma_raise_syntax_error ("invalid quantifier"); } - if (lit_utf8_iterator_peek_next (iter_p) == LIT_CHAR_RIGHT_BRACE) + if (*parser_ctx_p->input_curr_p == LIT_CHAR_RIGHT_BRACE) { if (digits == 0) { return ecma_raise_syntax_error ("RegExp quantifier error: missing digits."); } - lit_utf8_iterator_advance (iter_p, 1); + parser_ctx_p->input_curr_p++; re_token_p->qmin = qmin; re_token_p->qmax = RE_ITERATOR_INFINITE; break; @@ -219,7 +220,7 @@ re_parse_iterator (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context */ } } - re_token_p->greedy = !re_parse_non_greedy_char (iter_p); + re_token_p->greedy = !re_parse_non_greedy_char (parser_ctx_p); break; } default: @@ -246,16 +247,15 @@ re_count_num_of_groups (re_parser_ctx_t *parser_ctx_p) /**< RegExp parser contex { int char_class_in = 0; parser_ctx_p->num_of_groups = 0; - lit_utf8_iterator_t iter = lit_utf8_iterator_create (parser_ctx_p->iter.buf_p, - parser_ctx_p->iter.buf_size); + lit_utf8_byte_t *curr_p = parser_ctx_p->input_start_p; - while (!lit_utf8_iterator_is_eos (&iter)) + while (curr_p < parser_ctx_p->input_end_p) { - switch (lit_utf8_iterator_read_next (&iter)) + switch (*curr_p++) { case LIT_CHAR_BACKSLASH: { - lit_utf8_iterator_advance (&iter, 1); + lit_utf8_incr (&curr_p); break; } case LIT_CHAR_LEFT_SQUARE: @@ -273,8 +273,8 @@ re_count_num_of_groups (re_parser_ctx_t *parser_ctx_p) /**< RegExp parser contex } case LIT_CHAR_LEFT_PAREN: { - if (!lit_utf8_iterator_is_eos (&iter) - && lit_utf8_iterator_peek_next (&iter) != LIT_CHAR_QUESTION + if (curr_p < parser_ctx_p->input_end_p + && *curr_p != LIT_CHAR_QUESTION && !char_class_in) { parser_ctx_p->num_of_groups++; @@ -304,21 +304,21 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */ uint32_t start = RE_CHAR_UNDEF; bool is_range = false; parser_ctx_p->num_of_classes = 0; - lit_utf8_iterator_t *iter_p = &(parser_ctx_p->iter); - if (lit_utf8_iterator_peek_prev (iter_p) != LIT_CHAR_LEFT_SQUARE) + + if (lit_utf8_peek_prev (parser_ctx_p->input_curr_p) != LIT_CHAR_LEFT_SQUARE) { - lit_utf8_iterator_read_prev (iter_p); - lit_utf8_iterator_read_prev (iter_p); + lit_utf8_decr (&parser_ctx_p->input_curr_p); + lit_utf8_decr (&parser_ctx_p->input_curr_p); } do { - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { return ecma_raise_syntax_error ("invalid character class, end of string"); } - uint32_t ch = lit_utf8_iterator_read_next (iter_p); + uint32_t ch = lit_utf8_read_next (&parser_ctx_p->input_curr_p); if (ch == LIT_CHAR_RIGHT_SQUARE) { @@ -330,14 +330,14 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */ } else if (ch == LIT_CHAR_MINUS) { - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { return ecma_raise_syntax_error ("invalid character class, end of string after '-'"); } if (start != RE_CHAR_UNDEF && !is_range - && lit_utf8_iterator_peek_next (iter_p) != LIT_CHAR_RIGHT_SQUARE) + && *parser_ctx_p->input_curr_p != LIT_CHAR_RIGHT_SQUARE) { is_range = true; continue; @@ -345,12 +345,12 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */ } else if (ch == LIT_CHAR_BACKSLASH) { - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { return ecma_raise_syntax_error ("invalid character class, end of string after '\\'"); } - ch = lit_utf8_iterator_read_next (iter_p); + ch = *parser_ctx_p->input_curr_p++; if (ch == LIT_CHAR_LOWERCASE_B) { @@ -378,9 +378,9 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */ } else if (ch == LIT_CHAR_LOWERCASE_C) { - if (!lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p) { - ch = lit_utf8_iterator_peek_next (iter_p); + ch = *parser_ctx_p->input_curr_p; if ((ch >= LIT_CHAR_ASCII_UPPERCASE_LETTERS_BEGIN && ch <= LIT_CHAR_ASCII_UPPERCASE_LETTERS_END) || (ch >= LIT_CHAR_ASCII_LOWERCASE_LETTERS_BEGIN && ch <= LIT_CHAR_ASCII_LOWERCASE_LETTERS_END) @@ -388,7 +388,7 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */ { /* See ECMA-262 v5, 15.10.2.10 (Point 3) */ ch = (ch % 32); - lit_utf8_iterator_incr (iter_p); + parser_ctx_p->input_curr_p++; } else { @@ -399,29 +399,25 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */ else if (ch == LIT_CHAR_LOWERCASE_X) { lit_code_point_t code_point; - const lit_utf8_byte_t *hex_start_p = parser_ctx_p->iter.buf_p + parser_ctx_p->iter.buf_pos.offset; - if (!lit_read_code_point_from_hex (hex_start_p, 2, &code_point)) + if (!lit_read_code_point_from_hex (parser_ctx_p->input_curr_p, 2, &code_point)) { return ecma_raise_syntax_error ("invalid character class, end of string after '\\x'"); } - lit_utf8_iterator_advance (iter_p, 2); - + parser_ctx_p->input_curr_p += 2; append_char_class (re_ctx_p, code_point, code_point); } else if (ch == LIT_CHAR_LOWERCASE_U) { lit_code_point_t code_point; - const lit_utf8_byte_t *hex_start_p = parser_ctx_p->iter.buf_p + parser_ctx_p->iter.buf_pos.offset; - if (!lit_read_code_point_from_hex (hex_start_p, 4, &code_point)) + if (!lit_read_code_point_from_hex (parser_ctx_p->input_curr_p, 4, &code_point)) { return ecma_raise_syntax_error ("invalid character class, end of string after '\\u'"); } - lit_utf8_iterator_advance (iter_p, 4); - + parser_ctx_p->input_curr_p += 4; append_char_class (re_ctx_p, code_point, code_point); } else if (ch == LIT_CHAR_LOWERCASE_D) @@ -493,8 +489,8 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */ && lit_char_is_octal_digit ((ecma_char_t) ch) && ch != LIT_CHAR_0) { - lit_utf8_iterator_decr (iter_p); - ch = re_parse_octal (iter_p); + parser_ctx_p->input_curr_p--; + ch = re_parse_octal (parser_ctx_p); } } /* ch == LIT_CHAR_BACKSLASH */ @@ -558,15 +554,14 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * re_token_t *out_token_p) /**< out: output token */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); - lit_utf8_iterator_t *iter_p = &(parser_ctx_p->iter); - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { out_token_p->type = RE_TOK_EOF; return ret_value; } - ecma_char_t ch = lit_utf8_iterator_read_next (iter_p); + ecma_char_t ch = lit_utf8_read_next (&parser_ctx_p->input_curr_p); switch (ch) { @@ -593,13 +588,13 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * } case LIT_CHAR_BACKSLASH: { - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { return ecma_raise_syntax_error ("invalid regular experssion"); } out_token_p->type = RE_TOK_CHAR; - ch = lit_utf8_iterator_read_next (iter_p); + ch = lit_utf8_read_next (&parser_ctx_p->input_curr_p); if (ch == LIT_CHAR_LOWERCASE_B) { @@ -631,55 +626,53 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * } else if (ch == LIT_CHAR_LOWERCASE_C) { - if (!lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p) { - ch = lit_utf8_iterator_peek_next (iter_p); + ch = *parser_ctx_p->input_curr_p; if ((ch >= LIT_CHAR_ASCII_UPPERCASE_LETTERS_BEGIN && ch <= LIT_CHAR_ASCII_UPPERCASE_LETTERS_END) || (ch >= LIT_CHAR_ASCII_LOWERCASE_LETTERS_BEGIN && ch <= LIT_CHAR_ASCII_LOWERCASE_LETTERS_END)) { out_token_p->value = (ch % 32); - lit_utf8_iterator_incr (iter_p); + parser_ctx_p->input_curr_p++; } else { out_token_p->value = LIT_CHAR_BACKSLASH; - lit_utf8_iterator_decr (iter_p); + parser_ctx_p->input_curr_p--; } } else { out_token_p->value = LIT_CHAR_BACKSLASH; - lit_utf8_iterator_decr (iter_p); + parser_ctx_p->input_curr_p--; } } else if (ch == LIT_CHAR_LOWERCASE_X - && re_hex_lookup (*iter_p, 2)) + && re_hex_lookup (parser_ctx_p, 2)) { lit_code_point_t code_point; - const lit_utf8_byte_t *hex_start_p = iter_p->buf_p + iter_p->buf_pos.offset; - if (!lit_read_code_point_from_hex (hex_start_p, 2, &code_point)) + if (!lit_read_code_point_from_hex (parser_ctx_p->input_curr_p, 2, &code_point)) { return ecma_raise_syntax_error ("decode error"); } + parser_ctx_p->input_curr_p += 2; out_token_p->value = code_point; - lit_utf8_iterator_advance (iter_p, 2); } else if (ch == LIT_CHAR_LOWERCASE_U - && re_hex_lookup (*iter_p, 4)) + && re_hex_lookup (parser_ctx_p, 4)) { lit_code_point_t code_point; - const lit_utf8_byte_t *hex_start_p = iter_p->buf_p + iter_p->buf_pos.offset; - if (!lit_read_code_point_from_hex (hex_start_p, 4, &code_point)) + if (!lit_read_code_point_from_hex (parser_ctx_p->input_curr_p, 4, &code_point)) { return ecma_raise_syntax_error ("decode error"); } + parser_ctx_p->input_curr_p += 4; out_token_p->value = code_point; - lit_utf8_iterator_advance (iter_p, 4); } else if (ch == LIT_CHAR_LOWERCASE_D) { @@ -715,8 +708,8 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * { if (ch == LIT_CHAR_0) { - if (!lit_utf8_iterator_is_eos (iter_p) - && lit_char_is_decimal_digit (lit_utf8_iterator_peek_next (iter_p))) + if (parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p + && lit_char_is_decimal_digit (*parser_ctx_p->input_curr_p)) { return ecma_raise_syntax_error ("RegExp escape pattern error."); } @@ -732,7 +725,7 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * if (parser_ctx_p->num_of_groups) { - lit_utf8_iterator_read_prev (iter_p); + parser_ctx_p->input_curr_p--; uint32_t number = 0; int index = 0; @@ -743,16 +736,16 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * ret_value = ecma_raise_syntax_error ("RegExp escape pattern error: decimal escape too long."); return ret_value; } - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { break; } - ecma_char_t digit = lit_utf8_iterator_read_next (iter_p); + ecma_char_t digit = *parser_ctx_p->input_curr_p++; if (!lit_char_is_decimal_digit (digit)) { - lit_utf8_iterator_read_prev (iter_p); + parser_ctx_p->input_curr_p--; break; } number = number * 10 + lit_char_hex_to_int (digit); @@ -768,22 +761,20 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * /* Invalid backreference, fallback to octal */ { /* Rewind to start of number. */ - while (index-- > 0) - { - lit_utf8_iterator_decr (iter_p); - } + parser_ctx_p->input_curr_p -= index; /* Try to reparse as octal. */ - ecma_char_t digit = lit_utf8_iterator_peek_next (iter_p); + ecma_char_t digit = *parser_ctx_p->input_curr_p; if (!lit_char_is_octal_digit (digit)) { /* Not octal, keep digit character value. */ - number = lit_utf8_iterator_read_next (iter_p); + number = digit; + parser_ctx_p->input_curr_p++; } else { - number = re_parse_octal (iter_p); + number = re_parse_octal (parser_ctx_p); } } out_token_p->value = number; @@ -798,8 +789,8 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * } else { - lit_utf8_iterator_decr (iter_p); - out_token_p->value = re_parse_octal (iter_p); + parser_ctx_p->input_curr_p--; + out_token_p->value = re_parse_octal (parser_ctx_p); } } } @@ -814,20 +805,20 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * } case LIT_CHAR_LEFT_PAREN: { - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { return ecma_raise_syntax_error ("Unterminated group"); } - if (lit_utf8_iterator_peek_next (iter_p) == LIT_CHAR_QUESTION) + if (*parser_ctx_p->input_curr_p == LIT_CHAR_QUESTION) { - lit_utf8_iterator_advance (iter_p, 1); - if (lit_utf8_iterator_is_eos (iter_p)) + parser_ctx_p->input_curr_p++; + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { return ecma_raise_syntax_error ("Invalid group"); } - ch = lit_utf8_iterator_read_next (iter_p); + ch = *parser_ctx_p->input_curr_p++; if (ch == LIT_CHAR_EQUALS) { @@ -866,15 +857,15 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * { out_token_p->type = RE_TOK_START_CHAR_CLASS; - if (lit_utf8_iterator_is_eos (iter_p)) + if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p) { return ecma_raise_syntax_error ("invalid character class"); } - if (lit_utf8_iterator_peek_next (iter_p) == LIT_CHAR_CIRCUMFLEX) + if (*parser_ctx_p->input_curr_p == LIT_CHAR_CIRCUMFLEX) { out_token_p->type = RE_TOK_START_INV_CHAR_CLASS; - lit_utf8_iterator_advance (iter_p, 1); + parser_ctx_p->input_curr_p++; } break; diff --git a/jerry-core/parser/regexp/re-parser.h b/jerry-core/parser/regexp/re-parser.h index 3ec8551192..c53b13eedb 100644 --- a/jerry-core/parser/regexp/re-parser.h +++ b/jerry-core/parser/regexp/re-parser.h @@ -81,7 +81,9 @@ typedef struct */ typedef struct { - lit_utf8_iterator_t iter; /**< iterator of input pattern */ + lit_utf8_byte_t *input_start_p; /**< start of input pattern */ + lit_utf8_byte_t *input_curr_p; /**< current position in input pattern */ + lit_utf8_byte_t *input_end_p; /**< end of input pattern */ int num_of_groups; /**< number of groups */ uint32_t num_of_classes; /**< number of character classes */ } re_parser_ctx_t; diff --git a/tests/unit/test-strings.cpp b/tests/unit/test-strings.cpp index 2d41726929..bc1efaeffb 100644 --- a/tests/unit/test-strings.cpp +++ b/tests/unit/test-strings.cpp @@ -30,18 +30,17 @@ typedef enum { - UTF8_ANY_SIZE, - UTF8_ONE_BYTE, - UTF8_TWO_BYTES, - UTF8_THREE_BYTES, - UTF8_FOUR_BYTES + CESU8_ANY_SIZE, + CESU8_ONE_BYTE, + CESU8_TWO_BYTES, + CESU8_THREE_BYTES, } utf8_char_size; static lit_utf8_size_t -generate_utf8_char (utf8_char_size char_size, - lit_utf8_byte_t *buf) +generate_cesu8_char (utf8_char_size char_size, + lit_utf8_byte_t *buf) { - JERRY_ASSERT (char_size >= 0 && char_size <= LIT_UTF8_MAX_BYTES_IN_CODE_POINT); + JERRY_ASSERT (char_size >= 0 && char_size <= LIT_CESU8_MAX_BYTES_IN_CODE_UNIT); lit_code_point_t code_point = (lit_code_point_t) rand (); if (char_size == 1) @@ -58,14 +57,9 @@ generate_utf8_char (utf8_char_size char_size, code_point = LIT_UTF8_3_BYTE_CODE_POINT_MIN + code_point % (LIT_UTF8_3_BYTE_CODE_POINT_MAX - LIT_UTF8_3_BYTE_CODE_POINT_MIN); } - else if (char_size == 4) - { - code_point = LIT_UTF8_4_BYTE_CODE_POINT_MIN + code_point % (LIT_UTF8_4_BYTE_CODE_POINT_MAX - - LIT_UTF8_4_BYTE_CODE_POINT_MIN); - } else { - code_point %= LIT_UTF8_4_BYTE_CODE_POINT_MAX; + code_point %= LIT_UTF8_3_BYTE_CODE_POINT_MAX; } if (code_point >= LIT_UTF16_HIGH_SURROGATE_MIN @@ -74,29 +68,29 @@ generate_utf8_char (utf8_char_size char_size, code_point = LIT_UTF16_HIGH_SURROGATE_MIN - 1; } - return lit_code_point_to_utf8 (code_point, buf); + return lit_code_unit_to_utf8 ((ecma_char_t) code_point, buf); } static ecma_length_t -generate_utf8_string (lit_utf8_byte_t *buf_p, - lit_utf8_size_t buf_size) +generate_cesu8_string (lit_utf8_byte_t *buf_p, + lit_utf8_size_t buf_size) { ecma_length_t length = 0; lit_utf8_size_t size = 0; while (size < buf_size) { - const utf8_char_size char_size = (((buf_size - size) > LIT_UTF8_MAX_BYTES_IN_CODE_POINT) - ? UTF8_ANY_SIZE + const utf8_char_size char_size = (((buf_size - size) > LIT_CESU8_MAX_BYTES_IN_CODE_UNIT) + ? CESU8_ANY_SIZE : (utf8_char_size) (buf_size - size)); - lit_utf8_size_t bytes_generated = generate_utf8_char (char_size, buf_p); + lit_utf8_size_t bytes_generated = generate_cesu8_char (char_size, buf_p); - JERRY_ASSERT (lit_is_utf8_string_valid (buf_p, bytes_generated)); + JERRY_ASSERT (lit_is_cesu8_string_valid (buf_p, bytes_generated)); size += bytes_generated; buf_p += bytes_generated; - length += (bytes_generated == LIT_UTF8_MAX_BYTES_IN_CODE_POINT) ? 2 : 1; + length++; } JERRY_ASSERT (size == buf_size); @@ -113,29 +107,31 @@ main (int __attr_unused___ argc, mem_init (); - lit_utf8_byte_t utf8_string[max_bytes_in_string]; + lit_utf8_byte_t cesu8_string[max_bytes_in_string]; ecma_char_t code_units[max_code_units_in_string]; - lit_utf8_iterator_pos_t saved_positions[max_code_units_in_string]; + lit_utf8_byte_t *saved_positions[max_code_units_in_string]; for (int i = 0; i < test_iters; i++) { - lit_utf8_size_t utf8_string_size = (i == 0) ? 0 : (lit_utf8_size_t) (rand () % max_bytes_in_string); - ecma_length_t length = generate_utf8_string (utf8_string, utf8_string_size); + lit_utf8_size_t cesu8_string_size = (i == 0) ? 0 : (lit_utf8_size_t) (rand () % max_bytes_in_string); + ecma_length_t length = generate_cesu8_string (cesu8_string, cesu8_string_size); - JERRY_ASSERT (lit_utf8_string_length (utf8_string, utf8_string_size) == length); + JERRY_ASSERT (lit_utf8_string_length (cesu8_string, cesu8_string_size) == length); - lit_utf8_iterator_t iter = lit_utf8_iterator_create (utf8_string, utf8_string_size); - ecma_length_t calculated_length = 0; + lit_utf8_byte_t *curr_p = cesu8_string; + const lit_utf8_byte_t *end_p = cesu8_string + cesu8_string_size; + ecma_length_t calculated_length = 0; ecma_length_t code_units_count = 0; - while (!lit_utf8_iterator_is_eos (&iter)) + + while (curr_p < end_p) { - code_units[code_units_count] = lit_utf8_iterator_peek_next (&iter); - saved_positions[code_units_count] = lit_utf8_iterator_get_pos (&iter); + code_units[code_units_count] = lit_utf8_peek_next (curr_p); + saved_positions[code_units_count] = curr_p; code_units_count++; calculated_length++; - lit_utf8_iterator_incr (&iter); + lit_utf8_incr (&curr_p); } JERRY_ASSERT (length == calculated_length); @@ -145,67 +141,56 @@ main (int __attr_unused___ argc, for (int j = 0; j < test_subiters; j++) { ecma_length_t index = (ecma_length_t) rand () % code_units_count; - lit_utf8_iterator_seek (&iter, saved_positions[index]); - JERRY_ASSERT (lit_utf8_iterator_peek_next (&iter) == code_units[index]); - JERRY_ASSERT (lit_utf8_iterator_get_index (&iter) == index); + curr_p = saved_positions[index]; + JERRY_ASSERT (lit_utf8_peek_next (curr_p) == code_units[index]); } } - lit_utf8_iterator_seek_eos (&iter); - while (!lit_utf8_iterator_is_bos (&iter)) + curr_p = (lit_utf8_byte_t *) end_p; + while (curr_p > cesu8_string) { JERRY_ASSERT (code_units_count > 0); calculated_length--; - JERRY_ASSERT (code_units[calculated_length] == lit_utf8_iterator_peek_prev (&iter)); - lit_utf8_iterator_decr (&iter); + JERRY_ASSERT (code_units[calculated_length] == lit_utf8_peek_prev (curr_p)); + lit_utf8_decr (&curr_p); } JERRY_ASSERT (calculated_length == 0); - while (!lit_utf8_iterator_is_eos (&iter)) + while (curr_p < end_p) { - ecma_char_t code_unit = lit_utf8_iterator_read_next (&iter); + ecma_char_t code_unit = lit_utf8_read_next (&curr_p); JERRY_ASSERT (code_unit == code_units[calculated_length]); calculated_length++; } JERRY_ASSERT (length == calculated_length); - while (!lit_utf8_iterator_is_bos (&iter)) + while (curr_p > cesu8_string) { JERRY_ASSERT (code_units_count > 0); calculated_length--; - JERRY_ASSERT (code_units[calculated_length] == lit_utf8_iterator_read_prev (&iter)); + JERRY_ASSERT (code_units[calculated_length] == lit_utf8_read_prev (&curr_p)); } JERRY_ASSERT (calculated_length == 0); } /* Overlong-encoded code point */ - lit_utf8_byte_t invalid_utf8_string_1[] = {0xC0, 0x82}; - JERRY_ASSERT (!lit_is_utf8_string_valid (invalid_utf8_string_1, sizeof (invalid_utf8_string_1))); + lit_utf8_byte_t invalid_cesu8_string_1[] = {0xC0, 0x82}; + JERRY_ASSERT (!lit_is_cesu8_string_valid (invalid_cesu8_string_1, sizeof (invalid_cesu8_string_1))); /* Overlong-encoded code point */ - lit_utf8_byte_t invalid_utf8_string_2[] = {0xE0, 0x80, 0x81}; - JERRY_ASSERT (!lit_is_utf8_string_valid (invalid_utf8_string_2, sizeof (invalid_utf8_string_2))); + lit_utf8_byte_t invalid_cesu8_string_2[] = {0xE0, 0x80, 0x81}; + JERRY_ASSERT (!lit_is_cesu8_string_valid (invalid_cesu8_string_2, sizeof (invalid_cesu8_string_2))); /* Pair of surrogates: 0xD901 0xDFF0 which encode Unicode character 0x507F0 */ - lit_utf8_byte_t invalid_utf8_string_3[] = {0xED, 0xA4, 0x81, 0xED, 0xBF, 0xB0}; - JERRY_ASSERT (!lit_is_utf8_string_valid (invalid_utf8_string_3, sizeof (invalid_utf8_string_3))); + lit_utf8_byte_t invalid_cesu8_string_3[] = {0xED, 0xA4, 0x81, 0xED, 0xBF, 0xB0}; + JERRY_ASSERT (lit_is_cesu8_string_valid (invalid_cesu8_string_3, sizeof (invalid_cesu8_string_3))); /* Isolated high surrogate 0xD901 */ lit_utf8_byte_t valid_utf8_string_1[] = {0xED, 0xA4, 0x81}; - JERRY_ASSERT (lit_is_utf8_string_valid (valid_utf8_string_1, sizeof (valid_utf8_string_1))); - - /* 4-byte long utf-8 character - Unicode character 0x507F0 */ - lit_utf8_byte_t valid_utf8_string_2[] = {0xF1, 0x90, 0x9F, 0xB0}; - JERRY_ASSERT (lit_is_utf8_string_valid (valid_utf8_string_2, sizeof (valid_utf8_string_2))); - - lit_utf8_byte_t buf[] = {0xF0, 0x90, 0x8D, 0x88}; - lit_code_point_t code_point; - lit_utf8_size_t bytes_count = lit_read_code_point_from_utf8 (buf, sizeof (buf), &code_point); - JERRY_ASSERT (bytes_count == 4); - JERRY_ASSERT (code_point == 0x10348); + JERRY_ASSERT (lit_is_cesu8_string_valid (valid_utf8_string_1, sizeof (valid_utf8_string_1))); lit_utf8_byte_t res_buf[3]; lit_utf8_size_t res_size;