Skip to content

Commit 7240d30

Browse files
authored
Merge pull request #45 from kareman/Generic-input
Support generic input
2 parents 0655aa7 + 4f2a8e9 commit 7240d30

File tree

26 files changed

+588
-229
lines changed

26 files changed

+588
-229
lines changed

.github/workflows/Test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ jobs:
1717
steps:
1818
- name: Checkout
1919
uses: actions/checkout@v2
20+
- name: Set Swift version
21+
if: matrix.os == 'ubuntu-18.04'
22+
run: echo "5.3-DEVELOPMENT-SNAPSHOT-2020-08-08-a" > .swift-version
2023
- name: Install Swift
2124
uses: YOCKOW/Action-setup-swift@master
2225
- name: Build

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.2.4
1+
5.3

.swiftpm/xcode/xcshareddata/xcbaselines/PerformanceTests.xcbaseline/1868159C-3A7D-4BC9-A194-7BBFC5CE3264.plist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@
6565
<key>baselineIntegrationDisplayName</key>
6666
<string>7 May 2020 at 21:25:02</string>
6767
</dict>
68+
<key>com.apple.dt.XCTMetric_CPU.instructions_retired</key>
69+
<dict>
70+
<key>baselineAverage</key>
71+
<real>3.9872e+06</real>
72+
<key>baselineIntegrationDisplayName</key>
73+
<string>Local Baseline</string>
74+
</dict>
6875
</dict>
6976
<key>testOneOrMore()</key>
7077
<dict>

Sources/Patterns/Atomic Patterns/Line.swift

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,46 @@
55
// Created by Kåre Morstøl on 25/05/2020.
66
//
77

8+
public protocol CharacterLike: Hashable {
9+
var isNewline: Bool { get }
10+
}
11+
12+
extension Character: CharacterLike {}
13+
extension String.UTF8View.Element: CharacterLike {
14+
@inlinable
15+
public var isNewline: Bool {
16+
// “\n” (U+000A): LINE FEED (LF), U+000B: LINE TABULATION (VT), U+000C: FORM FEED (FF), “\r” (U+000D): CARRIAGE RETURN (CR)
17+
self < 14 && self > 9
18+
}
19+
}
20+
21+
// U+0085: NEXT LINE (NEL), U+2028: LINE SEPARATOR, U+2029: PARAGRAPH SEPARATOR
22+
@usableFromInline
23+
let newlines = Set([0x000A as UInt16, 0x000B, 0x000C, 0x000D, 0x0085, 0x2028, 0x2029].map { Unicode.Scalar($0)! })
24+
25+
extension String.UnicodeScalarView.Element: CharacterLike {
26+
@inlinable
27+
public var isNewline: Bool {
28+
newlines.contains(self)
29+
}
30+
}
31+
32+
extension String.UTF16View.Element: CharacterLike {
33+
@inlinable
34+
public var isNewline: Bool {
35+
Unicode.Scalar(self).map(newlines.contains(_:)) ?? false
36+
}
37+
}
38+
839
/// Matches one line, not including newline characters.
9-
public struct Line: Pattern {
40+
public struct Line<Input: BidirectionalCollection>: Pattern
41+
where Input.Element: CharacterLike, Input.Index == String.Index {
1042
public init() {}
1143

1244
public var description: String { "Line()" }
1345

14-
public static let start = Start()
15-
public static let end = End()
16-
1746
@inlinable
18-
public func createInstructions(_ instructions: inout Instructions) throws {
47+
public func createInstructions(_ instructions: inout ContiguousArray<Instruction<Input>>) throws {
1948
try (Start() Skip() End()).createInstructions(&instructions)
2049
}
2150

@@ -31,7 +60,7 @@ public struct Line: Pattern {
3160
}
3261

3362
@inlinable
34-
public func createInstructions(_ instructions: inout Instructions) {
63+
public func createInstructions(_ instructions: inout ContiguousArray<Instruction<Input>>) {
3564
instructions.append(.checkIndex(self.parse(_:at:)))
3665
}
3766
}
@@ -48,8 +77,13 @@ public struct Line: Pattern {
4877
}
4978

5079
@inlinable
51-
public func createInstructions(_ instructions: inout Instructions) {
80+
public func createInstructions(_ instructions: inout ContiguousArray<Instruction<Input>>) {
5281
instructions.append(.checkIndex(self.parse(_:at:)))
5382
}
5483
}
5584
}
85+
86+
extension Line where Input == String {
87+
public static let start = Start()
88+
public static let end = End()
89+
}

Sources/Patterns/Atomic Patterns/Literal.swift

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,75 @@ import Foundation
1010
/// Matches a sequence of elements.
1111
///
1212
/// If empty, it will always succeed without consuming any input.
13-
public struct Literal: Pattern {
13+
public struct Literal<Input: BidirectionalCollection>: Pattern where Input.Element: Hashable {
1414
public let elements: Input
1515

1616
public var description: String {
17-
#""\#(String(elements).replacingOccurrences(of: "\n", with: "\\n"))""#
17+
#""\#(String(describing: elements).replacingOccurrences(of: "\n", with: "\\n"))""#
18+
}
19+
20+
@inlinable
21+
public init(_ input: Input) {
22+
elements = input
1823
}
1924

2025
/// Matches `sequence`.
2126
@inlinable
22-
public init<S: Sequence>(_ sequence: S) where S.Element == Pattern.Input.Element {
23-
self.elements = Pattern.Input(sequence)
27+
public init<S: Sequence>(_ sequence: S) where S.Element == Input.Element, Input == String {
28+
self.elements = Input(sequence)
29+
}
30+
31+
@inlinable
32+
public func createInstructions(_ instructions: inout ContiguousArray<Instruction<Input>>) {
33+
instructions.append(contentsOf: elements.map(Instruction<Input>.elementEquals))
2434
}
35+
}
2536

37+
extension Literal where Input == String {
2638
/// Matches this character.
2739
@inlinable
2840
public init(_ character: Character) {
2941
self.init(String(character))
3042
}
43+
}
44+
45+
// MARK: Create from string literal.
3146

47+
extension Literal: ExpressibleByUnicodeScalarLiteral where Input: LosslessStringConvertible {
3248
@inlinable
33-
public func createInstructions(_ instructions: inout Instructions) {
34-
instructions.append(contentsOf: elements.map(Instruction.elementEquals))
49+
public init(unicodeScalarLiteral value: StaticString) {
50+
elements = Input(String(describing: value))!
3551
}
3652
}
3753

38-
extension Literal: ExpressibleByStringLiteral {
54+
extension Literal: ExpressibleByExtendedGraphemeClusterLiteral where Input: LosslessStringConvertible {
55+
public typealias ExtendedGraphemeClusterLiteralType = StaticString
56+
}
57+
58+
extension Literal: ExpressibleByStringLiteral where Input: LosslessStringConvertible {
3959
@inlinable
4060
public init(stringLiteral value: StaticString) {
41-
self.init(String(describing: value))
61+
elements = Input(String(describing: value))!
62+
}
63+
}
64+
65+
extension String.UTF8View: LosslessStringConvertible {
66+
@inlinable
67+
public init?(_ description: String) {
68+
self = description.utf8
69+
}
70+
}
71+
72+
extension String.UTF16View: LosslessStringConvertible {
73+
@inlinable
74+
public init?(_ description: String) {
75+
self = description.utf16
76+
}
77+
}
78+
79+
extension String.UnicodeScalarView: LosslessStringConvertible {
80+
@inlinable
81+
public init?(_ description: String) {
82+
self = description.unicodeScalars
4283
}
4384
}

0 commit comments

Comments
 (0)