Commit | Line | Data |
---|---|---|
cb1a09d0 | 1 | |
75f92628 | 2 | package Text::Tabs; |
a0d0e21e | 3 | |
4 | require Exporter; | |
5 | ||
6 | @ISA = (Exporter); | |
7 | @EXPORT = qw(expand unexpand $tabstop); | |
8 | ||
8bc713e8 | 9 | use vars qw($VERSION $tabstop $debug); |
146174a9 | 10 | $VERSION = 98.112801; |
8bc713e8 | 11 | |
12 | use strict; | |
13 | ||
14 | BEGIN { | |
15 | $tabstop = 8; | |
16 | $debug = 0; | |
17 | } | |
a0d0e21e | 18 | |
19 | sub expand | |
20 | { | |
146174a9 | 21 | my (@l) = @_; |
4633a7c4 | 22 | for $_ (@l) { |
8bc713e8 | 23 | 1 while s/(^|\n)([^\t\n]*)(\t+)/ |
24 | $1. $2 . (" " x | |
25 | ($tabstop * length($3) | |
26 | - (length($2) % $tabstop))) | |
27 | /sex; | |
a0d0e21e | 28 | } |
4633a7c4 | 29 | return @l if wantarray; |
4fc6b8d8 | 30 | return $l[0]; |
a0d0e21e | 31 | } |
32 | ||
33 | sub unexpand | |
34 | { | |
146174a9 | 35 | my (@l) = @_; |
a0d0e21e | 36 | my @e; |
8bc713e8 | 37 | my $x; |
38 | my $line; | |
39 | my @lines; | |
40 | my $lastbit; | |
4633a7c4 | 41 | for $x (@l) { |
8bc713e8 | 42 | @lines = split("\n", $x, -1); |
43 | for $line (@lines) { | |
44 | $line = expand($line); | |
45 | @e = split(/(.{$tabstop})/,$line,-1); | |
46 | $lastbit = pop(@e); | |
47 | $lastbit = '' unless defined $lastbit; | |
48 | $lastbit = "\t" | |
49 | if $lastbit eq " "x$tabstop; | |
50 | for $_ (@e) { | |
51 | if ($debug) { | |
52 | my $x = $_; | |
53 | $x =~ s/\t/^I\t/gs; | |
54 | print "sub on '$x'\n"; | |
55 | } | |
56 | s/ +$/\t/; | |
57 | } | |
58 | $line = join('',@e, $lastbit); | |
a0d0e21e | 59 | } |
8bc713e8 | 60 | $x = join("\n", @lines); |
a0d0e21e | 61 | } |
4633a7c4 | 62 | return @l if wantarray; |
4fc6b8d8 | 63 | return $l[0]; |
a0d0e21e | 64 | } |
65 | ||
66 | 1; | |
8bc713e8 | 67 | __END__ |
68 | ||
69 | ||
70 | =head1 NAME | |
71 | ||
4fc6b8d8 | 72 | Text::Tabs -- expand and unexpand tabs per the unix expand(1) and unexpand(1) |
8bc713e8 | 73 | |
74 | =head1 SYNOPSIS | |
75 | ||
cb50131a | 76 | use Text::Tabs; |
8bc713e8 | 77 | |
cb50131a | 78 | $tabstop = 4; |
79 | @lines_without_tabs = expand(@lines_with_tabs); | |
80 | @lines_with_tabs = unexpand(@lines_without_tabs); | |
8bc713e8 | 81 | |
82 | =head1 DESCRIPTION | |
83 | ||
84 | Text::Tabs does about what the unix utilities expand(1) and unexpand(1) | |
85 | do. Given a line with tabs in it, expand will replace the tabs with | |
86 | the appropriate number of spaces. Given a line with or without tabs in | |
87 | it, unexpand will add tabs when it can save bytes by doing so. Invisible | |
88 | compression with plain ascii! | |
89 | ||
90 | =head1 BUGS | |
91 | ||
92 | expand doesn't handle newlines very quickly -- do not feed it an | |
93 | entire document in one string. Instead feed it an array of lines. | |
94 | ||
95 | =head1 AUTHOR | |
96 | ||
4fc6b8d8 | 97 | David Muir Sharnoff <muir@idiom.com> |