1package Text::Tabs; 2 3require Exporter; 4 5@ISA = (Exporter); 6@EXPORT = qw(expand unexpand $tabstop); 7 8use vars qw($VERSION $SUBVERSION $tabstop $debug); 9$VERSION = 2013.0523; 10$SUBVERSION = 'modern'; 11 12use strict; 13 14use 5.010_000; 15 16BEGIN { 17 $tabstop = 8; 18 $debug = 0; 19} 20 21my $CHUNK = qr/\X/; 22 23sub _xlen (_) { scalar(() = $_[0] =~ /$CHUNK/g) } 24sub _xpos (_) { _xlen( substr( $_[0], 0, pos($_[0]) ) ) } 25 26sub expand { 27 my @l; 28 my $pad; 29 for ( @_ ) { 30 my $s = ''; 31 for (split(/^/m, $_, -1)) { 32 my $offs = 0; 33 s{\t}{ 34 # this works on both 5.10 and 5.11 35 $pad = $tabstop - (_xlen(${^PREMATCH}) + $offs) % $tabstop; 36 # this works on 5.11, but fails on 5.10 37 #XXX# $pad = $tabstop - (_xpos() + $offs) % $tabstop; 38 $offs += $pad - 1; 39 " " x $pad; 40 }peg; 41 $s .= $_; 42 } 43 push(@l, $s); 44 } 45 return @l if wantarray; 46 return $l[0]; 47} 48 49sub unexpand 50{ 51 my (@l) = @_; 52 my @e; 53 my $x; 54 my $line; 55 my @lines; 56 my $lastbit; 57 my $ts_as_space = " " x $tabstop; 58 for $x (@l) { 59 @lines = split("\n", $x, -1); 60 for $line (@lines) { 61 $line = expand($line); 62 @e = split(/(${CHUNK}{$tabstop})/,$line,-1); 63 $lastbit = pop(@e); 64 $lastbit = '' 65 unless defined $lastbit; 66 $lastbit = "\t" 67 if $lastbit eq $ts_as_space; 68 for $_ (@e) { 69 if ($debug) { 70 my $x = $_; 71 $x =~ s/\t/^I\t/gs; 72 print "sub on '$x'\n"; 73 } 74 s/ +$/\t/; 75 } 76 $line = join('',@e, $lastbit); 77 } 78 $x = join("\n", @lines); 79 } 80 return @l if wantarray; 81 return $l[0]; 82} 83 841; 85__END__ 86 87sub expand 88{ 89 my (@l) = @_; 90 for $_ (@l) { 91 1 while s/(^|\n)([^\t\n]*)(\t+)/ 92 $1. $2 . (" " x 93 ($tabstop * length($3) 94 - (length($2) % $tabstop))) 95 /sex; 96 } 97 return @l if wantarray; 98 return $l[0]; 99} 100 101 102=head1 NAME 103 104Text::Tabs - expand and unexpand tabs like unix expand(1) and unexpand(1) 105 106=head1 SYNOPSIS 107 108 use Text::Tabs; 109 110 $tabstop = 4; # default = 8 111 @lines_without_tabs = expand(@lines_with_tabs); 112 @lines_with_tabs = unexpand(@lines_without_tabs); 113 114=head1 DESCRIPTION 115 116Text::Tabs does most of what the unix utilities expand(1) and unexpand(1) 117do. Given a line with tabs in it, C<expand> replaces those tabs with 118the appropriate number of spaces. Given a line with or without tabs in 119it, C<unexpand> adds tabs when it can save bytes by doing so, 120like the C<unexpand -a> command. 121 122Unlike the old unix utilities, this module correctly accounts for 123any Unicode combining characters (such as diacriticals) that may occur 124in each line for both expansion and unexpansion. These are overstrike 125characters that do not increment the logical position. Make sure 126you have the appropriate Unicode settings enabled. 127 128=head1 EXPORTS 129 130The following are exported: 131 132=over 4 133 134=item expand 135 136=item unexpand 137 138=item $tabstop 139 140The C<$tabstop> variable controls how many column positions apart each 141tabstop is. The default is 8. 142 143Please note that C<local($tabstop)> doesn't do the right thing and if you want 144to use C<local> to override C<$tabstop>, you need to use 145C<local($Text::Tabs::tabstop)>. 146 147=back 148 149=head1 EXAMPLE 150 151 #!perl 152 # unexpand -a 153 use Text::Tabs; 154 155 while (<>) { 156 print unexpand $_; 157 } 158 159Instead of the shell's C<expand> command, use: 160 161 perl -MText::Tabs -n -e 'print expand $_' 162 163Instead of the shell's C<unexpand -a> command, use: 164 165 perl -MText::Tabs -n -e 'print unexpand $_' 166 167=head1 SUBVERSION 168 169This module comes in two flavors: one for modern perls (5.10 and above) 170and one for ancient obsolete perls. The version for modern perls has 171support for Unicode. The version for old perls does not. You can tell 172which version you have installed by looking at C<$Text::Tabs::SUBVERSION>: 173it is C<old> for obsolete perls and C<modern> for current perls. 174 175This man page is for the version for modern perls and so that's probably 176what you've got. 177 178=head1 BUGS 179 180Text::Tabs handles only tabs (C<"\t">) and combining characters (C</\pM/>). It doesn't 181count backwards for backspaces (C<"\t">), omit other non-printing control characters (C</\pC/>), 182or otherwise deal with any other zero-, half-, and full-width characters. 183 184=head1 LICENSE 185 186Copyright (C) 1996-2002,2005,2006 David Muir Sharnoff. 187Copyright (C) 2005 Aristotle Pagaltzis 188Copyright (C) 2012-2013 Google, Inc. 189This module may be modified, used, copied, and redistributed at your own risk. 190Although allowed by the preceding license, please do not publicly 191redistribute modified versions of this code with the name "Text::Tabs" 192unless it passes the unmodified Text::Tabs test suite. 193