@@ -539,7 +539,7 @@ void CharacterRange::AddUnicodeCaseEquivalents(ZoneList<CharacterRange>* ranges, |
539 | 539 | } |
540 | 540 | // Clear the ranges list without freeing the backing store. |
541 | 541 | ranges->Rewind(0); |
542 | | - set.closeOver(USET_SIMPLE_CASE_INSENSITIVE); |
| 542 | + RegExpCaseFolding::CloseOver(set, RegExpCaseFolding::Mode::kUnicode); |
543 | 543 | for (int i = 0; i < set.getRangeCount(); i++) { |
544 | 544 | ranges->Add(Range(set.getRangeStart(i), set.getRangeEnd(i)), zone); |
545 | 545 | } |
@@ -830,29 +830,39 @@ int CompareFirstChar(RegExpTree* const* a, RegExpTree* const* b) { |
830 | 830 |
|
831 | 831 | #ifdef V8_INTL_SUPPORT |
832 | 832 |
|
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; |
836 | 836 | } |
837 | 837 |
|
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, |
839 | 850 | RegExpTree* const* b) { |
840 | 851 | RegExpAtom* atom1 = FirstAtom(*a); |
841 | 852 | 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)); |
844 | 854 | } |
845 | 855 |
|
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) { |
848 | 858 | if (a == b) return true; |
849 | | - if (ignore_case) return CompareCaseInsensitive(a, b) == 0; |
| 859 | + if (ignore_case) return CompareCaseInsensitive(mode, a, b) == 0; |
850 | 860 | return false; // Case-sensitive equality already checked above. |
851 | 861 | } |
852 | 862 |
|
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)); |
856 | 866 | } |
857 | 867 |
|
858 | 868 | #else |
@@ -931,19 +941,20 @@ bool RegExpDisjunction::SortConsecutiveAtoms(RegExpCompiler* compiler) { |
931 | 941 | if (!StartsWithAtom(alternative)) break; |
932 | 942 | i++; |
933 | 943 | } |
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. |
940 | 947 | DCHECK_LT(first_atom, alternatives->length()); |
941 | 948 | DCHECK_LE(i, alternatives->length()); |
942 | 949 | DCHECK_LE(first_atom, i); |
943 | 950 | if (IsIgnoreCase(compiler->flags())) { |
944 | 951 | #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); |
947 | 958 | #else |
948 | 959 | unibrow::Mapping<unibrow::Ecma262Canonicalize>* canonicalize = |
949 | 960 | compiler->isolate()->regexp_macro_assembler_canonicalize(); |
@@ -967,6 +978,9 @@ void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) { |
967 | 978 | ZoneList<RegExpTree*>* alternatives = this->alternatives(); |
968 | 979 | int length = alternatives->length(); |
969 | 980 | 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 |
970 | 984 |
|
971 | 985 | int write_posn = 0; |
972 | 986 | int i = 0; |
@@ -980,7 +994,7 @@ void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) { |
980 | 994 | RegExpAtom* const atom = FirstAtom(alternative); |
981 | 995 |
|
982 | 996 | #ifdef V8_INTL_SUPPORT |
983 | | - icu::UnicodeString common_prefix(atom->data().at(0)); |
| 997 | + base::uc16 common_prefix = atom->data().at(0); |
984 | 998 | #else |
985 | 999 | unibrow::Mapping<unibrow::Ecma262Canonicalize>* const canonicalize = |
986 | 1000 | compiler->isolate()->regexp_macro_assembler_canonicalize(); |
@@ -997,8 +1011,8 @@ void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) { |
997 | 1011 | if (!StartsWithAtom(alternative)) break; |
998 | 1012 | RegExpAtom* const alt_atom = FirstAtom(alternative); |
999 | 1013 | #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; |
1002 | 1016 | #else |
1003 | 1017 | unibrow::uchar new_prefix = alt_atom->data().at(0); |
1004 | 1018 | if (!Equals(ignore_case, canonicalize, new_prefix, common_prefix)) break; |
@@ -1021,7 +1035,7 @@ void RegExpDisjunction::RationalizeConsecutiveAtoms(RegExpCompiler* compiler) { |
1021 | 1035 | FirstAtom(alternatives->at(j + first_with_prefix)); |
1022 | 1036 | for (int k = 1; k < prefix_length; k++) { |
1023 | 1037 | #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)) { |
1025 | 1039 | #else |
1026 | 1040 | if (!CharAtEquals(ignore_case, canonicalize, k, alt_atom, old_atom)) { |
1027 | 1041 | #endif // V8_INTL_SUPPORT |
@@ -1690,22 +1704,8 @@ void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone, |
1690 | 1704 | others.add(from, to); |
1691 | 1705 | } |
1692 | 1706 |
|
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. |
1705 | 1707 | 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); |
1709 | 1709 | others.removeAll(already_added); |
1710 | 1710 |
|
1711 | 1711 | // Add others to the ranges |
|