Skip to content

Commit ea30f84

Browse files
Merge branch 'main' into seo/96303-csharp-concurrentqueue
2 parents b69819b + 7985dc0 commit ea30f84

7 files changed

Lines changed: 108 additions & 41 deletions

File tree

‎strings-csharp/ReplaceLineBreaksInAStringCSharp/ReplaceLineBreaksInAStringCSharp/Program.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88

99
var regularExpressionReplaceResult = ReplaceLineBreak.ReplaceLineBreaksUsingTheRegularExpressionReplaceMethod();
1010
Console.WriteLine(regularExpressionReplaceResult);
11+
12+
var removeLineBreaksResult = ReplaceLineBreak.RemoveLineBreaks();
13+
Console.WriteLine(removeLineBreaksResult);

‎strings-csharp/ReplaceLineBreaksInAStringCSharp/ReplaceLineBreaksInAStringCSharp/ReplaceLineBreak.cs‎

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,34 @@
22

33
namespace ReplaceLineBreaksInAStringCSharp;
44

5-
public static class ReplaceLineBreak
5+
public static partial class ReplaceLineBreak
66
{
7-
public static string ReplaceLineBreaksUsingTheStringReplaceMethod()
8-
{
9-
const string text = "This is a line.\rThis is another line.";
10-
var newText = text.Replace("\r\n", "\n").Replace("\r", "\n");
11-
12-
return newText;
13-
}
14-
15-
public static string ReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod()
16-
{
17-
const string text = "This is a line.\rThis is another line.";
18-
var newText = text.ReplaceLineEndings("\n");
19-
20-
return newText;
21-
}
22-
23-
public static string ReplaceLineBreaksUsingTheRegularExpressionReplaceMethod()
24-
{
25-
const string text = "This is a line.\rThis is another line.";
26-
var newText = Regex.Replace(text, @"(\r\n|\r)", "\n");
27-
28-
return newText;
29-
}
7+
public const string Text = "Line one.\r\nLine two.\nLine three.\rLine four.";
8+
9+
public static string ReplaceLineBreaksUsingTheStringReplaceMethod() =>
10+
ReplaceLineBreaksUsingTheStringReplaceMethod(Text);
11+
12+
public static string ReplaceLineBreaksUsingTheStringReplaceMethod(string text) =>
13+
text.Replace("\r\n", "\n").Replace("\r", "\n");
14+
15+
public static string ReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod() =>
16+
ReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod(Text);
17+
18+
public static string ReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod(string text) =>
19+
text.ReplaceLineEndings("\n");
20+
21+
public static string ReplaceLineBreaksUsingTheRegularExpressionReplaceMethod() =>
22+
ReplaceLineBreaksUsingTheRegularExpressionReplaceMethod(Text);
23+
24+
public static string ReplaceLineBreaksUsingTheRegularExpressionReplaceMethod(string text) =>
25+
LineBreakRegex().Replace(text, "\n");
26+
27+
public static string RemoveLineBreaks() =>
28+
RemoveLineBreaks(Text);
29+
30+
public static string RemoveLineBreaks(string text) =>
31+
text.ReplaceLineEndings(string.Empty);
32+
33+
[GeneratedRegex(@"\r\n|\r|\n")]
34+
private static partial Regex LineBreakRegex();
3035
}

‎strings-csharp/ReplaceLineBreaksInAStringCSharp/ReplaceLineBreaksInAStringCSharp/ReplaceLineBreaksInAStringCSharp.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

‎strings-csharp/ReplaceLineBreaksInAStringCSharp/ReplaceLineBreaksInAStringCSharpBenchmarks/ReplaceLineBreakBenchmarks.cs‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,41 @@
44
namespace ReplaceLineBreaksInAStringCSharpBenchmarks;
55

66
[MemoryDiagnoser]
7-
public class ReplaceLineBreakBenchmarks
7+
public class ReplaceLineBreakBenchmarks
88
{
9+
private const string ShortText = "This is a line.\rThis is another line.";
10+
11+
private const string MixedText =
12+
"The quick brown fox jumps over the lazy dog.\r\n" +
13+
"Pack my box with five dozen liquor jugs.\n" +
14+
"How vexingly quick daft zebras jump.\r" +
15+
"Sphinx of black quartz, judge my vow.\r\n" +
16+
"Jackdaws love my big sphinx of quartz.\n" +
17+
"The five boxing wizards jump quickly.\r" +
18+
"Bright vixens jump; dozy fowl quack.\r\n" +
19+
"Quick zephyrs blow, vexing daft Jim.\n" +
20+
"Two driven jocks help fax my big quiz.\r" +
21+
"Waltz, bad nymph, for quick jigs vex.\r\n" +
22+
"Glib jocks quiz nymph to vex dwarf.\n" +
23+
"Fickle jinx bog dwarves spy math quiz.\r";
24+
25+
[Params("Short", "Mixed")]
26+
public string Input { get; set; } = "Short";
27+
28+
private string _text = ShortText;
29+
30+
[GlobalSetup]
31+
public void GlobalSetup() => _text = Input == "Short" ? ShortText : MixedText;
32+
933
[Benchmark]
10-
public string StringReplace() => ReplaceLineBreak.ReplaceLineBreaksUsingTheStringReplaceMethod();
34+
public string StringReplace() =>
35+
ReplaceLineBreak.ReplaceLineBreaksUsingTheStringReplaceMethod(_text);
1136

1237
[Benchmark]
13-
public string StringReplaceLineEndings() => ReplaceLineBreak.ReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod();
38+
public string StringReplaceLineEndings() =>
39+
ReplaceLineBreak.ReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod(_text);
1440

1541
[Benchmark]
16-
public string RegexReplace() => ReplaceLineBreak.ReplaceLineBreaksUsingTheRegularExpressionReplaceMethod();
42+
public string RegexReplace() =>
43+
ReplaceLineBreak.ReplaceLineBreaksUsingTheRegularExpressionReplaceMethod(_text);
1744
}

‎strings-csharp/ReplaceLineBreaksInAStringCSharp/ReplaceLineBreaksInAStringCSharpBenchmarks/ReplaceLineBreaksInAStringCSharpBenchmarks.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="BenchmarkDotNet" Version="0.13.8" />
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

‎strings-csharp/ReplaceLineBreaksInAStringCSharp/ReplaceLineBreaksInAStringCSharpTests/ReplaceLineBreakTests.cs‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,61 @@ namespace ReplaceLineBreaksInAStringCSharpTests;
44

55
public class ReplaceLineBreakTests
66
{
7-
private const string Expected = "This is a line.\nThis is another line.";
8-
7+
private const string Expected = "Line one.\nLine two.\nLine three.\nLine four.";
8+
99
[Fact]
10-
public void WhenReplaceLineBreaksUsingTheStringReplaceMethod_ThenReturnStringOnOneLine()
10+
public void WhenReplaceLineBreaksUsingTheStringReplaceMethod_ThenReturnStringWithUpdatedLineEndings()
1111
{
1212
var actual = ReplaceLineBreak.ReplaceLineBreaksUsingTheStringReplaceMethod();
1313

1414
Assert.Equal(Expected, actual);
1515
}
1616

1717
[Fact]
18-
public void WhenReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod_ThenReturnStringWithUpdatedLineEnding()
18+
public void WhenReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod_ThenReturnStringWithUpdatedLineEndings()
1919
{
2020
var actual = ReplaceLineBreak.ReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod();
2121

2222
Assert.Equal(Expected, actual);
2323
}
2424

2525
[Fact]
26-
public void WhenReplaceLineBreaksUsingTheRegularExpressionReplaceMethod_ThenReturnStringWithUpdatedLineEnding()
26+
public void WhenReplaceLineBreaksUsingTheRegularExpressionReplaceMethod_ThenReturnStringWithUpdatedLineEndings()
2727
{
2828
var actual = ReplaceLineBreak.ReplaceLineBreaksUsingTheRegularExpressionReplaceMethod();
2929

3030
Assert.Equal(Expected, actual);
3131
}
32+
33+
[Fact]
34+
public void WhenRemoveLineBreaks_ThenReturnStringWithNoLineBreaksAtAll()
35+
{
36+
var actual = ReplaceLineBreak.RemoveLineBreaks();
37+
38+
Assert.Equal("Line one.Line two.Line three.Line four.", actual);
39+
}
40+
41+
[Fact]
42+
public void WhenReplacingCarriageReturnBeforeCarriageReturnLineFeed_ThenWindowsLineBreakDoubles()
43+
{
44+
const string text = "Line one.\r\nLine two.";
45+
46+
var wrongOrder = text.Replace("\r", "\n");
47+
var rightOrder = text.Replace("\r\n", "\n").Replace("\r", "\n");
48+
49+
Assert.Equal("Line one.\n\nLine two.", wrongOrder);
50+
Assert.Equal("Line one.\nLine two.", rightOrder);
51+
}
52+
53+
[Fact]
54+
public void WhenTextContainsALineSeparator_ThenOnlyReplaceLineEndingsMatchesIt()
55+
{
56+
const string text = "Line one.\u2028Line two.";
57+
58+
var replaceLineEndings = ReplaceLineBreak.ReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod(text);
59+
var replaceChain = ReplaceLineBreak.ReplaceLineBreaksUsingTheStringReplaceMethod(text);
60+
61+
Assert.Equal("Line one.\nLine two.", replaceLineEndings);
62+
Assert.Equal(text, replaceChain);
63+
}
3264
}

‎strings-csharp/ReplaceLineBreaksInAStringCSharp/ReplaceLineBreaksInAStringCSharpTests/ReplaceLineBreaksInAStringCSharpTests.csproj‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

@@ -10,13 +10,13 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
14-
<PackageReference Include="xunit" Version="2.5.0" />
15-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
14+
<PackageReference Include="xunit" Version="2.9.3" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
<PrivateAssets>all</PrivateAssets>
1818
</PackageReference>
19-
<PackageReference Include="coverlet.collector" Version="6.0.0">
19+
<PackageReference Include="coverlet.collector" Version="10.0.1">
2020
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2121
<PrivateAssets>all</PrivateAssets>
2222
</PackageReference>

0 commit comments

Comments
 (0)