mirror of
https://github.com/ccxvii/mujs.git
synced 2026-02-06 01:41:37 +08:00
Bug 703461: Escape '/' characters in regular expression source.
This commit is contained in:
7
jsdump.c
7
jsdump.c
@@ -286,7 +286,12 @@ static void pstr(const char *s)
|
|||||||
static void pregexp(const char *prog, int flags)
|
static void pregexp(const char *prog, int flags)
|
||||||
{
|
{
|
||||||
pc('/');
|
pc('/');
|
||||||
ps(prog);
|
while (*prog) {
|
||||||
|
if (*prog == '/')
|
||||||
|
pc('\\');
|
||||||
|
pc(*prog);
|
||||||
|
++prog;
|
||||||
|
}
|
||||||
pc('/');
|
pc('/');
|
||||||
if (flags & JS_REGEXP_G) pc('g');
|
if (flags & JS_REGEXP_G) pc('g');
|
||||||
if (flags & JS_REGEXP_I) pc('i');
|
if (flags & JS_REGEXP_I) pc('i');
|
||||||
|
|||||||
32
jsregexp.c
32
jsregexp.c
@@ -3,7 +3,26 @@
|
|||||||
#include "jsbuiltin.h"
|
#include "jsbuiltin.h"
|
||||||
#include "regexp.h"
|
#include "regexp.h"
|
||||||
|
|
||||||
void js_newregexp(js_State *J, const char *pattern, int flags)
|
static char *escaperegexp(js_State *J, const char *pattern) {
|
||||||
|
char *copy, *p;
|
||||||
|
const char *s;
|
||||||
|
int n = 0;
|
||||||
|
for (s = pattern; *s; ++s) {
|
||||||
|
if (*s == '/')
|
||||||
|
++n;
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
copy = p = js_malloc(J, n+1);
|
||||||
|
for (s = pattern; *s; ++s) {
|
||||||
|
if (*s == '/')
|
||||||
|
*p++ = '\\';
|
||||||
|
*p++ = *s;
|
||||||
|
}
|
||||||
|
*p = 0;
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void js_newregexpx(js_State *J, const char *pattern, int flags, int is_clone)
|
||||||
{
|
{
|
||||||
const char *error;
|
const char *error;
|
||||||
js_Object *obj;
|
js_Object *obj;
|
||||||
@@ -21,12 +40,17 @@ void js_newregexp(js_State *J, const char *pattern, int flags)
|
|||||||
js_syntaxerror(J, "regular expression: %s", error);
|
js_syntaxerror(J, "regular expression: %s", error);
|
||||||
|
|
||||||
obj->u.r.prog = prog;
|
obj->u.r.prog = prog;
|
||||||
obj->u.r.source = js_strdup(J, pattern);
|
obj->u.r.source = is_clone ? js_strdup(J, pattern) : escaperegexp(J, pattern);
|
||||||
obj->u.r.flags = flags;
|
obj->u.r.flags = flags;
|
||||||
obj->u.r.last = 0;
|
obj->u.r.last = 0;
|
||||||
js_pushobject(J, obj);
|
js_pushobject(J, obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void js_newregexp(js_State *J, const char *pattern, int flags)
|
||||||
|
{
|
||||||
|
js_newregexpx(J, pattern, flags, 0);
|
||||||
|
}
|
||||||
|
|
||||||
void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text)
|
void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
@@ -116,6 +140,7 @@ static void jsB_new_RegExp(js_State *J)
|
|||||||
js_Regexp *old;
|
js_Regexp *old;
|
||||||
const char *pattern;
|
const char *pattern;
|
||||||
int flags;
|
int flags;
|
||||||
|
int is_clone = 0;
|
||||||
|
|
||||||
if (js_isregexp(J, 1)) {
|
if (js_isregexp(J, 1)) {
|
||||||
if (js_isdefined(J, 2))
|
if (js_isdefined(J, 2))
|
||||||
@@ -123,6 +148,7 @@ static void jsB_new_RegExp(js_State *J)
|
|||||||
old = js_toregexp(J, 1);
|
old = js_toregexp(J, 1);
|
||||||
pattern = old->source;
|
pattern = old->source;
|
||||||
flags = old->flags;
|
flags = old->flags;
|
||||||
|
is_clone = 1;
|
||||||
} else if (js_isundefined(J, 1)) {
|
} else if (js_isundefined(J, 1)) {
|
||||||
pattern = "(?:)";
|
pattern = "(?:)";
|
||||||
flags = 0;
|
flags = 0;
|
||||||
@@ -152,7 +178,7 @@ static void jsB_new_RegExp(js_State *J)
|
|||||||
if (m) flags |= JS_REGEXP_M;
|
if (m) flags |= JS_REGEXP_M;
|
||||||
}
|
}
|
||||||
|
|
||||||
js_newregexp(J, pattern, flags);
|
js_newregexpx(J, pattern, flags, is_clone);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void jsB_RegExp(js_State *J)
|
static void jsB_RegExp(js_State *J)
|
||||||
|
|||||||
Reference in New Issue
Block a user