@@ -29,48 +29,54 @@ extension RegexProtocol {
29
29
input. base, in: input. startIndex..< input. endIndex)
30
30
}
31
31
32
- // FIXME: This is mostly hacky because we go down two different paths based on
33
- // whether there are captures. This will be cleaned up once we deprecate the
34
- // legacy virtual machines.
35
- func _match(
32
+ // TODO: Should we expose parameters for testing?
33
+ // Currently, tests just use the execution interface directly.
34
+ func _performLegacyMatch(
36
35
_ input: String ,
37
36
in inputRange: Range < String . Index > ,
38
- mode: MatchMode = . wholeString
37
+ mode: MatchMode
39
38
) -> RegexMatch < Match > ? {
40
- // TODO: Remove this branch when the matching engine supports captures.
41
- if regex. hasCapture {
42
- let vm = HareVM ( program: regex. program. legacyLoweredProgram)
43
- guard let ( range, captures) = vm. execute (
44
- input: input, in: inputRange, mode: mode
45
- ) ? . destructure else {
46
- return nil
47
- }
48
- let convertedMatch : Match
49
- if Match . self == ( Substring, DynamicCaptures) . self {
50
- convertedMatch = ( input [ range] , DynamicCaptures ( captures) ) as! Match
51
- } else {
52
- let typeErasedMatch = captures. matchValue (
53
- withWholeMatch: input [ range]
54
- )
55
- convertedMatch = typeErasedMatch as! Match
56
- }
57
- return RegexMatch ( range: range, match: convertedMatch)
39
+ let vm = HareVM ( program: regex. program. legacyLoweredProgram)
40
+ guard let ( range, captures) = vm. execute (
41
+ input: input, in: inputRange, mode: mode
42
+ ) ? . destructure else {
43
+ return nil
44
+ }
45
+ let convertedMatch : Match
46
+ if Match . self == ( Substring, DynamicCaptures) . self {
47
+ convertedMatch = ( input [ range] , DynamicCaptures ( captures) ) as! Match
48
+ } else {
49
+ let typeErasedMatch = captures. matchValue (
50
+ withWholeMatch: input [ range]
51
+ )
52
+ convertedMatch = typeErasedMatch as! Match
58
53
}
54
+ return RegexMatch ( range: range, match: convertedMatch)
55
+ }
59
56
57
+ func _match(
58
+ _ input: String ,
59
+ in inputRange: Range < String . Index > ,
60
+ mode: MatchMode = . wholeString
61
+ ) -> RegexMatch < Match > ? {
60
62
let executor = Executor ( program: regex. program. loweredProgram)
61
- guard let result = executor. execute (
63
+ guard let ( range , captures ) = executor. execute (
62
64
input: input, in: inputRange, mode: mode
63
- ) else {
65
+ ) ? . destructure else {
64
66
return nil
65
67
}
66
68
let convertedMatch : Match
67
69
if Match . self == ( Substring, DynamicCaptures) . self {
68
- convertedMatch = ( input [ result. range] , DynamicCaptures . empty) as! Match
70
+ convertedMatch = ( input [ range] , DynamicCaptures ( captures) ) as! Match
71
+ } else if Match . self == Substring . self {
72
+ convertedMatch = input [ range] as! Match
69
73
} else {
70
- assert ( Match . self == Substring . self)
71
- convertedMatch = input [ result. range] as! Match
74
+ let typeErasedMatch = captures. matchValue (
75
+ withWholeMatch: input [ range]
76
+ )
77
+ convertedMatch = typeErasedMatch as! Match
72
78
}
73
- return RegexMatch ( range: result . range, match: convertedMatch)
79
+ return RegexMatch ( range: range, match: convertedMatch)
74
80
}
75
81
}
76
82
0 commit comments