Skip to content

Add more AsSpan overloads to StringSegment #50428

@BrennanConroy

Description

@BrennanConroy

Background and Motivation

Many classes provide AsSpan methods and most provide at least the 3 basic methods, AsSpan(), AsSpan(int start), AsSpan(int start, int length).

The StringSegment class only provides AsSpan(), but it should provide the other two for consistency and ease of use.

Proposed API

namespace Microsoft.Extensions.Primitives
{
    public readonly struct StringSegment : IEquatable<StringSegment>, IEquatable<string>
    {
        public ReadOnlySpan<char> AsSpan();
+       public ReadOnlySpan<char> AsSpan(int start);
+       public ReadOnlySpan<char> AsSpan(int start, int length);
    }
}

Usage Examples

// input.AsSpan().Slice(startIndex, typeLength);
input.AsSpan(startIndex, typeLength);

Alternative Designs

No, other classes that provide AsSpan have similar overloads

Risks

None

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions