File Coverage

blib/lib/Text/Tabs.pm
Criterion Covered Total %
statement 46 46 100.0
branch 12 14 85.7
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 65 67 97.0


line stmt bran cond sub pod time code
1 20     20   17902 use strict; use warnings;
  20     20   38  
  20         790  
  20         99  
  20         35  
  20         2194  
2              
3             package Text::Tabs;
4              
5 20     20   126 BEGIN { require Exporter; *import = \&Exporter::import }
  20         15908  
6              
7             our @EXPORT = qw( expand unexpand $tabstop );
8              
9             our $VERSION = '2024.001';
10             our $SUBVERSION = 'modern'; # back-compat vestige
11              
12             our $tabstop = 8;
13              
14             sub expand {
15 955     955 1 485758 my @l;
16             my $pad;
17 955         1822 for ( @_ ) {
18 955 100       2076 defined or do { push @l, ''; next };
  1         1  
  1         13  
19 954         1560 my $s = '';
20 954         10026 for (split(/^/m, $_, -1)) {
21 823         2666 my $offs;
22 823         7120 for (split(/\t/, $_, -1)) {
23 887 100       1919 if (defined $offs) {
24 64         77 $pad = $tabstop - $offs % $tabstop;
25 64         99 $s .= " " x $pad;
26             }
27 887         6518 $s .= $_;
28 887         111019 $offs = /^\pM/ + ( () = /\PM/g );
29             }
30             }
31 954         3091 push(@l, $s);
32             }
33 955 100       2570 return @l if wantarray;
34 734         1751 return $l[0];
35             }
36              
37             sub unexpand
38             {
39 412     412 1 125887 my (@l) = @_;
40 412         1560 my @e;
41             my $x;
42 412         0 my $line;
43 412         0 my @lines;
44 412         0 my $lastbit;
45 412         1020 my $ts_as_space = " " x $tabstop;
46 412         869 for $x (@l) {
47 414 100       966 defined $x or next;
48 411         4906 @lines = split("\n", $x, -1);
49 411         739 for $line (@lines) {
50 609         1124 $line = expand($line);
51 609         677434 @e = split(/((?:\PM\pM*|^\pM+){$tabstop})/,$line,-1);
52 609         1215 $lastbit = pop(@e);
53 609 100       1374 $lastbit = ''
54             unless defined $lastbit;
55 609 50       1341 $lastbit = "\t"
56             if $lastbit eq $ts_as_space;
57 609         1263 for $_ (@e) {
58 3578         6193 s/ +$/\t/;
59             }
60 609         4419 $line = join('',@e, $lastbit);
61             }
62 411         1327 $x = join("\n", @lines);
63             }
64 412 50       875 return @l if wantarray;
65 412         2230 return $l[0];
66             }
67              
68             1;
69              
70             __END__