File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -166,6 +166,31 @@ We also propose the following regex-powered algorithms as well as their generic
166
166
| `prefixMatch (of: )`| Matches the specified `RegexComponent` against the collection at the beginning |
167
167
| `matches (of: )`| Returns a collection containing all matches of the specified `RegexComponent` |
168
168
169
+ We also propose an overload of `~= ` allowing regexes to be used in `case ` expressions:
170
+
171
+ ```swift
172
+ switch " abcde" {
173
+ case / a. * f/ : // never taken
174
+ case / abc/ : // never taken
175
+ case / ab. * e/ : return " success"
176
+ default : // never taken
177
+ }
178
+
179
+ switch " 2022-04-22" {
180
+ case decimalParser: // never taken
181
+
182
+ case OneOrMore {
183
+ CharacterClass.whitespace
184
+ }: // never taken
185
+
186
+ case #/ \d {2} /\d {2} /\d {4} /# // never taken
187
+
188
+ case dateParser: return " success"
189
+
190
+ default : // never taken
191
+ }
192
+ ```
193
+
169
194
170
195
## Detailed design
171
196
@@ -1025,6 +1050,19 @@ extension RangeReplaceableCollection where Element: Equatable {
1025
1050
}
1026
1051
```
1027
1052
1053
+ ### Language- level pattern matching via `~= `
1054
+
1055
+ We propose allowing any regex component be used in case statements by overloading the `~= ` operator for matching against the entire input:
1056
+
1057
+ ```swift
1058
+ extension RegexComponent {
1059
+ public static func ~= (regex : Self , input : String ) -> Bool
1060
+
1061
+ public static func ~= (regex : Self , input : Substring) -> Bool
1062
+ }
1063
+ ```
1064
+
1065
+
1028
1066
[SE- 0346 ]: https: // github.com/apple/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md
1029
1067
[stdlib- pitch]: https: // forums.swift.org/t/pitch-primary-associated-types-in-the-standard-library/56426
1030
1068
Original file line number Diff line number Diff line change @@ -116,6 +116,9 @@ class RegexConsumerTests: XCTestCase {
116
116
} :
117
117
XCTFail ( )
118
118
119
+ case OneOrMore { CharacterClass . whitespace } :
120
+ XCTFail ( )
121
+
119
122
case " abc " :
120
123
XCTFail ( )
121
124
You can’t perform that action at this time.
0 commit comments