From 27c941bbd22a4bbc53005a15a0440443fd892f70 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Fri, 26 May 2023 08:43:45 -0700 Subject: [PATCH] Fix `Formatted` parser (#301) * Fix `Formatted` parser It should only consume the match range. * wip --- .../Parsing/ParserPrinters/ParseableFormatStyle.swift | 2 +- Tests/ParsingTests/ParseableFormatTests.swift | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/Parsing/ParserPrinters/ParseableFormatStyle.swift b/Sources/Parsing/ParserPrinters/ParseableFormatStyle.swift index a5258dd4b3..2123945782 100644 --- a/Sources/Parsing/ParserPrinters/ParseableFormatStyle.swift +++ b/Sources/Parsing/ParserPrinters/ParseableFormatStyle.swift @@ -24,7 +24,7 @@ at: input ) } - input.removeFirst(input.distance(from: input.startIndex, to: input.endIndex)) + input.removeFirst(input.distance(from: match.range.lowerBound, to: match.range.upperBound)) return match.output } diff --git a/Tests/ParsingTests/ParseableFormatTests.swift b/Tests/ParsingTests/ParseableFormatTests.swift index a3c2aa73c2..202b6e832c 100644 --- a/Tests/ParsingTests/ParseableFormatTests.swift +++ b/Tests/ParsingTests/ParseableFormatTests.swift @@ -19,5 +19,15 @@ "TOTAL: $42.42" ) } + + func testFormatted_PartiallyConsumes() throws { + var input = "COORD: 12.34°N"[...] + let p = Parse { + "COORD: " + Formatted(.number) + } + XCTAssertEqual(12.34, try p.parse(&input)) + XCTAssertEqual(String(input), "°N") + } } #endif