Skip to content

Commit 7eb4ad5

Browse files
Added .AsSpan() extension for Captures.
1 parent 7a81815 commit 7eb4ad5

4 files changed

Lines changed: 61 additions & 26 deletions

File tree

Source/Open.Text.csproj

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

3-
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
55
<LangVersion>latest</LangVersion>
66
<Nullable>enable</Nullable>
77
<EnableNETAnalyzers>true</EnableNETAnalyzers>
88
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
99
<Authors>electricessence</Authors>
10-
<Description>A set of useful classes for working with strings and formatting values.
10+
<Description>A set of useful classes for working with strings and formatting values.
1111

1212
Part of the "Open" set of libraries.
13-
</Description>
13+
</Description>
1414
<Copyright>© electricessence (Oren F.) All rights reserved.</Copyright>
1515
<PackageProjectUrl>https://github.com/Open-NET-Libraries/Open.Text</PackageProjectUrl>
1616
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Text</RepositoryUrl>
1717
<RepositoryType>git</RepositoryType>
1818
<PackageTags>dotnet, dotnetcore, string, text, format, stringbuilder, extensions</PackageTags>
19-
<Version>2.3.2</Version>
20-
<PackageReleaseNotes></PackageReleaseNotes>
19+
<Version>2.3.3</Version>
20+
<PackageReleaseNotes></PackageReleaseNotes>
2121
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2222
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2323
<IncludeSymbols>true</IncludeSymbols>
2424
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2525
<PackageIcon>logo.png</PackageIcon>
26-
</PropertyGroup>
26+
</PropertyGroup>
2727

2828
<ItemGroup>
2929
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
3030
</ItemGroup>
31-
32-
<ItemGroup>
33-
<None Remove=".git" />
34-
<None Remove=".gitignore" />
35-
<None Include="logo.png">
36-
<Pack>True</Pack>
37-
<PackagePath></PackagePath>
38-
</None>
39-
</ItemGroup>
4031

41-
<ItemGroup>
42-
<PackageReference Include="Open.MemoryExtensions" Version="2.8.0" />
43-
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
44-
<PrivateAssets>all</PrivateAssets>
45-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
46-
</PackageReference>
32+
<ItemGroup>
33+
<None Remove=".git" />
34+
<None Remove=".gitignore" />
35+
<None Include="logo.png">
36+
<Pack>True</Pack>
37+
<PackagePath></PackagePath>
38+
</None>
39+
</ItemGroup>
40+
41+
<ItemGroup>
42+
<PackageReference Include="Open.MemoryExtensions" Version="2.8.0" />
4743
</ItemGroup>
4844

4945
</Project>

Source/Text.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Globalization;
1111
using System.IO;
1212
using System.Linq;
13+
using System.Reflection;
1314
using System.Text.RegularExpressions;
1415

1516

@@ -735,5 +736,17 @@ public static string Supplant<T>(this string format, T[] values, CultureInfo? cu
735736
_ => string.Format(cultureInfo ?? CultureInfo.InvariantCulture, format, values as object[] ?? values.Cast<object>().ToArray()),
736737
};
737738

739+
private static readonly Func<Capture, string> _textDelegate = (Func<Capture, string>)
740+
typeof(Capture).GetProperty("Text", BindingFlags.Instance | BindingFlags.NonPublic)
741+
.GetGetMethod(nonPublic: true)
742+
.CreateDelegate(typeof(Func<Capture, string>));
743+
744+
/// <summary>
745+
/// Returns a ReadOnlySpan of the capture without creating a new string.
746+
/// </summary>
747+
/// <remarks>This is a stop-gap until .NET 6 releases the .ValueSpan property.</remarks>
748+
/// <param name="capture">The capture to get the span from.</param>
749+
public static ReadOnlySpan<char> AsSpan(this Capture capture) =>
750+
_textDelegate.Invoke(capture).AsSpan(capture.Index, capture.Length);
738751
}
739752
}

Tests/Open.Text.Tests.csproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
11-
<PackageReference Include="xunit" Version="2.4.0" />
12-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
13-
<PackageReference Include="coverlet.collector" Version="1.2.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
11+
<PackageReference Include="xunit" Version="2.4.1" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
13+
<PrivateAssets>all</PrivateAssets>
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
</PackageReference>
16+
<PackageReference Include="coverlet.collector" Version="3.1.0">
17+
<PrivateAssets>all</PrivateAssets>
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
</PackageReference>
1420
</ItemGroup>
1521

1622
<ItemGroup>

Tests/RegexTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Text.RegularExpressions;
5+
using Xunit;
6+
7+
namespace Open.Text.Tests
8+
{
9+
public static class RegexTests
10+
{
11+
[Fact]
12+
public static void CaptureSpanTest()
13+
{
14+
var pattern = new Regex("you");
15+
var match = pattern.Match("hello how are you Dr. Bob.");
16+
var span = match.AsSpan();
17+
Assert.Equal("you", span.ToString());
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)