-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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 Span-based method for writing into String's buffer #22819
Comments
Approved in this shape: namespace System.Buffers
{
public delegate void SpanAction<T, in TArg>(Span<T> span, TArg arg);
public delegate void ReadOnlySpanAction<T, in TArg>(ReadOnlySpan<T> span, TArg arg);
}
namespace System
{
public class String
{
public static string Create<TState>(int length, TState state, SpanAction<char, TState> action);
}
} Discussed points:
|
Added |
Span<T> and ReadOnlySpan<T> cannot be used as a type argument, so the above Create<TState> method cannot be used with spans as input/state. I can imagine that could be something fairly common to do. Could another overload be added? public delegate void ReadOnlySpanSpanAction<TIn, TOut>(
ReadOnlySpan<TIn> input, Span<TOut> output);
public class String
{
public static string Create<TIn>(
int length, ReadOnlySpan<TIn> state, ReadOnlySpanSpanAction<TIn, char> action);
} Two examples from https://github.com/dotnet/corefx/issues/21281 that could benefit from this overload: namespace System
{
public class Convert
{
public static string ToBase64String(ReadOnlySpan<byte> bytes, Base64FormattingOptions options = Base64FormattingOptions.None);
}
}
namespace System.Text
{
public class Encoding
{
public virtual string GetString(ReadOnlySpan<byte> bytes);
}
} |
I suggest we wait until there's a proven need for it. While you may be right and it could end up being common, most of the cases I've seen historically are not based on taking in string-like data as input, but rather some other kind of data that's then used to produce characters. Often these characters are stored into a heap-allocated char[], so this new overload avoids that allocation and then the copy from it into the string, or into a stack-allocated char*, in which case this new overload helps avoid the copy. |
Is there a planned use for |
@KrzysztofCwalina, I believe you asked for this? Or am I misremembering? |
I just remembered where: |
Separated out of https://github.com/dotnet/corefx/issues/21281 for tracking purposes.
Mostly approved, but open issue around the delegate, its name, what namespace it lives in (or nested type within String), etc. A custom delegate is needed as span can't be used as a generic type argument with one of the built-in action types.
The text was updated successfully, but these errors were encountered: