Skip to content

Pass more information about execution mode to RegexRunners #68242

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

Merged
merged 3 commits into from
Apr 21, 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
243 changes: 0 additions & 243 deletions src/libraries/System.Text.RegularExpressions/src/README.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="System\Text\RegularExpressions\RegexReplacement.cs" />
<Compile Include="System\Text\RegularExpressions\RegexRunner.cs" />
<Compile Include="System\Text\RegularExpressions\RegexRunnerFactory.cs" />
<Compile Include="System\Text\RegularExpressions\RegexRunnerMode.cs" />
<Compile Include="System\Text\RegularExpressions\RegexTree.cs" />
<Compile Include="System\Text\RegularExpressions\RegexTreeAnalyzer.cs" />
<Compile Include="System\Text\RegularExpressions\RegexWriter.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Match NextMatch()
Regex? r = _regex;
Debug.Assert(Text != null);
return r != null ?
r.RunSingleMatch(false, Length, Text, _textbeg, _textend - _textbeg, _textpos)! :
r.RunSingleMatch(RegexRunnerMode.FullMatchRequired, Length, Text, _textbeg, _textend - _textbeg, _textpos)! :
this;
}

Expand Down Expand Up @@ -272,39 +272,44 @@ internal int MatchLength(int cap)
}

/// <summary>Tidy the match so that it can be used as an immutable result</summary>
internal void Tidy(int textpos, int beginningOfSpanSlice)
internal void Tidy(int textpos, int beginningOfSpanSlice, RegexRunnerMode mode)
{
Debug.Assert(mode != RegexRunnerMode.ExistenceRequired);

int[] matchcount = _matchcount;
int[][] matches = _matches;
_capcount = matchcount[0]; // used to indicate Success
_textpos = textpos; // used to determine where to perform next match

_textpos = textpos;
_capcount = matchcount[0];
int[][] matches = _matches;
int[] interval = matches[0];
Index = interval[0];
Length = interval[1];
if (_balancing)
{
TidyBalancing();
}
Length = interval[1]; // the length of the match
Index = interval[0] + beginningOfSpanSlice; // the index of the match, adjusted for input slicing

// At this point the Match is consistent for handing back to a caller, with regards to the span that was processed.
// However, the caller may have actually provided a string, and may have done so with a non-zero beginning.
// In such a case, all offsets need to be shifted by beginning, e.g. if beginning is 5 and a capture occurred at
// offset 17, that 17 offset needs to be increased to 22 to account for the fact that it was actually 17 from the
// beginning, which the implementation saw as 0 but which from the caller's perspective was 5.
Debug.Assert(!_balancing);
if (beginningOfSpanSlice != 0)
if (mode == RegexRunnerMode.FullMatchRequired)
{
Index += beginningOfSpanSlice;
for (int groupNumber = 0; groupNumber < matches.Length; groupNumber++)
if (_balancing)
{
TidyBalancing();
}
Debug.Assert(!_balancing);

if (beginningOfSpanSlice != 0)
{
int[] captures = matches[groupNumber];
if (captures is not null)
for (int groupNumber = 0; groupNumber < matches.Length; groupNumber++)
{
int capturesLength = matchcount[groupNumber] * 2; // each capture has an offset and a length
for (int c = 0; c < capturesLength; c += 2)
int[] captures = matches[groupNumber];
if (captures is not null)
{
captures[c] += beginningOfSpanSlice;
int capturesLength = matchcount[groupNumber] * 2; // each capture has an offset and a length
for (int c = 0; c < capturesLength; c += 2)
{
captures[c] += beginningOfSpanSlice;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public virtual Match this[int i]
Match match;
do
{
match = _regex.RunSingleMatch(false, _prevlen, _input, 0, _input.Length, _startat)!;
match = _regex.RunSingleMatch(RegexRunnerMode.FullMatchRequired, _prevlen, _input, 0, _input.Length, _startat)!;
if (!match.Success)
{
_done = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public int Count(string input)
{
count++;
return true;
}, reuseMatchObject: true);
}, RegexRunnerMode.BoundsRequired, reuseMatchObject: true);

return count;
}
Expand All @@ -42,7 +42,7 @@ public int Count(ReadOnlySpan<char> input)
{
count++;
return true;
}, reuseMatchObject: true);
}, RegexRunnerMode.BoundsRequired, reuseMatchObject: true);

return count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ internal ValueMatchEnumerator(Regex regex, ReadOnlySpan<char> input, int startAt
/// </returns>
public bool MoveNext()
{
Match? match = _regex.RunSingleMatch(quick: false, _prevLen, _input, _startAt);
Debug.Assert(match != null, "Match shouldn't be null because we passed quick = false.");
if (match != RegularExpressions.Match.Empty)
(bool Success, int Index, int Length, int TextPosition) match = _regex.RunSingleMatch(RegexRunnerMode.BoundsRequired, _prevLen, _input, _startAt);
if (match.Success)
{
_current = new ValueMatch(match.Index, match.Length);
_startAt = match._textpos;
_startAt = match.TextPosition;
_prevLen = match.Length;
return true;
}

return false;
}

Expand Down
Loading