EVERYTHING FROM THE OTHER REPO
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
package Text::ParseWords;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
require 5.006;
|
||||
our $VERSION = "3.31";
|
||||
|
||||
use Exporter;
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT = qw(shellwords quotewords nested_quotewords parse_line);
|
||||
our @EXPORT_OK = qw(old_shellwords);
|
||||
our $PERL_SINGLE_QUOTE;
|
||||
|
||||
sub shellwords {
|
||||
my (@lines) = @_;
|
||||
my @allwords;
|
||||
|
||||
foreach my $line (@lines) {
|
||||
$line =~ s/^\s+//;
|
||||
my @words = parse_line('\s+', 0, $line);
|
||||
pop @words if (@words and !defined $words[-1]);
|
||||
return() unless (@words || !length($line));
|
||||
push(@allwords, @words);
|
||||
}
|
||||
return(@allwords);
|
||||
}
|
||||
|
||||
sub quotewords {
|
||||
my($delim, $keep, @lines) = @_;
|
||||
my($line, @words, @allwords);
|
||||
|
||||
foreach $line (@lines) {
|
||||
@words = parse_line($delim, $keep, $line);
|
||||
return() unless (@words || !length($line));
|
||||
push(@allwords, @words);
|
||||
}
|
||||
return(@allwords);
|
||||
}
|
||||
|
||||
sub nested_quotewords {
|
||||
my($delim, $keep, @lines) = @_;
|
||||
my($i, @allwords);
|
||||
|
||||
for ($i = 0; $i < @lines; $i++) {
|
||||
@{$allwords[$i]} = parse_line($delim, $keep, $lines[$i]);
|
||||
return() unless (@{$allwords[$i]} || !length($lines[$i]));
|
||||
}
|
||||
return(@allwords);
|
||||
}
|
||||
|
||||
sub parse_line {
|
||||
my($delimiter, $keep, $line) = @_;
|
||||
my($word, @pieces);
|
||||
|
||||
no warnings 'uninitialized'; # we will be testing undef strings
|
||||
|
||||
while (length($line)) {
|
||||
# This pattern is optimised to be stack conservative on older perls.
|
||||
# Do not refactor without being careful and testing it on very long strings.
|
||||
# See Perl bug #42980 for an example of a stack busting input.
|
||||
$line =~ s/^
|
||||
(?:
|
||||
# double quoted string
|
||||
(") # $quote
|
||||
((?>[^\\"]*(?:\\.[^\\"]*)*))" # $quoted
|
||||
| # --OR--
|
||||
# singe quoted string
|
||||
(') # $quote
|
||||
((?>[^\\']*(?:\\.[^\\']*)*))' # $quoted
|
||||
| # --OR--
|
||||
# unquoted string
|
||||
( # $unquoted
|
||||
(?:\\.|[^\\"'])*?
|
||||
)
|
||||
# followed by
|
||||
( # $delim
|
||||
\Z(?!\n) # EOL
|
||||
| # --OR--
|
||||
(?-x:$delimiter) # delimiter
|
||||
| # --OR--
|
||||
(?!^)(?=["']) # a quote
|
||||
)
|
||||
)//xs or return; # extended layout
|
||||
my ($quote, $quoted, $unquoted, $delim) = (($1 ? ($1,$2) : ($3,$4)), $5, $6);
|
||||
|
||||
return() unless( defined($quote) || length($unquoted) || length($delim));
|
||||
|
||||
if ($keep) {
|
||||
$quoted = "$quote$quoted$quote";
|
||||
}
|
||||
else {
|
||||
$unquoted =~ s/\\(.)/$1/sg;
|
||||
if (defined $quote) {
|
||||
$quoted =~ s/\\(.)/$1/sg if ($quote eq '"');
|
||||
$quoted =~ s/\\([\\'])/$1/g if ( $PERL_SINGLE_QUOTE && $quote eq "'");
|
||||
}
|
||||
}
|
||||
$word .= substr($line, 0, 0); # leave results tainted
|
||||
$word .= defined $quote ? $quoted : $unquoted;
|
||||
|
||||
if (length($delim)) {
|
||||
push(@pieces, $word);
|
||||
push(@pieces, $delim) if ($keep eq 'delimiters');
|
||||
undef $word;
|
||||
}
|
||||
if (!length($line)) {
|
||||
push(@pieces, $word);
|
||||
}
|
||||
}
|
||||
return(@pieces);
|
||||
}
|
||||
|
||||
sub old_shellwords {
|
||||
|
||||
# Usage:
|
||||
# use ParseWords;
|
||||
# @words = old_shellwords($line);
|
||||
# or
|
||||
# @words = old_shellwords(@lines);
|
||||
# or
|
||||
# @words = old_shellwords(); # defaults to $_ (and clobbers it)
|
||||
|
||||
no warnings 'uninitialized'; # we will be testing undef strings
|
||||
local *_ = \join('', @_) if @_;
|
||||
my (@words, $snippet);
|
||||
|
||||
s/\A\s+//;
|
||||
while ($_ ne '') {
|
||||
my $field = substr($_, 0, 0); # leave results tainted
|
||||
for (;;) {
|
||||
if (s/\A"(([^"\\]|\\.)*)"//s) {
|
||||
($snippet = $1) =~ s#\\(.)#$1#sg;
|
||||
}
|
||||
elsif (/\A"/) {
|
||||
require Carp;
|
||||
Carp::carp("Unmatched double quote: $_");
|
||||
return();
|
||||
}
|
||||
elsif (s/\A'(([^'\\]|\\.)*)'//s) {
|
||||
($snippet = $1) =~ s#\\(.)#$1#sg;
|
||||
}
|
||||
elsif (/\A'/) {
|
||||
require Carp;
|
||||
Carp::carp("Unmatched single quote: $_");
|
||||
return();
|
||||
}
|
||||
elsif (s/\A\\(.?)//s) {
|
||||
$snippet = $1;
|
||||
}
|
||||
elsif (s/\A([^\s\\'"]+)//) {
|
||||
$snippet = $1;
|
||||
}
|
||||
else {
|
||||
s/\A\s+//;
|
||||
last;
|
||||
}
|
||||
$field .= $snippet;
|
||||
}
|
||||
push(@words, $field);
|
||||
}
|
||||
return @words;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
use strict; use warnings;
|
||||
|
||||
package Text::Tabs;
|
||||
|
||||
BEGIN { require Exporter; *import = \&Exporter::import }
|
||||
|
||||
our @EXPORT = qw( expand unexpand $tabstop );
|
||||
|
||||
our $VERSION = '2024.001';
|
||||
our $SUBVERSION = 'modern'; # back-compat vestige
|
||||
|
||||
our $tabstop = 8;
|
||||
|
||||
sub expand {
|
||||
my @l;
|
||||
my $pad;
|
||||
for ( @_ ) {
|
||||
defined or do { push @l, ''; next };
|
||||
my $s = '';
|
||||
for (split(/^/m, $_, -1)) {
|
||||
my $offs;
|
||||
for (split(/\t/, $_, -1)) {
|
||||
if (defined $offs) {
|
||||
$pad = $tabstop - $offs % $tabstop;
|
||||
$s .= " " x $pad;
|
||||
}
|
||||
$s .= $_;
|
||||
$offs = /^\pM/ + ( () = /\PM/g );
|
||||
}
|
||||
}
|
||||
push(@l, $s);
|
||||
}
|
||||
return @l if wantarray;
|
||||
return $l[0];
|
||||
}
|
||||
|
||||
sub unexpand
|
||||
{
|
||||
my (@l) = @_;
|
||||
my @e;
|
||||
my $x;
|
||||
my $line;
|
||||
my @lines;
|
||||
my $lastbit;
|
||||
my $ts_as_space = " " x $tabstop;
|
||||
for $x (@l) {
|
||||
defined $x or next;
|
||||
@lines = split("\n", $x, -1);
|
||||
for $line (@lines) {
|
||||
$line = expand($line);
|
||||
@e = split(/((?:\PM\pM*|^\pM+){$tabstop})/,$line,-1);
|
||||
$lastbit = pop(@e);
|
||||
$lastbit = ''
|
||||
unless defined $lastbit;
|
||||
$lastbit = "\t"
|
||||
if $lastbit eq $ts_as_space;
|
||||
for $_ (@e) {
|
||||
s/ +$/\t/;
|
||||
}
|
||||
$line = join('',@e, $lastbit);
|
||||
}
|
||||
$x = join("\n", @lines);
|
||||
}
|
||||
return @l if wantarray;
|
||||
return $l[0];
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
use strict; use warnings;
|
||||
|
||||
package Text::Wrap;
|
||||
|
||||
use warnings::register;
|
||||
|
||||
BEGIN { require Exporter; *import = \&Exporter::import }
|
||||
|
||||
our @EXPORT = qw( wrap fill );
|
||||
our @EXPORT_OK = qw( $columns $break $huge );
|
||||
|
||||
our $VERSION = '2024.001';
|
||||
our $SUBVERSION = 'modern'; # back-compat vestige
|
||||
|
||||
BEGIN { eval sprintf 'sub REGEXPS_USE_BYTES () { %d }', scalar( pack('U*', 0x80) =~ /\xc2/ ) }
|
||||
|
||||
my $brkspc = "\x{a0}\x{202f}" =~ /\s/ ? '[^\x{a0}\x{202f}\S]' : '\s';
|
||||
|
||||
our $columns = 76; # <= screen width
|
||||
our $break = '(?>\n|\r\n|'.$brkspc.'\pM*)';
|
||||
our $huge = 'wrap'; # alternatively: 'die' or 'overflow'
|
||||
our $unexpand = 1;
|
||||
our $tabstop = 8;
|
||||
our $separator = "\n";
|
||||
our $separator2 = undef;
|
||||
|
||||
sub _xlen { $_[0] =~ /^\pM/ + ( () = $_[0] =~ /\PM/g ) }
|
||||
|
||||
use Text::Tabs qw(expand unexpand);
|
||||
|
||||
sub wrap
|
||||
{
|
||||
my ($ip, $xp, @t) = map +( defined $_ ? $_ : '' ), @_;
|
||||
|
||||
local($Text::Tabs::tabstop) = $tabstop;
|
||||
my $r = "";
|
||||
my $tail = pop(@t);
|
||||
my $t = expand(join("", (map { /\s+\z/ ? ( $_ ) : ($_, ' ') } @t), $tail));
|
||||
my $lead = $ip;
|
||||
my $nll = $columns - _xlen(expand($xp)) - 1;
|
||||
if ($nll <= 0 && $xp ne '') {
|
||||
my $nc = _xlen(expand($xp)) + 2;
|
||||
warnings::warnif "Increasing \$Text::Wrap::columns from $columns to $nc to accommodate length of subsequent tab";
|
||||
$columns = $nc;
|
||||
$nll = 1;
|
||||
}
|
||||
my $ll = $columns - _xlen(expand($ip)) - 1;
|
||||
$ll = 0 if $ll < 0;
|
||||
my $nl = "";
|
||||
my $remainder = "";
|
||||
|
||||
use re 'taint';
|
||||
|
||||
pos($t) = 0;
|
||||
while ($t !~ /\G(?:$break)*\Z/gc) {
|
||||
if ($t =~ /\G((?>(?!\n)\PM\pM*|(?<![^\n])\pM+){0,$ll})($break|\n+|\z)/xmgc) {
|
||||
$r .= $unexpand
|
||||
? unexpand($nl . $lead . $1)
|
||||
: $nl . $lead . $1;
|
||||
$remainder = $2;
|
||||
} elsif ($huge eq 'wrap' && $t =~ /\G((?>(?!\n)\PM\pM*|(?<![^\n])\pM+){$ll})/gc) {
|
||||
$r .= $unexpand
|
||||
? unexpand($nl . $lead . $1)
|
||||
: $nl . $lead . $1;
|
||||
$remainder = defined($separator2) ? $separator2 : $separator;
|
||||
} elsif ($huge eq 'overflow' && $t =~ /\G([^\n]*?)(?!(?<![^\n])\pM)($break|\n+|\z)/xmgc) {
|
||||
$r .= $unexpand
|
||||
? unexpand($nl . $lead . $1)
|
||||
: $nl . $lead . $1;
|
||||
$remainder = $2;
|
||||
} elsif ($huge eq 'die') {
|
||||
die "couldn't wrap '$t'";
|
||||
} elsif ($columns < 2) {
|
||||
warnings::warnif "Increasing \$Text::Wrap::columns from $columns to 2";
|
||||
$columns = 2;
|
||||
return @_;
|
||||
} else {
|
||||
die "This shouldn't happen";
|
||||
}
|
||||
|
||||
$lead = $xp;
|
||||
$ll = $nll;
|
||||
$nl = defined($separator2)
|
||||
? ($remainder eq "\n"
|
||||
? "\n"
|
||||
: $separator2)
|
||||
: $separator;
|
||||
}
|
||||
$r .= $remainder;
|
||||
|
||||
$r .= $lead . substr($t, pos($t), length($t) - pos($t))
|
||||
if pos($t) ne length($t);
|
||||
|
||||
# the 5.6 regexp engine ignores the UTF8 flag, so using capture buffers acts as an implicit _utf8_off
|
||||
# that means on 5.6 we now have to manually set UTF8=on on the output if the input had it, for which
|
||||
# we extract just the UTF8 flag from the input and check if it forces chr(0x80) to become multibyte
|
||||
return REGEXPS_USE_BYTES && (substr($t,0,0)."\x80") =~ /\xc2/ ? pack('U0a*', $r) : $r;
|
||||
}
|
||||
|
||||
sub fill
|
||||
{
|
||||
my ($ip, $xp, @raw) = map +( defined $_ ? $_ : '' ), @_;
|
||||
my @para;
|
||||
my $pp;
|
||||
|
||||
for $pp (split(/\n\s+/, join("\n",@raw))) {
|
||||
$pp =~ s/\s+/ /g;
|
||||
my $x = wrap($ip, $xp, $pp);
|
||||
push(@para, $x);
|
||||
}
|
||||
|
||||
# if paragraph_indent is the same as line_indent,
|
||||
# separate paragraphs with blank lines
|
||||
|
||||
my $ps = ($ip eq $xp) ? "\n\n" : "\n";
|
||||
return join ($ps, @para);
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
Reference in New Issue
Block a user