@@ -54,20 +54,14 @@ class RegExpTextBuilder { |
54 | 54 | : zone_(zone), flags_(flags), terms_(terms_storage), text_(zone) {} |
55 | 55 | void AddCharacter(base::uc16 character); |
56 | 56 | void AddUnicodeCharacter(base::uc32 character); |
57 | | - void AddEscapedUnicodeCharacter(base::uc32 character); |
58 | 57 | void AddAtom(RegExpTree* atom); |
59 | 58 | void AddTerm(RegExpTree* term); |
60 | 59 | void AddClassRanges(RegExpClassRanges* cc); |
61 | | - void FlushPendingSurrogate(); |
62 | 60 | void FlushText(); |
63 | 61 | RegExpTree* PopLastAtom(); |
64 | 62 | RegExpTree* ToRegExp(); |
65 | 63 |
|
66 | 64 | private: |
67 | | - static const base::uc16 kNoPendingSurrogate = 0; |
68 | | - |
69 | | - void AddLeadSurrogate(base::uc16 lead_surrogate); |
70 | | - void AddTrailSurrogate(base::uc16 trail_surrogate); |
71 | 65 | void FlushCharacters(); |
72 | 66 | bool NeedsDesugaringForUnicode(RegExpClassRanges* cc); |
73 | 67 | bool NeedsDesugaringForIgnoreCase(base::uc32 c); |
@@ -83,53 +77,11 @@ class RegExpTextBuilder { |
83 | 77 | Zone* const zone_; |
84 | 78 | const RegExpFlags flags_; |
85 | 79 | ZoneList<base::uc16>* characters_ = nullptr; |
86 | | - base::uc16 pending_surrogate_ = kNoPendingSurrogate; |
87 | 80 | SmallRegExpTreeVector* terms_; |
88 | 81 | SmallRegExpTreeVector text_; |
89 | 82 | }; |
90 | 83 |
|
91 | | -void RegExpTextBuilder::AddLeadSurrogate(base::uc16 lead_surrogate) { |
92 | | - DCHECK(unibrow::Utf16::IsLeadSurrogate(lead_surrogate)); |
93 | | - FlushPendingSurrogate(); |
94 | | - // Hold onto the lead surrogate, waiting for a trail surrogate to follow. |
95 | | - pending_surrogate_ = lead_surrogate; |
96 | | -} |
97 | | - |
98 | | -void RegExpTextBuilder::AddTrailSurrogate(base::uc16 trail_surrogate) { |
99 | | - DCHECK(unibrow::Utf16::IsTrailSurrogate(trail_surrogate)); |
100 | | - if (pending_surrogate_ != kNoPendingSurrogate) { |
101 | | - base::uc16 lead_surrogate = pending_surrogate_; |
102 | | - pending_surrogate_ = kNoPendingSurrogate; |
103 | | - DCHECK(unibrow::Utf16::IsLeadSurrogate(lead_surrogate)); |
104 | | - base::uc32 combined = |
105 | | - unibrow::Utf16::CombineSurrogatePair(lead_surrogate, trail_surrogate); |
106 | | - if (NeedsDesugaringForIgnoreCase(combined)) { |
107 | | - AddClassRangesForDesugaring(combined); |
108 | | - } else { |
109 | | - ZoneList<base::uc16> surrogate_pair(2, zone()); |
110 | | - surrogate_pair.Add(lead_surrogate, zone()); |
111 | | - surrogate_pair.Add(trail_surrogate, zone()); |
112 | | - RegExpAtom* atom = |
113 | | - zone()->New<RegExpAtom>(surrogate_pair.ToConstVector()); |
114 | | - AddAtom(atom); |
115 | | - } |
116 | | - } else { |
117 | | - pending_surrogate_ = trail_surrogate; |
118 | | - FlushPendingSurrogate(); |
119 | | - } |
120 | | -} |
121 | | - |
122 | | -void RegExpTextBuilder::FlushPendingSurrogate() { |
123 | | - if (pending_surrogate_ != kNoPendingSurrogate) { |
124 | | - DCHECK(IsUnicodeMode()); |
125 | | - base::uc32 c = pending_surrogate_; |
126 | | - pending_surrogate_ = kNoPendingSurrogate; |
127 | | - AddClassRangesForDesugaring(c); |
128 | | - } |
129 | | -} |
130 | | - |
131 | 84 | void RegExpTextBuilder::FlushCharacters() { |
132 | | - FlushPendingSurrogate(); |
133 | 85 | if (characters_ != nullptr) { |
134 | 86 | RegExpTree* atom = zone()->New<RegExpAtom>(characters_->ToConstVector()); |
135 | 87 | characters_ = nullptr; |
@@ -155,7 +107,6 @@ void RegExpTextBuilder::FlushText() { |
155 | 107 | } |
156 | 108 |
|
157 | 109 | void RegExpTextBuilder::AddCharacter(base::uc16 c) { |
158 | | - FlushPendingSurrogate(); |
159 | 110 | if (characters_ == nullptr) { |
160 | 111 | characters_ = zone()->New<ZoneList<base::uc16>>(4, zone()); |
161 | 112 | } |
@@ -165,25 +116,24 @@ void RegExpTextBuilder::AddCharacter(base::uc16 c) { |
165 | 116 | void RegExpTextBuilder::AddUnicodeCharacter(base::uc32 c) { |
166 | 117 | if (c > static_cast<base::uc32>(unibrow::Utf16::kMaxNonSurrogateCharCode)) { |
167 | 118 | DCHECK(IsUnicodeMode()); |
168 | | - AddLeadSurrogate(unibrow::Utf16::LeadSurrogate(c)); |
169 | | - AddTrailSurrogate(unibrow::Utf16::TrailSurrogate(c)); |
170 | | - } else if (IsUnicodeMode() && unibrow::Utf16::IsLeadSurrogate(c)) { |
171 | | - AddLeadSurrogate(c); |
172 | | - } else if (IsUnicodeMode() && unibrow::Utf16::IsTrailSurrogate(c)) { |
173 | | - AddTrailSurrogate(c); |
| 119 | + if (NeedsDesugaringForIgnoreCase(c)) { |
| 120 | + AddClassRangesForDesugaring(c); |
| 121 | + } else { |
| 122 | + ZoneList<base::uc16> surrogate_pair(2, zone()); |
| 123 | + surrogate_pair.Add(unibrow::Utf16::LeadSurrogate(c), zone()); |
| 124 | + surrogate_pair.Add(unibrow::Utf16::TrailSurrogate(c), zone()); |
| 125 | + RegExpAtom* atom = |
| 126 | + zone()->template New<RegExpAtom>(surrogate_pair.ToConstVector()); |
| 127 | + AddAtom(atom); |
| 128 | + } |
| 129 | + } else if (IsUnicodeMode() && (unibrow::Utf16::IsLeadSurrogate(c) || |
| 130 | + unibrow::Utf16::IsTrailSurrogate(c))) { |
| 131 | + AddClassRangesForDesugaring(c); |
174 | 132 | } else { |
175 | 133 | AddCharacter(static_cast<base::uc16>(c)); |
176 | 134 | } |
177 | 135 | } |
178 | 136 |
|
179 | | -void RegExpTextBuilder::AddEscapedUnicodeCharacter(base::uc32 character) { |
180 | | - // A lead or trail surrogate parsed via escape sequence will not |
181 | | - // pair up with any preceding lead or following trail surrogate. |
182 | | - FlushPendingSurrogate(); |
183 | | - AddUnicodeCharacter(character); |
184 | | - FlushPendingSurrogate(); |
185 | | -} |
186 | | - |
187 | 137 | void RegExpTextBuilder::AddClassRanges(RegExpClassRanges* cr) { |
188 | 138 | if (NeedsDesugaringForUnicode(cr)) { |
189 | 139 | // With /u or /v, character class needs to be desugared, so it |
@@ -256,7 +206,6 @@ bool RegExpTextBuilder::NeedsDesugaringForIgnoreCase(base::uc32 c) { |
256 | 206 | } |
257 | 207 |
|
258 | 208 | RegExpTree* RegExpTextBuilder::PopLastAtom() { |
259 | | - FlushPendingSurrogate(); |
260 | 209 | RegExpTree* atom; |
261 | 210 | if (characters_ != nullptr) { |
262 | 211 | base::Vector<const base::uc16> char_vector = characters_->ToConstVector(); |
@@ -298,7 +247,6 @@ class RegExpBuilder { |
298 | 247 | text_builder_(RegExpTextBuilder{zone, &terms_, flags}) {} |
299 | 248 | void AddCharacter(base::uc16 character); |
300 | 249 | void AddUnicodeCharacter(base::uc32 character); |
301 | | - void AddEscapedUnicodeCharacter(base::uc32 character); |
302 | 250 | // "Adds" an empty expression. Does nothing except consume a |
303 | 251 | // following quantifier |
304 | 252 | void AddEmpty(); |
@@ -510,8 +458,7 @@ class RegExpParserImpl final { |
510 | 458 | base::uc32* character); |
511 | 459 | base::uc32 ParseClassSetCharacter(); |
512 | 460 | // Parses and returns a single escaped character. |
513 | | - base::uc32 ParseCharacterEscape(InClassEscapeState in_class_escape_state, |
514 | | - bool* is_escaped_unicode_character); |
| 461 | + base::uc32 ParseCharacterEscape(InClassEscapeState in_class_escape_state); |
515 | 462 |
|
516 | 463 | void AddMaybeSimpleCaseFoldedRange(ZoneList<CharacterRange>* ranges, |
517 | 464 | CharacterRange new_range); |
@@ -1218,15 +1165,9 @@ RegExpTree* RegExpParserImpl<CharT>::ParseDisjunction() { |
1218 | 1165 | // AtomEscape :: |
1219 | 1166 | // CharacterEscape |
1220 | 1167 | default: { |
1221 | | - bool is_escaped_unicode_character = false; |
1222 | 1168 | base::uc32 c = ParseCharacterEscape( |
1223 | | - InClassEscapeState::kNotInClass, |
1224 | | - &is_escaped_unicode_character CHECK_FAILED); |
1225 | | - if (is_escaped_unicode_character) { |
1226 | | - builder->AddEscapedUnicodeCharacter(c); |
1227 | | - } else { |
1228 | | - builder->AddCharacter(c); |
1229 | | - } |
| 1169 | + InClassEscapeState::kNotInClass CHECK_FAILED); |
| 1170 | + builder->AddUnicodeCharacter(c); |
1230 | 1171 | break; |
1231 | 1172 | } |
1232 | 1173 | } |
@@ -2346,8 +2287,7 @@ bool RegExpParserImpl<CharT>::ParseUnlimitedLengthHexNumber(int max_value, |
2346 | 2287 | // https://tc39.es/ecma262/#prod-CharacterEscape |
2347 | 2288 | template <class CharT> |
2348 | 2289 | base::uc32 RegExpParserImpl<CharT>::ParseCharacterEscape( |
2349 | | - InClassEscapeState in_class_escape_state, |
2350 | | - bool* is_escaped_unicode_character) { |
| 2290 | + InClassEscapeState in_class_escape_state) { |
2351 | 2291 | DCHECK_EQ('\\', current()); |
2352 | 2292 | DCHECK(has_next()); |
2353 | 2293 |
|
@@ -2451,10 +2391,7 @@ base::uc32 RegExpParserImpl<CharT>::ParseCharacterEscape( |
2451 | 2391 | case 'u': { |
2452 | 2392 | Advance(); |
2453 | 2393 | base::uc32 value; |
2454 | | - if (ParseUnicodeEscape(&value)) { |
2455 | | - *is_escaped_unicode_character = true; |
2456 | | - return value; |
2457 | | - } |
| 2394 | + if (ParseUnicodeEscape(&value)) return value; |
2458 | 2395 | if (IsUnicodeMode()) { |
2459 | 2396 | // With /u or /v, invalid escapes are not treated as identity escapes. |
2460 | 2397 | ReportError(RegExpError::kInvalidUnicodeEscape); |
@@ -2596,8 +2533,7 @@ void RegExpParserImpl<CharT>::ParseClassEscape( |
2596 | 2533 | add_unicode_case_equivalents); |
2597 | 2534 | if (*is_class_escape) return; |
2598 | 2535 |
|
2599 | | - bool dummy = false; // Unused. |
2600 | | - *char_out = ParseCharacterEscape(kInClassEscape, &dummy); |
| 2536 | + *char_out = ParseCharacterEscape(kInClassEscape); |
2601 | 2537 | } |
2602 | 2538 |
|
2603 | 2539 | // https://tc39.es/ecma262/#prod-CharacterClassEscape |
@@ -2807,8 +2743,7 @@ base::uc32 RegExpParserImpl<CharT>::ParseClassSetCharacter() { |
2807 | 2743 | static constexpr InClassEscapeState kInClassEscape = |
2808 | 2744 | InClassEscapeState::kInClass; |
2809 | 2745 |
|
2810 | | - bool dummy = false; // Unused. |
2811 | | - return ParseCharacterEscape(kInClassEscape, &dummy); |
| 2746 | + return ParseCharacterEscape(kInClassEscape); |
2812 | 2747 | } |
2813 | 2748 | if (IsClassSetSyntaxCharacter(c)) { |
2814 | 2749 | ReportError(RegExpError::kInvalidCharacterInClass); |
@@ -3176,13 +3111,7 @@ void RegExpBuilder::AddUnicodeCharacter(base::uc32 c) { |
3176 | 3111 | text_builder().AddUnicodeCharacter(c); |
3177 | 3112 | } |
3178 | 3113 |
|
3179 | | -void RegExpBuilder::AddEscapedUnicodeCharacter(base::uc32 character) { |
3180 | | - pending_empty_ = false; |
3181 | | - text_builder().AddEscapedUnicodeCharacter(character); |
3182 | | -} |
3183 | | - |
3184 | 3114 | void RegExpBuilder::AddEmpty() { |
3185 | | - text_builder().FlushPendingSurrogate(); |
3186 | 3115 | pending_empty_ = true; |
3187 | 3116 | } |
3188 | 3117 |
|
|