File Coverage

blib/lib/Compress/LeadingBlankSpaces.pm
Criterion Covered Total %
statement 46 47 97.8
branch 14 16 87.5
condition 7 9 77.7
subroutine 6 6 100.0
pod 0 3 0.0
total 73 81 90.1


line stmt bran cond sub pod time code
1             package Compress::LeadingBlankSpaces;
2              
3 1     1   36989 use 5.004;
  1         4  
  1         105  
4 1     1   7 use strict;
  1         2  
  1         247  
5              
6 1     1   7 use vars qw($VERSION);
  1         7  
  1         2104  
7             $VERSION = '0.06';
8              
9             sub new { # class/instance constructor, ready to sub-class
10 1     1 0 140 my $proto = shift;
11 1   33     9 my $class = ref($proto) || $proto;
12 1         3 my $self = {};
13 1         4 bless ($self, $class);
14              
15 1         8 $self->{TAGS} = []; # a reference to the array of special tags
16 1         4 $self->{TAGS}->[0]->{HEADER} = '
17 1         4 $self->{TAGS}->[0]->{FOOTER} = '';
18 1         3 $self->{TAGS}->[1]->{HEADER} = '
19 1         3 $self->{TAGS}->[1]->{FOOTER} = '';
20 1         3 $self->{TAGS}->[2]->{HEADER} = '
21 1         3 $self->{TAGS}->[2]->{FOOTER} = '';
22              
23 1         3 $self->{FORMATTED} = -1; # index of currently active special tag.
24             # we should never compress blank spaces within the FORMATTED content.
25 1         3 return $self;
26             }
27              
28             # sub format_status takes one optional parameter.
29             # If called with an argument, it sets the FORMATTED field;
30             # otherwise it just returns the value held by that field,
31             #
32             sub format_status {
33 28     28 0 668 my $self = shift;
34 28         36 my $val = shift;
35 28 100       53 $self->{FORMATTED} = $val if defined ($val);
36 28         68 return $self->{FORMATTED};
37             }
38              
39             sub squeeze_string {
40 20     20 0 604 my $self = shift;
41 20         26 my $buf = shift;
42 20 50       65 return '' unless $buf; # empty, zero or undefined input...
43 20         28 chomp $buf; # the problem of the file's last character that is not a 'new-string'
44             # was brought to my attention by Chris Clandingboel on 07/29/04. Thanks Chris!
45 20 100       44 if ( $self->{FORMATTED} >= 0 ){
46             # no compression:
47 6         13 my $end_tag = $self->{TAGS}->[$self->{FORMATTED}]->{FOOTER};
48             # note: full end-tag should appear within one string only!
49 6 100       46 $self->{FORMATTED} = -1 if $buf =~ /$end_tag/i; # resume the compression
50             # since the next input
51             } else { # try to compress
52 14         38 $buf =~ s/^\s+(\S.*)/$1/;
53 14         45 while ($buf =~ /^\s/o){
54 0         0 $buf =~ s/^\s+//o;
55             }
56 14         15 my $index = 0;
57 14         14 foreach ( @{ $self->{TAGS} } ){
  14         31  
58            
59 27         49 my $beg_tag = $self->{TAGS}->[$index]->{HEADER};
60 27 100       209 if ($buf =~ /$beg_tag/i){ # it might be a special tag
61 13 100 100     167 if ($buf =~ /$beg_tag>/i # simple
      100        
62             or $buf =~ /$beg_tag\s/i # has in-line parameters
63             or $buf =~ /$beg_tag$/i # has next-line parameters
64             ){ # this _is_ a special tag
65 12         25 $self->{FORMATTED} = $index; # hold on the compression
66             # since the next input
67             }
68             }
69 27 100       65 last if $self->{FORMATTED} >= 0;
70 15         26 $index += 1;
71             }
72             }
73 20 50       46 return '' unless length($buf) > 0;
74 20         59 return $buf."\n";
75             }
76              
77             1;
78             __END__