3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-25 04:02:37 +01:00

Interpreter: improve split_line (add strip_commas) and split_args (fix whitespace separator issue)

This commit is contained in:
Pragmatic Software 2020-06-04 02:10:24 -07:00
parent e63e425622
commit 32e25a1c61

View File

@ -517,6 +517,7 @@ sub split_line {
strip_quotes => 0, strip_quotes => 0,
keep_spaces => 0, keep_spaces => 0,
preserve_escapes => 1, preserve_escapes => 1,
strip_commas => 0,
); );
%opts = (%default_opts, %opts); %opts = (%default_opts, %opts);
@ -588,7 +589,7 @@ sub split_line {
next; next;
} }
if (($last_ch =~ /[\s:{(\[.+=]/) and not defined $quote and ($ch eq "'" or $ch eq '"')) { if (($last_ch =~ /[\s:{(\[.+=,]/) and not defined $quote and ($ch eq "'" or $ch eq '"')) {
if ($ignore_quote) { if ($ignore_quote) {
# treat unbalanced quote as part of this argument # treat unbalanced quote as part of this argument
$token .= $ch; $token .= $ch;
@ -603,12 +604,14 @@ sub split_line {
next; next;
} }
if ($ch eq ' ' or $ch eq "\n" or $ch eq "\t") { if ($ch eq ' ' or $ch eq "\n" or $ch eq "\t" or ($opts{strip_commas} and $ch eq ',')) {
if (++$spaces > 1 and $opts{keep_spaces}) { if (++$spaces > 1 and $opts{keep_spaces}) {
$token .= $ch; $token .= $ch;
next; next;
} else { } else {
unless ($opts{strip_commas} and $token eq ',') {
push @args, $token if length $token; push @args, $token if length $token;
}
$token = ''; $token = '';
next; next;
} }
@ -703,13 +706,32 @@ sub split_args {
} }
# join the get rest as a string # join the get rest as a string
my $rest; my $rest = '';
if ($preserve_quotes) { if ($preserve_quotes) {
# get from second half of args, which contains quotes # get from second half of args, which contains quotes
$rest = join ' ', @$args[@$args / 2 + $i .. @$args - 1]; # and don't insert space before separators
my $sep = '';
my $last_quoted = 0;
foreach my $arg (@$args[@$args / 2 + $i .. @$args - 1]) {
if ($arg =~ m/^[,;)+!.?-]/ and $last_quoted) {
$sep = '';
} else {
$sep = ' ';
}
$rest .= $sep unless not length $rest;
$rest .= $arg;
if ($arg =~ /["']$/) {
$last_quoted = 1;
} else {
$last_quoted = 0;
}
}
} else { } else {
$rest = join ' ', @$args[$i .. $max - 1]; $rest = join ' ', @$args[$i .. $max - 1];
} }
push @result, $rest if length $rest; push @result, $rest if length $rest;
return @result; return @result;
} }