From: mike@bill.iac.net Date: Sat, 27 Jun 1998 03:49:13 +0000 (+0000) Subject: [ PATCH 5.004 68 ] Text::ParseWords, ^W fixed, version 3.1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b174585de5ccc9973ba572393b2b34e1a6a5b749;p=p5sagit%2Fp5-mst-13.2.git [ PATCH 5.004 68 ] Text::ParseWords, ^W fixed, version 3.1 Message-ID: <19980627034913.A32220@bill.minivend.com> p4raw-id: //depot/perl@1249 --- diff --git a/lib/Text/ParseWords.pm b/lib/Text/ParseWords.pm index d3a89f0..95f0e9b 100644 --- a/lib/Text/ParseWords.pm +++ b/lib/Text/ParseWords.pm @@ -1,7 +1,7 @@ package Text::ParseWords; -use vars qw($VERSION @ISA @EXPORT); -$VERSION = "3.0"; +use vars qw($VERSION @ISA @EXPORT $PERL_SINGLE_QUOTE); +$VERSION = "3.1"; require 5.000; @@ -48,22 +48,28 @@ sub nested_quotewords { sub parse_line { + # We will be testing undef strings + local($^W) = 0; + my($delimiter, $keep, $line) = @_; my($quote, $quoted, $unquoted, $delim, $word, @pieces); while (length($line)) { - ($quote, $quoted, $unquoted, $delim) = + + ($quote, $quoted, undef, $unquoted, $delim, undef) = $line =~ m/^(["']) # a $quote - ((?:\\.|[^\1\\])*?) # and $quoted text - \1 # followed by the same quote + ((?:\\.|(?!\1)[^\\])*) # and $quoted text + \1 # followed by the same quote + ([\000-\377]*) # and the rest | # --OR-- ^((?:\\.|[^\\"'])*?) # an $unquoted text - (\Z(?!\n)|$delimiter|(?!^)(?=["'])) + (\Z(?!\n)|$delimiter|(?!^)(?=["'])) # plus EOL, delimiter, or quote - /x; # extended layout + ([\000-\377]*) # the rest + /x; # extended layout + return() unless( $quote || length($unquoted) || length($delim)); - return() unless(length($&)); - $line = $'; + $line = $+; if ($keep) { $quoted = "$quote$quoted$quote"; @@ -71,6 +77,7 @@ sub parse_line { else { $unquoted =~ s/\\(.)/$1/g; $quoted =~ s/\\(.)/$1/g if ($quote eq '"'); + $quoted =~ s/\\([\\'])/$1/g if ( $PERL_SINGLE_QUOTE && $quote eq "'"); } $word .= ($quote) ? $quoted : $unquoted; diff --git a/t/lib/parsewords.t b/t/lib/parsewords.t index 21ed0d3..9079179 100755 --- a/t/lib/parsewords.t +++ b/t/lib/parsewords.t @@ -7,7 +7,7 @@ BEGIN { use Text::ParseWords; -print "1..15\n"; +print "1..17\n"; @words = shellwords(qq(foo "bar quiz" zoo)); print "not " if $words[0] ne 'foo'; @@ -17,11 +17,16 @@ print "ok 2\n"; print "not " if $words[2] ne 'zoo'; print "ok 3\n"; +# Gonna get some undefined things back +local($^W) = 0; + # Test quotewords() with other parameters and null last field @words = quotewords(':+', 1, 'foo:::"bar:foo":zoo zoo:'); print "not " unless join(";", @words) eq qq(foo;"bar:foo";zoo zoo;); print "ok 4\n"; +$^W = 1; + # Test $keep eq 'delimiters' and last field zero @words = quotewords('\s+', 'delimiters', '4 3 2 1 0'); print "not " unless join(";", @words) eq qq(4; ;3; ;2; ;1; ;0); @@ -66,6 +71,9 @@ print "ok 11\n"; print "not " if (@words); print "ok 12\n"; +# Gonna get some more undefined things back +$^W = 0; + @words = nested_quotewords('s+', 0, $string); print "not " if (@words); print "ok 13\n"; @@ -79,3 +87,17 @@ print "ok 14\n"; $result = join('|', parse_line(':', 0, ':"0":')); print "not " unless ($result eq '|0|'); print "ok 15\n"; + +# Test for \001 in quoted string +$result = join('|', parse_line(':', 0, ':"' . "\001" . '":')); +print "not " unless ($result eq "|\1|"); +print "ok 16\n"; + +$^W = 1; + +# Now test perlish single quote behavior +$Text::ParseWords::PERL_SINGLE_QUOTE = 1; +$string = 'aaaa"bbbbb" cc\ cc \\\\\"dddd\' eee\\\\\"\\\'ffff\' gg'; +$result = join('|', parse_line('\s+', 0, $string)); +print "not " unless $result eq 'aaaabbbbb|cc cc|\"dddd eee\\\\"\'ffff|gg'; +print "ok 17\n";