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

[release/5.0] Fix handling of \G in Regex.Split/Replace #44985

Merged
merged 1 commit into from
Nov 21, 2020
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 @@ -322,6 +322,10 @@ internal void Scan<TState>(Regex regex, string text, int textstart, ref TState s
return;
}

// Now that we've matched successfully, update the starting position to reflect
// the current position, just as Match.NextMatch() would pass in _textpos as textstart.
runtextstart = runtextpos;

// Reset state for another iteration.
runtrackpos = runtrack!.Length;
runstackpos = runstack!.Length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using System.Linq;
using Xunit;

namespace System.Text.RegularExpressions.Tests
Expand Down Expand Up @@ -873,6 +874,33 @@ public void Docs_EndOfLineComment(RegexOptions options)
}


// https://docs.microsoft.com/en-us/dotnet/standard/base-types/anchors-in-regular-expressions#contiguous-matches-g
[Theory]
[InlineData(RegexOptions.None)]
[InlineData(RegexOptions.Compiled)]
public void Docs_Anchors_ContiguousMatches(RegexOptions options)
{
const string Input = "capybara,squirrel,chipmunk,porcupine";
const string Pattern = @"\G(\w+\s?\w*),?";
string[] expected = new[] { "capybara", "squirrel", "chipmunk", "porcupine" };

Match m = Regex.Match(Input, Pattern, options);

string[] actual = new string[4];
for (int i = 0; i < actual.Length; i++)
{
Assert.True(m.Success);
actual[i] = m.Groups[1].Value;
m = m.NextMatch();
}
Assert.False(m.Success);
Assert.Equal(expected, actual);

Assert.Equal(
",arabypac,lerriuqs,knumpihcenipucrop",
Regex.Replace(Input, Pattern, m => string.Concat(m.Value.Reverse())));
}


//
// These patterns come from real-world customer usages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public static IEnumerable<object[]> Replace_String_TestData()

yield return new object[] { "([1-9])([1-9])([1-9])def", "abc123def!", "$+", RegexOptions.RightToLeft, -1, 10, "abc3!" };
yield return new object[] { "([1-9])([1-9])([1-9])def", "abc123def!", "$_", RegexOptions.RightToLeft, -1, 10, "abcabc123def!!" };

// Anchors
yield return new object[] { @"\Ga", "aaaaa", "b", RegexOptions.None, 5, 0, "bbbbb" };
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public static IEnumerable<object[]> Split_TestData()
yield return new object[] { @"\d", "1a2b3c4d5e6f7g8h9i0k", RegexOptions.RightToLeft, 10, 20, new string[] { "1a", "b", "c", "d", "e", "f", "g", "h", "i", "k" } };
yield return new object[] { @"\d", "1a2b3c4d5e6f7g8h9i0k", RegexOptions.RightToLeft, 2, 20, new string[] { "1a2b3c4d5e6f7g8h9i", "k" } };
yield return new object[] { @"\d", "1a2b3c4d5e6f7g8h9i0k", RegexOptions.RightToLeft, 1, 20, new string[] { "1a2b3c4d5e6f7g8h9i0k" } };

// Anchors
yield return new object[] { @"(?<=\G..)(?=..)", "aabbccdd", RegexOptions.None, 8, 0, new string[] { "aa", "bb", "cc", "dd" } };
}

[Theory]
Expand All @@ -60,7 +63,7 @@ public static IEnumerable<object[]> Split_TestData()
public void Split(string pattern, string input, RegexOptions options, int count, int start, string[] expected)
{
bool isDefaultStart = RegexHelpers.IsDefaultStart(input, options, start);
bool isDefaultCount = RegexHelpers.IsDefaultStart(input, options, count);
bool isDefaultCount = RegexHelpers.IsDefaultCount(input, options, count);
if (options == RegexOptions.None)
{
// Use Split(string), Split(string, string), Split(string, int) or Split(string, int, int)
Expand Down