Skip to content

[API Proposal]: Add no-allocation span-based methods to StringInfo#125070

Closed
Copilot wants to merge 3 commits intomainfrom
copilot/propose-api-for-issue-123092
Closed

[API Proposal]: Add no-allocation span-based methods to StringInfo#125070
Copilot wants to merge 3 commits intomainfrom
copilot/propose-api-for-issue-123092

Conversation

Copy link
Contributor

Copilot AI commented Mar 2, 2026

StringInfo lacks allocation-free APIs for counting grapheme clusters and extracting text-element ranges. The existing LengthInTextElements builds a cached int[], and SubstringByTextElements always allocates a string. This prototype implements three new APIs to close these gaps.

New APIs

namespace System.Globalization;

public partial class StringInfo
{
    public static int GetLengthInTextElements(ReadOnlySpan<char> str);
    public static Range? GetRangeByTextElements(ReadOnlySpan<char> str, int startingTextElement, int lengthInTextElements);
    public Range? RangeByTextElements(int startingTextElement, int lengthInTextElements);
}

Contract: GetRangeByTextElements / RangeByTextElements throw ArgumentOutOfRangeException for negative arguments; return null when the requested range extends beyond the end of the string.

Usage

// Zero-allocation length validation
if (StringInfo.GetLengthInTextElements(username) > 20) { ... }

// Slice a span without allocating a substring
Range? r = StringInfo.GetRangeByTextElements(span, 0, 50);
ReadOnlySpan<char> truncated = r.HasValue ? span[r.Value] : span;

// Repeated page-slicing on the same string
StringInfo si = new(longText);
Range? page = si.RangeByTextElements(pageIndex * pageSize, pageSize);

Open questions for API review

  • Range? return vs. bool TryGetRangeByTextElements(…, out Range)
  • Whether explicit string overloads are needed for GetLengthInTextElements
  • Whether a single-arg RangeByTextElements(int startingTextElement) (to end of string) should be added

Prototype commit

1409ae6

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/graphql
    • Triggering command: /usr/bin/gh gh pr comment 125070 --body-file /tmp/proposal.md n.Metadata/src/System/Reflection/Metadata/Internal/StringHeap.cs n.Metadata/src/System/Reflection/Metadata/Internal/BlobHeap.cs n.Metadata/src/S/home/REDACTED/work/runtime/runtime/.dotnet/dotnet n.Metadata/src/S/home/REDACTED/work/runtime/runtime/.dotnet/sdk/11.0.100-preview.1.261�� n.Metadata/src/S/noautoresponse n.Metadata/src/S/nologo n.Me�� n.Metadata/src/S/nodeReuse:true n.Metadata/src/S/low:false System.Security.Cryptography/src/home/REDACTED/work/runtime/runtime/src/libraries/System.Private.C-t System.Security.git System.Security.push System.Security.-v System.Security.origin (http block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/runtime-infrastructure
See info in area-owners.md if you want to be subscribed.

@ericstj
Copy link
Member

ericstj commented Mar 2, 2026

This PR is just testing out the API design skill.

@ericstj ericstj added NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) NO-REVIEW Experimental/testing PR, do NOT review it labels Mar 2, 2026
Copilot AI and others added 2 commits March 2, 2026 20:14
…lements to StringInfo

Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com>
…mprove test coverage

Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com>
Copilot AI changed the title [WIP] Propose API changes for issue 123092 [API Proposal]: Add no-allocation span-based methods to StringInfo Mar 2, 2026
@ericstj
Copy link
Member

ericstj commented Mar 2, 2026

Seems like the publishing failed but it updated this PR with the proposal details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Infrastructure NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) NO-REVIEW Experimental/testing PR, do NOT review it

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants