From 7b3042fdc9cfd9f9196d8d13af9b1bf339928dab Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Fri, 3 Jul 2015 02:17:14 +0300 Subject: [PATCH] Remove usage of isalpha, isdigit, isxdigit, isspace in the whole engine except implementation of JSON built-in, moving the functions to JSON built-in's module. JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com --- .../builtin-objects/ecma-builtin-global.cpp | 20 ++++--- .../builtin-objects/ecma-builtin-json.cpp | 21 +++++++ .../ecma-builtin-string-prototype.cpp | 33 ++++++++-- jerry-core/parser/regexp/re-parser.cpp | 25 ++++---- jerry-libc/include/ctype.h | 7 --- jerry-libc/jerry-libc.c | 60 ------------------- 6 files changed, 72 insertions(+), 94 deletions(-) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp index bedfec337..37baec4cb 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp @@ -126,7 +126,8 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /* ecma_length_t end = str_size; for (ecma_length_t i = 0; i < end; i++) { - if (!(isspace (utf8_string_buff[i]))) + if (!lit_char_is_white_space (utf8_string_buff[i]) + && !lit_char_is_line_terminator (utf8_string_buff[i])) { start = i; break; @@ -203,7 +204,7 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /* { utf8_string_buff[i] = (lit_utf8_byte_t) (utf8_string_buff[i] - 'A' + 10); } - else if (isdigit (utf8_string_buff[i])) + else if (lit_char_is_decimal_digit (utf8_string_buff[i])) { utf8_string_buff[i] = (lit_utf8_byte_t) (utf8_string_buff[i] - '0'); } @@ -290,7 +291,8 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___, lit_utf8_size_t start = 0; for (lit_utf8_size_t i = 0; i < str_size; i++) { - if (!isspace (utf8_string_buff[i])) + if (!lit_char_is_white_space (utf8_string_buff[i]) + && !lit_char_is_line_terminator (utf8_string_buff[i])) { start = i; break; @@ -332,14 +334,14 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___, bool has_whole_part = false; bool has_fraction_part = false; - if (isdigit (utf8_string_buff[current])) + if (lit_char_is_decimal_digit (utf8_string_buff[current])) { has_whole_part = true; /* Check digits of whole part. */ for (lit_utf8_size_t i = current; i < str_size; i++, current++) { - if (!isdigit (utf8_string_buff[current])) + if (!lit_char_is_decimal_digit (utf8_string_buff[current])) { break; } @@ -353,14 +355,14 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___, { current++; - if (isdigit (utf8_string_buff[current])) + if (lit_char_is_decimal_digit (utf8_string_buff[current])) { has_fraction_part = true; /* Check digits of fractional part. */ for (lit_utf8_size_t i = current; i < str_size; i++, current++) { - if (!isdigit (utf8_string_buff[current])) + if (!lit_char_is_decimal_digit (utf8_string_buff[current])) { break; } @@ -382,13 +384,13 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___, current++; } - if (isdigit (utf8_string_buff[current])) + if (lit_char_is_decimal_digit (utf8_string_buff[current])) { /* Check digits of exponent part. */ for (lit_utf8_size_t i = current; i < str_size; i++, current++) { - if (!isdigit (utf8_string_buff[current])) + if (!lit_char_is_decimal_digit (utf8_string_buff[current])) { break; } diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-json.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-json.cpp index 743ed5687..713d234d6 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-json.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-json.cpp @@ -39,6 +39,27 @@ #define BUILTIN_UNDERSCORED_ID json #include "ecma-builtin-internal-routines-template.inc.h" +/* + * FIXME: + * Replace usage of isalpha and isdigit functions in the module with lit_char helpers and remove the functions. + * + * Related issue: #424 + */ + +/** Checks for an alphabetic character. */ +static int +isalpha (int c) +{ + return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); +} + +/** Checks for a digit (0 through 9). */ +static int +isdigit (int c) +{ + return c >= '0' && c <= '9'; +} + /** \addtogroup ecma ECMA * @{ * 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 17e592263..d39866b42 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp @@ -805,16 +805,37 @@ ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argu uint32_t prefix = 0, postfix = 0; uint32_t new_len = 0; - while (prefix < length && isspace (lit_utf8_string_code_unit_at (original_utf8_str_p, size, prefix))) + while (prefix < length) { - prefix++; + ecma_char_t next_char = lit_utf8_string_code_unit_at (original_utf8_str_p, + size, + prefix); + + if (lit_char_is_white_space (next_char) + || lit_char_is_line_terminator (next_char)) + { + prefix++; + } + else + { + break; + } } - while (postfix < length - prefix && isspace (lit_utf8_string_code_unit_at (original_utf8_str_p, - size, - length - postfix - 1))) + while (postfix < length - prefix) { - postfix++; + ecma_char_t next_char = lit_utf8_string_code_unit_at (original_utf8_str_p, + size, + length - postfix - 1); + if (lit_char_is_white_space (next_char) + || lit_char_is_line_terminator (next_char)) + { + postfix++; + } + else + { + break; + } } new_len = prefix < size ? size - prefix - postfix : 0; diff --git a/jerry-core/parser/regexp/re-parser.cpp b/jerry-core/parser/regexp/re-parser.cpp index 31e24615d..722a6f0da 100644 --- a/jerry-core/parser/regexp/re-parser.cpp +++ b/jerry-core/parser/regexp/re-parser.cpp @@ -126,7 +126,7 @@ re_parse_iterator (lit_utf8_byte_t *pattern_p, /**< RegExp pattern */ (*advance_p)++; ch1 = RE_LOOKUP (pattern_p, lookup + *advance_p); - if (isdigit (ch1)) + if (lit_char_is_decimal_digit (ch1)) { if (digits >= ECMA_NUMBER_MAX_DIGITS) { @@ -423,9 +423,10 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */ append_char_class (re_ctx_p, 0x007BUL, 0xFFFFUL); ch = RE_CHAR_UNDEF; } - else if (isdigit (ch)) + else if (lit_char_is_decimal_digit (ch)) { - if (ch != '\0' || isdigit (RE_LOOKUP (*pattern_p, 1))) + if (ch != LIT_CHAR_0 + || lit_char_is_decimal_digit (RE_LOOKUP (*pattern_p, 1))) { /* FIXME: octal support */ } @@ -597,8 +598,8 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * } } else if (ch1 == 'x' - && isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 2)) - && isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 3))) + && lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 2)) + && lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 3))) { advance = 4; out_token_p->type = RE_TOK_CHAR; @@ -606,10 +607,10 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * /* result.value = ...; */ } else if (ch1 == 'u' - && isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 2)) - && isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 3)) - && isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 4)) - && isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 5))) + && lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 2)) + && lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 3)) + && lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 4)) + && lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 5))) { advance = 4; out_token_p->type = RE_TOK_CHAR; @@ -646,11 +647,11 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * advance = 2; out_token_p->type = RE_TOK_NOT_WORD_CHAR; } - else if (isdigit (ch1)) + else if (lit_char_is_decimal_digit (ch1)) { if (ch1 == '0') { - if (isdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 2))) + if (lit_char_is_decimal_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 2))) { ret_value = ecma_raise_syntax_error ("RegExp escape pattern error."); break; @@ -683,7 +684,7 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context * advance++; ecma_char_t digit = RE_LOOKUP (parser_ctx_p->current_char_p, advance); - if (!isdigit (digit)) + if (!lit_char_is_decimal_digit (digit)) { break; } diff --git a/jerry-libc/include/ctype.h b/jerry-libc/include/ctype.h index c7d196b89..749908998 100644 --- a/jerry-libc/include/ctype.h +++ b/jerry-libc/include/ctype.h @@ -22,11 +22,4 @@ # define EXTERN_C #endif /* !__cplusplus */ -extern EXTERN_C int isxdigit (int c); -extern EXTERN_C int isalpha (int c); -extern EXTERN_C int isdigit (int c); -extern EXTERN_C int islower (int c); -extern EXTERN_C int isspace (int c); -extern EXTERN_C int isupper (int c); - #endif /* !JERRY_LIBC_CTYPE_H */ diff --git a/jerry-libc/jerry-libc.c b/jerry-libc/jerry-libc.c index 16ccd3201..4db3063ca 100644 --- a/jerry-libc/jerry-libc.c +++ b/jerry-libc/jerry-libc.c @@ -272,66 +272,6 @@ strlen (const char *s) return i; } -/** Checks for white-space characters. In the "C" and "POSIX" locales, these are: space, - form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). */ -int -isspace (int c) -{ - switch (c) - { - case ' ': - case '\f': - case '\n': - case '\r': - case '\t': - case '\v': - { - return 1; - } - default: - { - return 0; - } - } -} - -/** Checks for an uppercase letter. */ -int -isupper (int c) -{ - return c >= 'A' && c <= 'Z'; -} - -/** Checks for an lowercase letter. */ -int -islower (int c) -{ - return c >= 'a' && c <= 'z'; -} - -/** Checks for an alphabetic character. - In the standard "C" locale, it is equivalent to (isupper (c) || islower (c)). */ -int -isalpha (int c) -{ - return isupper (c) || islower (c); -} - -/** Checks for a digit (0 through 9). */ -int -isdigit (int c) -{ - return c >= '0' && c <= '9'; -} - -/** checks for a hexadecimal digits, that is, one of - 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F. */ -int -isxdigit (int c) -{ - return isdigit (c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); -} - /** * Generate pseudo-random integer *