Commit bbb2159d authored by yangguo's avatar yangguo Committed by Commit bot

[regexp] Fix regexp source escaping with preceding backslashes.

R=franzih@chromium.org
BUG=chromium:515897

Review-Url: https://codereview.chromium.org/2137033002
Cr-Commit-Position: refs/heads/master@{#37664}
parent 64ff8f8d
......@@ -15878,7 +15878,13 @@ inline int CountRequiredEscapes(Handle<String> source) {
int escapes = 0;
Vector<const Char> src = source->GetCharVector<Char>();
for (int i = 0; i < src.length(); i++) {
if (src[i] == '/' && (i == 0 || src[i - 1] != '\\')) escapes++;
if (src[i] == '\\') {
// Escape. Skip next character;
i++;
} else if (src[i] == '/') {
// Not escaped forward-slash needs escape.
escapes++;
}
}
return escapes;
}
......@@ -15893,7 +15899,14 @@ inline Handle<StringType> WriteEscapedRegExpSource(Handle<String> source,
int s = 0;
int d = 0;
while (s < src.length()) {
if (src[s] == '/' && (s == 0 || src[s - 1] != '\\')) dst[d++] = '\\';
if (src[s] == '\\') {
// Escape. Copy this and next character.
dst[d++] = src[s++];
if (s == src.length()) break;
} else if (src[s] == '/') {
// Not escaped forward-slash needs escape.
dst[d++] = '\\';
}
dst[d++] = src[s++];
}
DCHECK_EQ(result->length(), d);
......
......@@ -12,7 +12,7 @@ testEscapes("\\/", /\//);
testEscapes("\\/\\/", /\/\//);
testEscapes("\\/", new RegExp("/"));
testEscapes("\\/", new RegExp("\\/"));
testEscapes("\\\\/", new RegExp("\\\\/"));
testEscapes("\\\\\\/", new RegExp("\\\\/"));
testEscapes("\\/\\/", new RegExp("\\/\\/"));
testEscapes("\\/\\/\\/\\/", new RegExp("////"));
testEscapes("\\/\\/\\/\\/", new RegExp("\\//\\//"));
......
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var r1 = new RegExp("\\\\/");
assertTrue(r1.test("\\/"));
var r2 = eval("/" + r1.source + "/");
assertEquals("\\\\\\/", r1.source);
assertEquals("\\\\\\/", r2.source);
......@@ -37,8 +37,8 @@ PASS testForwardSlash("^/$", "/"); is true
PASS testForwardSlash("^\/$", "/"); is true
PASS testForwardSlash("^\\/$", "\/"); is true
PASS testForwardSlash("^\\\/$", "\/"); is true
FAIL testForwardSlash("^\\\\/$", "\\/"); should be true. Threw exception SyntaxError: Invalid regular expression flags
FAIL testForwardSlash("^\\\\\/$", "\\/"); should be true. Threw exception SyntaxError: Invalid regular expression flags
PASS testForwardSlash("^\\\\/$", "\\/"); is true
PASS testForwardSlash("^\\\\\/$", "\\/"); is true
PASS testForwardSlash("x/x/x", "x\/x\/x"); is true
PASS testForwardSlash("x\/x/x", "x\/x\/x"); is true
PASS testForwardSlash("x/x\/x", "x\/x\/x"); is true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment