11
11
12
12
import _MatchingEngine
13
13
14
- @dynamicMemberLookup
15
- public struct RegexMatch < Match> {
16
- public let range : Range < String . Index >
17
- public let match : Match
18
-
19
- public subscript< T> ( dynamicMember keyPath: KeyPath < Match , T > ) -> T {
20
- match [ keyPath: keyPath]
21
- }
22
- }
23
14
24
15
/// A type that represents a regular expression.
25
16
public protocol RegexProtocol {
@@ -134,93 +125,7 @@ public struct Regex<Match: MatchProtocol>: RegexProtocol {
134
125
}
135
126
}
136
127
137
- extension RegexProtocol {
138
- public func match( in input: String ) -> RegexMatch < Match > ? {
139
- _match (
140
- input, in: input. startIndex..< input. endIndex)
141
- }
142
- public func match( in input: Substring ) -> RegexMatch < Match > ? {
143
- _match (
144
- input. base, in: input. startIndex..< input. endIndex)
145
- }
146
128
147
- // FIXME: This is mostly hacky because we go down two different paths based on
148
- // whether there are captures. This will be cleaned up once we deprecate the
149
- // legacy virtual machines.
150
- func _match(
151
- _ input: String ,
152
- in inputRange: Range < String . Index > ,
153
- mode: MatchMode = . wholeString
154
- ) -> RegexMatch < Match > ? {
155
- // Casts a Swift tuple to the custom `Tuple<n>`, assuming their memory
156
- // layout is compatible.
157
- func bitCastToMatch< T> ( _ x: T ) -> Match {
158
- assert ( MemoryLayout< T> . size == MemoryLayout< Match> . size)
159
- return unsafeBitCast ( x, to: Match . self)
160
- }
161
- // TODO: Remove this branch when the matching engine supports captures.
162
- if regex. hasCapture {
163
- let vm = HareVM ( program: regex. program. legacyLoweredProgram)
164
- guard let ( range, captures) = vm. execute (
165
- input: input, in: inputRange, mode: mode
166
- ) ? . destructure else {
167
- return nil
168
- }
169
- let convertedMatch : Match
170
- if Match . self == Tuple2< Substring, DynamicCaptures> . self {
171
- convertedMatch = Tuple2 (
172
- input [ range] , DynamicCaptures ( captures)
173
- ) as! Match
174
- } else {
175
- let typeErasedMatch = captures. matchValue (
176
- withWholeMatch: input [ range]
177
- )
178
- convertedMatch = _openExistential ( typeErasedMatch, do: bitCastToMatch)
179
- }
180
- return RegexMatch ( range: range, match: convertedMatch)
181
- }
182
-
183
- let executor = Executor ( program: regex. program. loweredProgram)
184
- guard let result = executor. execute (
185
- input: input, in: inputRange, mode: mode
186
- ) else {
187
- return nil
188
- }
189
- let convertedMatch : Match
190
- if Match . self == Tuple2< Substring, DynamicCaptures> . self {
191
- convertedMatch = Tuple2 (
192
- input [ result. range] , DynamicCaptures . empty
193
- ) as! Match
194
- } else {
195
- assert ( Match . self == Substring . self)
196
- convertedMatch = input [ result. range] as! Match
197
- }
198
- return RegexMatch ( range: result. range, match: convertedMatch)
199
- }
200
- }
201
-
202
- extension String {
203
- public func match< R: RegexProtocol > ( _ regex: R ) -> RegexMatch < R . Match > ? {
204
- regex. match ( in: self )
205
- }
206
-
207
- public func match< R: RegexProtocol > (
208
- @RegexBuilder _ content: ( ) -> R
209
- ) -> RegexMatch < R . Match > ? {
210
- match ( content ( ) )
211
- }
212
- }
213
- extension Substring {
214
- public func match< R: RegexProtocol > ( _ regex: R ) -> RegexMatch < R . Match > ? {
215
- regex. match ( in: self )
216
- }
217
-
218
- public func match< R: RegexProtocol > (
219
- @RegexBuilder _ content: ( ) -> R
220
- ) -> RegexMatch < R . Match > ? {
221
- match ( content ( ) )
222
- }
223
- }
224
129
public struct MockRegexLiteral < Match: MatchProtocol > : RegexProtocol {
225
130
public typealias MatchValue = Substring
226
131
public let regex : Regex < Match >
@@ -247,10 +152,4 @@ public struct EmptyCapture: EmptyCaptureProtocol {}
247
152
extension Array : EmptyCaptureProtocol where Element: EmptyCaptureProtocol { }
248
153
extension Optional : EmptyCaptureProtocol where Wrapped: EmptyCaptureProtocol { }
249
154
250
- public protocol MatchProtocol {
251
- associatedtype Capture
252
- }
253
- extension Substring : MatchProtocol {
254
- public typealias Capture = EmptyCapture
255
- }
256
155
0 commit comments