-
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
[API Proposal]: MemoryExtensions.Replace(Span<T>, T, T) #75322
Comments
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsBackground and motivation
Span<char> tmp = ...;
int pos;
while ((pos = tmp.IndexOf(oldValue)) >= 0)
{
tmp[pos] = newValue;
tmp = tmp.Slice(pos + 1);
} It'd be good to have this as a reusable helper on MemoryExtensions so that such open-coded loops can be replaced. There are other more complicated variations of this as well, but this is a simple and common one we can include. e.g. runtime/src/libraries/System.Private.Uri/src/System/Uri.cs Lines 1037 to 1043 in e8b1491
runtime/src/libraries/Common/src/System/IO/Archiving.Utils.Windows.cs Lines 79 to 84 in e8b1491
API Proposalnamespace System
{
public static class MemoryExtensions
{
+ public static void Replace<T>(this Span<T> span, T oldValue, T newValue) where T : IEquatable<T>?;
}
} API Usagechar[] result = ...;
result.AsSpan().Replace('\\', '/');
return new string(result); Alternative DesignsNo response RisksNo response
|
I'm looking forward that this API gets approved -- so I can implement it 😉 |
namespace System
{
public static class MemoryExtensions
{
public static void Replace<T>(this Span<T> span, T oldValue, T newValue) where T : IEquatable<T>?;
}
} |
One issue we didn't discuss today but should: We should come up with a pattern we want to use for these sorts of things, as it's going to start showing up more often, e.g. in #75901, in #26651, etc. |
Ok 😄 I assigned it to you. |
Background and motivation
string.Replace(char, char)
is useful for replacing every occurrence of a character value. When using spans/arrays to build up strings, that capability is also useful, but today it needs to be hand-rolled, e.g.It'd be good to have this as a reusable helper on MemoryExtensions so that such open-coded loops can be replaced. There are other more complicated variations of this as well, but this is a simple and common one we can include.
e.g.
runtime/src/libraries/System.Private.Uri/src/System/Uri.cs
Lines 1037 to 1043 in e8b1491
runtime/src/libraries/Common/src/System/IO/Archiving.Utils.Windows.cs
Lines 79 to 84 in e8b1491
API Proposal
namespace System { public static class MemoryExtensions { + public static void Replace<T>(this Span<T> span, T oldValue, T newValue) where T : IEquatable<T>?; } }
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: