Skip to content

Commit df0291d

Browse files
schuayRenegade334
authored andcommitted
deps: V8: backport ee1c92f63568
Original commit message: [regexp] Share matcher case equivalence when factoring alternatives Alternative sorting and prefix factoring used full case folding, which can merge characters that the matcher treats as distinct in legacy /i mode, such as U+017F and s. Use shared CaseFolding::EquivalenceKey and CloseOver operations for comparisons and character expansion. Preserve UTF-16 code-unit handling in non-unicode mode and code-point decoding in Unicode backreferences. Remove the generated SpecialAddSet while retaining exact Canonicalize for IgnoreSet generation. Add exhaustive release-only BMP equivalence and closure checks, plus coverage for alternatives, literals, ranges, and backreferences. Restore the real assertEquals in regress-10573. Change-Id: Idbf3866ca99c5446aeab15fd21f841d2f77d4243 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8456723 Commit-Queue: Patrick Thier <pthier@chromium.org> Reviewed-by: Patrick Thier <pthier@chromium.org> Auto-Submit: Jakob Linke <jgruber@chromium.org> Cr-Commit-Position: refs/heads/main@{#110082} Refs: v8/v8@ee1c92f Co-authored-by: Renegade334 <rene@renegade334.me.uk> PR-URL: #67527 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent ef65ffd commit df0291d

11 files changed

Lines changed: 333 additions & 151 deletions

‎common.gypi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
# Reset this number to 0 on major V8 upgrades.
5252
# Increment by one for each non-official patch applied to deps/v8.
53-
'v8_embedder_string': '-node.53',
53+
'v8_embedder_string': '-node.54',
5454

5555
##### V8 defaults for Node.js #####
5656

‎deps/v8/src/regexp/gen-regexp-special-case.cc‎

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "src/base/strings.h"
1111
#include "src/regexp/special-case.h"
12+
#include "unicode/unistr.h"
1213

1314
namespace v8 {
1415
namespace internal {
@@ -17,6 +18,38 @@ static const base::uc32 kSurrogateStart = 0xd800;
1718
static const base::uc32 kSurrogateEnd = 0xdfff;
1819
static const base::uc32 kNonBmpStart = 0x10000;
1920

21+
// This implements ECMAScript 2020 21.2.2.8.2 (Runtime Semantics:
22+
// Canonicalize) step 3, which is used to determine whether
23+
// characters match when ignoreCase is true and unicode is false.
24+
UChar32 Canonicalize(UChar32 ch) {
25+
// a. Assert: ch is a UTF-16 code unit.
26+
CHECK_LE(ch, 0xffff);
27+
28+
// b. Let s be the String value consisting of the single code unit ch.
29+
icu::UnicodeString s(ch);
30+
31+
// c. Let u be the same result produced as if by performing the algorithm
32+
// for String.prototype.toUpperCase using s as the this value.
33+
// d. Assert: Type(u) is String.
34+
icu::UnicodeString& u = s.toUpper();
35+
36+
// e. If u does not consist of a single code unit, return ch.
37+
if (u.length() != 1) {
38+
return ch;
39+
}
40+
41+
// f. Let cu be u's single code unit element.
42+
UChar32 cu = u.char32At(0);
43+
44+
// g. If the value of ch >= 128 and the value of cu < 128, return ch.
45+
if (ch >= 128 && cu < 128) {
46+
return ch;
47+
}
48+
49+
// h. Return cu.
50+
return cu;
51+
}
52+
2053
// The following code generates "src/regexp/special-case.cc".
2154
void PrintSet(std::ofstream& out, const char* name,
2255
const icu::UnicodeSet& set) {
@@ -62,12 +95,12 @@ void PrintSpecial(std::ofstream& out) {
6295
continue; // Ignore surrogate range
6396
}
6497
current.set(i, i);
65-
current.closeOver(USET_CASE_INSENSITIVE);
98+
current.closeOver(USET_SIMPLE_CASE_INSENSITIVE);
6699

67100
// Check to see if all characters in the case-folding equivalence
68101
// class as defined by UnicodeSet::closeOver all map to the same
69102
// canonical value.
70-
UChar32 canonical = RegExpCaseFolding::Canonicalize(i);
103+
UChar32 canonical = Canonicalize(i);
71104
bool class_has_matching_canonical_char = false;
72105
bool class_has_non_matching_canonical_char = false;
73106
for (int32_t j = 0; j < current.getRangeCount(); j++) {
@@ -76,7 +109,7 @@ void PrintSpecial(std::ofstream& out) {
76109
if (c == i) {
77110
continue;
78111
}
79-
UChar32 other_canonical = RegExpCaseFolding::Canonicalize(c);
112+
UChar32 other_canonical = Canonicalize(c);
80113
if (canonical == other_canonical) {
81114
class_has_matching_canonical_char = true;
82115
} else {
@@ -100,30 +133,29 @@ void PrintSpecial(std::ofstream& out) {
100133
}
101134

102135
// Verify that no Unicode equivalence class contains two non-trivial
103-
// JS equivalence classes. Every character in SpecialAddSet has the
136+
// JS equivalence classes. Every character in special_add has the
104137
// same canonical value as every other non-IgnoreSet character in
105138
// its Unicode equivalence class. Therefore, if we call closeOver on
106139
// a set containing no IgnoreSet characters, the only characters
107140
// that must be removed from the result are in IgnoreSet. This fact
108-
// is used in CharacterRange::AddCaseEquivalents.
141+
// is used in RegExpCaseFolding::CloseOver.
109142
for (int32_t i = 0; i < special_add.getRangeCount(); i++) {
110143
for (UChar32 c = special_add.getRangeStart(i);
111144
c <= special_add.getRangeEnd(i); c++) {
112-
UChar32 canonical = RegExpCaseFolding::Canonicalize(c);
145+
UChar32 canonical = Canonicalize(c);
113146
current.set(c, c);
114-
current.closeOver(USET_CASE_INSENSITIVE);
147+
current.closeOver(USET_SIMPLE_CASE_INSENSITIVE);
115148
current.removeAll(ignore);
116149
for (int32_t j = 0; j < current.getRangeCount(); j++) {
117150
for (UChar32 c2 = current.getRangeStart(j);
118151
c2 <= current.getRangeEnd(j); c2++) {
119-
CHECK_EQ(canonical, RegExpCaseFolding::Canonicalize(c2));
152+
CHECK_EQ(canonical, Canonicalize(c2));
120153
}
121154
}
122155
}
123156
}
124157

125158
PrintSet(out, "IgnoreSet", ignore);
126-
PrintSet(out, "SpecialAddSet", special_add);
127159
}
128160

129161
void WriteHeader(const char* header_filename) {
@@ -135,9 +167,9 @@ void WriteHeader(const char* header_filename) {
135167
<< "// Automatically generated by regexp/gen-regexp-special-case.cc\n\n"
136168
<< "// The following functions are used to build UnicodeSets\n"
137169
<< "// for special cases where the case-folding algorithm used by\n"
138-
<< "// UnicodeSet::closeOver(USET_CASE_INSENSITIVE) does not match\n"
139-
<< "// the algorithm defined in ECMAScript 2020 21.2.2.8.2 (Runtime\n"
140-
<< "// Semantics: Canonicalize) step 3.\n\n"
170+
<< "// UnicodeSet::closeOver(USET_SIMPLE_CASE_INSENSITIVE) does\n"
171+
<< "// not match the algorithm defined in ECMAScript 2020 21.2.2.8.2\n"
172+
<< "// (Runtime Semantics: Canonicalize) step 3.\n\n"
141173
<< "#ifdef V8_INTL_SUPPORT\n"
142174
<< "#include \"src/base/lazy-instance.h\"\n\n"
143175
<< "#include \"src/regexp/special-case.h\"\n\n"

‎deps/v8/src/regexp/regexp-compiler-tonode.cc‎

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ void CharacterRange::AddUnicodeCaseEquivalents(ZoneList<CharacterRange>* ranges,
539539
}
540540
// Clear the ranges list without freeing the backing store.
541541
ranges->Rewind(0);
542-
set.closeOver(USET_SIMPLE_CASE_INSENSITIVE);
542+
RegExpCaseFolding::CloseOver(set, RegExpCaseFolding::Mode::kUnicode);
543543
for (int i = 0; i < set.getRangeCount(); i++) {
544544
ranges->Add(Range(set.getRangeStart(i), set.getRangeEnd(i)), zone);
545545
}
@@ -830,29 +830,39 @@ int CompareFirstChar(RegExpTree* const* a, RegExpTree* const* b) {
830830

831831
#ifdef V8_INTL_SUPPORT
832832

833-
int CompareCaseInsensitive(const icu::UnicodeString& a,
834-
const icu::UnicodeString& b) {
835-
return a.caseCompare(b, U_FOLD_CASE_DEFAULT);
833+
RegExpCaseFolding::Mode CaseFoldingMode(RegExpFlags flags) {
834+
return IsEitherUnicode(flags) ? RegExpCaseFolding::Mode::kUnicode
835+
: RegExpCaseFolding::Mode::kNonUnicode;
836836
}
837837

838-
int CompareFirstCharCaseInsensitive(RegExpTree* const* a,
838+
// Use the matcher's case equivalence (see
839+
// TextNode::GetCaseIndependentLetters). Full case folding would, for example,
840+
// conflate U+017F and 's' under /i.
841+
int CompareCaseInsensitive(RegExpCaseFolding::Mode mode, base::uc16 a,
842+
base::uc16 b) {
843+
if (a == b) return 0;
844+
return RegExpCaseFolding::EquivalenceKey(a, mode) -
845+
RegExpCaseFolding::EquivalenceKey(b, mode);
846+
}
847+
848+
int CompareFirstCharCaseInsensitive(RegExpCaseFolding::Mode mode,
849+
RegExpTree* const* a,
839850
RegExpTree* const* b) {
840851
RegExpAtom* atom1 = FirstAtom(*a);
841852
RegExpAtom* atom2 = FirstAtom(*b);
842-
return CompareCaseInsensitive(icu::UnicodeString{atom1->data().at(0)},
843-
icu::UnicodeString{atom2->data().at(0)});
853+
return CompareCaseInsensitive(mode, atom1->data().at(0), atom2->data().at(0));
844854
}
845855

846-
bool Equals(bool ignore_case, const icu::UnicodeString& a,
847-
const icu::UnicodeString& b) {
856+
bool Equals(bool ignore_case, RegExpCaseFolding::Mode mode, base::uc16 a,
857+
base::uc16 b) {
848858
if (a == b) return true;
849-
if (ignore_case) return CompareCaseInsensitive(a, b) == 0;
859+
if (ignore_case) return CompareCaseInsensitive(mode, a, b) == 0;
850860
return false; // Case-sensitive equality already checked above.
851861
}
852862

853-
bool CharAtEquals(bool ignore_case, int index, const RegExpAtom* a,
854-
const RegExpAtom* b) {
855-
return Equals(ignore_case, a->data().at(index), b->data().at(index));
863+
bool CharAtEquals(bool ignore_case, RegExpCaseFolding::Mode mode, int index,
864+
const RegExpAtom* a, const RegExpAtom* b) {
865+
return Equals(ignore_case, mode, a->data().at(index), b->data().at(index));
856866
}
857867

858868
#else
@@ -931,19 +941,20 @@ bool RegExpDisjunction::SortConsecutiveAtoms(RegExpCompiler* compiler) {
931941
if (!StartsWithAtom(alternative)) break;
932942
i++;
933943
}
934-
// Sort atoms to get ones with common prefixes together.
935-
// This step is more tricky if we are in a case-independent regexp,
936-
// because it would change /is|I/ to /I|is/, and order matters when
937-
// the regexp parts don't match only disjoint starting points. To fix
938-
// this we have a version of CompareFirstChar that uses case-
939-
// independent character classes for comparison.
944+
// Sort atoms to bring common prefixes together. A case-insensitive sort
945+
// must preserve the order of alternatives whose first characters are
946+
// equivalent: changing /is|I/ to /I|is/ would change the match result.
940947
DCHECK_LT(first_atom, alternatives->length());
941948
DCHECK_LE(i, alternatives->length());
942949
DCHECK_LE(first_atom, i);
943950
if (IsIgnoreCase(compiler->flags())) {
944951
#ifdef V8_INTL_SUPPORT
945-
alternatives->StableSort(CompareFirstCharCaseInsensitive, first_atom,
946-
i - first_atom);
952+
const RegExpCaseFolding::Mode mode = CaseFoldingMode(compiler->flags());
953+
auto compare_closure = [mode](RegExpTree* const* a,
954+
RegExpTree* const* b) {
955+
return CompareFirstCharCaseInsensitive(mode, a, b);
956+
};
957+
alternatives->StableSort(compare_closure, first_atom, i - first_atom);
947958
#else
948959
unibrow::Mapping<unibrow::Ecma262Canonicalize>* canonicalize =
949960
compiler->isolate()->regexp_macro_assembler_canonicalize();
@@ -967,6 +978,9 @@ void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) {
967978
ZoneList<RegExpTree*>* alternatives = this->alternatives();
968979
int length = alternatives->length();
969980
const bool ignore_case = IsIgnoreCase(compiler->flags());
981+
#ifdef V8_INTL_SUPPORT
982+
const RegExpCaseFolding::Mode mode = CaseFoldingMode(compiler->flags());
983+
#endif // V8_INTL_SUPPORT
970984

971985
int write_posn = 0;
972986
int i = 0;
@@ -980,7 +994,7 @@ void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) {
980994
RegExpAtom* const atom = FirstAtom(alternative);
981995

982996
#ifdef V8_INTL_SUPPORT
983-
icu::UnicodeString common_prefix(atom->data().at(0));
997+
base::uc16 common_prefix = atom->data().at(0);
984998
#else
985999
unibrow::Mapping<unibrow::Ecma262Canonicalize>* const canonicalize =
9861000
compiler->isolate()->regexp_macro_assembler_canonicalize();
@@ -997,8 +1011,8 @@ void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) {
9971011
if (!StartsWithAtom(alternative)) break;
9981012
RegExpAtom* const alt_atom = FirstAtom(alternative);
9991013
#ifdef V8_INTL_SUPPORT
1000-
icu::UnicodeString new_prefix(alt_atom->data().at(0));
1001-
if (!Equals(ignore_case, new_prefix, common_prefix)) break;
1014+
base::uc16 new_prefix = alt_atom->data().at(0);
1015+
if (!Equals(ignore_case, mode, new_prefix, common_prefix)) break;
10021016
#else
10031017
unibrow::uchar new_prefix = alt_atom->data().at(0);
10041018
if (!Equals(ignore_case, canonicalize, new_prefix, common_prefix)) break;
@@ -1021,7 +1035,7 @@ void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) {
10211035
FirstAtom(alternatives->at(j + first_with_prefix));
10221036
for (int k = 1; k < prefix_length; k++) {
10231037
#ifdef V8_INTL_SUPPORT
1024-
if (!CharAtEquals(ignore_case, k, alt_atom, old_atom)) {
1038+
if (!CharAtEquals(ignore_case, mode, k, alt_atom, old_atom)) {
10251039
#else
10261040
if (!CharAtEquals(ignore_case, canonicalize, k, alt_atom, old_atom)) {
10271041
#endif // V8_INTL_SUPPORT
@@ -1690,22 +1704,8 @@ void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone,
16901704
others.add(from, to);
16911705
}
16921706

1693-
// Compute the set of additional characters that should be added,
1694-
// using UnicodeSet::closeOver. ECMA 262 defines slightly different
1695-
// case-folding rules than Unicode, so some characters that are
1696-
// added by closeOver do not match anything other than themselves in
1697-
// JS. For example, 'ſ' (U+017F LATIN SMALL LETTER LONG S) is the
1698-
// same case-insensitive character as 's' or 'S' according to
1699-
// Unicode, but does not match any other character in JS. To handle
1700-
// this case, we add such characters to the IgnoreSet and filter
1701-
// them out. We filter twice: once before calling closeOver (to
1702-
// prevent 'ſ' from adding 's'), and once after calling closeOver
1703-
// (to prevent 's' from adding 'ſ'). See regexp/special-case.h for
1704-
// more information.
17051707
icu::UnicodeSet already_added(others);
1706-
others.removeAll(RegExpCaseFolding::IgnoreSet());
1707-
others.closeOver(USET_CASE_INSENSITIVE);
1708-
others.removeAll(RegExpCaseFolding::IgnoreSet());
1708+
RegExpCaseFolding::CloseOver(others, RegExpCaseFolding::Mode::kNonUnicode);
17091709
others.removeAll(already_added);
17101710

17111711
// Add others to the ranges

‎deps/v8/src/regexp/regexp-compiler.cc‎

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -903,28 +903,11 @@ int GetCaseIndependentLetters(Isolate* isolate, base::uc16 character,
903903
}
904904
#ifdef V8_INTL_SUPPORT
905905

906-
if (!unicode && RegExpCaseFolding::IgnoreSet().contains(character)) {
907-
if (one_byte_subject && character > String::kMaxOneByteCharCode) {
908-
// This function promises not to return a character that is impossible
909-
// for the subject encoding.
910-
return 0;
911-
}
912-
letters[0] = character;
913-
DCHECK(ContainsOnlyUtf16CodeUnits(letters, 1));
914-
return 1;
915-
}
916-
bool in_special_add_set =
917-
RegExpCaseFolding::SpecialAddSet().contains(character);
918-
919906
icu::UnicodeSet set;
920907
set.add(character);
921-
set = set.closeOver(unicode ? USET_SIMPLE_CASE_INSENSITIVE
922-
: USET_CASE_INSENSITIVE);
923-
924-
UChar32 canon = 0;
925-
if (in_special_add_set && !unicode) {
926-
canon = RegExpCaseFolding::Canonicalize(character);
927-
}
908+
RegExpCaseFolding::CloseOver(set,
909+
unicode ? RegExpCaseFolding::Mode::kUnicode
910+
: RegExpCaseFolding::Mode::kNonUnicode);
928911

929912
int32_t range_count = set.getRangeCount();
930913
int items = 0;
@@ -934,10 +917,6 @@ int GetCaseIndependentLetters(Isolate* isolate, base::uc16 character,
934917
CHECK(end - start + items <= letter_length);
935918
for (UChar32 cu = start; cu <= end; cu++) {
936919
if (one_byte_subject && cu > String::kMaxOneByteCharCode) continue;
937-
if (!unicode && in_special_add_set &&
938-
RegExpCaseFolding::Canonicalize(cu) != canon) {
939-
continue;
940-
}
941920
letters[items++] = static_cast<unibrow::uchar>(cu);
942921
}
943922
}

‎deps/v8/src/regexp/regexp-macro-assembler.cc‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ int RegExpMacroAssembler::CaseInsensitiveCompareNonUnicode(Address byte_offset1,
5858
base::uc16* substring2 = reinterpret_cast<base::uc16*>(byte_offset2);
5959

6060
for (size_t i = 0; i < length; i++) {
61-
UChar32 c1 = RegExpCaseFolding::Canonicalize(substring1[i]);
62-
UChar32 c2 = RegExpCaseFolding::Canonicalize(substring2[i]);
63-
if (c1 != c2) {
61+
UChar32 c1 = substring1[i];
62+
UChar32 c2 = substring2[i];
63+
if (c1 == c2) continue;
64+
if (
65+
RegExpCaseFolding::EquivalenceKey(
66+
c1, RegExpCaseFolding::Mode::kNonUnicode) !=
67+
RegExpCaseFolding::EquivalenceKey(
68+
c2, RegExpCaseFolding::Mode::kNonUnicode)) {
6469
return 0;
6570
}
6671
}
@@ -96,8 +101,11 @@ int RegExpMacroAssembler::CaseInsensitiveCompareUnicode(Address byte_offset1,
96101
U16_NEXT(str2, i2, length, c2);
97102
if (i1 != i2) return 0;
98103
if (c1 == c2) continue;
99-
if (u_foldCase(c1, U_FOLD_CASE_DEFAULT) !=
100-
u_foldCase(c2, U_FOLD_CASE_DEFAULT)) {
104+
if (
105+
RegExpCaseFolding::EquivalenceKey(
106+
c1, RegExpCaseFolding::Mode::kUnicode) !=
107+
RegExpCaseFolding::EquivalenceKey(
108+
c2, RegExpCaseFolding::Mode::kUnicode)) {
101109
return 0;
102110
}
103111
}

0 commit comments

Comments
 (0)