Skip to content

Commit 7b3042f

Browse files
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 [email protected]
1 parent bcedc90 commit 7b3042f

File tree

6 files changed

+72
-94
lines changed

6 files changed

+72
-94
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
126126
ecma_length_t end = str_size;
127127
for (ecma_length_t i = 0; i < end; i++)
128128
{
129-
if (!(isspace (utf8_string_buff[i])))
129+
if (!lit_char_is_white_space (utf8_string_buff[i])
130+
&& !lit_char_is_line_terminator (utf8_string_buff[i]))
130131
{
131132
start = i;
132133
break;
@@ -203,7 +204,7 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
203204
{
204205
utf8_string_buff[i] = (lit_utf8_byte_t) (utf8_string_buff[i] - 'A' + 10);
205206
}
206-
else if (isdigit (utf8_string_buff[i]))
207+
else if (lit_char_is_decimal_digit (utf8_string_buff[i]))
207208
{
208209
utf8_string_buff[i] = (lit_utf8_byte_t) (utf8_string_buff[i] - '0');
209210
}
@@ -290,7 +291,8 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
290291
lit_utf8_size_t start = 0;
291292
for (lit_utf8_size_t i = 0; i < str_size; i++)
292293
{
293-
if (!isspace (utf8_string_buff[i]))
294+
if (!lit_char_is_white_space (utf8_string_buff[i])
295+
&& !lit_char_is_line_terminator (utf8_string_buff[i]))
294296
{
295297
start = i;
296298
break;
@@ -332,14 +334,14 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
332334
bool has_whole_part = false;
333335
bool has_fraction_part = false;
334336

335-
if (isdigit (utf8_string_buff[current]))
337+
if (lit_char_is_decimal_digit (utf8_string_buff[current]))
336338
{
337339
has_whole_part = true;
338340

339341
/* Check digits of whole part. */
340342
for (lit_utf8_size_t i = current; i < str_size; i++, current++)
341343
{
342-
if (!isdigit (utf8_string_buff[current]))
344+
if (!lit_char_is_decimal_digit (utf8_string_buff[current]))
343345
{
344346
break;
345347
}
@@ -353,14 +355,14 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
353355
{
354356
current++;
355357

356-
if (isdigit (utf8_string_buff[current]))
358+
if (lit_char_is_decimal_digit (utf8_string_buff[current]))
357359
{
358360
has_fraction_part = true;
359361

360362
/* Check digits of fractional part. */
361363
for (lit_utf8_size_t i = current; i < str_size; i++, current++)
362364
{
363-
if (!isdigit (utf8_string_buff[current]))
365+
if (!lit_char_is_decimal_digit (utf8_string_buff[current]))
364366
{
365367
break;
366368
}
@@ -382,13 +384,13 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
382384
current++;
383385
}
384386

385-
if (isdigit (utf8_string_buff[current]))
387+
if (lit_char_is_decimal_digit (utf8_string_buff[current]))
386388
{
387389

388390
/* Check digits of exponent part. */
389391
for (lit_utf8_size_t i = current; i < str_size; i++, current++)
390392
{
391-
if (!isdigit (utf8_string_buff[current]))
393+
if (!lit_char_is_decimal_digit (utf8_string_buff[current]))
392394
{
393395
break;
394396
}

jerry-core/ecma/builtin-objects/ecma-builtin-json.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@
3939
#define BUILTIN_UNDERSCORED_ID json
4040
#include "ecma-builtin-internal-routines-template.inc.h"
4141

42+
/*
43+
* FIXME:
44+
* Replace usage of isalpha and isdigit functions in the module with lit_char helpers and remove the functions.
45+
*
46+
* Related issue: #424
47+
*/
48+
49+
/** Checks for an alphabetic character. */
50+
static int
51+
isalpha (int c)
52+
{
53+
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
54+
}
55+
56+
/** Checks for a digit (0 through 9). */
57+
static int
58+
isdigit (int c)
59+
{
60+
return c >= '0' && c <= '9';
61+
}
62+
4263
/** \addtogroup ecma ECMA
4364
* @{
4465
*

jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -805,16 +805,37 @@ ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argu
805805
uint32_t prefix = 0, postfix = 0;
806806
uint32_t new_len = 0;
807807

808-
while (prefix < length && isspace (lit_utf8_string_code_unit_at (original_utf8_str_p, size, prefix)))
808+
while (prefix < length)
809809
{
810-
prefix++;
810+
ecma_char_t next_char = lit_utf8_string_code_unit_at (original_utf8_str_p,
811+
size,
812+
prefix);
813+
814+
if (lit_char_is_white_space (next_char)
815+
|| lit_char_is_line_terminator (next_char))
816+
{
817+
prefix++;
818+
}
819+
else
820+
{
821+
break;
822+
}
811823
}
812824

813-
while (postfix < length - prefix && isspace (lit_utf8_string_code_unit_at (original_utf8_str_p,
814-
size,
815-
length - postfix - 1)))
825+
while (postfix < length - prefix)
816826
{
817-
postfix++;
827+
ecma_char_t next_char = lit_utf8_string_code_unit_at (original_utf8_str_p,
828+
size,
829+
length - postfix - 1);
830+
if (lit_char_is_white_space (next_char)
831+
|| lit_char_is_line_terminator (next_char))
832+
{
833+
postfix++;
834+
}
835+
else
836+
{
837+
break;
838+
}
818839
}
819840

820841
new_len = prefix < size ? size - prefix - postfix : 0;

jerry-core/parser/regexp/re-parser.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ re_parse_iterator (lit_utf8_byte_t *pattern_p, /**< RegExp pattern */
126126
(*advance_p)++;
127127
ch1 = RE_LOOKUP (pattern_p, lookup + *advance_p);
128128

129-
if (isdigit (ch1))
129+
if (lit_char_is_decimal_digit (ch1))
130130
{
131131
if (digits >= ECMA_NUMBER_MAX_DIGITS)
132132
{
@@ -423,9 +423,10 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */
423423
append_char_class (re_ctx_p, 0x007BUL, 0xFFFFUL);
424424
ch = RE_CHAR_UNDEF;
425425
}
426-
else if (isdigit (ch))
426+
else if (lit_char_is_decimal_digit (ch))
427427
{
428-
if (ch != '\0' || isdigit (RE_LOOKUP (*pattern_p, 1)))
428+
if (ch != LIT_CHAR_0
429+
|| lit_char_is_decimal_digit (RE_LOOKUP (*pattern_p, 1)))
429430
{
430431
/* FIXME: octal support */
431432
}
@@ -597,19 +598,19 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context *
597598
}
598599
}
599600
else if (ch1 == 'x'
600-
&& isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 2))
601-
&& isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 3)))
601+
&& lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 2))
602+
&& lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 3)))
602603
{
603604
advance = 4;
604605
out_token_p->type = RE_TOK_CHAR;
605606
/* FIXME: get unicode char from hex-digits */
606607
/* result.value = ...; */
607608
}
608609
else if (ch1 == 'u'
609-
&& isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 2))
610-
&& isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 3))
611-
&& isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 4))
612-
&& isxdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 5)))
610+
&& lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 2))
611+
&& lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 3))
612+
&& lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 4))
613+
&& lit_char_is_hex_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 5)))
613614
{
614615
advance = 4;
615616
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 *
646647
advance = 2;
647648
out_token_p->type = RE_TOK_NOT_WORD_CHAR;
648649
}
649-
else if (isdigit (ch1))
650+
else if (lit_char_is_decimal_digit (ch1))
650651
{
651652
if (ch1 == '0')
652653
{
653-
if (isdigit (RE_LOOKUP (parser_ctx_p->current_char_p, 2)))
654+
if (lit_char_is_decimal_digit (RE_LOOKUP (parser_ctx_p->current_char_p, 2)))
654655
{
655656
ret_value = ecma_raise_syntax_error ("RegExp escape pattern error.");
656657
break;
@@ -683,7 +684,7 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context *
683684
advance++;
684685
ecma_char_t digit = RE_LOOKUP (parser_ctx_p->current_char_p,
685686
advance);
686-
if (!isdigit (digit))
687+
if (!lit_char_is_decimal_digit (digit))
687688
{
688689
break;
689690
}

jerry-libc/include/ctype.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,4 @@
2222
# define EXTERN_C
2323
#endif /* !__cplusplus */
2424

25-
extern EXTERN_C int isxdigit (int c);
26-
extern EXTERN_C int isalpha (int c);
27-
extern EXTERN_C int isdigit (int c);
28-
extern EXTERN_C int islower (int c);
29-
extern EXTERN_C int isspace (int c);
30-
extern EXTERN_C int isupper (int c);
31-
3225
#endif /* !JERRY_LIBC_CTYPE_H */

jerry-libc/jerry-libc.c

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -272,66 +272,6 @@ strlen (const char *s)
272272
return i;
273273
}
274274

275-
/** Checks for white-space characters. In the "C" and "POSIX" locales, these are: space,
276-
form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). */
277-
int
278-
isspace (int c)
279-
{
280-
switch (c)
281-
{
282-
case ' ':
283-
case '\f':
284-
case '\n':
285-
case '\r':
286-
case '\t':
287-
case '\v':
288-
{
289-
return 1;
290-
}
291-
default:
292-
{
293-
return 0;
294-
}
295-
}
296-
}
297-
298-
/** Checks for an uppercase letter. */
299-
int
300-
isupper (int c)
301-
{
302-
return c >= 'A' && c <= 'Z';
303-
}
304-
305-
/** Checks for an lowercase letter. */
306-
int
307-
islower (int c)
308-
{
309-
return c >= 'a' && c <= 'z';
310-
}
311-
312-
/** Checks for an alphabetic character.
313-
In the standard "C" locale, it is equivalent to (isupper (c) || islower (c)). */
314-
int
315-
isalpha (int c)
316-
{
317-
return isupper (c) || islower (c);
318-
}
319-
320-
/** Checks for a digit (0 through 9). */
321-
int
322-
isdigit (int c)
323-
{
324-
return c >= '0' && c <= '9';
325-
}
326-
327-
/** checks for a hexadecimal digits, that is, one of
328-
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F. */
329-
int
330-
isxdigit (int c)
331-
{
332-
return isdigit (c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
333-
}
334-
335275
/**
336276
* Generate pseudo-random integer
337277
*

0 commit comments

Comments
 (0)