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

OP_SUBSTR_LEFT - a specialised OP_SUBSTR variant #22785

Open
wants to merge 4 commits into
base: blead
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -6439,6 +6439,7 @@ t/op/studytied.t See if study works with tied scalars
t/op/sub.t See if subroutines work
t/op/sub_lval.t See if lvalue subroutines work
t/op/substr.t See if substr works
t/op/substr_left.t See if substr($x, 0, $l, '') optimisation works
t/op/substr_thr.t See if substr works in another thread
t/op/svflags.t See if POK is set as expected.
t/op/svleak.pl Test file for svleak.t
Expand Down
3 changes: 2 additions & 1 deletion ext/Opcode/Opcode.pm
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ invert_opset function.
slt sgt sle sge seq sne scmp
isa

substr vec stringify study pos length index rindex ord chr
substr substr_left vec stringify study pos length index
rindex ord chr

ucfirst lcfirst uc lc fc quotemeta trans transr chop schop
chomp schomp
Expand Down
19 changes: 19 additions & 0 deletions lib/B/Deparse.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3436,6 +3436,25 @@ sub pp_substr {
maybe_local(@_, listop(@_, "substr"))
}

sub pp_substr_left {
my ($self,$op,$cx) = @_;

my $lex = ($op->private & OPpTARGET_MY);

my $val = 'substr(' . $self->deparse($op->first->sibling, $cx)
. ', 0, ' . $self->deparse($op->first->sibling->sibling->sibling, $cx)
. ( (($op->private & 7) == 3) ? '' : ", '')" );

if ($lex) {
my $targ = $op->targ;
my $var = $self->maybe_my($op, $cx, $self->padname($op->targ),
$self->padname_sv($targ),
0);
$val = $self->maybe_parens("$var = $val", $cx, 7);
}
$val;
}

sub pp_index {
# Also handles pp_rindex.
#
Expand Down
8 changes: 8 additions & 0 deletions lib/B/Deparse.t
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,14 @@ print sort(foo('bar'));
substr(my $a, 0, 0) = (foo(), bar());
$a++;
####
# 4-arg substr (non-chop)
my $str = 'ABCD';
my $bbb = substr($str, 1, 1, '');
####
# 4-arg substr (chop)
my $str = 'ABCD';
my $aaa = substr($str, 0, 1, '');
####
# This following line works around an unfixed bug that we are not trying to
# test for here:
# CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
Expand Down
5 changes: 3 additions & 2 deletions lib/B/Op_private.pm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading