-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Consider providing a string.Create
method which accepts a ROS<T> or ROS<char> as state
#30175
Comments
In order to consider this we would need a formal API proposal with rationale and usage examples. |
I think this issue is not about Most simple syntetic example for is hex converting. Let we need to implement two extension methods:
For second method we need pass ReadOnlySpan with int and char together, for compiler it will be optimal to create something like new ref struct ValueTuple and pass it to this method as state. So this issue is related to dotnet/csharplang#2584 |
As a workaround for people trying to solve the issue without copying the context - pinning the span via int outputLength = 128;
Span<char> output = stackalloc char[outputLength];
// Some operation on output
fixed (void* pOutput = output)
{
var str = string.Create(outputLength, (IntPtr) pOutput, static (span, ptr) =>
{
new Span<char>(ptr.ToPointer(), span.Length).CopyTo(span);
});
} And if I understand correctly, it should be possible to do the same thing without pinning now int outputLength = 128;
Span<char> output = stackalloc char[outputLength];
// Some operation on output
ref var outputRef = ref MemoryMarshal.GetReference(output);
var outputPtr = (IntPtr) Unsafe.AsPointer(ref outputRef);
var str = string.Create(outputLength, outputPtr, static (span, ptr) =>
{
new Span<char>(ptr.ToPointer(), span.Length).CopyTo(span);
}); |
Yes, e.g. int outputLength = 128;
Span<char> output = stackalloc char[outputLength];
...
var str = string.Create(outputLength, &output, static (span, ptr) =>
{
Span<char> output = *(ReadOnlySpan<char>*)ptr;
...
}); |
Closing this issue for now given this. If anyone is interested in driving it forward themselves, please feel free to open a new issue that follows the API suggestion template. Our API review process and a link to the template/good example is here: https://github.com/dotnet/runtime/blob/main/docs/project/api-review-process.md#steps. |
Currently,
string.Create
only accepts a genericTState
as the state, which meansReadOnlySpan<T>
andSpan<T>
can't be used as state. In situations where, for example, the state is a stackalloc'd buffer which can't be converted to a ROM. It can be unpleasantly worked around with pinning and passing pointer and length manually, but it is ugly and clunky. It seems simple enough to add( with potentially nicer naming )
and provide a
string.Create<TState>(int length, ReadOnlySpan<TState> state, SpanReadOnlySpanAction action
that is basically the same as the current impl.The text was updated successfully, but these errors were encountered: