Skip to content

Make ReadOnlySpan<char>.StartsWith/EndsWith(string) ordinal - #637

Merged
SimonCropp merged 3 commits into
mainfrom
fix/span-startswith-endswith-ordinal
Oct 12, 2026
Merged

SimonCropp merged 3 commits into
mainfrom
fix/span-startswith-endswith-ordinal

Conversation

@SimonCropp

Copy link
Copy Markdown
Owner

Bug

Below netcoreapp3.0 the polyfills were declared as

EndsWith(this ReadOnlySpan<char> target, string other, StringComparison comparison = StringComparison.CurrentCulture)
StartsWith(this ReadOnlySpan<char> target, string other, StringComparison comparison = StringComparison.CurrentCulture)

so span.EndsWith("c") did a current-culture comparison. On newer frameworks the same call binds to MemoryExtensions.EndsWith<T>(ReadOnlySpan<T>, ReadOnlySpan<T>), which is ordinal. For example "abc­".AsSpan().EndsWith("c") returned true on net48 and false on net10.0 (the soft hyphen is ignored by culture-sensitive comparison).

Fix

Each method is split into two overloads: one without a comparison that does the ordinal sequence comparison, and one with a required StringComparison. The API count goes up by two as a result.

Test

ReadOnlySpan_StartsWith_String_IsOrdinal and ReadOnlySpan_EndsWith_String_IsOrdinal in PolyfillTests_Memory.cs check that the no-comparison call does not ignore a soft hyphen, that the explicit InvariantCulture call does, and that an exact ordinal match still succeeds. Both failed on net48 before the fix and pass on net48 (polyfill) and net10.0 (BCL) after it.

@SimonCropp SimonCropp added this to the 11.4.4 milestone Oct 11, 2026
@SimonCropp
SimonCropp force-pushed the fix/span-startswith-endswith-ordinal branch from ef8530c to 890bdc2 Compare October 12, 2026 02:04
@SimonCropp
SimonCropp merged commit 79c1951 into main Oct 12, 2026
@SimonCropp
SimonCropp deleted the fix/span-startswith-endswith-ordinal branch October 12, 2026 02:07
This was referenced Oct 12, 2026
This was referenced Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants