Skip to content

Commit 796d69b

Browse files
authored
Merge pull request #157 from rxwei/character-class-protocol-lookup
Static lookup of character classes in generic contexts
2 parents 0f6525e + e3c02bc commit 796d69b

File tree

3 files changed

+25
-47
lines changed

3 files changed

+25
-47
lines changed

Sources/_StringProcessing/CharacterClass.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public struct CharacterClass: Hashable {
178178
}
179179
}
180180

181-
extension CharacterClass {
181+
extension RegexProtocol where Self == CharacterClass {
182182
public static var any: CharacterClass {
183183
.init(cc: .any, matchLevel: .graphemeCluster)
184184
}
@@ -216,7 +216,7 @@ extension CharacterClass {
216216
}
217217

218218
public static func custom(
219-
_ components: [CharacterSetComponent]
219+
_ components: [CharacterClass.CharacterSetComponent]
220220
) -> CharacterClass {
221221
.init(cc: .custom(components), matchLevel: .graphemeCluster)
222222
}

Sources/_StringProcessing/RegexDSL/DSL.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,6 @@ postfix operator .?
145145
postfix operator .*
146146
postfix operator .+
147147

148-
// Overloads for quantifying over a character class.
149-
public func zeroOrOne(
150-
_ cc: CharacterClass,
151-
_ behavior: QuantificationBehavior = .eagerly
152-
) -> Regex<Substring> {
153-
.init(node: .quantification(.zeroOrOne, behavior.astKind, cc.regex.root))
154-
}
155-
156-
public func many(
157-
_ cc: CharacterClass,
158-
_ behavior: QuantificationBehavior = .eagerly
159-
) -> Regex<Substring> {
160-
.init(node: .quantification(.zeroOrMore, behavior.astKind, cc.regex.root))
161-
}
162-
163-
public func oneOrMore(
164-
_ cc: CharacterClass,
165-
_ behavior: QuantificationBehavior = .eagerly
166-
) -> Regex<Substring> {
167-
.init(node: .quantification(.oneOrMore, behavior.astKind, cc.regex.root))
168-
}
169-
170148
// MARK: Alternation
171149

172150
// TODO: Variadic generics

Tests/RegexTests/RegexDSLTests.swift

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class RegexDSLTests: XCTestCase {
5959
("a c", ("a c", " ", "c")),
6060
captureType: (Substring, Substring, Substring).self, ==)
6161
{
62-
CharacterClass.any
63-
capture(CharacterClass.whitespace) // Substring
62+
.any
63+
capture(.whitespace) // Substring
6464
capture("c") // Substring
6565
}
6666
}
@@ -140,7 +140,7 @@ class RegexDSLTests: XCTestCase {
140140
"a".+
141141
capture(oneOrMore(Character("b"))) // Substring
142142
capture(many("c")) // Substring
143-
capture(CharacterClass.hexDigit).* // [Substring]
143+
capture(.hexDigit).* // [Substring]
144144
"e".?
145145
capture("t" | "k") // Substring
146146
oneOf { capture("k"); capture("j") } // (Substring?, Substring?)
@@ -154,7 +154,7 @@ class RegexDSLTests: XCTestCase {
154154
{
155155
oneOrMore {
156156
oneOrMore(.word)
157-
capture(CharacterClass.digit)
157+
capture(.digit)
158158
}
159159
}
160160

@@ -164,7 +164,7 @@ class RegexDSLTests: XCTestCase {
164164
{
165165
oneOrMore {
166166
oneOrMore(.word, .reluctantly)
167-
capture(CharacterClass.digit)
167+
capture(.digit)
168168
}
169169
}
170170

@@ -174,9 +174,9 @@ class RegexDSLTests: XCTestCase {
174174
{
175175
oneOrMore {
176176
oneOrMore(.reluctantly) {
177-
CharacterClass.word
177+
.word
178178
}
179-
capture(CharacterClass.digit)
179+
capture(.digit)
180180
}
181181
}
182182
}
@@ -293,13 +293,13 @@ class RegexDSLTests: XCTestCase {
293293
func testUnicodeScalarPostProcessing() throws {
294294
let spaces = Regex {
295295
many {
296-
CharacterClass.whitespace
296+
.whitespace
297297
}
298298
}
299299

300300
let unicodeScalar = Regex {
301301
oneOrMore {
302-
CharacterClass.hexDigit
302+
.hexDigit
303303
}
304304
spaces
305305
}
@@ -316,12 +316,12 @@ class RegexDSLTests: XCTestCase {
316316

317317
capture {
318318
oneOrMore {
319-
CharacterClass.word
319+
.word
320320
}
321321
}
322322

323323
many {
324-
CharacterClass.any
324+
.any
325325
}
326326
}
327327

@@ -342,19 +342,19 @@ class RegexDSLTests: XCTestCase {
342342

343343
let regexWithCapture = Regex {
344344
capture {
345-
oneOrMore(CharacterClass.hexDigit)
345+
oneOrMore(.hexDigit)
346346
} transform: { Unicode.Scalar(hex: $0) }
347347
optionally {
348348
".."
349349
capture {
350-
oneOrMore(CharacterClass.hexDigit)
350+
oneOrMore(.hexDigit)
351351
} transform: { Unicode.Scalar(hex: $0) }
352352
}
353-
oneOrMore(CharacterClass.whitespace)
353+
oneOrMore(.whitespace)
354354
";"
355-
oneOrMore(CharacterClass.whitespace)
356-
capture(oneOrMore(CharacterClass.word))
357-
many(CharacterClass.any)
355+
oneOrMore(.whitespace)
356+
capture(oneOrMore(.word))
357+
many(.any)
358358
} // Regex<(Substring, Unicode.Scalar?, Unicode.Scalar??, Substring)>
359359
do {
360360
// Assert the inferred capture type.
@@ -373,23 +373,23 @@ class RegexDSLTests: XCTestCase {
373373

374374
let regexWithTryCapture = Regex {
375375
tryCapture {
376-
oneOrMore(CharacterClass.hexDigit)
376+
oneOrMore(.hexDigit)
377377
} transform: {
378378
Unicode.Scalar(hex: $0)
379379
}
380380
optionally {
381381
".."
382382
tryCapture {
383-
oneOrMore(CharacterClass.hexDigit)
383+
oneOrMore(.hexDigit)
384384
} transform: {
385385
Unicode.Scalar(hex: $0)
386386
}
387387
}
388-
oneOrMore(CharacterClass.whitespace)
388+
oneOrMore(.whitespace)
389389
";"
390-
oneOrMore(CharacterClass.whitespace)
391-
capture(oneOrMore(CharacterClass.word))
392-
many(CharacterClass.any)
390+
oneOrMore(.whitespace)
391+
capture(oneOrMore(.word))
392+
many(.any)
393393
} // Regex<(Substring, Unicode.Scalar, Unicode.Scalar?, Substring)>
394394
do {
395395
// Assert the inferred capture type.

0 commit comments

Comments
 (0)