Improve some string Rune helpers#127939
Open
MihaZupan wants to merge 2 commits intodotnet:mainfrom
Open
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
Member
Author
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);
[MemoryDiagnoser]
public class Bench
{
private string _testString = default!;
private string _testStringSupplementary = default!;
private Rune _bmpRune;
private Rune _supplementaryRune;
private Rune _bmpReplacementRune;
private Rune _supplementaryReplacementRune;
[GlobalSetup]
public void Setup()
{
_bmpRune = new Rune('a');
_supplementaryRune = new Rune(0x1F600); // 😀
_bmpReplacementRune = new Rune('b');
_supplementaryReplacementRune = new Rune(0x1F601); // 😁
_testString = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxa";
_testStringSupplementary = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\U0001F600";
}
[Benchmark]
public bool ContainsBmp() => _testString.Contains(_bmpRune);
[Benchmark]
public bool ContainsSupplementary() => _testStringSupplementary.Contains(_supplementaryRune);
[Benchmark]
public bool StartsWithBmp() => _testString.StartsWith(_bmpRune);
[Benchmark]
public bool StartsWithSupplementary() => _testStringSupplementary.StartsWith(_supplementaryRune);
[Benchmark]
public bool EndsWithBmp() => _testString.EndsWith(_bmpRune);
[Benchmark]
public bool EndsWithSupplementary() => _testStringSupplementary.EndsWith(_supplementaryRune);
[Benchmark]
public string ReplaceBothBmp() => _testString.Replace(_bmpRune, _bmpReplacementRune);
[Benchmark]
public string ReplaceSupplementary() => _testStringSupplementary.Replace(_supplementaryRune, _supplementaryReplacementRune);
[Benchmark]
public string[] SplitBmp() => _testString.Split(_bmpRune);
[Benchmark]
public string[] SplitSupplementary() => _testStringSupplementary.Split(_supplementaryRune);
[Benchmark]
public string TrimBmp() => "aaahello worldaaa".Trim(_bmpRune);
[Benchmark]
public string TrimSupplementary() => "\U0001F600hello world\U0001F600".Trim(_supplementaryRune);
[Benchmark]
public string TrimStartBmp() => "aaahello world".TrimStart(_bmpRune);
[Benchmark]
public string TrimStartSupplementary() => "\U0001F600hello world".TrimStart(_supplementaryRune);
[Benchmark]
public string TrimEndBmp() => "hello worldaaa".TrimEnd(_bmpRune);
[Benchmark]
public string TrimEndSupplementary() => "hello world\U0001F600".TrimEnd(_supplementaryRune);
} |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates several string overloads that accept Rune to take faster paths for BMP runes and to simplify ordinal StartsWith / EndsWith checks.
Changes:
- Special-cases BMP
Runeinputs to delegate to existingcharoverloads forContains,Replace, andSplit. - Implements ordinal
StartsWith(Rune)/EndsWith(Rune)via direct UTF-16 surrogate comparisons for non-BMP runes. - Refactors
Trim(Rune)/TrimStart(Rune)/TrimEnd(Rune)to useTrimHelperwith a pinned UTF-16 span.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/String.Searching.cs | Adds BMP fast-path for Contains(Rune) by forwarding to Contains(char). |
| src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs | Adds BMP fast-paths for Replace / Split and refactors Trim* (Rune) implementations to call TrimHelper. |
| src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs | Adds BMP fast-paths and direct surrogate-pair comparisons for ordinal StartsWith(Rune) / EndsWith(Rune). |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.