add Perl_reg_set_capture_string() function

Cut and paste into a separate function, the block of code in
regexec_flags() that is responsible (on successful match) for setting
RX_SAVED_COPY, RX_SUBBEG etc, ready for use by capture vars like $1, $&.

Although this function is currently only called from one place, we will
shortly use it elsewhere too.

This should contain no functional changes.
re-enable intuit-only matches

The COW changes inadvertently disabled intuit-only matches.
These are where calling intuit_start() to find the starting point for a
match is enough to know that the whole pattern will match, and so you can
skip calling regexec() too. For example, fixed strings without captures
such as /abc/.

The COW breakage meant that regexec was always called, making something
like /abc/ abut 3 times slower.

This commit re-enables intuit-only matches.

However, it turns out that this opens up a can of worms.
Normally, recording the just-matched-against string so that things like $&
and captures work, is done within regexec(). When this is skipped,
pp_match has to do a similar thing itself. The code that does this (which
is in principle a copy of the code in regexec()) is a bit of a mess. Due
to a logic error, a big chunk of it has actually been dead code for 10+
years.  Its had lots of modifications (e.g. people have made the same
changes to regexec() and pp_match()), but since it never gets executed,
errors aren't detected. And the bits that are executed haven't completely
received all the COW and SAWAMERSAND updates that have happened recently.

The Best way to fix this is is to extract out the capture code in
regexec() into a separate function (which we did in the previous commit),
then throw away all the broken capture code in pp_match() and replace it
with a call to the new function (which this commit does).

One side effect of this commit is that as well as restoring intuit-only
behaviour for the patterns that used to pre-COW, it also enables this
behaviour for patterns which formerly didn't, namely where $& or //p are
seen.

This commit is the barest minimum necessary to fix this; subsequent
commits will clean and improve this.
pp_match(): factor out some common code

Some identical code is used in two separate branches to set pos()
after a successful match. Hoist the common code to above the branch.
pp_match(): minor refactor: consolidate RETPUSHYES

Make the code slightly simpler by doing an early RETPUSHYES after success
where possible.
pp_match(): refactor intuit-only code

change

    if (intuit_only)
        goto yup:
    ...
  yup:
    A; B; X; Y;

to

    if (intuit_only)
        A; B;
        goto yup:
    ...
  yup:
    X; Y;

where A and B are intuit_only-specific steps while X and Y are done by the
regexec() branch too. This will shortly allow us to merge the two
branches.
pp_match(): combine intuit and regexec branches

There was some code that looked roughly like:

    if (can_match_on_intuit_only) {
        ....
        goto yup;
    }
    if (!regexec())
        goto ret_no;

  gotcha:
    A; B;
    if (simple)
        RETURNYES;
    X; Y;
    RETURN;

  yup:
    A;
    if (!simple)
        goto gotcha;
    B;
    RETURNYES

Refactor it to look like

    if (can_match_on_intuit_only) {
        ....
        goto gotcha;
    }
    if (!regexec())
        goto ret_no;

  gotcha:
    A; B;
    if (simple)
        RETURNYES;
    X; Y;
    RETURN;

As well as simplifying the code, it also avoids duplicating some work
(the 'A' above was done twice sometimes) - harmless but less efficient.
pp_match(): remove ret_no label

The nope: and ret_no: labels labelled the same point in the code.
Eliminate one of them.
pp_match(): intuit can handle refs these days

It looks like we no longer need to skip intuit-only matching when the
match is a ref or overloaded (e.g. $ref =~ /ARRAY/)
pp_match(): fix UTF* match setting

A recent commit did RX_MATCH_UTF8_set() based on the utf8-ness of the
pattern rather than the match string. I didn't matter because in that
branch they were guaranteed to have the same value, but fix it anyway,
both for correctness sake, and because it it *will* matter shortly
make intuit_start() handle mixed utf8-ness

Fix a bug in intuit_start() that makes it fail when the utf8-ness of the
string and pattern differ. This was mostly masked, since pp_match() skips
calling intuit in this case (and has done since 2000, presumably as a
workaround for this issue, and possibly for other issues since fixed).
But pp_subst() didn't skip, so code like this would fail:

    $c = "\x{c0}";
    utf8::upgrade($c);
    print "ok\n" if $c =~ s/\xC0{1,2}$/\xC0/i;

Now that intuit is (hopefully) fixed, also remove the guard in pp_match().
move intuit call from pp_match() into regexec()

Currently the main part of pp_match() looks like:

    if (can_use_intuit) {
        if (!intuit_start())
            goto nope;
        if (can_match_based_only_on_intuit_result) {
            ... set up $&, $-[0] etc ...
            goto gotcha;
        }
    }
    if (!regexec(..., REXEC_CHECKED|r_flags))
        goto nope;

  gotcha:
    ...

This rather breaks the regex API encapulation. The caller of the regex
engine shouldn't have to worry about whether to call intuit() or
regexec(), and to know to set $& in the intuit-only case.

So, move all the intuit-calling and $& setting into regexec itself.
This is cleaner, and will also shortly allow us to enable intuit-only
matches in pp_subst() too. After this change, the code above looks like
(in its entirety):

    if (!regexec(..., r_flags))
        goto nope;

    ...

There, isn't that nicer?
add intuit-only match to s///

pp_match() has an intuit-only match mode: if intuit_start() succeeds and
the regex is marked as only needing intuit (RXf_CHECK_ALL), then calling
regexec() is skipped, and just $& set and then returns.

The commit which originally added that feature to pp_match() also added a
comment to pp_subst() suggesting that the same thing could be done there.

This commit finally achieves that. It builds on the previous commit (which
moved this mechanism from pp_match() directly into regexec()), skipping
calling intuit_start() and directly calling regexec() with the
REXEC_CHECKED flag not set.

This appears to reduce the execution time of a simple substitution
like s/abc/def/ by a fifth.
make Perl_reg_set_capture_string static

This function was introduced a few commits ago. Since it's now only
called from within regexec.c, make it static.
skip creating new capture COW SV if possible

Each time we do a match, we currently (where possible) make a COW copy of
the just-matched string. This involves creating a new SV that shares the
same PVX buffer with the string. In a repeated match like while (/.../g),
that means the each time round we free the old capture SV and create a new
one.

As as optimisation, skip the free/create if the old capture SV is already
a COW clone of the match string.
fix /.\G/ under threading

When a regex was being duped, it's (constant) gofs field wasn't being
copied, but rather was being set to zero. Skip this and lots of TODO tests
pass.
add some basic floating /\G/ tests

Floating is when the \G is an unknown number of characters from the start
of the pattern, such as /a+\G/. Surprisingly, there were no tests for this
form.

Here are a few basic tests just to exercise the main code paths. More
comprehensive tests could do with being added at some point.
pp_match(): skip passing gpos arg to regexec()

In one specific case, pp_match() passes the value of pos() to regexec()
via the otherwise unused 'data' arg.

It turns out that pp_match() only passes this value when it exists and is
>= 0, while regexec() only uses it when there's no pos magic or pos() < 0.

So its never used as far as I can tell.

So, strip it for now.
pp_match(): eliminate unused t variable

and restrict usage of s variable
pp_match: avoid setting $+[0]

This function sometimes set $+[0] to pos() before calling regexec().
This value isn't used by regexec(), and was really just a way of updating
the new start position for //g. Replace it with a local var instead.
pp_match(): stop setting $-[0] before regexec()

It doesn't actually achieve anything.