@@ -109,10 +109,10 @@ fileprivate extension Compiler.ByteCodeGen {
109
109
}
110
110
111
111
mutating func emitQuotedLiteral( _ s: String ) {
112
- assert ( !reverse)
113
112
guard options. semanticLevel == . graphemeCluster else {
114
113
for char in s {
115
- for scalar in char. unicodeScalars {
114
+ let scalars : any Collection < UnicodeScalar > = reverse ? char. unicodeScalars. reversed ( ) : char. unicodeScalars
115
+ for scalar in scalars {
116
116
emitMatchScalar ( scalar)
117
117
}
118
118
}
@@ -121,49 +121,27 @@ fileprivate extension Compiler.ByteCodeGen {
121
121
122
122
// Fast path for eliding boundary checks for an all ascii quoted literal
123
123
if optimizationsEnabled && s. allSatisfy ( \. isASCII) && !s. isEmpty {
124
- let lastIdx = s. unicodeScalars. indices. last!
125
- for idx in s. unicodeScalars. indices {
126
- let boundaryCheck = idx == lastIdx
127
- let scalar = s. unicodeScalars [ idx]
128
- if options. isCaseInsensitive && scalar. properties. isCased {
129
- builder. buildMatchScalarCaseInsensitive ( scalar, boundaryCheck: boundaryCheck, reverse: false )
130
- } else {
131
- builder. buildMatchScalar ( scalar, boundaryCheck: boundaryCheck, reverse: false )
132
- }
133
- }
134
- return
135
- }
136
-
137
- for c in s { emitCharacter ( c) }
138
- }
139
-
140
- mutating func emitReverseQuotedLiteral( _ s: String ) {
141
- assert ( reverse)
142
- guard options. semanticLevel == . graphemeCluster else {
143
- for char in s {
144
- for scalar in char. unicodeScalars. reversed ( ) {
145
- emitMatchScalar ( scalar)
146
- }
147
- }
148
- return
149
- }
124
+ let boundaryIdx = reverse ? s. unicodeScalars. indices. first! : s. unicodeScalars. indices. last!
125
+ let indices : any Collection < String . UnicodeScalarIndex > = reverse
126
+ ? s. unicodeScalars. indices. reversed ( )
127
+ : s. unicodeScalars. indices
150
128
151
- // Fast path for eliding boundary checks for an all ascii quoted literal
152
- if optimizationsEnabled && s. allSatisfy ( \. isASCII) && !s. isEmpty {
153
- let lastIdx = s. unicodeScalars. indices. first!
154
- for idx in s. unicodeScalars. indices. reversed ( ) {
155
- let boundaryCheck = idx == lastIdx
129
+ for idx in indices {
130
+ let boundaryCheck = idx == boundaryIdx
156
131
let scalar = s. unicodeScalars [ idx]
157
132
if options. isCaseInsensitive && scalar. properties. isCased {
158
- builder. buildMatchScalarCaseInsensitive ( scalar, boundaryCheck: boundaryCheck, reverse: true )
133
+ builder. buildMatchScalarCaseInsensitive ( scalar, boundaryCheck: boundaryCheck, reverse: reverse )
159
134
} else {
160
- builder. buildMatchScalar ( scalar, boundaryCheck: boundaryCheck, reverse: true )
135
+ builder. buildMatchScalar ( scalar, boundaryCheck: boundaryCheck, reverse: reverse )
161
136
}
162
137
}
163
138
return
164
139
}
165
140
166
- for c in s. reversed ( ) { emitCharacter ( c) }
141
+ let chars : any Collection < Character > = reverse ? s. reversed ( ) : s
142
+ for char in chars {
143
+ emitCharacter ( char)
144
+ }
167
145
}
168
146
169
147
mutating func emitBackreference(
@@ -1300,11 +1278,7 @@ fileprivate extension Compiler.ByteCodeGen {
1300
1278
try emitAtom ( a)
1301
1279
1302
1280
case let . quotedLiteral( s) :
1303
- if reverse {
1304
- emitReverseQuotedLiteral ( s)
1305
- } else {
1306
- emitQuotedLiteral ( s)
1307
- }
1281
+ emitQuotedLiteral ( s)
1308
1282
1309
1283
case let . convertedRegexLiteral( n, _) :
1310
1284
return try emitNode ( n)
0 commit comments