Browse Source
Update AbpStringExtensions.ReplaceFirst, to spans
pull/15878/head
Merijn Geurts
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
16 additions and
1 deletions
-
framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs
|
|
|
@ -209,7 +209,22 @@ public static class AbpStringExtensions |
|
|
|
return str; |
|
|
|
} |
|
|
|
|
|
|
|
return str.Substring(0, pos) + replace + str.Substring(pos + search.Length); |
|
|
|
var searchLength = search.Length; |
|
|
|
var replaceLength = replace.Length; |
|
|
|
var newLength = str.Length - searchLength + replaceLength; |
|
|
|
|
|
|
|
Span<char> buffer = stackalloc char[newLength]; |
|
|
|
|
|
|
|
// Copy the part of the original string before the search term
|
|
|
|
str.AsSpan(0, pos).CopyTo(buffer); |
|
|
|
|
|
|
|
// Copy the replacement text
|
|
|
|
replace.AsSpan().CopyTo(buffer.Slice(pos)); |
|
|
|
|
|
|
|
// Copy the remainder of the original string
|
|
|
|
str.AsSpan(pos + searchLength).CopyTo(buffer.Slice(pos + replaceLength)); |
|
|
|
|
|
|
|
return buffer.ToString(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|