Skip to content

Commit 21f7910

Browse files
committed
Subsume referencedCaptureOffsets
1 parent a53a40b commit 21f7910

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

Sources/_StringProcessing/Engine/Structuralize.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@_implementationOnly import _RegexParser
2+
23
extension CaptureList {
34
@available(SwiftStdlib 5.7, *)
45
func createElements(
@@ -9,11 +10,12 @@ extension CaptureList {
910

1011
var result = [AnyRegexOutput.ElementRepresentation]()
1112

12-
for (cap, meStored) in zip(captures, list.values) {
13+
for (i, (cap, meStored)) in zip(captures, list.values).enumerated() {
1314
let element = AnyRegexOutput.ElementRepresentation(
1415
optionalDepth: cap.optionalDepth,
1516
bounds: meStored.latest,
1617
name: cap.name,
18+
referenceID: list.referencedCaptureOffsets.first { $1 == i }?.key,
1719
value: meStored.latestValue
1820
)
1921

Sources/_StringProcessing/Executor.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ struct Executor {
6464
return .init(
6565
anyRegexOutput: anyRegexOutput,
6666
range: range,
67-
referencedCaptureOffsets: capList.referencedCaptureOffsets,
6867
value: value
6968
)
7069
}

Sources/_StringProcessing/Regex/AnyRegexOutput.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public struct AnyRegexOutput {
7070
/// The name of the capture.
7171
var name: String? = nil
7272

73+
/// The capture reference this element refers to.
74+
var referenceID: ReferenceID? = nil
75+
7376
/// If the output vaule is strongly typed, then this will be set.
7477
var value: Any? = nil
7578
}
@@ -145,7 +148,11 @@ extension AnyRegexOutput: RandomAccessCollection {
145148
public var range: Range<String.Index>? {
146149
representation.bounds
147150
}
148-
151+
152+
var referenceID: ReferenceID? {
153+
representation.referenceID
154+
}
155+
149156
/// The slice of the input over which a value was captured. `nil` for no-capture.
150157
public var substring: Substring? {
151158
range.map { input[$0] }
@@ -201,7 +208,6 @@ extension Regex.Match where Output == AnyRegexOutput {
201208
self.init(
202209
anyRegexOutput: match.anyRegexOutput,
203210
range: match.range,
204-
referencedCaptureOffsets: match.referencedCaptureOffsets,
205211
value: match.value
206212
)
207213
}

Sources/_StringProcessing/Regex/Match.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ extension Regex {
2222
/// The range of the overall match.
2323
public let range: Range<String.Index>
2424

25-
let referencedCaptureOffsets: [ReferenceID: Int]
26-
2725
let value: Any?
2826
}
2927
}
@@ -35,8 +33,7 @@ extension Regex.Match {
3533
if Output.self == AnyRegexOutput.self {
3634
let wholeMatchCapture = AnyRegexOutput.ElementRepresentation(
3735
optionalDepth: 0,
38-
bounds: range,
39-
value: nil
36+
bounds: range
4037
)
4138

4239
let output = AnyRegexOutput(
@@ -79,11 +76,13 @@ extension Regex.Match {
7976

8077
@_spi(RegexBuilder)
8178
public subscript<Capture>(_ id: ReferenceID) -> Capture {
82-
guard let offset = referencedCaptureOffsets[id] else {
83-
preconditionFailure(
84-
"Reference did not capture any match in the regex")
79+
guard let element = anyRegexOutput.first(
80+
where: { $0.referenceID == id }
81+
) else {
82+
preconditionFailure("Reference did not capture any match in the regex")
8583
}
86-
return anyRegexOutput[offset].existentialOutputComponent(
84+
85+
return element.existentialOutputComponent(
8786
from: anyRegexOutput.input[...]
8887
) as! Capture
8988
}

0 commit comments

Comments
 (0)