-
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
Add String support to ReadOnlyMemory<char> #24064
Comments
Adding a few more folks that asked for |
For API review for items 2 and 3 above: namespace System
{
public static class SpanExtensions // Existing class containing AsReadOnlySpan(this string)... use it, something else, or rename it?
{
public static ReadOnlyMemory<char> AsReadOnlyMemory(this string text);
...
}
}
namespace System.Runtime.InteropServices
{
public static class MemoryMarshal // class added in https://github.com/dotnet/corefx/issues/23879#issuecomment-340861229
{
public static bool TryGetString(this ReadOnlyMemory<char> readOnlyMemory, out string value, out int offset, out int count);
...
}
} Alternative forms of TryGetString to consider instead? public static string TryGetString(this ReadOnlyMemory<char> readOnlyMemory, out int offset, out int count);
public static (String: string, Offset: int, Count: int) TryGetString(this ReadOnlyMemory<char> readOnlyMemory); Adding these will be dependent on verifying as part of the work that the extra check for string does not unduly impact other uses of |
Not a fan of those alternative suggestions for |
Those alternatives were more about the shape; the name could be different. |
Based on the discussion with @stephentoub we concluded that we should merge the two types: namespace System
{
public static class MemoryExtensions
{
// Existing class containing AsReadOnlySpan(this string)
public static ReadOnlyMemory<char> AsReadOnlyMemory(this string text);
// We already have ReadOnlyMemory<T>.TryGetArray(out ArraySegment<T> segment)
public static bool TryGetString(this ReadOnlyMemory<char> readOnlyMemory, out string value, out int offset, out int count);
}
} |
FYI: The API review discussion was recorded - see https://youtu.be/HnKmLJcEM74?t=1163 (28 min duration) |
Fixed by dotnet/corefx#25120 |
Today
ReadOnlySpan<char>
can reference aSystem.String
(viaAsReadOnlySpan()
), butReadOnlyMemory<char>
can't. We should fix that so thatReadOnlyMemory<char>
can be used as a way to slice strings where the slices can live on the heap.To me that means:
ReadOnlyMemory<T>
to wrap astring
in addition to wrapping aT[]
and anOwnedMemory<T>
. This will mean an extra type check on operations likeReadOnlyMemory<T>.Span
, but it can be guarded behind atypeof(T) == typeof(char)
check such that the additional branch and few additional instructions will only impactT==char
, andstring
will end up being a very common case forT==char
, making it worth it.AsReadOnlyMemory()
extension method forstring
just as there's anAsReadOnlySpan()
extension.TryGetString
method onMemoryMarshal
or some similar place that enables getting the string/offset/count out of aReadOnlyMemory<char>
if it's wrapping one.ReadOnlyMemory<char>
, related to https://github.com/dotnet/corefx/issues/20378 and https://github.com/dotnet/corefx/issues/21395cc: @KrzysztofCwalina, @ahsonkhan, @terrajobst, @jkotas
Replaces https://github.com/dotnet/corefx/issues/20378?
The text was updated successfully, but these errors were encountered: