Skip to content

Commit 3a4e05b

Browse files
committed
wip
1 parent fe2795d commit 3a4e05b

File tree

4 files changed

+80
-25
lines changed

4 files changed

+80
-25
lines changed

Sources/_StringProcessing/Engine/MEBuiltins.swift

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,78 @@ extension Character {
99
}
1010

1111
extension Processor {
12-
mutating func matchBuiltin(
12+
mutating func matchBuiltinCC(
1313
_ cc: _CharacterClassModel.Representation,
14-
_ isInverted: Bool,
15-
_ isStrictASCII: Bool,
16-
_ isScalarSemantics: Bool
14+
isInverted: Bool,
15+
isStrictASCII: Bool,
16+
isScalarSemantics: Bool
1717
) -> Bool {
18-
guard let next = _doMatchBuiltin(
18+
guard let next = _doMatchBuiltinCC(
1919
cc,
20-
isInverted,
21-
isStrictASCII,
22-
isScalarSemantics
20+
isInverted: isInverted,
21+
isStrictASCII: isStrictASCII,
22+
isScalarSemantics: isScalarSemantics
2323
) else {
2424
signalFailure()
2525
return false
2626
}
2727
currentPosition = next
2828
return true
2929
}
30-
31-
func _doMatchBuiltin(
30+
31+
func _doMatchBuiltinCC(
3232
_ cc: _CharacterClassModel.Representation,
33-
_ isInverted: Bool,
34-
_ isStrictASCII: Bool,
35-
_ isScalarSemantics: Bool
33+
isInverted: Bool,
34+
isStrictASCII: Bool,
35+
isScalarSemantics: Bool
3636
) -> Input.Index? {
37+
switch _quickMatchBuiltinCC(
38+
cc,
39+
isInverted: isInverted,
40+
isStrictASCII: isStrictASCII,
41+
isScalarSemantics: isScalarSemantics
42+
) {
43+
case .no:
44+
return nil
45+
case .yes(let next):
46+
return next
47+
case .maybe:
48+
return _slowMatchBuiltinCC(
49+
cc,
50+
isInverted: isInverted,
51+
isStrictASCII: isStrictASCII,
52+
isScalarSemantics: isScalarSemantics)
53+
}
54+
}
3755

38-
// ASCII fast-path
39-
if let (next, result) = input._quickMatch(
56+
@inline(__always)
57+
func _quickMatchBuiltinCC(
58+
_ cc: _CharacterClassModel.Representation,
59+
isInverted: Bool,
60+
isStrictASCII: Bool,
61+
isScalarSemantics: Bool
62+
) -> QuickResult<Input.Index> {
63+
guard let (next, result) = input._quickMatch(
4064
cc, at: currentPosition, isScalarSemantics: isScalarSemantics
41-
) {
42-
return result == isInverted ? nil : next
65+
) else {
66+
return .maybe
4367
}
68+
let idx = result == isInverted ? nil : next
69+
assert(idx == _slowMatchBuiltinCC(
70+
cc,
71+
isInverted: isInverted,
72+
isStrictASCII: isStrictASCII,
73+
isScalarSemantics: isScalarSemantics))
74+
return .definite(idx)
75+
}
4476

77+
@inline(never)
78+
func _slowMatchBuiltinCC(
79+
_ cc: _CharacterClassModel.Representation,
80+
isInverted: Bool,
81+
isStrictASCII: Bool,
82+
isScalarSemantics: Bool
83+
) -> Input.Index? {
4584
guard let char = load(), let scalar = loadScalar() else {
4685
return nil
4786
}

Sources/_StringProcessing/Engine/MEQuantify.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ extension Processor {
99
UnicodeScalar.init(_value: UInt32(payload.asciiChar)), true)
1010
case .builtin:
1111
// We only emit .quantify if it consumes a single character
12-
next = _doMatchBuiltin(
12+
next = _doMatchBuiltinCC(
1313
payload.builtin,
14-
payload.builtinIsInverted,
15-
payload.builtinIsStrict,
16-
false)
14+
isInverted: payload.builtinIsInverted,
15+
isStrictASCII: payload.builtinIsStrict,
16+
isScalarSemantics: false)
1717
case .any:
1818
let matched = currentPosition != input.endIndex
1919
&& (!input[currentPosition].isNewline || payload.anyMatchesNewline)

Sources/_StringProcessing/Engine/Processor.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,11 @@ extension Processor {
583583

584584
case .matchBuiltin:
585585
let payload = payload.characterClassPayload
586-
if matchBuiltin(
586+
if matchBuiltinCC(
587587
payload.cc,
588-
payload.isInverted,
589-
payload.isStrictASCII,
590-
payload.isScalarSemantics
588+
isInverted: payload.isInverted,
589+
isStrictASCII: payload.isStrictASCII,
590+
isScalarSemantics: payload.isScalarSemantics
591591
) {
592592
controller.step()
593593
}

Sources/_StringProcessing/StringExtras.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,19 @@ extension String {
128128

129129
}
130130

131+
/// An enum for quick-check functions, which could return a yes, no, or maybe
132+
/// result.
133+
enum QuickResult<R> {
134+
case yes(_ r: R)
135+
case no
136+
case maybe
137+
138+
static func definite(_ r: R?) -> QuickResult {
139+
if let r = r {
140+
return .yes(r)
141+
}
142+
return .no
143+
}
144+
}
145+
146+

0 commit comments

Comments
 (0)