Skip to content

Conversation

Alex-Sob
Copy link

@Alex-Sob Alex-Sob commented Sep 2, 2025

In .NET 8+ there are MemoryExtensions.Split methods to split strings that are Span-based, but they are unavailable for analyzers since they target netstandard2.0. What about providing some Span-based helpers that could be used in analyzers?

Changes

  • Added SpanExtensions static class with these methods as simplified versions of MemoryExtensions.Split:

    public static SpanSplitEnumerator Split(this ReadOnlySpan<char> source, char separator, StringSplitOptions options = StringSplitOptions.None);
    public static SpanSplitEnumerator Split(this ReadOnlySpan<char> source, ReadOnlySpan<char> separator, StringSplitOptions options = StringSplitOptions.None);
    
    public static int Split(this ReadOnlySpan<char> source, Span<Range> destination, char separator, StringSplitOptions options = StringSplitOptions.None);
    public static int Split(this ReadOnlySpan<char> source, Span<Range> destination, ReadOnlySpan<char> separator, StringSplitOptions options = StringSplitOptions.None);
  • Replaced string.Split calls in a few places with one of the methods above. This should not only avoid allocating string[] array in each case, but this also allows to reduce allocations further, for example, if it's needed to call Trim on string parts or slice those parts. So that parts can be "materialized" into a string only if it's necessary, e.g. to pass to an API that takes a string.

    I'm replacing one more call in Reduce allocations in EnumsShouldHaveZeroValueAnalyzer #50411

@Alex-Sob Alex-Sob requested a review from a team as a code owner September 2, 2025 13:18
Copy link
Contributor

github-actions bot commented Sep 2, 2025

This PR is targeting main, which is now for .NET 11-facing work. If you intended to target .NET 10, either retarget this PR to release/10.0.1xx or make sure you backport the change to release/10.0.1xx after merging. See #50394 for more details.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like there's no test project for Analyzer.Utilities projects, so I'm not sure where to add unit tests.

return Split(source, destination, separatorSpan, options);
}

public static int Split(this ReadOnlySpan<char> source, Span<Range> destination, ReadOnlySpan<char> separator, StringSplitOptions options = StringSplitOptions.None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel uneasy about this method taking a StringSplitOptions parameter and silently ignoring unimplemented options: currently StringSplitOptions.TrimEntries, but perhaps others in the future.

Copy link
Author

@Alex-Sob Alex-Sob Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TrimEntries is not available for netstandard2.0 which is the TF that analyzers target, so I'm not sure it should support this. And I believe there's no reason for that as there's MemoryExtensions, but since it cannot be used in analyzers my intention was to provide an internal helper with similar Split methods to reduce allocations.

@Alex-Sob
Copy link
Author

Alex-Sob commented Sep 3, 2025

Hello @stephentoub! May I ask you to review this please? Thank you!

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

Successfully merging this pull request may close these issues.

3 participants