3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-20 02:49:49 +01:00

Interpreter: split_line() now takes an options hash; now accepts "strip_quotes" and "keep_spaces"

This commit is contained in:
Pragmatic Software 2019-06-09 13:57:08 -07:00
parent b3ac27758d
commit 0f81584d8f
3 changed files with 28 additions and 18 deletions

View File

@ -614,7 +614,7 @@ sub factunset {
my ($channel, $trigger, $arguments) = $self->find_factoid_with_optional_channel($from, $args, 'factunset', $usage, 1); my ($channel, $trigger, $arguments) = $self->find_factoid_with_optional_channel($from, $args, 'factunset', $usage, 1);
return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message
my ($key) = $self->{pbot}->{interpreter}->split_line($arguments, 1); my ($key) = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
return $usage if not length $key; return $usage if not length $key;

View File

@ -546,9 +546,7 @@ sub extract_bracketed {
# unbalanced quotes gracefully by treating them as part of the argument # unbalanced quotes gracefully by treating them as part of the argument
# they were found within. # they were found within.
sub split_line { sub split_line {
my ($self, $line, $strip_quotes) = @_; my ($self, $line, %opts) = @_;
$strip_quotes = 0 if not defined $strip_quotes;
my @chars = split //, $line; my @chars = split //, $line;
@ -561,6 +559,7 @@ sub split_line {
my $i = 0; my $i = 0;
my $pos; my $pos;
my $ignore_quote = 0; my $ignore_quote = 0;
my $spaces = 0;
while (1) { while (1) {
$last_ch = $ch; $last_ch = $ch;
@ -582,6 +581,8 @@ sub split_line {
$ch = $chars[$i++]; $ch = $chars[$i++];
$spaces = 0 if $ch ne ' ';
if ($escaped) { if ($escaped) {
$token .= "\\$ch"; $token .= "\\$ch";
$escaped = 0; $escaped = 0;
@ -596,7 +597,7 @@ sub split_line {
if (defined $quote) { if (defined $quote) {
if ($ch eq $quote) { if ($ch eq $quote) {
# closing quote # closing quote
$token .= $ch unless $strip_quotes; $token .= $ch unless $opts{strip_quotes};
push @args, $token; push @args, $token;
$quote = undef; $quote = undef;
$token = ''; $token = '';
@ -616,16 +617,21 @@ sub split_line {
# begin potential quoted argument # begin potential quoted argument
$pos = $i - 1; $pos = $i - 1;
$quote = $ch; $quote = $ch;
$token .= $ch unless $strip_quotes; $token .= $ch unless $opts{strip_quotes};
} }
next; next;
} }
if ($ch eq ' ') { if ($ch eq ' ') {
if (++$spaces > 1 and $opts{keep_spaces}) {
$token .= $ch;
next;
} else {
push @args, $token if length $token; push @args, $token if length $token;
$token = ''; $token = '';
next; next;
} }
}
$token .= $ch; $token .= $ch;
} }
@ -637,16 +643,16 @@ sub split_line {
sub make_args { sub make_args {
my ($self, $string) = @_; my ($self, $string) = @_;
my @args = $self->split_line($string); my @args = $self->split_line($string, keep_spaces => 1);
my @arglist; my @arglist;
my @arglist_quotes; my @arglist_unstripped;
while (@args) { while (@args) {
my $arg = shift @args; my $arg = shift @args;
# add argument with quotes preserved # add argument with quotes and spaces preserved
push @arglist_quotes, $arg; push @arglist_unstripped, $arg;
# strip quotes from argument # strip quotes from argument
if ($arg =~ m/^'.*'$/) { if ($arg =~ m/^'.*'$/) {
@ -657,12 +663,15 @@ sub make_args {
$arg =~ s/"$//; $arg =~ s/"$//;
} }
# add unquoted argument # strip leading spaces from argument
$arg =~ s/^\s+//;
# add stripped argument
push @arglist, $arg; push @arglist, $arg;
} }
# copy quoted arguments to end of arglist # copy unstripped arguments to end of arglist
push @arglist, @arglist_quotes; push @arglist, @arglist_unstripped;
return \@arglist; return \@arglist;
} }
@ -675,6 +684,7 @@ sub arglist_size {
# shifts first argument off array of arguments # shifts first argument off array of arguments
sub shift_arg { sub shift_arg {
my ($self, $args) = @_; my ($self, $args) = @_;
return undef if not @$args;
splice @$args, @$args / 2, 1; # remove original quoted argument splice @$args, @$args / 2, 1; # remove original quoted argument
return shift @$args; return shift @$args;
} }
@ -695,7 +705,7 @@ sub split_args {
} while (--$count > 1 and $i < $max); } while (--$count > 1 and $i < $max);
} }
# get rest from 2nd half of arglist, which contains original quotes # get rest from 2nd half of arglist, which contains original quotes and spaces
my $rest = join ' ', @$args[@$args / 2 + $i .. @$args - 1]; my $rest = join ' ', @$args[@$args / 2 + $i .. @$args - 1];
push @result, $rest if length $rest; push @result, $rest if length $rest;
return @result; return @result;

View File

@ -169,7 +169,7 @@ sub grab_quotegrab {
my ($grab_nick, $grab_history, $channel, $grab_nicks, $grab_text); my ($grab_nick, $grab_history, $channel, $grab_nicks, $grab_text);
foreach my $grab (@grabs) { foreach my $grab (@grabs) {
($grab_nick, $grab_history, $channel) = $self->{pbot}->{interpreter}->split_line($grab, 1); ($grab_nick, $grab_history, $channel) = $self->{pbot}->{interpreter}->split_line($grab, strip_quotes => 1);
$grab_history = $nick eq $grab_nick ? 2 : 1 if not defined $grab_history; # skip grab command if grabbing self without arguments $grab_history = $nick eq $grab_nick ? 2 : 1 if not defined $grab_history; # skip grab command if grabbing self without arguments
$channel = $from if not defined $channel; $channel = $from if not defined $channel;