Skip to content

Simplify code emitted for single char repeater #62322

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 1 commit into from
Dec 3, 2021
Merged
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 @@ -2483,25 +2483,42 @@ void EmitSingleCharFixedRepeater(RegexNode node, bool emitLengthCheck = true)
return;
}

// if ((uint)(textSpanPos + iterations - 1) >= (uint)textSpan.Length) goto doneLabel;
if (emitLengthCheck)
{
EmitSpanLengthCheck(iterations);
}

if (iterations <= MaxUnrollSize)
{
// if (textSpan[textSpanPos] != c1 ||
// if ((uint)(textSpanPos + iterations - 1) >= (uint)textSpan.Length ||
// textSpan[textSpanPos] != c1 ||
// textSpan[textSpanPos + 1] != c2 ||
// ...)
// goto doneLabel;
for (int i = 0; i < iterations; i++)
// {
// goto doneLabel;
// }
writer.Write($"if (");
if (emitLengthCheck)
{
EmitSingleChar(node, emitLengthCheck: false);
writer.WriteLine($"{SpanLengthCheck(iterations)} ||");
writer.Write(" ");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside comment, I've seen in some places in the emitter we just do:

writer.Indent += 4;
// .. your indented writes
writer.Indent -= 4;

And we also have few places where we just write the spaces manually. If you agree, I'm happy to put up a PR that just normalizes to always use the Indent property to be consistent.

Copy link
Member Author

@stephentoub stephentoub Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with whichever is more readable and maintainable. Places that currently manually indent looked subjectively better to my eyes, but it can be changed.

}
EmitSingleChar(node, emitLengthCheck: false, clauseOnly: true);
for (int i = 1; i < iterations; i++)
{
writer.WriteLine(" ||");
writer.Write(" ");
EmitSingleChar(node, emitLengthCheck: false, clauseOnly: true);
}
writer.WriteLine(")");
using (EmitBlock(writer, null))
{
writer.WriteLine($"goto {doneLabel};");
}
}
else
{
// if ((uint)(textSpanPos + iterations - 1) >= (uint)textSpan.Length) goto doneLabel;
if (emitLengthCheck)
{
EmitSpanLengthCheck(iterations);
}

string spanLocal = "slice"; // As this repeater doesn't wrap arbitrary node emits, this shouldn't conflict with anything
writer.WriteLine($"global::System.ReadOnlySpan<char> {spanLocal} = {textSpanLocal}.Slice({textSpanPos}, {iterations});");
string i = ReserveName("charrepeater_iteration");
Expand Down