Skip to content
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: string.Concat(ReadOnlySpan<char>, ...) #28310

Closed
stephentoub opened this issue Jan 3, 2019 · 4 comments · Fixed by dotnet/corefx#34451
Closed

API proposal: string.Concat(ReadOnlySpan<char>, ...) #28310

stephentoub opened this issue Jan 3, 2019 · 4 comments · Fixed by dotnet/corefx#34451
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime
Milestone

Comments

@stephentoub
Copy link
Member

We have String.Concat APIs for concatenating objects and strings, but we have no way of concatenating ReadOnlySpan<char>s into a string that don't involve unnecessary copies or allocation. This shows up when optimizing APIs that already need to return strings, but as a result internally end up incurring more allocation or copies than is desirable, e.g. a call like String.Remove("some long string", "g") needs to concatenate two substrings to produce the final result.

We should add the following overloads to string:

public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1);
public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2);
public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2, ReadOnlySpan<char> str3);

(these parameter names match the corresponding overloads that take strings).

We don't currently have a good way to pass an arbitrary number of spans into another Concat overload, so for now we would only support the above numbers of arguments, and in the future we could potentially expand to higher and/or arbitrary counts when such support is available.

@stephentoub
Copy link
Member Author

These are now implemented and used in coreclr, they're just internal (for now hopefully) rather than public.
https://github.com/dotnet/coreclr/blob/8f91ac8f374d30cd1a1122f78a90c35d7827a64a/src/System.Private.CoreLib/shared/System/String.Manipulation.cs#L344-L391

@juliusfriedman
Copy link
Contributor

juliusfriedman commented Jan 6, 2019

+1 but I would have expected these methods as extensions just like the others in:
https://github.com/dotnet/corefx/issues/21395

I personally think they have a better home with those methods or in a separate Extensions class since they return string and do not force the string type itself to have reference on ROS, that is unless such a span type becomes implicit via using stackalloc.

@stephentoub stephentoub self-assigned this Jan 8, 2019
@Tornhoof
Copy link
Contributor

Tornhoof commented Jan 8, 2019

Does it make sense to have string.Concat for ReadOnlySequence? To concat each segment?

@stephentoub
Copy link
Member Author

stephentoub commented Jan 8, 2019

Does it make sense to have string.Concat for ReadOnlySequence? To concat each segment?

You can implement that with String.Create (which can't be done with span because it's a ref struct and thus can't be passed in to the delegate without pinning and unsafe code). String already has a dependency on span, in both public API and implementation, and span lives in Corelib. I don't think we should make string have a dependency on ReadOnlySequence, nor move it to Corelib, at least not at present.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants