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

Simplify code emitted for single char repeater #62322

Merged
merged 1 commit into from
Dec 3, 2021

Conversation

stephentoub
Copy link
Member

Rather than outputting an if block per unrolled iteration, just output a clause for each iteration as part of a single if block. We already do this for concatenations, but we don't yet for standalone repeaters.

e.g. for the expression \d{3}, previously we would have output:

if ((uint)textSpan.Length < 3)
{
    goto NoMatch;
}
if (!char.IsDigit(textSpan[0]))
{
    goto NoMatch;
}
if (!char.IsDigit(textSpan[1]))
{
    goto NoMatch;
}
if (!char.IsDigit(textSpan[2]))
{
    goto NoMatch;
}

and now we'll output

if ((uint)textSpan.Length < 3 ||
    !char.IsDigit(textSpan[0]) ||
    !char.IsDigit(textSpan[1]) ||
    !char.IsDigit(textSpan[2]))
{
    goto NoMatch;
}

cc: @joperezr

Rather than outputting an if block per unrolled iteration, just output a clause for each iteration as part of a single if block.  We already do this for concatenations, but we don't yet for standalone repeaters.
@ghost
Copy link

ghost commented Dec 3, 2021

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

Issue Details

Rather than outputting an if block per unrolled iteration, just output a clause for each iteration as part of a single if block. We already do this for concatenations, but we don't yet for standalone repeaters.

e.g. for the expression \d{3}, previously we would have output:

if ((uint)textSpan.Length < 3)
{
    goto NoMatch;
}
if (!char.IsDigit(textSpan[0]))
{
    goto NoMatch;
}
if (!char.IsDigit(textSpan[1]))
{
    goto NoMatch;
}
if (!char.IsDigit(textSpan[2]))
{
    goto NoMatch;
}

and now we'll output

if ((uint)textSpan.Length < 3 ||
    !char.IsDigit(textSpan[0]) ||
    !char.IsDigit(textSpan[1]) ||
    !char.IsDigit(textSpan[2]))
{
    goto NoMatch;
}

cc: @joperezr

Author: stephentoub
Assignees: -
Labels:

area-System.Text.RegularExpressions

Milestone: -

@stephentoub stephentoub merged commit 53ed08b into dotnet:main Dec 3, 2021
@stephentoub stephentoub deleted the singlecharrepeater branch December 3, 2021 11:26
{
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.

Copy link
Member

@joperezr joperezr left a comment

Choose a reason for hiding this comment

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

LGTM

@ghost ghost locked as resolved and limited conversation to collaborators Jan 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants