@@ -38,12 +38,17 @@ extension Regex.Match where Output == AnyRegexOutput {
38
38
) -> Substring {
39
39
input [ range]
40
40
}
41
+
42
+ public subscript( name: String ) -> AnyRegexOutput . Element ? {
43
+ namedCaptureOffsets [ name] . map { self [ $0 + 1 ] }
44
+ }
41
45
}
42
46
43
47
/// A type-erased regex output
44
48
@available ( SwiftStdlib 5 . 7 , * )
45
49
public struct AnyRegexOutput {
46
50
let input : String
51
+ let namedCaptureOffsets : [ String : Int ]
47
52
fileprivate let _elements : [ ElementRepresentation ]
48
53
49
54
/// The underlying representation of the element of a type-erased regex
@@ -94,9 +99,12 @@ extension AnyRegexOutput {
94
99
@available ( SwiftStdlib 5 . 7 , * )
95
100
extension AnyRegexOutput {
96
101
internal init < C: Collection > (
97
- input: String , elements: C
102
+ input: String , namedCaptureOffsets : [ String : Int ] , elements: C
98
103
) where C. Element == StructuredCapture {
99
- self . init ( input: input, _elements: elements. map ( ElementRepresentation . init) )
104
+ self . init (
105
+ input: input,
106
+ namedCaptureOffsets: namedCaptureOffsets,
107
+ _elements: elements. map ( ElementRepresentation . init) )
100
108
}
101
109
}
102
110
@@ -170,6 +178,13 @@ extension AnyRegexOutput: RandomAccessCollection {
170
178
}
171
179
}
172
180
181
+ @available ( SwiftStdlib 5 . 7 , * )
182
+ extension AnyRegexOutput {
183
+ public subscript( name: String ) -> Element ? {
184
+ namedCaptureOffsets [ name] . map { self [ $0 + 1 ] }
185
+ }
186
+ }
187
+
173
188
@available ( SwiftStdlib 5 . 7 , * )
174
189
extension Regex . Match where Output == AnyRegexOutput {
175
190
/// Creates a type-erased regex match from an existing match.
0 commit comments