Skip to content

Commit

Permalink
Prefix should match when maxLength < match.count (#50)
Browse files Browse the repository at this point in the history
* Prefix should match when maxLength < match.count

* cleanup

* wip
  • Loading branch information
stephencelis authored Sep 7, 2021
1 parent 3df762d commit 5cae667
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
},
{
"package": "swift-benchmark",
"package": "Benchmark",
"repositoryURL": "https://github.com/google/swift-benchmark",
"state": {
"branch": null,
Expand Down
19 changes: 6 additions & 13 deletions Sources/Parsing/Parsers/Prefix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,12 @@ where

@inlinable
public func parse(_ input: inout Input) -> Input? {
if let predicate = self.predicate {
let prefix = input.prefix(while: predicate)
let count = prefix.count
guard count >= self.minLength, self.maxLength.map({ count <= $0 }) ?? true else { return nil }
input.removeFirst(count)
return prefix
} else {
let prefix = self.maxLength.map(input.prefix) ?? input
let count = prefix.count
guard count >= self.minLength else { return nil }
input.removeFirst(count)
return prefix
}
var prefix = maxLength.map(input.prefix) ?? input
prefix = predicate.map { prefix.prefix(while: $0) } ?? prefix
let count = prefix.count
guard count >= self.minLength else { return nil }
input.removeFirst(count)
return prefix
}
}

Expand Down
6 changes: 6 additions & 0 deletions Tests/ParsingTests/PrefixTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ final class PrefixTests: XCTestCase {
XCTAssertEqual("42", Prefix(...10, while: { $0.isNumber }).parse(&input))
XCTAssertEqual(" Hello, world!", input)
}

func testPrefixLengthFromWhileSuccess() {
var input = "42 Hello, world!"[...]
XCTAssertEqual("4", Prefix(1, while: { $0.isNumber }).parse(&input))
XCTAssertEqual("2 Hello, world!", input)
}
}

0 comments on commit 5cae667

Please sign in to comment.