Skip to content

feat: ISpanFormattable and IUtf8SpanFormattable on every range, range set and value set (#41) - #654

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
feat/41-span-formattable
Nov 30, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
feat/41-span-formattable

Conversation

@CaffeinatedCoder

@CaffeinatedCoder CaffeinatedCoder commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

Closes #41

Problem

Ranges, range sets and value sets were IFormattable only. String interpolation, Utf8.TryWrite and other span writers had to go through ToString, and generic code over the factory interfaces couldn't reach a span path without a type test. The maintainer decided (2026-11-30) to put ISpanFormattable and IUtf8SpanFormattable in the base lists of the three factory interfaces for 11.0.0, plus a static virtual TryFormatValue defaulting to FormatValue.

Change

  • Interfaces (Core/): IRangeFactory<TRange, T>, IRangeSetFactory<TSet, TRange, T> and IValueSetFactory<TSet, T> now extend ISpanFormattable and IUtf8SpanFormattable, with no default instance member (that is the Native AOT on .NET 11 RC2: every value set ToString traps (SIGTRAP) in the default IFormattable.ToString of IValueSetFactory<TSet, T> #291 trap). This is breaking for a range type of your own. The other two can't be implemented outside the library (internal members). TryFormatValue(T, Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?) is a static virtual on IRangeFactory and IValueSetFactory. Its default formats with FormatValue and copies.
  • 60 types: public TryFormat(Span<char>, …) and TryFormat(Span<byte>, …) on every range union (13, inherited by each variant), range set (13, forwarding to RangeSetEngine like ToString does) and value set (34, wrapper arities included).
  • First cut, as steering asked: Internals/SpanLiteral.cs formats the string and copies it, so the output is ToString's text by construction. An empty format span counts as no format. A destination that is too small gets false with a count of 0. UTF-8 goes through Utf8.FromUtf16, which is lossless here because a string set refuses a lone surrogate. Writing straight into the span through TryFormatValue is the follow-up Write range, range set and value set literals straight into a span #656.
  • UnknownRangeVariantTests.RogueRange (the one outside implementer in the repo) gets both methods. That is the break in practice.
  • Docs: a paragraph in docs/serialization.md#print-a-range-as-a-literal, and a migration section, "If you implement IRangeFactory yourself". Fragments: changes/41.md (Added) and changes/41-interfaces.md (Breaking).

Tests

  • New oracle SpanFormatContractTests (differential, registered in oracles.json), run over every discovered range shape, range set and value set:
    • UTF-16 and UTF-8 output equals ToString(format, provider) for formats null, "", G, o and N2 × four providers. When a format is refused, both span forms refuse it the same way.
    • Every shorter destination returns false with 0 written. The exact length fits with nothing written past it.
    • TryFormatValue writes FormatValue's text, and a buffer one char too small gets false.
    • A non-ASCII string set with quoted elements makes the UTF-8 and UTF-16 lengths differ.
  • LiteralProviderContractTests and LiteralCultureContractTests now also cover TryFormat (char and byte), string.Create(provider, $"{value}"), $"{value}" and Utf8.TryWrite under the five cultures.
  • Seeds: span-format-writes-a-cut-literal (7 tests fail) and span-format-measures-utf8-in-chars (3 fail). Both were caught, and both patches were generated from the committed tree.
  • Claims J43-tryformat-writes-the-tostring-text and J44-tryformatvalue-writes-formatvalue-text. Surface coverage is complete.
  • AOT: four smoke cases (aot-tryformat*): generic over ISpanFormattable/IUtf8SpanFormattable, interpolation and Utf8.TryWrite, and TryFormatValue through both factory interfaces, including Int32Set<Code> (an instantiation over a value type that nothing else creates). Expected lines were measured under JIT and under a local osx-arm64 Native AOT publish (no trim warnings, same output). CI's smoke test is the authority.
  • Local: Tests 1425, NodaTime 243, EF 514, EF NodaTime 118, SchemaKit 20, AspNetCore 99, Conventions 472, all green. Integration was not run (CI).

Found on the way: Float64Range/Float32Range.FormatValue read "" as an explicit format (their set siblings don't), so FormatValue(1.5, "", de-DE) is 1,5. The literal isn't affected because ToString normalizes first. Filed as #655; this PR's sweep compares an empty span against FormatValue(…, null, …), which is what an empty span means.

🤖 Generated with Claude Code

CaffeinatedCoder and others added 2 commits November 30, 2026 19:41
…aces, TryFormat on every type (#41)

The three factory interfaces take both span interfaces in their base lists, with no
default, and a static virtual TryFormatValue defaulting to FormatValue. Every range
union (so every variant), range set and value set writes the text ToString gives, by
construction: the first cut formats the string and copies it.

SpanFormatContractTests holds parity in UTF-16 and UTF-8 and refuses every too-small
destination; the literal provider and culture sweeps reach the span forms and
interpolation too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…#41)

Two seeds for SpanFormatContractTests (a cut literal reported as written, a UTF-8
destination measured in chars), both caught; claims J43 and J44 for the new
docs/serialization.md paragraph; a migration section for a range type of one's own;
Added and Breaking fragments; four Native AOT smoke cases, measured under JIT and a
local osx-arm64 Native AOT publish.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Nov 30, 2026
@CaffeinatedCoder CaffeinatedCoder added enhancement New feature or request breaking Changes an answer or a signature — needs a major package/nodatime Affects CodoMetis.ValueRanges.NodaTime area/literals Parse, TryParse and formatting package/core Affects CodoMetis.ValueRanges labels Nov 30, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit c3df36e into main Nov 30, 2026
22 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/41-span-formattable branch November 30, 2026 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/literals Parse, TryParse and formatting breaking Changes an answer or a signature — needs a major enhancement New feature or request package/core Affects CodoMetis.ValueRanges package/nodatime Affects CodoMetis.ValueRanges.NodaTime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ISpanFormattable / TryFormat for ranges, range sets and value sets

1 participant