Skip to content

Commit 554907e

Browse files
authored
Add RegExp.escape (#687)
1 parent b5d4181 commit 554907e

File tree

6 files changed

+70
-8
lines changed

6 files changed

+70
-8
lines changed

libunicode-table.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,16 @@ static const uint8_t unicode_prop_ID_Continue1_index[66] = {
572572
0x01, 0x0e,
573573
};
574574

575+
static const uint8_t unicode_prop_White_Space_table[22] = {
576+
0x88, 0x84, 0x91, 0x80, 0xe3, 0x80, 0x99, 0x80,
577+
0x55, 0xde, 0x80, 0x49, 0x7e, 0x8a, 0x9c, 0x0c,
578+
0x80, 0xae, 0x80, 0x4f, 0x9f, 0x80,
579+
};
580+
581+
static const uint8_t unicode_prop_White_Space_index[3] = {
582+
0x01, 0x30, 0x00,
583+
};
584+
575585
static const uint8_t unicode_cc_table[916] = {
576586
0xb2, 0xcf, 0xd4, 0x00, 0xe8, 0x03, 0xdc, 0x00,
577587
0xe8, 0x00, 0xd8, 0x04, 0xdc, 0x01, 0xca, 0x03,
@@ -4262,12 +4272,6 @@ static const uint8_t unicode_prop_Variation_Selector_table[13] = {
42624272
0x6d, 0x02, 0xef, 0x40, 0xef,
42634273
};
42644274

4265-
static const uint8_t unicode_prop_White_Space_table[22] = {
4266-
0x88, 0x84, 0x91, 0x80, 0xe3, 0x80, 0x99, 0x80,
4267-
0x55, 0xde, 0x80, 0x49, 0x7e, 0x8a, 0x9c, 0x0c,
4268-
0x80, 0xae, 0x80, 0x4f, 0x9f, 0x80,
4269-
};
4270-
42714275
static const uint8_t unicode_prop_Bidi_Mirrored_table[173] = {
42724276
0xa7, 0x81, 0x91, 0x00, 0x80, 0x9b, 0x00, 0x80,
42734277
0x9c, 0x00, 0x80, 0xac, 0x80, 0x8e, 0x80, 0x4e,

libunicode.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ BOOL lre_is_id_continue(uint32_t c)
545545
sizeof(unicode_prop_ID_Continue1_index) / 3);
546546
}
547547

548+
BOOL lre_is_white_space(uint32_t c)
549+
{
550+
return lre_is_in_table(c, unicode_prop_White_Space_table,
551+
unicode_prop_White_Space_index,
552+
sizeof(unicode_prop_White_Space_index) / 3);
553+
}
554+
548555
#define UNICODE_DECOMP_LEN_MAX 18
549556

550557
typedef enum {

libunicode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ int cr_regexp_canonicalize(CharRange *cr, BOOL is_unicode);
107107

108108
LRE_BOOL lre_is_id_start(uint32_t c);
109109
LRE_BOOL lre_is_id_continue(uint32_t c);
110+
LRE_BOOL lre_is_white_space(uint32_t c);
110111

111112
int unicode_normalize(uint32_t **pdst, const uint32_t *src, int src_len,
112113
UnicodeNormalizationEnum n_type,

quickjs.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43836,6 +43836,53 @@ void *lre_realloc(void *opaque, void *ptr, size_t size)
4383643836
return js_realloc_rt(ctx->rt, ptr, size);
4383743837
}
4383843838

43839+
static JSValue js_regexp_escape(JSContext *ctx, JSValue this_val,
43840+
int argc, JSValue *argv)
43841+
{
43842+
StringBuffer b_s, *b = &b_s;
43843+
JSString *p;
43844+
uint32_t c, i;
43845+
char s[16];
43846+
43847+
if (!JS_IsString(argv[0]))
43848+
return JS_ThrowTypeError(ctx, "not a string");
43849+
p = JS_VALUE_GET_STRING(argv[0]);
43850+
string_buffer_init2(ctx, b, 0, p->is_wide_char);
43851+
for (i = 0; i < p->len; i++) {
43852+
c = p->is_wide_char ? (uint32_t)p->u.str16[i] : (uint32_t)p->u.str8[i];
43853+
if (c < 33) {
43854+
if (c >= 9 && c <= 13) {
43855+
string_buffer_putc8(b, '\\');
43856+
string_buffer_putc8(b, "tnvfr"[c - 9]);
43857+
} else {
43858+
goto hex2;
43859+
}
43860+
} else if (c < 128) {
43861+
if ((c >= '0' && c <= '9')
43862+
|| (c >= 'A' && c <= 'Z')
43863+
|| (c >= 'a' && c <= 'z')) {
43864+
if (i == 0)
43865+
goto hex2;
43866+
} else if (strchr(",-=<>#&!%:;@~'`\"", c)) {
43867+
goto hex2;
43868+
} else if (c != '_') {
43869+
string_buffer_putc8(b, '\\');
43870+
}
43871+
string_buffer_putc8(b, c);
43872+
} else if (c < 256) {
43873+
hex2:
43874+
snprintf(s, sizeof(s), "\\x%02x", c);
43875+
string_buffer_puts8(b, s);
43876+
} else if (is_surrogate(c) || lre_is_white_space(c) || c == 0xFEFF) {
43877+
snprintf(s, sizeof(s), "\\u%04x", c);
43878+
string_buffer_puts8(b, s);
43879+
} else {
43880+
string_buffer_putc16(b, c);
43881+
}
43882+
}
43883+
return string_buffer_end(b);
43884+
}
43885+
4383943886
static JSValue js_regexp_exec(JSContext *ctx, JSValue this_val,
4384043887
int argc, JSValue *argv)
4384143888
{
@@ -44864,6 +44911,7 @@ static JSValue js_regexp_Symbol_split(JSContext *ctx, JSValue this_val,
4486444911
}
4486544912

4486644913
static const JSCFunctionListEntry js_regexp_funcs[] = {
44914+
JS_CFUNC_DEF("escape", 1, js_regexp_escape ),
4486744915
JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL ),
4486844916
};
4486944917

test262.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ regexp-modifiers=skip
174174
regexp-named-groups
175175
regexp-unicode-property-escapes
176176
regexp-v-flag
177-
RegExp.escape=skip
177+
RegExp.escape
178178
resizable-arraybuffer
179179
rest-parameters
180180
Set

unicode_gen.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,7 @@ void build_flags_tables(FILE *f)
15741574
build_prop_table(f, PROP_Case_Ignorable, TRUE);
15751575
build_prop_table(f, PROP_ID_Start, TRUE);
15761576
build_prop_table(f, PROP_ID_Continue1, TRUE);
1577+
build_prop_table(f, PROP_White_Space, TRUE);
15771578
}
15781579

15791580
void dump_name_table(FILE *f, const char *cname, const char **tab_name, int len,
@@ -1813,7 +1814,8 @@ void build_prop_list_table(FILE *f)
18131814
for(i = 0; i < PROP_TABLE_COUNT; i++) {
18141815
if (i == PROP_ID_Start ||
18151816
i == PROP_Case_Ignorable ||
1816-
i == PROP_ID_Continue1) {
1817+
i == PROP_ID_Continue1 ||
1818+
i == PROP_White_Space) {
18171819
/* already generated */
18181820
} else {
18191821
build_prop_table(f, i, FALSE);

0 commit comments

Comments
 (0)