@@ -45,15 +45,16 @@ extension Regex.Match where Output == AnyRegexOutput {
4545 }
4646
4747 public subscript( name: String ) -> AnyRegexOutput . Element ? {
48- anyRegexOutput. namedCaptureOffsets [ name] . map { self [ $0 + 1 ] }
48+ anyRegexOutput. first {
49+ $0. name == name
50+ }
4951 }
5052}
5153
5254/// A type-erased regex output.
5355@available ( SwiftStdlib 5 . 7 , * )
5456public struct AnyRegexOutput {
5557 let input : String
56- let namedCaptureOffsets : [ String : Int ]
5758 let _elements : [ ElementRepresentation ]
5859
5960 /// The underlying representation of the element of a type-erased regex
@@ -65,6 +66,10 @@ public struct AnyRegexOutput {
6566
6667 /// The bounds of the output element.
6768 let bounds : Range < String . Index > ?
69+
70+ /// The name of the capture.
71+ var name : String ? = nil
72+
6873 /// If the output vaule is strongly typed, then this will be set.
6974 var value : Any ? = nil
7075 }
@@ -96,27 +101,16 @@ extension AnyRegexOutput {
96101
97102@available ( SwiftStdlib 5 . 7 , * )
98103extension AnyRegexOutput {
99- internal init < C: Collection > (
100- input: String , namedCaptureOffsets: [ String : Int ] , elements: C
101- ) where C. Element == StructuredCapture {
104+ internal init ( input: String , elements: [ ElementRepresentation ] ) {
102105 self . init (
103106 input: input,
104- namedCaptureOffsets: namedCaptureOffsets,
105- _elements: elements. map ( ElementRepresentation . init)
107+ _elements: elements
106108 )
107109 }
108110}
109111
110112@available ( SwiftStdlib 5 . 7 , * )
111113extension AnyRegexOutput . ElementRepresentation {
112- init ( _ element: StructuredCapture ) {
113- self . init (
114- optionalDepth: element. optionalCount,
115- bounds: element. storedCapture. flatMap ( \. range) ,
116- value: element. storedCapture. flatMap ( \. value)
117- )
118- }
119-
120114 func value( forInput input: String ) -> Any {
121115 // Ok for now because `existentialMatchComponent`
122116 // wont slice the input if there's no range to slice with
@@ -128,7 +122,8 @@ extension AnyRegexOutput.ElementRepresentation {
128122 from: input,
129123 in: bounds,
130124 value: nil ,
131- optionalCount: optionalDepth)
125+ optionalCount: optionalDepth
126+ )
132127 }
133128}
134129
@@ -141,7 +136,11 @@ extension AnyRegexOutput: RandomAccessCollection {
141136 var optionalDepth : Int {
142137 representation. optionalDepth
143138 }
144-
139+
140+ var name : String ? {
141+ representation. name
142+ }
143+
145144 /// The range over which a value was captured. `nil` for no-capture.
146145 public var range : Range < String . Index > ? {
147146 representation. bounds
@@ -186,7 +185,9 @@ extension AnyRegexOutput: RandomAccessCollection {
186185@available ( SwiftStdlib 5 . 7 , * )
187186extension AnyRegexOutput {
188187 public subscript( name: String ) -> Element ? {
189- namedCaptureOffsets [ name] . map { self [ $0 + 1 ] }
188+ first {
189+ $0. name == name
190+ }
190191 }
191192}
192193
0 commit comments