Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of unquoted URLs #409

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Sources/Yams/Constructor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,11 @@ public protocol SexagesimalConvertible: ExpressibleByIntegerLiteral {
}

private extension SexagesimalConvertible {
init(sexagesimal value: String) {
self = value.sexagesimal()
init?(sexagesimal value: String) {
guard let value = value.sexagesimal() as Self? else {
return nil
}
self = value
}
}

Expand Down Expand Up @@ -575,7 +578,7 @@ extension Int64: SexagesimalConvertible {}
extension UInt64: SexagesimalConvertible {}

private extension String {
func sexagesimal<T>() -> T where T: SexagesimalConvertible {
func sexagesimal<T>() -> T? where T: SexagesimalConvertible {
assert(contains(":"))
var scalar = self

Expand All @@ -589,7 +592,12 @@ private extension String {
} else {
sign = 1
}
let digits = scalar.components(separatedBy: ":").compactMap(T.create).reversed()
let components = scalar.components(separatedBy: ":")
let mappedComponents = components.compactMap(T.create)
guard mappedComponents.count == components.count else {
return nil
}
let digits = mappedComponents.reversed()
let (_, value) = digits.reduce((1, 0) as (T, T)) { baseAndValue, digit in
let value = baseAndValue.1 + (digit * baseAndValue.0)
let base = baseAndValue.0 * 60
Expand Down
7 changes: 7 additions & 0 deletions Tests/YamsTests/ConstructorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ class ConstructorTests: XCTestCase { // swiftlint:disable:this type_body_length
XCTAssertEqual(nodes[1]["link with"]?[1]?["="], "library2.dll")
XCTAssertEqual(nodes[1]["link with"]?[1]?["version"], "2.3")
}

func testStrictParsingSexagesimal() throws {
XCTAssertEqual(Int.construct(from: .init("12:34:56")), 45296)
XCTAssertEqual(Double.construct(from: .init("12:34:56.789")), 45296.789)
XCTAssertNil(Int.construct(from: .init("http://example.com")))
XCTAssertNil(Double.construct(from: .init("http://example.com")))
}
}

extension ConstructorTests {
Expand Down
Loading