Skip to content

Commit 7dd18f3

Browse files
committed
Rename localSpan to slice in order to avoid confusion with runtextSpan
1 parent 8f546b8 commit 7dd18f3

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ string ReserveName(string prefix)
650650
var additionalDeclarations = new HashSet<string>();
651651

652652
// Declare some locals.
653-
string textSpanLocal = "localSpan";
653+
string sliceSpan = "slice";
654654
writer.WriteLine("global::System.ReadOnlySpan<char> runtextSpan = base.runtext;");
655655
writer.WriteLine("int runtextpos = base.runtextpos;");
656656
writer.WriteLine("int runtextend = base.runtextend;");
@@ -714,7 +714,7 @@ void LoadTextSpanLocal(IndentedTextWriter writer, bool defineLocal = false)
714714
{
715715
writer.Write("global::System.ReadOnlySpan<char> ");
716716
}
717-
writer.WriteLine($"{textSpanLocal} = runtextSpan.Slice(runtextpos, runtextend - runtextpos);");
717+
writer.WriteLine($"{sliceSpan} = runtextSpan.Slice(runtextpos, runtextend - runtextpos);");
718718
}
719719

720720
// Emits the sum of a constant and a value from a local.
@@ -734,16 +734,16 @@ void EmitSpanLengthCheck(int requiredLength, string? dynamicRequiredLength = nul
734734
}
735735

736736
string SpanLengthCheck(int requiredLength, string? dynamicRequiredLength = null) =>
737-
$"(uint){textSpanLocal}.Length < {Sum(textSpanPos + requiredLength, dynamicRequiredLength)}";
737+
$"(uint){sliceSpan}.Length < {Sum(textSpanPos + requiredLength, dynamicRequiredLength)}";
738738

739-
// Adds the value of textSpanPos into the runtextpos local, slices localSpan by the corresponding amount,
739+
// Adds the value of textSpanPos into the runtextpos local, slices slice by the corresponding amount,
740740
// and zeros out textSpanPos.
741741
void TransferTextSpanPosToRunTextPos()
742742
{
743743
if (textSpanPos > 0)
744744
{
745745
EmitAdd(writer, "runtextpos", textSpanPos);
746-
writer.WriteLine($"{textSpanLocal} = {textSpanLocal}.Slice({textSpanPos});");
746+
writer.WriteLine($"{sliceSpan} = {sliceSpan}.Slice({textSpanPos});");
747747
textSpanPos = 0;
748748
}
749749
}
@@ -815,7 +815,7 @@ void EmitSwitchedBranches()
815815
EmitSpanLengthCheck(1);
816816
writer.WriteLine();
817817

818-
using (EmitBlock(writer, $"switch ({ToLowerIfNeeded(hasTextInfo, options, $"{textSpanLocal}[{textSpanPos++}]", IsCaseInsensitive(node))})"))
818+
using (EmitBlock(writer, $"switch ({ToLowerIfNeeded(hasTextInfo, options, $"{sliceSpan}[{textSpanPos++}]", IsCaseInsensitive(node))})"))
819819
{
820820
int startingTextSpanPos = textSpanPos;
821821
for (int i = 0; i < childCount; i++)
@@ -987,7 +987,7 @@ void EmitAllBranches()
987987

988988
// Reset state for next branch and loop around to generate it. This includes
989989
// setting runtextpos back to what it was at the beginning of the alternation,
990-
// updating localSpan to be the full length it was, and if there's a capture that
990+
// updating slice to be the full length it was, and if there's a capture that
991991
// needs to be reset, uncapturing it.
992992
if (!isLastBranch)
993993
{
@@ -1051,7 +1051,7 @@ void EmitBackreference(RegexNode node)
10511051
{
10521052
string matchLength = ReserveName("backreference_matchLength");
10531053
writer.WriteLine($"int {matchLength} = base.MatchLength({capnum});");
1054-
using (EmitBlock(writer, $"if ({textSpanLocal}.Length < {matchLength})"))
1054+
using (EmitBlock(writer, $"if ({sliceSpan}.Length < {matchLength})"))
10551055
{
10561056
writer.WriteLine($"goto {doneLabel};");
10571057
}
@@ -1063,7 +1063,7 @@ void EmitBackreference(RegexNode node)
10631063
string i = ReserveName("backreference_iteration");
10641064
using (EmitBlock(writer, $"for (int {i} = 0; {i} < {matchLength}; {i}++)"))
10651065
{
1066-
using (EmitBlock(writer, $"if ({ToLowerIfNeeded(hasTextInfo, options, $"runtextSpan[{matchIndex} + {i}]", IsCaseInsensitive(node))} != {ToLowerIfNeeded(hasTextInfo, options, $"{textSpanLocal}[{i}]", IsCaseInsensitive(node))})"))
1066+
using (EmitBlock(writer, $"if ({ToLowerIfNeeded(hasTextInfo, options, $"runtextSpan[{matchIndex} + {i}]", IsCaseInsensitive(node))} != {ToLowerIfNeeded(hasTextInfo, options, $"{sliceSpan}[{i}]", IsCaseInsensitive(node))})"))
10671067
{
10681068
writer.WriteLine($"goto {doneLabel};");
10691069
}
@@ -1747,7 +1747,7 @@ void EmitSingleChar(RegexNode node, bool emitLengthCheck = true, string? offset
17471747
// to generate the code for a single check, so we map those looping constructs to the
17481748
// appropriate single check.
17491749

1750-
string expr = $"{textSpanLocal}[{Sum(textSpanPos, offset)}]";
1750+
string expr = $"{sliceSpan}[{Sum(textSpanPos, offset)}]";
17511751

17521752
if (node.IsSetFamily)
17531753
{
@@ -1830,14 +1830,14 @@ void EmitAnchors(RegexNode node)
18301830
case RegexNode.Bol:
18311831
if (textSpanPos > 0)
18321832
{
1833-
using (EmitBlock(writer, $"if ({textSpanLocal}[{textSpanPos - 1}] != '\\n')"))
1833+
using (EmitBlock(writer, $"if ({sliceSpan}[{textSpanPos - 1}] != '\\n')"))
18341834
{
18351835
writer.WriteLine($"goto {doneLabel};");
18361836
}
18371837
}
18381838
else
18391839
{
1840-
// We can't use our localSpan in this case, because we'd need to access localSpan[-1], so we access the runtextSpan field directly:
1840+
// We can't use our slice in this case, because we'd need to access slice[-1], so we access the runtextSpan field directly:
18411841
using (EmitBlock(writer, $"if (runtextpos > runtextbeg && runtextSpan[runtextpos - 1] != '\\n')"))
18421842
{
18431843
writer.WriteLine($"goto {doneLabel};");
@@ -1846,22 +1846,22 @@ void EmitAnchors(RegexNode node)
18461846
break;
18471847

18481848
case RegexNode.End:
1849-
using (EmitBlock(writer, $"if ({textSpanLocal}.Length > {textSpanPos})"))
1849+
using (EmitBlock(writer, $"if ({sliceSpan}.Length > {textSpanPos})"))
18501850
{
18511851
writer.WriteLine($"goto {doneLabel};");
18521852
}
18531853
break;
18541854

18551855
case RegexNode.EndZ:
1856-
writer.WriteLine($"if ({textSpanPos} < {textSpanLocal}.Length - 1 || ({textSpanPos} < {textSpanLocal}.Length && {textSpanLocal}[{textSpanPos}] != '\\n'))");
1856+
writer.WriteLine($"if ({textSpanPos} < {sliceSpan}.Length - 1 || ({textSpanPos} < {sliceSpan}.Length && {sliceSpan}[{textSpanPos}] != '\\n'))");
18571857
using (EmitBlock(writer, null))
18581858
{
18591859
writer.WriteLine($"goto {doneLabel};");
18601860
}
18611861
break;
18621862

18631863
case RegexNode.Eol:
1864-
using (EmitBlock(writer, $"if ({textSpanPos} < {textSpanLocal}.Length && {textSpanLocal}[{textSpanPos}] != '\\n')"))
1864+
using (EmitBlock(writer, $"if ({textSpanPos} < {sliceSpan}.Length && {sliceSpan}[{textSpanPos}] != '\\n')"))
18651865
{
18661866
writer.WriteLine($"goto {doneLabel};");
18671867
}
@@ -1890,15 +1890,15 @@ void EmitMultiChar(RegexNode node, bool emitLengthCheck = true)
18901890
if (useMultiCharReads)
18911891
{
18921892
additionalDeclarations.Add("global::System.ReadOnlySpan<byte> byteSpan;");
1893-
writer.WriteLine($"byteSpan = global::System.Runtime.InteropServices.MemoryMarshal.AsBytes({textSpanLocal});");
1893+
writer.WriteLine($"byteSpan = global::System.Runtime.InteropServices.MemoryMarshal.AsBytes({sliceSpan});");
18941894
}
18951895

18961896
writer.Write("if (");
18971897

18981898
bool emittedFirstCheck = false;
18991899
if (emitLengthCheck)
19001900
{
1901-
writer.Write($"(uint){textSpanLocal}.Length < {textSpanPos + str.Length}");
1901+
writer.Write($"(uint){sliceSpan}.Length < {textSpanPos + str.Length}");
19021902
emittedFirstCheck = true;
19031903
}
19041904

@@ -1937,7 +1937,7 @@ void EmitOr()
19371937
for (int i = (str.Length * sizeof(char) - byteStr.Length) / sizeof(char); i < str.Length; i++)
19381938
{
19391939
EmitOr();
1940-
writer.Write($"{ToLowerIfNeeded(hasTextInfo, options, $"{textSpanLocal}[{textSpanPos}]", caseInsensitive)} != {Literal(str[i])}");
1940+
writer.Write($"{ToLowerIfNeeded(hasTextInfo, options, $"{sliceSpan}[{textSpanPos}]", caseInsensitive)} != {Literal(str[i])}");
19411941
textSpanPos++;
19421942
}
19431943

@@ -1955,7 +1955,7 @@ void EmitOr()
19551955
// character-by-character while respecting the culture.
19561956
if (!caseInsensitive)
19571957
{
1958-
string sourceSpan = textSpanPos > 0 ? $"{textSpanLocal}.Slice({textSpanPos})" : textSpanLocal;
1958+
string sourceSpan = textSpanPos > 0 ? $"{sliceSpan}.Slice({textSpanPos})" : sliceSpan;
19591959
using (EmitBlock(writer, $"if (!global::System.MemoryExtensions.StartsWith({sourceSpan}, {Literal(node.Str)}))"))
19601960
{
19611961
writer.WriteLine($"goto {doneLabel};");
@@ -1968,7 +1968,7 @@ void EmitOr()
19681968
using (EmitBlock(writer, $"for (int i = 0; i < {Literal(node.Str)}.Length; i++)"))
19691969
{
19701970
string textSpanIndex = textSpanPos > 0 ? $"i + {textSpanPos}" : "i";
1971-
using (EmitBlock(writer, $"if ({ToLower(hasTextInfo, options, $"{textSpanLocal}[{textSpanIndex}]")} != {Literal(str)}[i])"))
1971+
using (EmitBlock(writer, $"if ({ToLower(hasTextInfo, options, $"{sliceSpan}[{textSpanIndex}]")} != {Literal(str)}[i])"))
19721972
{
19731973
writer.WriteLine($"goto {doneLabel};");
19741974
}
@@ -2427,9 +2427,9 @@ void EmitSingleCharFixedRepeater(RegexNode node, bool emitLengthCheck = true)
24272427

24282428
if (iterations <= MaxUnrollSize)
24292429
{
2430-
// if ((uint)(textSpanPos + iterations - 1) >= (uint)localSpan.Length ||
2431-
// localSpan[textSpanPos] != c1 ||
2432-
// localSpan[textSpanPos + 1] != c2 ||
2430+
// if ((uint)(textSpanPos + iterations - 1) >= (uint)slice.Length ||
2431+
// slice[textSpanPos] != c1 ||
2432+
// slice[textSpanPos + 1] != c2 ||
24332433
// ...)
24342434
// {
24352435
// goto doneLabel;
@@ -2455,25 +2455,25 @@ void EmitSingleCharFixedRepeater(RegexNode node, bool emitLengthCheck = true)
24552455
}
24562456
else
24572457
{
2458-
// if ((uint)(textSpanPos + iterations - 1) >= (uint)localSpan.Length) goto doneLabel;
2458+
// if ((uint)(textSpanPos + iterations - 1) >= (uint)slice.Length) goto doneLabel;
24592459
if (emitLengthCheck)
24602460
{
24612461
EmitSpanLengthCheck(iterations);
24622462
}
24632463

2464-
string spanLocal = "slice"; // As this repeater doesn't wrap arbitrary node emits, this shouldn't conflict with anything
2465-
writer.WriteLine($"global::System.ReadOnlySpan<char> {spanLocal} = {textSpanLocal}.Slice({textSpanPos}, {iterations});");
2464+
string repeaterSpan = "repeaterSlice"; // As this repeater doesn't wrap arbitrary node emits, this shouldn't conflict with anything
2465+
writer.WriteLine($"global::System.ReadOnlySpan<char> {repeaterSpan} = {sliceSpan}.Slice({textSpanPos}, {iterations});");
24662466
string i = ReserveName("charrepeater_iteration");
2467-
using (EmitBlock(writer, $"for (int {i} = 0; {i} < {spanLocal}.Length; {i}++)"))
2467+
using (EmitBlock(writer, $"for (int {i} = 0; {i} < {repeaterSpan}.Length; {i}++)"))
24682468
{
24692469
EmitTimeoutCheck(writer, hasTimeout);
24702470

2471-
string tmpTextSpanLocal = textSpanLocal; // we want EmitSingleChar to refer to this temporary
2471+
string tmpTextSpanLocal = sliceSpan; // we want EmitSingleChar to refer to this temporary
24722472
int tmpTextSpanPos = textSpanPos;
2473-
textSpanLocal = spanLocal;
2473+
sliceSpan = repeaterSpan;
24742474
textSpanPos = 0;
24752475
EmitSingleChar(node, emitLengthCheck: false, offset: i);
2476-
textSpanLocal = tmpTextSpanLocal;
2476+
sliceSpan = tmpTextSpanLocal;
24772477
textSpanPos = tmpTextSpanPos;
24782478
}
24792479
textSpanPos += iterations;
@@ -2515,7 +2515,7 @@ void EmitSingleCharAtomicLoop(RegexNode node, bool emitLengthChecksIfRequired =
25152515
// restriction is purely for simplicity; it could be removed in the future with additional code to
25162516
// handle the unbounded case.
25172517

2518-
writer.Write($"int {iterationLocal} = global::System.MemoryExtensions.IndexOf({textSpanLocal}");
2518+
writer.Write($"int {iterationLocal} = global::System.MemoryExtensions.IndexOf({sliceSpan}");
25192519
if (textSpanPos > 0)
25202520
{
25212521
writer.Write($".Slice({textSpanPos})");
@@ -2525,8 +2525,8 @@ void EmitSingleCharAtomicLoop(RegexNode node, bool emitLengthChecksIfRequired =
25252525
using (EmitBlock(writer, $"if ({iterationLocal} == -1)"))
25262526
{
25272527
writer.WriteLine(textSpanPos > 0 ?
2528-
$"{iterationLocal} = {textSpanLocal}.Length - {textSpanPos};" :
2529-
$"{iterationLocal} = {textSpanLocal}.Length;");
2528+
$"{iterationLocal} = {sliceSpan}.Length - {textSpanPos};" :
2529+
$"{iterationLocal} = {sliceSpan}.Length;");
25302530
}
25312531
}
25322532
else if (node.IsSetFamily &&
@@ -2540,7 +2540,7 @@ void EmitSingleCharAtomicLoop(RegexNode node, bool emitLengthChecksIfRequired =
25402540
// As with the notoneloopatomic above, the unbounded constraint is purely for simplicity.
25412541
Debug.Assert(numSetChars > 1);
25422542

2543-
writer.Write($"int {iterationLocal} = global::System.MemoryExtensions.IndexOfAny({textSpanLocal}");
2543+
writer.Write($"int {iterationLocal} = global::System.MemoryExtensions.IndexOfAny({sliceSpan}");
25442544
if (textSpanPos != 0)
25452545
{
25462546
writer.Write($".Slice({textSpanPos})");
@@ -2554,8 +2554,8 @@ void EmitSingleCharAtomicLoop(RegexNode node, bool emitLengthChecksIfRequired =
25542554
using (EmitBlock(writer, $"if ({iterationLocal} == -1)"))
25552555
{
25562556
writer.WriteLine(textSpanPos > 0 ?
2557-
$"{iterationLocal} = {textSpanLocal}.Length - {textSpanPos};" :
2558-
$"{iterationLocal} = {textSpanLocal}.Length;");
2557+
$"{iterationLocal} = {sliceSpan}.Length - {textSpanPos};" :
2558+
$"{iterationLocal} = {sliceSpan}.Length;");
25592559
}
25602560
}
25612561
else if (node.IsSetFamily && maxIterations == int.MaxValue && node.Str == RegexCharClass.AnyClass)
@@ -2571,7 +2571,7 @@ void EmitSingleCharAtomicLoop(RegexNode node, bool emitLengthChecksIfRequired =
25712571
{
25722572
// For everything else, do a normal loop.
25732573

2574-
string expr = $"{textSpanLocal}[{iterationLocal}]";
2574+
string expr = $"{sliceSpan}[{iterationLocal}]";
25752575
if (node.IsSetFamily)
25762576
{
25772577
expr = MatchCharacterClass(hasTextInfo, options, expr, node.Str!, IsCaseInsensitive(node), additionalDeclarations);
@@ -2594,7 +2594,7 @@ void EmitSingleCharAtomicLoop(RegexNode node, bool emitLengthChecksIfRequired =
25942594
textSpanPos = 0;
25952595

25962596
string maxClause = maxIterations != int.MaxValue ? $"{iterationLocal} < {maxIterations} && " : "";
2597-
using (EmitBlock(writer, $"while ({maxClause}(uint){iterationLocal} < (uint){textSpanLocal}.Length && {expr})"))
2597+
using (EmitBlock(writer, $"while ({maxClause}(uint){iterationLocal} < (uint){sliceSpan}.Length && {expr})"))
25982598
{
25992599
EmitTimeoutCheck(writer, hasTimeout);
26002600
writer.WriteLine($"{iterationLocal}++;");
@@ -2613,7 +2613,7 @@ void EmitSingleCharAtomicLoop(RegexNode node, bool emitLengthChecksIfRequired =
26132613
// Now that we've completed our optional iterations, advance the text span
26142614
// and runtextpos by the number of iterations completed.
26152615

2616-
writer.WriteLine($"{textSpanLocal} = {textSpanLocal}.Slice({iterationLocal});");
2616+
writer.WriteLine($"{sliceSpan} = {sliceSpan}.Slice({iterationLocal});");
26172617
writer.WriteLine($"runtextpos += {iterationLocal};");
26182618
}
26192619

@@ -2622,7 +2622,7 @@ void EmitAtomicSingleCharZeroOrOne(RegexNode node)
26222622
{
26232623
Debug.Assert(node.M == 0 && node.N == 1);
26242624

2625-
string expr = $"{textSpanLocal}[{textSpanPos}]";
2625+
string expr = $"{sliceSpan}[{textSpanPos}]";
26262626
if (node.IsSetFamily)
26272627
{
26282628
expr = MatchCharacterClass(hasTextInfo, options, expr, node.Str!, IsCaseInsensitive(node), additionalDeclarations);
@@ -2633,9 +2633,9 @@ void EmitAtomicSingleCharZeroOrOne(RegexNode node)
26332633
expr = $"{expr} {(node.IsOneFamily ? "==" : "!=")} {Literal(node.Ch)}";
26342634
}
26352635

2636-
using (EmitBlock(writer, $"if ((uint){textSpanPos} < (uint){textSpanLocal}.Length && {expr})"))
2636+
using (EmitBlock(writer, $"if ((uint){textSpanPos} < (uint){sliceSpan}.Length && {expr})"))
26372637
{
2638-
writer.WriteLine($"{textSpanLocal} = {textSpanLocal}.Slice(1);");
2638+
writer.WriteLine($"{sliceSpan} = {sliceSpan}.Slice(1);");
26392639
writer.WriteLine($"runtextpos++;");
26402640
}
26412641
}

0 commit comments

Comments
 (0)