Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7898f9b

Browse files
authored
Add Capture.ValueSpan (#57357)
1 parentc29be0a commit7898f9b

File tree

7 files changed

+71
-22
lines changed

7 files changed

+71
-22
lines changed

‎src/libraries/System.Text.RegularExpressions/ref/System.Text.RegularExpressions.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ internal Capture() { }
1212
publicintIndex{get{thrownull;}}
1313
publicintLength{get{thrownull;}}
1414
publicstringValue{get{thrownull;}}
15+
publicSystem.ReadOnlySpan<char>ValueSpan{get{thrownull;}}
1516
publicoverridestringToString(){thrownull;}
1617
}
1718
publicpartialclassCaptureCollection: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

‎src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Capture.cs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ internal Capture(string text, int index, int length)
2525
/// <summary>The original string</summary>
2626
internalstringText{get;set;}
2727

28-
/// <summary>
29-
/// Returns the value of this Regex Capture.
30-
/// </summary>
28+
/// <summary>Gets the captured substring from the input string.</summary>
29+
/// <value>The substring that is captured by the match.</value>
3130
publicstringValue=>Text.Substring(Index,Length);
3231

32+
/// <summary>Gets the captured span from the input string.</summary>
33+
/// <value>The span that is captured by the match.</value>
34+
publicReadOnlySpan<char>ValueSpan=>Text.AsSpan(Index,Length);
35+
3336
/// <summary>Returns the substring that was matched.</summary>
3437
publicoverridestringToString()=>Value;
3538

‎src/libraries/System.Text.RegularExpressions/tests/Regex.Match.Tests.cs‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,12 @@ public void Match_VaryingLengthStrings(RegexOptions options)
555555
privatestaticvoidVerifyMatch(Matchmatch,boolexpectedSuccess,stringexpectedValue)
556556
{
557557
Assert.Equal(expectedSuccess,match.Success);
558-
Assert.Equal(expectedValue,match.Value);
558+
RegexAssert.Equal(expectedValue,match);
559559

560560
// Groups can never be empty
561561
Assert.True(match.Groups.Count>=1);
562562
Assert.Equal(expectedSuccess,match.Groups[0].Success);
563-
Assert.Equal(expectedValue,match.Groups[0].Value);
563+
RegexAssert.Equal(expectedValue,match.Groups[0]);
564564
}
565565

566566
[Theory]
@@ -582,7 +582,7 @@ public void Match_DeepNesting(RegexOptions options, int count)
582582
Matchm=r.Match(input);
583583

584584
Assert.True(m.Success);
585-
Assert.Equal(input,m.Value);
585+
RegexAssert.Equal(input,m);
586586
Assert.Equal(count+1,m.Groups.Count);
587587
}
588588

@@ -592,7 +592,7 @@ public void Match_Timeout()
592592
Regexregex=newRegex(@"\p{Lu}",RegexOptions.IgnoreCase,TimeSpan.FromHours(1));
593593
Matchmatch=regex.Match("abc");
594594
Assert.True(match.Success);
595-
Assert.Equal("a",match.Value);
595+
RegexAssert.Equal("a",match);
596596
}
597597

598598
[Theory]
@@ -1063,12 +1063,12 @@ private static void VerifyMatch(Match match, bool expectedSuccess, CaptureData[]
10631063
{
10641064
Assert.Equal(expectedSuccess,match.Success);
10651065

1066-
Assert.Equal(expected[0].Value,match.Value);
1066+
RegexAssert.Equal(expected[0].Value,match);
10671067
Assert.Equal(expected[0].Index,match.Index);
10681068
Assert.Equal(expected[0].Length,match.Length);
10691069

10701070
Assert.Equal(1,match.Captures.Count);
1071-
Assert.Equal(expected[0].Value,match.Captures[0].Value);
1071+
RegexAssert.Equal(expected[0].Value,match.Captures[0]);
10721072
Assert.Equal(expected[0].Index,match.Captures[0].Index);
10731073
Assert.Equal(expected[0].Length,match.Captures[0].Length);
10741074

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

1080-
Assert.Equal(expected[i].Value,match.Groups[i].Value);
1080+
RegexAssert.Equal(expected[i].Value,match.Groups[i]);
10811081
Assert.Equal(expected[i].Index,match.Groups[i].Index);
10821082
Assert.Equal(expected[i].Length,match.Groups[i].Length);
10831083

10841084
Assert.Equal(expected[i].Captures.Length,match.Groups[i].Captures.Count);
10851085
for(intj=0;j<match.Groups[i].Captures.Count;j++)
10861086
{
1087-
Assert.Equal(expected[i].Captures[j].Value,match.Groups[i].Captures[j].Value);
1087+
RegexAssert.Equal(expected[i].Captures[j].Value,match.Groups[i].Captures[j]);
10881088
Assert.Equal(expected[i].Captures[j].Index,match.Groups[i].Captures[j].Index);
10891089
Assert.Equal(expected[i].Captures[j].Length,match.Groups[i].Captures[j].Length);
10901090
}
@@ -1233,12 +1233,12 @@ public void Synchronized()
12331233
{
12341234
varm=newRegex("abc").Match("abc");
12351235
Assert.True(m.Success);
1236-
Assert.Equal("abc",m.Value);
1236+
RegexAssert.Equal("abc",m);
12371237

12381238
varm2=System.Text.RegularExpressions.Match.Synchronized(m);
12391239
Assert.Same(m,m2);
12401240
Assert.True(m2.Success);
1241-
Assert.Equal("abc",m2.Value);
1241+
RegexAssert.Equal("abc",m2);
12421242

12431243
AssertExtensions.Throws<ArgumentNullException>("inner",()=>System.Text.RegularExpressions.Match.Synchronized(null));
12441244
}

‎src/libraries/System.Text.RegularExpressions/tests/Regex.MultipleMatches.Tests.cs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@ public void Matches_MultipleCapturingGroups()
2222
while(match.Success)
2323
{
2424
stringexpected="abracadabra";
25-
Assert.Equal(expected,match.Value);
25+
RegexAssert.Equal(expected,match);
2626

2727
Assert.Equal(3,match.Groups.Count);
2828
for(inti=0;i<match.Groups.Count;i++)
2929
{
30-
Assert.Equal(expectedGroupValues[i],match.Groups[i].Value);
30+
RegexAssert.Equal(expectedGroupValues[i],match.Groups[i]);
3131
if(i==1)
3232
{
3333
Assert.Equal(2,match.Groups[i].Captures.Count);
3434
for(intj=0;j<match.Groups[i].Captures.Count;j++)
3535
{
36-
Assert.Equal(expectedGroupCaptureValues[j],match.Groups[i].Captures[j].Value);
36+
RegexAssert.Equal(expectedGroupCaptureValues[j],match.Groups[i].Captures[j]);
3737
}
3838
}
3939
elseif(i==2)
4040
{
4141
Assert.Equal(1,match.Groups[i].Captures.Count);
42-
Assert.Equal("cad",match.Groups[i].Captures[0].Value);
42+
RegexAssert.Equal("cad",match.Groups[i].Captures[0]);
4343
}
4444
}
4545
Assert.Equal(1,match.Captures.Count);
46-
Assert.Equal("abracadabra",match.Captures[0].Value);
46+
RegexAssert.Equal("abracadabra",match.Captures[0]);
4747
match=match.NextMatch();
4848
}
4949
}
@@ -334,16 +334,16 @@ private static void VerifyMatches(MatchCollection matches, CaptureData[] expecte
334334
privatestaticvoidVerifyMatch(Matchmatch,CaptureDataexpected)
335335
{
336336
Assert.True(match.Success);
337-
Assert.Equal(expected.Value,match.Value);
337+
RegexAssert.Equal(expected.Value,match);
338338
Assert.Equal(expected.Index,match.Index);
339339
Assert.Equal(expected.Length,match.Length);
340340

341-
Assert.Equal(expected.Value,match.Groups[0].Value);
341+
RegexAssert.Equal(expected.Value,match.Groups[0]);
342342
Assert.Equal(expected.Index,match.Groups[0].Index);
343343
Assert.Equal(expected.Length,match.Groups[0].Length);
344344

345345
Assert.Equal(1,match.Captures.Count);
346-
Assert.Equal(expected.Value,match.Captures[0].Value);
346+
RegexAssert.Equal(expected.Value,match.Captures[0]);
347347
Assert.Equal(expected.Index,match.Captures[0].Index);
348348
Assert.Equal(expected.Length,match.Captures[0].Length);
349349
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
usingSystem.Collections.Generic;
5+
usingSystem.Diagnostics;
6+
usingSystem.Globalization;
7+
usingSystem.Linq;
8+
usingSystem.Tests;
9+
usingMicrosoft.DotNet.RemoteExecutor;
10+
usingXunit;
11+
12+
namespaceSystem.Text.RegularExpressions.Tests
13+
{
14+
publicstaticclassRegexAssert
15+
{
16+
publicstaticvoidEqual(stringexpected,Captureactual)
17+
{
18+
Assert.Equal(expected,actual.Value);
19+
Assert.Equal(expected,actual.ValueSpan.ToString());
20+
}
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
usingSystem.Collections.Generic;
5+
usingSystem.Diagnostics;
6+
usingSystem.Globalization;
7+
usingSystem.Linq;
8+
usingSystem.Tests;
9+
usingMicrosoft.DotNet.RemoteExecutor;
10+
usingXunit;
11+
12+
namespaceSystem.Text.RegularExpressions.Tests
13+
{
14+
publicstaticclassRegexAssert
15+
{
16+
publicstaticvoidEqual(stringexpected,Captureactual)
17+
{
18+
Assert.Equal(expected,actual.Value);
19+
}
20+
}
21+
}

‎src/libraries/System.Text.RegularExpressions/tests/System.Text.RegularExpressions.Tests.csproj‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<ProjectSdk="Microsoft.NET.Sdk">
1+
<ProjectSdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
44
<!-- xUnit2008 is about regexes and isn't appropriate in the test project for regexes-->
@@ -34,9 +34,11 @@
3434
</ItemGroup>
3535
<ItemGroupCondition="'$(TargetFramework)' == 'net48'">
3636
<CompileInclude="..\src\System\Text\RegularExpressions\RegexParseError.cs"Link="System\Text\RegularExpressions\RegexParseError.cs" />
37+
<CompileInclude="RegexAssert.netfx.cs" />
3738
<CompileInclude="RegexParserTests.netfx.cs" />
3839
</ItemGroup>
3940
<ItemGroupCondition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
41+
<CompileInclude="RegexAssert.netcoreapp.cs" />
4042
<CompileInclude="RegexParserTests.netcoreapp.cs" />
4143
<CompileInclude="RegexValidations.netcoreapp.cs" />
4244
<CompileInclude="GroupCollectionReadOnlyDictionaryTests.cs" />

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp