Skip to content

Commit f211b6d

Browse files
committed
Merge emitQuotedLiteral and emitReverseQuotedLiteral
1 parent 0b349cc commit f211b6d

File tree

1 file changed

+15
-41
lines changed

1 file changed

+15
-41
lines changed

Sources/_StringProcessing/ByteCodeGen.swift

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ fileprivate extension Compiler.ByteCodeGen {
109109
}
110110

111111
mutating func emitQuotedLiteral(_ s: String) {
112-
assert(!reverse)
113112
guard options.semanticLevel == .graphemeCluster else {
114113
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 {
116116
emitMatchScalar(scalar)
117117
}
118118
}
@@ -121,49 +121,27 @@ fileprivate extension Compiler.ByteCodeGen {
121121

122122
// Fast path for eliding boundary checks for an all ascii quoted literal
123123
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
150128

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
156131
let scalar = s.unicodeScalars[idx]
157132
if options.isCaseInsensitive && scalar.properties.isCased {
158-
builder.buildMatchScalarCaseInsensitive(scalar, boundaryCheck: boundaryCheck, reverse: true)
133+
builder.buildMatchScalarCaseInsensitive(scalar, boundaryCheck: boundaryCheck, reverse: reverse)
159134
} else {
160-
builder.buildMatchScalar(scalar, boundaryCheck: boundaryCheck, reverse: true)
135+
builder.buildMatchScalar(scalar, boundaryCheck: boundaryCheck, reverse: reverse)
161136
}
162137
}
163138
return
164139
}
165140

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+
}
167145
}
168146

169147
mutating func emitBackreference(
@@ -1300,11 +1278,7 @@ fileprivate extension Compiler.ByteCodeGen {
13001278
try emitAtom(a)
13011279

13021280
case let .quotedLiteral(s):
1303-
if reverse {
1304-
emitReverseQuotedLiteral(s)
1305-
} else {
1306-
emitQuotedLiteral(s)
1307-
}
1281+
emitQuotedLiteral(s)
13081282

13091283
case let .convertedRegexLiteral(n, _):
13101284
return try emitNode(n)

0 commit comments

Comments
 (0)