Replace line breaks: retarget net10.0, fix the regex, real mixed-ending fixture - #2179
Open
vladimir-pecanac-main wants to merge 1 commit into
Conversation
…ng fixture
- All three projects retargeted net7.0 -> net10.0.
- Regex: @"(\r\n|\r)" never named \n, so the sample silently ignored Unix line
endings, which is the most common text a .NET application reads. Fixed to
\r\n|\r|\n and the unused capturing group dropped. Converted to a
[GeneratedRegex] partial method, which is the current idiom and also takes the
static-cache lookup out of the benchmarked path.
- Fixture: "This is a line.\rThis is another line." carried a single carriage
return and neither of the other two common sequences, so no method in the
sample exercised what the article is about. Now
"Line one.\r\nLine two.\nLine three.\rLine four." Each method gains a
string-taking overload so the benchmark can drive two inputs; the parameterless
ones keep the fixture.
- New RemoveLineBreaks() backing the removal case, via ReplaceLineEndings("").
- Tests: three expectations follow the new fixture, plus three new cases - the
removal method, the CRLF ordering trap (replacing \r before \r\n doubles every
Windows break), and a U+2028 line separator that ReplaceLineEndings() matches
and the Replace() chain does not. 6/6 green on net10.0.
- Benchmarks: second input added, a paragraph carrying \r\n, \n and \r mixed,
with the old short string kept as a labelled case.
- Packages: BenchmarkDotNet 0.15.8, Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3,
xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1.
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.
Sample update for the republished article Replace Line Breaks in a String in C#: ReplaceLineEndings.
What changes
net7.0->net10.0(app, tests, benchmarks).net7.0was the only version string in the sample and it is out of support.@"(\r\n|\r)"never names\n, so the sample silently ignored Unix line endings, which is the most common kind of text a .NET application reads today. Fixed to\r\n|\r|\n, and the capturing group dropped because nothing captures. Converted to a[GeneratedRegex]partial method: the current idiom, and it also takes the static-cache lookup out of the benchmarked path."This is a line.\rThis is another line.", one carriage return, no\r\nand no\n. It is now"Line one.\r\nLine two.\nLine three.\rLine four.", which carries all three common sequences and makes the CRLF ordering trap visible. Each method gains astring-taking overload so the benchmark can drive two inputs; the parameterless ones keep the fixture.RemoveLineBreaks(), viaReplaceLineEndings(""), backing the article's new removal section.\rbefore\r\ndoubles every Windows break), and a U+2028 line separator thatReplaceLineEndings()matches and theReplace()chain does not.\r\n,\nand\r, with the old short string kept as a labelled case.Verification
dotnet build -c Releaseclean, 0 warnings.dotnet test -c Release6/6 passing on .NET 10.0.10.Benchmark re-run on .NET 10.0.10, X64 RyuJIT:
The article's published 2023 figures (
Replace()24.69 ns,ReplaceLineEndings()79.84 ns,Regex.Replace()201.44 ns) do not survive. On .NET 10 the first two are a tie on both inputs, swapping places between runs;String.Replace()allocates twice as much on mixed text because it makes two passes;Regex.Replace()is the slowest by a wide margin either way.