Skip to content
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

Add a Lines(this string) extensions method #52

Merged
merged 7 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,4 @@ MigrationBackup/
.ionide/

# Visual Studio Code configuration files
.vscode
.vscode
17 changes: 17 additions & 0 deletions Ardalis.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks", "{9BD3A717-5243-4A0A-91AD-8B5189908282}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ardalis.Extensions.Benchmarks", "benchmarks\Ardalis.Extensions.Benchmarks\Ardalis.Extensions.Benchmarks.csproj", "{458B05FD-97B9-4824-A9DF-77F498664AA4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -53,13 +57,26 @@ Global
{8AA20E3C-933F-4744-AE84-9696EB3F8568}.Release|x64.Build.0 = Release|Any CPU
{8AA20E3C-933F-4744-AE84-9696EB3F8568}.Release|x86.ActiveCfg = Release|Any CPU
{8AA20E3C-933F-4744-AE84-9696EB3F8568}.Release|x86.Build.0 = Release|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Debug|x64.ActiveCfg = Debug|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Debug|x64.Build.0 = Debug|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Debug|x86.ActiveCfg = Debug|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Debug|x86.Build.0 = Debug|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Release|Any CPU.Build.0 = Release|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Release|x64.ActiveCfg = Release|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Release|x64.Build.0 = Release|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Release|x86.ActiveCfg = Release|Any CPU
{458B05FD-97B9-4824-A9DF-77F498664AA4}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D5129614-5C05-4B1F-B512-897968D071BC} = {06458D58-19B8-4571-86A0-7D933BD254AB}
{8AA20E3C-933F-4744-AE84-9696EB3F8568} = {617119CF-264F-475E-A240-28D3B6EA0E79}
{458B05FD-97B9-4824-A9DF-77F498664AA4} = {9BD3A717-5243-4A0A-91AD-8B5189908282}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BE624DA9-B755-49B0-ADAD-4180463EC0DA}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>

<PropertyGroup>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<Configuration>Release</Configuration>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.12.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Ardalis.Extensions\Ardalis.Extensions.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="bible.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions benchmarks/Ardalis.Extensions.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BenchmarkDotNet.Running;
using Ardalis.Extensions.Benchmarks.StringManipulation;

namespace Ardalis.Extensions.Benchmarks;

public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<LinesBenchmarks>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider ensuring benchmark methods return identical results

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.IO;
using BenchmarkDotNet.Attributes;
using Ardalis.Extensions.StringManipulation;
using System.Collections.Generic;
using System.Linq;
using System;

namespace Ardalis.Extensions.Benchmarks.StringManipulation;

[MemoryDiagnoser]
public class LinesBenchmarks
{
private string Bible { get; set; }

[Params(1, 5, 10, 15)]
public int N { get; set; }
public LinesBenchmarks()
{
Bible = File.ReadAllText("bible.txt");
}

[Benchmark(Baseline = true)]
public string[] LinesSplit()
{
return Bible.LinesSplit().Where((line, index) => index % N == 0).ToArray();
}

[Benchmark]
public string[] LinesSubstring()
{
return Bible.LinesSub().Where((line, index) => index % N == 0).ToArray();
}

[Benchmark]
public string[] LinesMemory()
{
return Bible.Lines().Where((line, index) => index % N == 0).Select(line => line.ToString()).ToArray();
}
}

static class LinesBenchmarksExtensions
{
public static IEnumerable<string> LinesSplit(this string text)
{

var lines = text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);

if (lines.Last() == "") return lines.Take(lines.Length - 1).ToArray();

return lines;
}

public static IEnumerable<string> LinesSub(this string text)
{
var lineStartIndex = 0;
var currentIndex = 0;

while (currentIndex < text.Length)
{
if (text[currentIndex] == '\n')
{
yield return text.Substring(lineStartIndex, currentIndex - lineStartIndex);
lineStartIndex = currentIndex + 1;
currentIndex++;
continue;
}

if (text[currentIndex] == '\r')
{
if (currentIndex + 1 < text.Length && text[currentIndex + 1] == '\n')
{
yield return text.Substring(lineStartIndex, currentIndex - lineStartIndex);
lineStartIndex = currentIndex + 2;
currentIndex += 2;
continue;
}
}

if (currentIndex == text.Length - 1)
{
yield return text.Substring(lineStartIndex);
}

currentIndex++;
}
}
}
Loading