Implement TrimStart TrimEnd and aliases (#4102)
Based on: https://tc39.es/ecma262/#sec-string.prototype.trim JerryScript-DCO-1.0-Signed-off-by: Bela Toth tbela@inf.u-szeged.hu
This commit is contained in:
@@ -729,13 +729,11 @@ ecma_number_parse_int (const lit_utf8_byte_t *string_buff, /**< routine's first
|
||||
return ecma_make_nan_value ();
|
||||
}
|
||||
|
||||
const lit_utf8_byte_t *string_curr_p = string_buff;
|
||||
/* 2. Remove leading whitespace. */
|
||||
|
||||
/* 2. Remove leading whitespace. */
|
||||
ecma_string_trim_helper (&string_curr_p, &string_buff_size);
|
||||
|
||||
const lit_utf8_byte_t *string_end_p = string_curr_p + string_buff_size;
|
||||
const lit_utf8_byte_t *start_p = string_curr_p;
|
||||
const lit_utf8_byte_t *string_end_p = string_buff + string_buff_size;
|
||||
const lit_utf8_byte_t *start_p = ecma_string_trim_front (string_buff, string_end_p);
|
||||
const lit_utf8_byte_t *string_curr_p = start_p;
|
||||
const lit_utf8_byte_t *end_p = string_end_p;
|
||||
|
||||
if (string_curr_p >= string_end_p)
|
||||
@@ -906,13 +904,11 @@ ecma_number_parse_float (const lit_utf8_byte_t *string_buff, /**< routine's firs
|
||||
return ecma_make_nan_value ();
|
||||
}
|
||||
|
||||
const lit_utf8_byte_t *str_curr_p = string_buff;
|
||||
|
||||
/* 2. Remove leading whitespace. */
|
||||
ecma_string_trim_helper (&str_curr_p, &string_buff_size);
|
||||
|
||||
const lit_utf8_byte_t *str_end_p = str_curr_p + string_buff_size;
|
||||
const lit_utf8_byte_t *start_p = str_curr_p;
|
||||
const lit_utf8_byte_t *str_end_p = string_buff + string_buff_size;
|
||||
const lit_utf8_byte_t *start_p = ecma_string_trim_front (string_buff, str_end_p);
|
||||
const lit_utf8_byte_t *str_curr_p = start_p;
|
||||
const lit_utf8_byte_t *end_p = str_end_p;
|
||||
|
||||
bool sign = false;
|
||||
|
||||
@@ -2426,49 +2426,81 @@ ecma_string_substr (const ecma_string_t *string_p, /**< pointer to an ecma strin
|
||||
* Helper function for trimming.
|
||||
*
|
||||
* Used by:
|
||||
* - ecma_string_trim
|
||||
* - ecma_utf8_string_to_number
|
||||
* - ecma_string_trim_helper
|
||||
* - ecma_builtin_global_object_parse_int
|
||||
* - ecma_builtin_global_object_parse_float
|
||||
*
|
||||
* @return position of the first non whitespace character.
|
||||
*/
|
||||
void
|
||||
ecma_string_trim_helper (const lit_utf8_byte_t **utf8_str_p, /**< [in, out] current string position */
|
||||
lit_utf8_size_t *utf8_str_size) /**< [in, out] size of the given string */
|
||||
const lit_utf8_byte_t *
|
||||
ecma_string_trim_front (const lit_utf8_byte_t *start_p, /**< current string's start position */
|
||||
const lit_utf8_byte_t *end_p) /**< current string's end position */
|
||||
{
|
||||
ecma_char_t ch;
|
||||
lit_utf8_size_t read_size;
|
||||
const lit_utf8_byte_t *nonws_start_p = *utf8_str_p + *utf8_str_size;
|
||||
const lit_utf8_byte_t *current_p = *utf8_str_p;
|
||||
|
||||
while (current_p < nonws_start_p)
|
||||
while (start_p < end_p)
|
||||
{
|
||||
read_size = lit_read_code_unit_from_utf8 (current_p, &ch);
|
||||
|
||||
if (!lit_char_is_white_space (ch))
|
||||
{
|
||||
nonws_start_p = current_p;
|
||||
break;
|
||||
}
|
||||
|
||||
current_p += read_size;
|
||||
}
|
||||
|
||||
current_p = *utf8_str_p + *utf8_str_size;
|
||||
|
||||
while (current_p > nonws_start_p)
|
||||
{
|
||||
read_size = lit_read_prev_code_unit_from_utf8 (current_p, &ch);
|
||||
lit_utf8_size_t read_size = lit_read_code_unit_from_utf8 (start_p, &ch);
|
||||
|
||||
if (!lit_char_is_white_space (ch))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
current_p -= read_size;
|
||||
start_p += read_size;
|
||||
}
|
||||
|
||||
*utf8_str_p = nonws_start_p;
|
||||
*utf8_str_size = (lit_utf8_size_t) (current_p - nonws_start_p);
|
||||
return start_p;
|
||||
} /* ecma_string_trim_front */
|
||||
|
||||
/**
|
||||
* Helper function for trimming.
|
||||
*
|
||||
* Used by:
|
||||
* - ecma_string_trim_helper
|
||||
*
|
||||
* @return position of the last non whitespace character.
|
||||
*/
|
||||
const lit_utf8_byte_t *
|
||||
ecma_string_trim_back (const lit_utf8_byte_t *start_p, /**< current string's start position */
|
||||
const lit_utf8_byte_t *end_p) /**< current string's end position */
|
||||
{
|
||||
ecma_char_t ch;
|
||||
|
||||
while (end_p > start_p)
|
||||
{
|
||||
lit_utf8_size_t read_size = lit_read_prev_code_unit_from_utf8 (end_p, &ch);
|
||||
|
||||
if (!lit_char_is_white_space (ch))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
end_p -= read_size;
|
||||
}
|
||||
|
||||
return end_p;
|
||||
} /* ecma_string_trim_back */
|
||||
|
||||
/**
|
||||
* Helper function for trimming.
|
||||
*
|
||||
* Used by:
|
||||
* - ecma_string_trim
|
||||
* - ecma_utf8_string_to_number
|
||||
*/
|
||||
inline void JERRY_ATTR_ALWAYS_INLINE
|
||||
ecma_string_trim_helper (const lit_utf8_byte_t **utf8_str_p, /**< [in, out] current string position */
|
||||
lit_utf8_size_t *utf8_str_size) /**< [in, out] size of the given string */
|
||||
{
|
||||
const lit_utf8_byte_t *end_p = *utf8_str_p + *utf8_str_size;
|
||||
const lit_utf8_byte_t *start_p = *utf8_str_p;
|
||||
|
||||
const lit_utf8_byte_t *new_start_p = ecma_string_trim_front (start_p, end_p);
|
||||
const lit_utf8_byte_t *new_end_p = ecma_string_trim_back (new_start_p, end_p);
|
||||
|
||||
*utf8_str_size = (lit_utf8_size_t) (new_end_p - new_start_p);
|
||||
*utf8_str_p = new_start_p;
|
||||
} /* ecma_string_trim_helper */
|
||||
|
||||
/**
|
||||
|
||||
@@ -397,6 +397,8 @@ lit_magic_string_id_t ecma_get_string_magic (const ecma_string_t *string_p);
|
||||
|
||||
lit_string_hash_t ecma_string_hash (const ecma_string_t *string_p);
|
||||
ecma_string_t *ecma_string_substr (const ecma_string_t *string_p, lit_utf8_size_t start_pos, lit_utf8_size_t end_pos);
|
||||
const lit_utf8_byte_t *ecma_string_trim_front (const lit_utf8_byte_t *start_p, const lit_utf8_byte_t *end_p);
|
||||
const lit_utf8_byte_t *ecma_string_trim_back (const lit_utf8_byte_t *start_p, const lit_utf8_byte_t *end_p);
|
||||
void ecma_string_trim_helper (const lit_utf8_byte_t **utf8_str_p,
|
||||
lit_utf8_size_t *utf8_str_size);
|
||||
ecma_string_t *ecma_string_trim (const ecma_string_t *string_p);
|
||||
|
||||
Reference in New Issue
Block a user