Skip to content

Commit

Permalink
Fix parsing of unquoted URLs (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
czechboy0 committed Mar 15, 2024
1 parent 9058878 commit 626b33b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
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

0 comments on commit 626b33b

Please sign in to comment.