summaryrefslogtreecommitdiff
path: root/lib/Text/Tabs.pm
blob: fa866988cfd6cd792d07a4217a7ebc3f9f76f9fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#
# expand and unexpand tabs as per the unix expand and 
# unexpand programs.
#
# expand and unexpand operate on arrays of lines.  
#
# David Muir Sharnoff <muir@idiom.com>
# Version: 4/19/95
# 

package Text::Tabs;

require Exporter;

@ISA = (Exporter);
@EXPORT = qw(expand unexpand $tabstop);

$tabstop = 8;

sub expand
{
	my (@l) = @_;
	my $l, @k;
	my $nl;
	for $l (@l) {
		$nl = $/ if chomp($l);
		@k = split($/,$l);
		for $_ (@k) {
			1 while s/^([^\t]*)(\t+)/
				$1 . (" " x 
					($tabstop * length($2)
					- (length($1) % $tabstop)))
				/e;
		}
		$l = join("\n",@k).$nl;
	}
	return @l if $#l > 0;
	return $l[0];
}

sub unexpand
{
	my (@l) = &expand(@_);
	my @e;
	my $k, @k;
	my $nl;
	for $k (@l) {
		$nl = $/ if chomp($k);
		@k = split($/,$k);
		for $x (@k) {
			@e = split(/(.{$tabstop})/,$x);
			for $_ (@e) {
				s/  +$/\t/;
			}
			$x = join('',@e);
		}
		$k = join("\n",@k).$nl;
	}
	return @l if $#l > 0;
	return $l[0];
}

1;