ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/staticperl/perl/lib/Text/Tabs.pm
Revision: 1.1
Committed: Thu Jun 30 14:27:16 2005 UTC (21 years, 3 months ago) by root
Branch: MAIN
CVS Tags: PERL-5-8-7, HEAD
Branch point for: PERL
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1
2     package Text::Tabs;
3    
4     require Exporter;
5    
6     @ISA = (Exporter);
7     @EXPORT = qw(expand unexpand $tabstop);
8    
9     use vars qw($VERSION $tabstop $debug);
10     $VERSION = 98.112801;
11    
12     use strict;
13    
14     BEGIN {
15     $tabstop = 8;
16     $debug = 0;
17     }
18    
19     sub expand
20     {
21     my (@l) = @_;
22     for $_ (@l) {
23     1 while s/(^|\n)([^\t\n]*)(\t+)/
24     $1. $2 . (" " x
25     ($tabstop * length($3)
26     - (length($2) % $tabstop)))
27     /sex;
28     }
29     return @l if wantarray;
30     return $l[0];
31     }
32    
33     sub unexpand
34     {
35     my (@l) = @_;
36     my @e;
37     my $x;
38     my $line;
39     my @lines;
40     my $lastbit;
41     for $x (@l) {
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);
59     }
60     $x = join("\n", @lines);
61     }
62     return @l if wantarray;
63     return $l[0];
64     }
65    
66     1;
67     __END__
68    
69    
70     =head1 NAME
71    
72     Text::Tabs -- expand and unexpand tabs per the unix expand(1) and unexpand(1)
73    
74     =head1 SYNOPSIS
75    
76     use Text::Tabs;
77    
78     $tabstop = 4;
79     @lines_without_tabs = expand(@lines_with_tabs);
80     @lines_with_tabs = unexpand(@lines_without_tabs);
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    
97     David Muir Sharnoff <muir@idiom.com>