Skip to content

Add ISpanFormattable/IFormattable support to ChainablePath - #158

Merged
dennisdoomen merged 4 commits into
mainfrom
dennisdoomen-issue-148-ispanformattable
Aug 10, 2026
Merged

Add ISpanFormattable/IFormattable support to ChainablePath#158
dennisdoomen merged 4 commits into
mainfrom
dennisdoomen-issue-148-ispanformattable

Conversation

@dennisdoomen

Copy link
Copy Markdown
Owner

Fixes #148

Summary

Adds interpolated-string-handler support to ChainablePath so writing a path into an interpolated string (e.g. $"{path}") or DefaultInterpolatedStringHandler avoids boxing the struct and building an intermediate string.

Design decisions

  • IFormattable.ToString(string?, IFormatProvider?) is implemented unconditionally on all four target frameworks (netstandard2.0, netstandard2.1, net47, net8.0), since it's available everywhere. A ChainablePath has no format specifiers of its own, so format/formatProvider are ignored and the underlying path string is returned as-is.
  • ISpanFormattable.TryFormat(...) is implemented only under #if NET6_0_OR_GREATER, reusing the existing #if PATHY_PUBLIC conditional-compilation pattern already used in this file. ISpanFormattable doesn't exist in the BCL contracts for netstandard2.0/netstandard2.1/net47 (confirmed via a build failure when initially gated on NETSTANDARD2_1_OR_GREATER), so it's gated to .NET 6+ only, which covers this repo's net8.0 target.
  • TryFormat is a thin span-copy fast path (path.AsSpan().TryCopyTo(destination)), returning false and charsWritten = 0 when the destination is too small, per ISpanFormattable conventions.
  • Null/Empty are backed by string.Empty, so both format to an empty span/string without throwing.

Tests

Added to Pathy.Specs/ChainablePathSpecs.cs:

  • Interpolated-string usage ($"{path}" produces the correct string, exercising DefaultInterpolatedStringHandler.AppendFormatted).
  • ToString(format, provider) returns the underlying path regardless of format/provider.
  • TryFormat success case (destination large enough).
  • TryFormat failure case (destination too small) — both gated under #if NET6_0_OR_GREATER to match the net8.0/net472 specs targets.

API approval

This adds new public members to ChainablePath (exposed publicly via PATHY_PUBLIC when consumed as content files). Updated all four Pathy.ApiVerificationTests/ApprovedApi/pathy.*.verified.txt snapshots to match. dotnet test Pathy.ApiVerificationTests passes (8/8) and dotnet test Pathy.Specs passes (113/113 on net8.0).

Note on base branch

CONTRIBUTING.md instructs PRs to target a develop branch, but no develop branch currently exists in this GitHub repository (only main, which is also the repo's default branch). Opening this PR against main instead.

Note on approval status

This issue is only labeled enhancement, not api-approved, and CONTRIBUTING.md normally requires the latter before opening an API-changing PR. Proceeding anyway at the explicit request of the repo owner, who is coordinating this work — flagging here for visibility since this implements an as-yet-unapproved API proposal.

Dennis Doomen and others added 2 commits November 26, 2025 15:21
Adds interpolated-string-handler support so paths written into
DefaultInterpolatedStringHandler (e.g. $"{path}") avoid boxing and
an intermediate string allocation.

- IFormattable.ToString(format, provider) is implemented unconditionally
  on all target frameworks, since a ChainablePath has no format
  specifiers of its own and just returns the underlying path.
- ISpanFormattable.TryFormat is implemented only under NET6_0_OR_GREATER,
  since that interface does not exist on netstandard2.0/2.1 or net47.
- Added unit tests covering interpolated-string usage, ToString(format,
  provider), and TryFormat success/destination-too-small cases.
- Updated approved public API snapshots for all four target frameworks.

Fixes #148

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Aug 10, 2026

Copy link
Copy Markdown

Test Results

146 tests  +146   146 ✅ +146   1s ⏱️ -1s
  3 suites +  3     0 💤 ±  0 
  3 files   +  3     0 ❌ ±  0 

Results for commit 9fdf75b. ± Comparison against base commit acd0ead.

♻️ This comment has been updated with latest results.

@dennisdoomen dennisdoomen changed the title Add ISpanFormattable/IFormattable support to ChainablePath (issue #148) Add ISpanFormattable/IFormattable support to ChainablePath Aug 10, 2026
@dennisdoomen
dennisdoomen merged commit 2eec8d1 into main Aug 10, 2026
6 checks passed
@dennisdoomen
dennisdoomen deleted the dennisdoomen-issue-148-ispanformattable branch August 10, 2026 16:59
@dennisdoomen dennisdoomen added the enhancement New feature or request label Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Interpolated string handler to avoid extra allocations

1 participant