From e3c02bc8cbb84b3583ffb831aaeb8f503ffa2e67 Mon Sep 17 00:00:00 2001 From: Richard Wei Date: Fri, 11 Feb 2022 02:25:58 -0800 Subject: [PATCH] Static lookup of character classes in generic contexts Eliminates quantifier overloads on character classes by utilizing SE-0299. --- .../_StringProcessing/CharacterClass.swift | 4 +- Sources/_StringProcessing/RegexDSL/DSL.swift | 22 --------- Tests/RegexTests/RegexDSLTests.swift | 46 +++++++++---------- 3 files changed, 25 insertions(+), 47 deletions(-) diff --git a/Sources/_StringProcessing/CharacterClass.swift b/Sources/_StringProcessing/CharacterClass.swift index 334f38167..124669a43 100644 --- a/Sources/_StringProcessing/CharacterClass.swift +++ b/Sources/_StringProcessing/CharacterClass.swift @@ -178,7 +178,7 @@ public struct CharacterClass: Hashable { } } -extension CharacterClass { +extension RegexProtocol where Self == CharacterClass { public static var any: CharacterClass { .init(cc: .any, matchLevel: .graphemeCluster) } @@ -216,7 +216,7 @@ extension CharacterClass { } public static func custom( - _ components: [CharacterSetComponent] + _ components: [CharacterClass.CharacterSetComponent] ) -> CharacterClass { .init(cc: .custom(components), matchLevel: .graphemeCluster) } diff --git a/Sources/_StringProcessing/RegexDSL/DSL.swift b/Sources/_StringProcessing/RegexDSL/DSL.swift index 80cdc9414..f5d933e95 100644 --- a/Sources/_StringProcessing/RegexDSL/DSL.swift +++ b/Sources/_StringProcessing/RegexDSL/DSL.swift @@ -145,28 +145,6 @@ postfix operator .? postfix operator .* postfix operator .+ -// Overloads for quantifying over a character class. -public func zeroOrOne( - _ cc: CharacterClass, - _ behavior: QuantificationBehavior = .eagerly -) -> Regex { - .init(node: .quantification(.zeroOrOne, behavior.astKind, cc.regex.root)) -} - -public func many( - _ cc: CharacterClass, - _ behavior: QuantificationBehavior = .eagerly -) -> Regex { - .init(node: .quantification(.zeroOrMore, behavior.astKind, cc.regex.root)) -} - -public func oneOrMore( - _ cc: CharacterClass, - _ behavior: QuantificationBehavior = .eagerly -) -> Regex { - .init(node: .quantification(.oneOrMore, behavior.astKind, cc.regex.root)) -} - // MARK: Alternation // TODO: Variadic generics diff --git a/Tests/RegexTests/RegexDSLTests.swift b/Tests/RegexTests/RegexDSLTests.swift index aa7b89d32..69bd30c74 100644 --- a/Tests/RegexTests/RegexDSLTests.swift +++ b/Tests/RegexTests/RegexDSLTests.swift @@ -59,8 +59,8 @@ class RegexDSLTests: XCTestCase { ("a c", ("a c", " ", "c")), captureType: (Substring, Substring, Substring).self, ==) { - CharacterClass.any - capture(CharacterClass.whitespace) // Substring + .any + capture(.whitespace) // Substring capture("c") // Substring } } @@ -140,7 +140,7 @@ class RegexDSLTests: XCTestCase { "a".+ capture(oneOrMore(Character("b"))) // Substring capture(many("c")) // Substring - capture(CharacterClass.hexDigit).* // [Substring] + capture(.hexDigit).* // [Substring] "e".? capture("t" | "k") // Substring oneOf { capture("k"); capture("j") } // (Substring?, Substring?) @@ -154,7 +154,7 @@ class RegexDSLTests: XCTestCase { { oneOrMore { oneOrMore(.word) - capture(CharacterClass.digit) + capture(.digit) } } @@ -164,7 +164,7 @@ class RegexDSLTests: XCTestCase { { oneOrMore { oneOrMore(.word, .reluctantly) - capture(CharacterClass.digit) + capture(.digit) } } @@ -174,9 +174,9 @@ class RegexDSLTests: XCTestCase { { oneOrMore { oneOrMore(.reluctantly) { - CharacterClass.word + .word } - capture(CharacterClass.digit) + capture(.digit) } } } @@ -293,13 +293,13 @@ class RegexDSLTests: XCTestCase { func testUnicodeScalarPostProcessing() throws { let spaces = Regex { many { - CharacterClass.whitespace + .whitespace } } let unicodeScalar = Regex { oneOrMore { - CharacterClass.hexDigit + .hexDigit } spaces } @@ -316,12 +316,12 @@ class RegexDSLTests: XCTestCase { capture { oneOrMore { - CharacterClass.word + .word } } many { - CharacterClass.any + .any } } @@ -342,19 +342,19 @@ class RegexDSLTests: XCTestCase { let regexWithCapture = Regex { capture { - oneOrMore(CharacterClass.hexDigit) + oneOrMore(.hexDigit) } transform: { Unicode.Scalar(hex: $0) } optionally { ".." capture { - oneOrMore(CharacterClass.hexDigit) + oneOrMore(.hexDigit) } transform: { Unicode.Scalar(hex: $0) } } - oneOrMore(CharacterClass.whitespace) + oneOrMore(.whitespace) ";" - oneOrMore(CharacterClass.whitespace) - capture(oneOrMore(CharacterClass.word)) - many(CharacterClass.any) + oneOrMore(.whitespace) + capture(oneOrMore(.word)) + many(.any) } // Regex<(Substring, Unicode.Scalar?, Unicode.Scalar??, Substring)> do { // Assert the inferred capture type. @@ -373,23 +373,23 @@ class RegexDSLTests: XCTestCase { let regexWithTryCapture = Regex { tryCapture { - oneOrMore(CharacterClass.hexDigit) + oneOrMore(.hexDigit) } transform: { Unicode.Scalar(hex: $0) } optionally { ".." tryCapture { - oneOrMore(CharacterClass.hexDigit) + oneOrMore(.hexDigit) } transform: { Unicode.Scalar(hex: $0) } } - oneOrMore(CharacterClass.whitespace) + oneOrMore(.whitespace) ";" - oneOrMore(CharacterClass.whitespace) - capture(oneOrMore(CharacterClass.word)) - many(CharacterClass.any) + oneOrMore(.whitespace) + capture(oneOrMore(.word)) + many(.any) } // Regex<(Substring, Unicode.Scalar, Unicode.Scalar?, Substring)> do { // Assert the inferred capture type.