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 Capture.ValueSpan #57357

Merged
merged 1 commit into from
Aug 13, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal Capture() { }
public int Index { get { throw null; } }
public int Length { get { throw null; } }
public string Value { get { throw null; } }
public System.ReadOnlySpan<char> ValueSpan { get { throw null; } }
public override string ToString() { throw null; }
}
public partial class CaptureCollection : System.Collections.Generic.ICollection<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IList<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IReadOnlyCollection<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IReadOnlyList<System.Text.RegularExpressions.Capture>, System.Collections.ICollection, System.Collections.IEnumerable, System.Collections.IList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ internal Capture(string text, int index, int length)
/// <summary>The original string</summary>
internal string Text { get; set; }

/// <summary>
/// Returns the value of this Regex Capture.
/// </summary>
/// <summary>Gets the captured substring from the input string.</summary>
/// <value>The substring that is captured by the match.</value>
public string Value => Text.Substring(Index, Length);

/// <summary>Gets the captured span from the input string.</summary>
/// <value>The span that is captured by the match.</value>
public ReadOnlySpan<char> ValueSpan => Text.AsSpan(Index, Length);

/// <summary>Returns the substring that was matched.</summary>
public override string ToString() => Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,12 @@ public void Match_VaryingLengthStrings(RegexOptions options)
private static void VerifyMatch(Match match, bool expectedSuccess, string expectedValue)
{
Assert.Equal(expectedSuccess, match.Success);
Assert.Equal(expectedValue, match.Value);
RegexAssert.Equal(expectedValue, match);

// Groups can never be empty
Assert.True(match.Groups.Count >= 1);
Assert.Equal(expectedSuccess, match.Groups[0].Success);
Assert.Equal(expectedValue, match.Groups[0].Value);
RegexAssert.Equal(expectedValue, match.Groups[0]);
}

[Theory]
Expand All @@ -582,7 +582,7 @@ public void Match_DeepNesting(RegexOptions options, int count)
Match m = r.Match(input);

Assert.True(m.Success);
Assert.Equal(input, m.Value);
RegexAssert.Equal(input, m);
Assert.Equal(count + 1, m.Groups.Count);
}

Expand All @@ -592,7 +592,7 @@ public void Match_Timeout()
Regex regex = new Regex(@"\p{Lu}", RegexOptions.IgnoreCase, TimeSpan.FromHours(1));
Match match = regex.Match("abc");
Assert.True(match.Success);
Assert.Equal("a", match.Value);
RegexAssert.Equal("a", match);
}

[Theory]
Expand Down Expand Up @@ -1063,12 +1063,12 @@ private static void VerifyMatch(Match match, bool expectedSuccess, CaptureData[]
{
Assert.Equal(expectedSuccess, match.Success);

Assert.Equal(expected[0].Value, match.Value);
RegexAssert.Equal(expected[0].Value, match);
Assert.Equal(expected[0].Index, match.Index);
Assert.Equal(expected[0].Length, match.Length);

Assert.Equal(1, match.Captures.Count);
Assert.Equal(expected[0].Value, match.Captures[0].Value);
RegexAssert.Equal(expected[0].Value, match.Captures[0]);
Assert.Equal(expected[0].Index, match.Captures[0].Index);
Assert.Equal(expected[0].Length, match.Captures[0].Length);

Expand All @@ -1077,14 +1077,14 @@ private static void VerifyMatch(Match match, bool expectedSuccess, CaptureData[]
{
Assert.Equal(expectedSuccess, match.Groups[i].Success);

Assert.Equal(expected[i].Value, match.Groups[i].Value);
RegexAssert.Equal(expected[i].Value, match.Groups[i]);
Assert.Equal(expected[i].Index, match.Groups[i].Index);
Assert.Equal(expected[i].Length, match.Groups[i].Length);

Assert.Equal(expected[i].Captures.Length, match.Groups[i].Captures.Count);
for (int j = 0; j < match.Groups[i].Captures.Count; j++)
{
Assert.Equal(expected[i].Captures[j].Value, match.Groups[i].Captures[j].Value);
RegexAssert.Equal(expected[i].Captures[j].Value, match.Groups[i].Captures[j]);
Assert.Equal(expected[i].Captures[j].Index, match.Groups[i].Captures[j].Index);
Assert.Equal(expected[i].Captures[j].Length, match.Groups[i].Captures[j].Length);
}
Expand Down Expand Up @@ -1233,12 +1233,12 @@ public void Synchronized()
{
var m = new Regex("abc").Match("abc");
Assert.True(m.Success);
Assert.Equal("abc", m.Value);
RegexAssert.Equal("abc", m);

var m2 = System.Text.RegularExpressions.Match.Synchronized(m);
Assert.Same(m, m2);
Assert.True(m2.Success);
Assert.Equal("abc", m2.Value);
RegexAssert.Equal("abc", m2);

AssertExtensions.Throws<ArgumentNullException>("inner", () => System.Text.RegularExpressions.Match.Synchronized(null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ public void Matches_MultipleCapturingGroups()
while (match.Success)
{
string expected = "abracadabra";
Assert.Equal(expected, match.Value);
RegexAssert.Equal(expected, match);

Assert.Equal(3, match.Groups.Count);
for (int i = 0; i < match.Groups.Count; i++)
{
Assert.Equal(expectedGroupValues[i], match.Groups[i].Value);
RegexAssert.Equal(expectedGroupValues[i], match.Groups[i]);
if (i == 1)
{
Assert.Equal(2, match.Groups[i].Captures.Count);
for (int j = 0; j < match.Groups[i].Captures.Count; j++)
{
Assert.Equal(expectedGroupCaptureValues[j], match.Groups[i].Captures[j].Value);
RegexAssert.Equal(expectedGroupCaptureValues[j], match.Groups[i].Captures[j]);
}
}
else if (i == 2)
{
Assert.Equal(1, match.Groups[i].Captures.Count);
Assert.Equal("cad", match.Groups[i].Captures[0].Value);
RegexAssert.Equal("cad", match.Groups[i].Captures[0]);
}
}
Assert.Equal(1, match.Captures.Count);
Assert.Equal("abracadabra", match.Captures[0].Value);
RegexAssert.Equal("abracadabra", match.Captures[0]);
match = match.NextMatch();
}
}
Expand Down Expand Up @@ -334,16 +334,16 @@ private static void VerifyMatches(MatchCollection matches, CaptureData[] expecte
private static void VerifyMatch(Match match, CaptureData expected)
{
Assert.True(match.Success);
Assert.Equal(expected.Value, match.Value);
RegexAssert.Equal(expected.Value, match);
Assert.Equal(expected.Index, match.Index);
Assert.Equal(expected.Length, match.Length);

Assert.Equal(expected.Value, match.Groups[0].Value);
RegexAssert.Equal(expected.Value, match.Groups[0]);
Assert.Equal(expected.Index, match.Groups[0].Index);
Assert.Equal(expected.Length, match.Groups[0].Length);

Assert.Equal(1, match.Captures.Count);
Assert.Equal(expected.Value, match.Captures[0].Value);
RegexAssert.Equal(expected.Value, match.Captures[0]);
Assert.Equal(expected.Index, match.Captures[0].Index);
Assert.Equal(expected.Length, match.Captures[0].Length);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Tests;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;

namespace System.Text.RegularExpressions.Tests
{
public static class RegexAssert
{
public static void Equal(string expected, Capture actual)
{
Assert.Equal(expected, actual.Value);
Assert.Equal(expected, actual.ValueSpan.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Tests;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;

namespace System.Text.RegularExpressions.Tests
{
public static class RegexAssert
{
public static void Equal(string expected, Capture actual)
{
Assert.Equal(expected, actual.Value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<!-- xUnit2008 is about regexes and isn't appropriate in the test project for regexes -->
Expand Down Expand Up @@ -34,9 +34,11 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<Compile Include="..\src\System\Text\RegularExpressions\RegexParseError.cs" Link="System\Text\RegularExpressions\RegexParseError.cs" />
<Compile Include="RegexAssert.netfx.cs" />
<Compile Include="RegexParserTests.netfx.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<Compile Include="RegexAssert.netcoreapp.cs" />
<Compile Include="RegexParserTests.netcoreapp.cs" />
<Compile Include="RegexValidations.netcoreapp.cs" />
<Compile Include="GroupCollectionReadOnlyDictionaryTests.cs" />
Expand Down