Skip to content

Commit 62a9fbe

Browse files
committed
put scalar matching on String, consistent few percent
1 parent 2ada1d5 commit 62a9fbe

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

Sources/_StringProcessing/Engine/MEQuantify.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ extension Processor {
55
case .bitset:
66
next = _doMatchBitset(registers[payload.bitset])
77
case .asciiChar:
8-
next = _doMatchScalar(
9-
UnicodeScalar.init(_value: UInt32(payload.asciiChar)), true)
8+
next = input.matchScalar(
9+
UnicodeScalar.init(_value: UInt32(payload.asciiChar)),
10+
at: currentPosition,
11+
limitedBy: end,
12+
boundaryCheck: true)
1013
case .builtin:
1114
// We only emit .quantify if it consumes a single character
1215
next = input._matchBuiltinCC(

Sources/_StringProcessing/Engine/Processor.swift

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -272,21 +272,10 @@ extension Processor {
272272
currentPosition < end ? input.unicodeScalars[currentPosition] : nil
273273
}
274274

275-
func _doMatchScalar(_ s: Unicode.Scalar, _ boundaryCheck: Bool) -> Input.Index? {
276-
if s == loadScalar(),
277-
let idx = input.unicodeScalars.index(
278-
currentPosition,
279-
offsetBy: 1,
280-
limitedBy: end),
281-
(!boundaryCheck || input.isOnGraphemeClusterBoundary(idx)) {
282-
return idx
283-
} else {
284-
return nil
285-
}
286-
}
287-
288275
mutating func matchScalar(_ s: Unicode.Scalar, boundaryCheck: Bool) -> Bool {
289-
guard let next = _doMatchScalar(s, boundaryCheck) else {
276+
guard let next = input.matchScalar(
277+
s, at: currentPosition, limitedBy: end, boundaryCheck: boundaryCheck
278+
) else {
290279
signalFailure()
291280
return false
292281
}
@@ -695,4 +684,48 @@ extension Processor {
695684
controller.step()
696685
}
697686
}
687+
688+
func sleep() {
689+
var i = 0
690+
for c in input {
691+
if i > 20 { break }
692+
i += 1
693+
if c == "C" {
694+
blackHole(c)
695+
}
696+
}
697+
}
698+
}
699+
700+
@inline(never)
701+
func blackHole<T>(_ t: T) { _ = t }
702+
703+
extension String {
704+
705+
// func consumeScalar(_ n: Distance) -> Bool {
706+
707+
// }
708+
709+
func matchScalar(
710+
_ scalar: Unicode.Scalar,
711+
at pos: Index,
712+
limitedBy end: String.Index,
713+
boundaryCheck: Bool
714+
) -> Index? {
715+
assert(end <= endIndex)
716+
717+
guard pos < end, unicodeScalars[pos] == scalar else {
718+
return nil
719+
}
720+
721+
let idx = unicodeScalars.index(after: pos)
722+
guard idx <= end else { return nil }
723+
724+
if boundaryCheck && !isOnGraphemeClusterBoundary(idx) {
725+
return nil
726+
}
727+
728+
return idx
729+
}
730+
698731
}

0 commit comments

Comments
 (0)