diff --git a/CHANGELOG.md b/CHANGELOG.md index 733e4597..364b0015 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ has the rules. ## [9.1.0] — 2026-10-17 +### Fixed + +- Literals: whitespace at either end of a quoted or escaped range bound is dropped, as in PostgreSQL, so `[" 2026-01-01",2026-02-01)` parses, where it threw `FormatException` ([Literals](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/serialization.md)). + ## [9.0.0] — 2026-10-11 Nothing was removed or renamed. What existing calls write, accept and translate changed, and the compiler can't show you where: [Migration from v8.x](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#migration-from-v8x). diff --git a/docs/serialization.md b/docs/serialization.md index 6e334dac..16766350 100644 --- a/docs/serialization.md +++ b/docs/serialization.md @@ -196,7 +196,8 @@ A bound is read the way PostgreSQL's `range_in` reads it: - **A doubled quote** inside a quoted section is a literal `"`. - **A backslash** takes the next character literally, inside quotes or out, so `[\1,5)` is `[1,5)`. -- **Whitespace** around the unquoted part of a bound is dropped. +- **Whitespace at either end of a bound** is dropped, inside quotes or out. So + `[" 2026-01-01",2026-02-01)` is `[2026-01-01,2026-02-01)`. Good to know: @@ -205,6 +206,10 @@ Good to know: (`FormatException`), as PostgreSQL does with `invalid input syntax for type integer: ""`. - **Before 9.0, an empty quoted bound read as infinite.** `["",5)` read as `(,4]`, and `("","")` as `(,)`. +- **Only ASCII whitespace is dropped:** space, tab, newline, carriage return, vertical tab and form + feed. A no-break space stays in the bound, so the bound fails, as it does in PostgreSQL. +- **Before 9.1, whitespace inside quotes was kept.** `[" 2026-01-01",2026-02-01)` failed + (`FormatException`), where PostgreSQL reads it. ### Bind a range from configuration diff --git a/src/CodoMetis.ValueRanges.NodaTime/CHANGELOG.md b/src/CodoMetis.ValueRanges.NodaTime/CHANGELOG.md index ce4c3f03..1ced9162 100644 --- a/src/CodoMetis.ValueRanges.NodaTime/CHANGELOG.md +++ b/src/CodoMetis.ValueRanges.NodaTime/CHANGELOG.md @@ -5,7 +5,9 @@ covers all four packages, which share one version number and release together. ## [9.1.0] — 2026-10-17 -No change in this package. +### Fixed + +- Literals: whitespace at either end of a quoted or escaped range bound is dropped, as in PostgreSQL, so `[" 2026-01-01",2026-02-01)` parses, where it threw `FormatException` ([Literals](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/serialization.md)). ## [9.0.0] — 2026-10-11 diff --git a/src/CodoMetis.ValueRanges/CHANGELOG.md b/src/CodoMetis.ValueRanges/CHANGELOG.md index 5e536d6c..61cf34ee 100644 --- a/src/CodoMetis.ValueRanges/CHANGELOG.md +++ b/src/CodoMetis.ValueRanges/CHANGELOG.md @@ -5,7 +5,9 @@ covers all four packages, which share one version number and release together. ## [9.1.0] — 2026-10-17 -No change in this package. +### Fixed + +- Literals: whitespace at either end of a quoted or escaped range bound is dropped, as in PostgreSQL, so `[" 2026-01-01",2026-02-01)` parses, where it threw `FormatException` ([Literals](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/serialization.md)). ## [9.0.0] — 2026-10-11 diff --git a/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs b/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs index 6eabbdf7..f4636984 100644 --- a/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs +++ b/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs @@ -71,20 +71,19 @@ internal static TRange Parse(ReadOnlySpan s, IFormatProvider? p /// Within a bound, a double quote opens or closes a quoted section anywhere ("1"2 is /// 12), a doubled quote inside one is a literal quote, and a backslash takes the next /// character literally, inside quotes or out. An unquoted, unescaped comma, ) or ] - /// ends the bound. Whitespace around the unquoted part is dropped: PostgreSQL hands it to the - /// element's input function, and every input function behind these types ignores it. That is - /// ASCII whitespace only (): a no-break space stays in the bound, - /// where the element parser refuses it as PostgreSQL's does. + /// ends the bound. Whitespace at either end of the bound text is dropped once the bound is read, + /// whether it was quoted, escaped or neither: PostgreSQL keeps it all and hands it to the + /// element's input function, and every input function behind these types strips it (measured + /// for all seven range types, #45). That is ASCII whitespace only (): + /// a no-break space stays in the bound, where the element parser refuses it as PostgreSQL's does. + /// Until 9.1.0 only unquoted whitespace was dropped, so [" 2026-01-01",2026-02-01) threw. /// /// private static string? ScanBound(ReadOnlySpan inner, ref int position, ReadOnlySpan literal) { - var start = position; - var sb = new StringBuilder(); - - // The length of the text up to its last character that is not unquoted whitespace. - var significant = 0; - var inQuotes = false; + var start = position; + var sb = new StringBuilder(); + var inQuotes = false; while (position < inner.Length && (inQuotes || inner[position] is not (',' or ')' or ']'))) { @@ -96,19 +95,16 @@ internal static TRange Parse(ReadOnlySpan s, IFormatProvider? p throw new FormatException($"The input string '{LiteralExcerpt.Of(literal)}' is not a valid range literal: unterminated escape."); sb.Append(inner[position++]); - significant = sb.Length; } else if (c == '"') { if (!inQuotes) { - inQuotes = true; - significant = sb.Length; + inQuotes = true; } else if (position < inner.Length && inner[position] == '"') { sb.Append('"'); - significant = sb.Length; position++; } else @@ -116,14 +112,9 @@ internal static TRange Parse(ReadOnlySpan s, IFormatProvider? p inQuotes = false; } } - else if (inQuotes || !LiteralWhiteSpace.Is(c)) + else { sb.Append(c); - significant = sb.Length; - } - else if (sb.Length > 0 || significant > 0) - { - sb.Append(c); // inner whitespace; dropped below if nothing significant follows } } @@ -132,8 +123,7 @@ internal static TRange Parse(ReadOnlySpan s, IFormatProvider? p if (position == start) return null; - sb.Length = significant; - return sb.ToString(); + return LiteralWhiteSpace.Trim(sb.ToString()).ToString(); } /// diff --git a/test/CodoMetis.ValueRanges.Conventions.Tests/RangeLiteralGrammarContractTests.cs b/test/CodoMetis.ValueRanges.Conventions.Tests/RangeLiteralGrammarContractTests.cs index 2ad34239..2a1664ba 100644 --- a/test/CodoMetis.ValueRanges.Conventions.Tests/RangeLiteralGrammarContractTests.cs +++ b/test/CodoMetis.ValueRanges.Conventions.Tests/RangeLiteralGrammarContractTests.cs @@ -9,8 +9,9 @@ namespace CodoMetis.ValueRanges.Conventions.Tests; /// Every range type reads a bound as PostgreSQL's range_in does: only an absent bound /// is infinite; a quoted "" or a bound of only whitespace is a value — one no element type here /// accepts; quotes may wrap any part of a bound and a backslash escapes the next character, inside -/// quotes or out. Swept through Parse, TryParse, the span overload, JSON, the -/// TypeConverter and the multirange literal. +/// quotes or out; ASCII whitespace at either end of a bound is stripped, quoted or not. Swept through +/// Parse, TryParse, the span overload, JSON, the TypeConverter and the +/// multirange literal. /// /// /// @@ -82,18 +83,36 @@ private static IEnumerable Failures() // Whitespace around an unquoted bound is not part of it for any element type here. Reads($"[ {lo} , {hi} )", finite); + // Nor is ASCII whitespace at either end of a quoted or escaped bound: range_in keeps it and + // every element input function behind these types strips it (#45, measured on PostgreSQL 17). + foreach (var ws in AsciiWhiteSpace) + { + Reads($"[\"{ws}{lo}\",\"{hi}{ws}\")", finite); + Reads($"[\"{ws}{lo}{ws}\",{hi})", finite); + Reads($"[\\{ws}{lo},{hi}\\{ws})", finite); + Reads($"[ \"{ws}{lo}\" ,{hi})", finite); + } + // Structure PostgreSQL refuses: a second comma, a closing bracket inside a bound, an // unterminated quote or escape, Unicode whitespace where only ASCII whitespace is skipped. foreach (var literal in new[] { $"[{lo},{hi},{hi})", $"[{lo}),{hi})", $"[{lo}],{hi})", $"[\"{lo},{hi})", $"[{lo},{hi}\\)", - // Only ASCII whitespace is skipped, as in PostgreSQL: a no-break space is bound text. - $"[\u00A0{lo},{hi})", $"\u00A0[{lo},{hi})" + // Only ASCII whitespace is skipped, as in PostgreSQL: a no-break space is bound text, + // quoted or not. + $"[\u00A0{lo},{hi})", $"\u00A0[{lo},{hi})", $"[\"\u00A0{lo}\",{hi})", $"[{lo},\"{hi}\u2003\")" }) { Refused(literal); } + // A quoted or escaped bound of only whitespace is present and empty once stripped. + foreach (var ws in AsciiWhiteSpace) + { + Refused($"[\"{ws}\",{hi})"); + Refused($"[{lo},\\{ws})"); + } + return failures; void Refused(string literal) @@ -147,5 +166,8 @@ void Reads(string literal, TRange expected) } } + /// The whitespace PostgreSQL's element input functions strip: isspace in the C locale. + private static readonly string[] AsciiWhiteSpace = [" ", "\t", "\n", "\r", "\v", "\f"]; + private static string EscapeEach(string text) => string.Concat(text.Select(c => $"\\{c}")); } diff --git a/test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/LiteralGrammarParityTests.cs b/test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/LiteralGrammarParityTests.cs index c6f35ccb..57c9d2cf 100644 --- a/test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/LiteralGrammarParityTests.cs +++ b/test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/LiteralGrammarParityTests.cs @@ -46,12 +46,31 @@ public sealed class LiteralGrammarParityTests [ "[2024-01-01,2024-02-01)", "[\"2024-01-01\",\"2024-02-01\")", "[\"\",2024-02-01)", "[ ,2024-02-01)", @"[2024\-01\-01,2024-02-01)", "(,infinity)", "[-infinity,2024-01-01)", - // Whitespace around an unquoted bound is the range grammar's to skip. Inside quotes it reaches - // the element, where PostgreSQL's date_in trims it and the strict ISO grammar refuses it — - // deliberately, and loudly (TemporalLiteralGrammarTests.Date_Rejects), so it is not listed. - "[ 2024-01-01 , 2024-02-01 )", " [2024-01-01,2024-02-01) " + "[ 2024-01-01 , 2024-02-01 )", " [2024-01-01,2024-02-01) ", "[\" 2024-01-01\",2024-02-01)" ]; + /// + /// Each range type with a lower and upper bound text its element parser and PostgreSQL's both + /// read. The server's output goes back through the same parser, so the comparison is by value. + /// + private static readonly (string Type, string Low, string High, Func Client)[] BoundWhitespaceTypes = + [ + ("int4range", "1", "5", l => Int32Range.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused"), + ("int8range", "1", "5", l => Int64Range.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused"), + ("numrange", "1.5", "5", l => DecimalRange.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused"), + ("daterange", "2026-01-01", "2026-02-01", l => DateRange.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused"), + ("daterange", "-infinity", "2026-02-01", l => DateRange.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused"), + ("tsrange", "2026-01-01 10:00:00", "2026-02-01 00:00:00", l => DateTimeRange.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused"), + ("tstzrange", "2026-01-01 10:00:00+00", "2026-02-01 00:00:00+00", l => DateTimeOffsetRange.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused"), + ("timerange", "09:00:00", "17:00:00", l => TimeRange.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused"), + ("daterange", "2026-01-01", "2026-02-01", l => LocalDateRange.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused"), + ("tsrange", "2026-01-01 10:00:00", "2026-02-01 00:00:00", l => LocalDateTimeRange.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused"), + ("tstzrange", "2026-01-01 10:00:00+00", "2026-02-01 00:00:00+00", l => InstantRange.TryParse(l, CultureInfo.InvariantCulture, out var r) ? r.ToString() : "refused") + ]; + + /// ASCII whitespace PostgreSQL strips, and two Unicode spaces it does not. + private static readonly string[] BoundWhiteSpace = [" ", "\t", "\n", "\r", "\v", "\f", "\u00A0", "\u2003"]; + private static readonly string[] Int4MultirangeLiterals = [ "{[1,5),[7,9)}", "{}", "{ [1,5) , [7,9) }", "{[\"\",5)}", "{[1,5),[\"\",9)}", "{[\"1\",\"5\")}", "{ [1,5)}" @@ -116,27 +135,61 @@ await Compare("int4multirange", Int4MultirangeLiterals, Assert.IsEmpty(failures, "Range literals read differently:\n" + string.Join("\n", failures)); - async Task Compare(string type, string[] literals, Func client) + Task Compare(string type, string[] literals, Func client) + => CompareRangeLiterals(connection, type, literals, client, failures); + } + + /// + /// Whitespace at either end of a quoted or escaped bound, and a bound of only whitespace, for + /// every range type: PostgreSQL's range_in keeps it in the bound and the element input + /// function decides (#45). Until 9.1.0 the library refused [" 2026-01-01",2026-02-01), + /// which PostgreSQL reads as [2026-01-01,2026-02-01). + /// + [TestMethod] + public async Task BoundWhitespace_ReadsAsPostgresReadsIt() + { + ContainerLifecycle.RequireDatabase(); + await using var connection = new NpgsqlConnection(ContainerLifecycle.ConnectionString); + await connection.OpenAsync(); + + var failures = new List(); + + foreach (var (type, lo, hi, client) in BoundWhitespaceTypes) { - foreach (var literal in literals) + var literals = BoundWhiteSpace.SelectMany(ws => new[] { - string server; - try - { - // Compared as values, not text: the library writes a discrete range closed - // ([1,4] for PostgreSQL's [1,5)) and an infinite date as MaxValue, so the - // server's canonical output goes through the same parser. - server = client((await ScalarAsync(connection, $"SELECT $1::{type}::text", literal))!); - } - catch (PostgresException) - { - server = "refused"; - } + $"[\"{ws}{lo}\",{hi})", $"[\"{lo}{ws}\",{hi})", $"[\"{ws}{lo}{ws}\",\"{ws}{hi}{ws}\")", + $"[\\{ws}{lo},{hi})", $"[{lo}\\{ws},{hi})", $"[ \"{ws}{lo}\" ,{hi})", + $"[\"{ws}\",{hi})", $"[{lo},\"{ws}{ws}\")", $"[\\{ws},{hi})" + }).ToArray(); + + await CompareRangeLiterals(connection, type, literals, client, failures); + } - var mine = client(literal); - if (mine != server) - failures.Add($" {type} {Show(literal)}: library {mine}, PostgreSQL {server}"); + Assert.IsEmpty(failures, "Bound whitespace read differently:\n" + string.Join("\n", failures)); + } + + private static async Task CompareRangeLiterals( + NpgsqlConnection connection, string type, string[] literals, Func client, List failures) + { + foreach (var literal in literals) + { + string server; + try + { + // Compared as values, not text: the library writes a discrete range closed + // ([1,4] for PostgreSQL's [1,5)) and an infinite date as MaxValue, so the + // server's canonical output goes through the same parser. + server = client((await ScalarAsync(connection, $"SELECT $1::{type}::text", literal))!); + } + catch (PostgresException) + { + server = "refused"; } + + var mine = client(literal); + if (mine != server) + failures.Add($" {type} {Show(literal)}: library {mine}, PostgreSQL {server}"); } } diff --git a/test/CodoMetis.ValueRanges.NodaTime.Tests/NodaTimeParseFormatTests.cs b/test/CodoMetis.ValueRanges.NodaTime.Tests/NodaTimeParseFormatTests.cs index a92da5d2..4daee1c6 100644 --- a/test/CodoMetis.ValueRanges.NodaTime.Tests/NodaTimeParseFormatTests.cs +++ b/test/CodoMetis.ValueRanges.NodaTime.Tests/NodaTimeParseFormatTests.cs @@ -170,4 +170,28 @@ public void TryParse_ValidLiteral_ReturnsTrue() Assert.IsTrue(LocalDateRange.TryParse("[2025-01-01,2025-01-31]", null, out var range)); Assert.AreEqual("[2025-01-01,2025-01-31]", range.ToString()); } + + /// + /// Whitespace at either end of a quoted bound is stripped for every NodaTime range type, as + /// PostgreSQL strips it (#45); a no-break space is not. + /// + [TestMethod] + public void Parse_QuotedBoundWithSurroundingWhitespace_ReadsTheTrimmedBound() + { + Assert.AreEqual( + LocalDateRange.CreateFinite(new LocalDate(2026, 1, 1), new LocalDate(2026, 2, 1), endInclusive: false), + LocalDateRange.Parse("[\" 2026-01-01\",\"2026-02-01\t\")", null)); + Assert.AreEqual( + LocalDateTimeRange.CreateFinite(new LocalDateTime(2026, 1, 1, 10, 0), new LocalDateTime(2026, 2, 1, 0, 0), endInclusive: false), + LocalDateTimeRange.Parse("[\" 2026-01-01 10:00:00\",2026-02-01T00:00:00)", null)); + Assert.AreEqual( + InstantRange.CreateFinite(Instant.FromUtc(2026, 1, 1, 10, 0), Instant.FromUtc(2026, 2, 1, 0, 0), endInclusive: false), + InstantRange.Parse("[\"\n2026-01-01T10:00:00Z \",2026-02-01T00:00:00Z)", null)); + Assert.AreEqual( + YearMonthRange.CreateFinite(new YearMonth(2026, 1), new YearMonth(2026, 3), endInclusive: false), + YearMonthRange.Parse("[\" 2026-01\",2026-03)", null)); + + Assert.ThrowsExactly(() => LocalDateRange.Parse("[\"\u00A02026-01-01\",2026-02-01)", null)); + Assert.ThrowsExactly(() => InstantRange.Parse("[\" \",2026-02-01T00:00:00Z)", null)); + } } diff --git a/test/CodoMetis.ValueRanges.Tests/RangeParseFormatTests.cs b/test/CodoMetis.ValueRanges.Tests/RangeParseFormatTests.cs index 93f5eef9..6f91a0e5 100644 --- a/test/CodoMetis.ValueRanges.Tests/RangeParseFormatTests.cs +++ b/test/CodoMetis.ValueRanges.Tests/RangeParseFormatTests.cs @@ -413,4 +413,43 @@ public void FormattingExamples_InTheSerializationGuide_PrintExactlyTheirComments Assert.AreEqual("(,)", Int32Range.Infinite.ToString()); Assert.AreEqual("empty", Int32Range.Empty.ToString()); } + + // ----------------------------------------------------------------------- + // Whitespace inside a quoted or escaped bound (#45) + // ----------------------------------------------------------------------- + + /// + /// A quoted bound with whitespace at either end reads as the bound without it, as PostgreSQL + /// reads it: range_in keeps the whitespace and every element input function strips it. + /// + /// + /// Until 9.1.0 only whitespace outside quotes was dropped, so the quoted space reached the strict + /// ISO grammar and the literal threw, where '[" 2026-01-01",2026-02-01)'::daterange is + /// [2026-01-01,2026-02-01). + /// + [TestMethod] + public void Parse_QuotedBoundWithSurroundingWhitespace_ReadsTheTrimmedBound() + { + var expected = DateRange.CreateFinite(new DateOnly(2026, 1, 1), new DateOnly(2026, 2, 1), endInclusive: false); + + Assert.AreEqual(expected, DateRange.Parse("[\" 2026-01-01\",2026-02-01)", CultureInfo.InvariantCulture)); + Assert.AreEqual(expected, DateRange.Parse("[\"2026-01-01\t\",\"\n2026-02-01 \")", CultureInfo.InvariantCulture)); + Assert.AreEqual(expected, DateRange.Parse("[\\ 2026-01-01,2026-02-01\\ )", CultureInfo.InvariantCulture)); + Assert.AreEqual( + TimeRange.CreateFinite(new TimeOnly(9, 0), new TimeOnly(17, 0), endInclusive: false), + TimeRange.Parse("[\" 09:00 \",17:00)", CultureInfo.InvariantCulture)); + } + + /// + /// Only ASCII whitespace is stripped, as in PostgreSQL, and a bound that is nothing but + /// whitespace is still present and empty — refused, never read as unbounded. + /// + [TestMethod] + [DataRow("[\"\u00A02026-01-01\",2026-02-01)")] + [DataRow("[\"2026-01-01\u2003\",2026-02-01)")] + [DataRow("[\" \",2026-02-01)")] + [DataRow("[\"\t\n\",2026-02-01)")] + [DataRow("[\\ ,2026-02-01)")] + public void Parse_QuotedBound_NonAsciiOrOnlyWhitespace_IsRefused(string literal) + => Assert.ThrowsExactly(() => DateRange.Parse(literal, CultureInfo.InvariantCulture)); } diff --git a/test/oracles/claims.json b/test/oracles/claims.json index 744505a9..0139ac91 100644 --- a/test/oracles/claims.json +++ b/test/oracles/claims.json @@ -1205,6 +1205,21 @@ "TemporalLiteralStrictnessContractTests" ] }, + { + "id": "J33-bound-whitespace-stripped", + "source": "docs/serialization.md#quote-a-bound", + "quote": "**Whitespace at either end of a bound** is dropped, inside quotes or out. So `[\" 2026-01-01\",2026-02-01)` is `[2026-01-01,2026-02-01)`.", + "claim": "For every range type, ASCII whitespace (space, tab, LF, CR, VT, FF) at either end of a bound is stripped whether it is quoted, backslash-escaped or bare, as PostgreSQL's element input functions strip it; a no-break or em space is kept and the bound throws, and a bound of only whitespace throws.", + "oracle": "differential", + "surfaces": [ + "*Range.Parse", + "*Range.TryParse" + ], + "heldBy": [ + "RangeLiteralGrammarContractTests", + "LiteralGrammarParityTests" + ] + }, { "id": "M01-rangeset-invariant-on-construction", "source": "docs/ranges.md#rangeset--multirange-support", diff --git a/test/oracles/seeds/quoted-bound-whitespace-kept.patch b/test/oracles/seeds/quoted-bound-whitespace-kept.patch new file mode 100644 index 00000000..384cf8ce --- /dev/null +++ b/test/oracles/seeds/quoted-bound-whitespace-kept.patch @@ -0,0 +1,73 @@ +Seed: whitespace inside a quoted or escaped range bound is kept, so [" 2026-01-01",2026-02-01) throws where PostgreSQL's range_in reads [2026-01-01,2026-02-01) +Oracles: RangeLiteralGrammarContractTests, LiteralGrammarParityTests +Fails: the quoted and escaped bound-whitespace rows of the range_in grammar sweep for every temporal range type; the bound-whitespace parity sweep against PostgreSQL +History: shipped before 9.1.0 (#45) + +diff --git a/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs b/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs +--- a/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs ++++ b/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs +@@ -81,10 +81,13 @@ + /// + private static string? ScanBound(ReadOnlySpan inner, ref int position, ReadOnlySpan literal) + { +- var start = position; +- var sb = new StringBuilder(); +- var inQuotes = false; ++ var start = position; ++ var sb = new StringBuilder(); + ++ // The length of the text up to its last character that is not unquoted whitespace. ++ var significant = 0; ++ var inQuotes = false; ++ + while (position < inner.Length && (inQuotes || inner[position] is not (',' or ')' or ']'))) + { + var c = inner[position++]; +@@ -95,16 +98,19 @@ + throw new FormatException($"The input string '{LiteralExcerpt.Of(literal)}' is not a valid range literal: unterminated escape."); + + sb.Append(inner[position++]); ++ significant = sb.Length; + } + else if (c == '"') + { + if (!inQuotes) + { +- inQuotes = true; ++ inQuotes = true; ++ significant = sb.Length; + } + else if (position < inner.Length && inner[position] == '"') + { + sb.Append('"'); ++ significant = sb.Length; + position++; + } + else +@@ -112,10 +118,15 @@ + inQuotes = false; + } + } +- else ++ else if (inQuotes || !LiteralWhiteSpace.Is(c)) + { + sb.Append(c); ++ significant = sb.Length; + } ++ else if (sb.Length > 0 || significant > 0) ++ { ++ sb.Append(c); // inner whitespace; dropped below if nothing significant follows ++ } + } + + if (inQuotes) +@@ -123,7 +134,8 @@ + + if (position == start) return null; + +- return LiteralWhiteSpace.Trim(sb.ToString()).ToString(); ++ sb.Length = significant; ++ return sb.ToString(); + } + + /// diff --git a/test/oracles/seeds/quoted-empty-bound-is-infinite.patch b/test/oracles/seeds/quoted-empty-bound-is-infinite.patch index 7f07a84c..5cb1aee3 100644 --- a/test/oracles/seeds/quoted-empty-bound-is-infinite.patch +++ b/test/oracles/seeds/quoted-empty-bound-is-infinite.patch @@ -4,15 +4,18 @@ Fails: the range_in grammar sweep for every range type; the parity sweep against History: shipped before 9.0.0 diff --git a/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs b/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs -index 6eabbdf..848710a 100644 --- a/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs +++ b/src/CodoMetis.ValueRanges/Internals/RangeFormat.cs -@@ -130,7 +130,7 @@ internal static class RangeFormat +@@ -121,9 +121,10 @@ if (inQuotes) throw new FormatException($"The input string '{LiteralExcerpt.Of(literal)}' is not a valid range literal: unterminated quoted bound."); - if (position == start) return null; -+ if (significant == 0) return null; ++ var text = LiteralWhiteSpace.Trim(sb.ToString()).ToString(); ++ if (text.Length == 0) return null; - sb.Length = significant; - return sb.ToString(); +- return LiteralWhiteSpace.Trim(sb.ToString()).ToString(); ++ return text; + } + + ///