-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: MemoryExtensions.AsSpan(this string? text, Range range) #77955
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsBackground and motivationSome APIs return API Proposalnamespace System;
public static class MemoryExtensions
{
public static ReadOnlySpan<char> AsSpan(this string? text, Range range);
} API UsageReadOnlySpan<char> span = string.AsSpan(range); Alternative DesignsNo response RisksNo response
|
(I have a memory of discussing this at some point, and deciding that |
Why is the string nullable in your proposal? |
For consistency with the other such methods, where null is permitted in certain circumstances, e.g.
|
There are some other minor inconsistencies with MemoryExtensions. runtime/src/libraries/System.Memory/ref/System.Memory.cs Lines 208 to 220 in e1081df
runtime/src/libraries/System.Memory/ref/System.Memory.cs Lines 195 to 207 in e1081df
|
Since there's already an AsMemory overload with the same shape, this seems like it's just properly squaring off the feature. Since namespace System;
public static partial class MemoryExtensions
{
public static ReadOnlySpan<char> AsSpan(this string? text, Range range);
public static ReadOnlySpan<char> AsSpan(this string? text, Index startIndex);
} |
Background and motivation
Some APIs return
Range
instances to specify subsets. You can use such aRange
with a string likestr[range]
to get the substring, but that allocates a string.MemoryExtensions
exposesAsSpan
methods that takeRange
s, but only forT[]
andArraySegment<T>
, notstring
. It does have aRange
-basedstring
extension, but onlyAsMemory
.API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: