File Coverage

blib/lib/Indent/Utils.pm
Criterion Covered Total %
statement 40 40 100.0
branch 2 2 100.0
condition 3 3 100.0
subroutine 11 11 100.0
pod 6 6 100.0
total 62 62 100.0


line stmt bran cond sub pod time code
1             package Indent::Utils;
2              
3 22     22   81579 use base qw(Exporter);
  22         80  
  22         2584  
4 22     22   153 use strict;
  22         52  
  22         487  
5 22     22   111 use warnings;
  22         35  
  22         745  
6              
7 22     22   3367 use Error::Pure qw(err);
  22         76253  
  22         798  
8 22     22   496 use Readonly;
  22         42  
  22         9645  
9              
10             # Constants.
11             Readonly::Scalar my $DEFAULT_TAB_LENGTH => 8;
12             Readonly::Scalar my $SPACE => q{ };
13              
14             our $VERSION = 0.08;
15              
16             # Length of tab.
17             our $TAB_LENGTH = $DEFAULT_TAB_LENGTH;
18              
19             # Export.
20             our @EXPORT_OK = qw(line_size_check reduce_duplicit_ws remove_first_ws
21             remove_last_ws remove_ws string_len);
22              
23             # Line size check.
24             sub line_size_check {
25 25     25 1 17997 my $self = shift;
26 25 100 100     237 if (! defined $self->{'line_size'}
27             || $self->{'line_size'} !~ m/^\d+$/ms) {
28              
29             err '\'line_size\' parameter must be a positive number.',
30 4         16 'line_size', $self->{'line_size'};
31             }
32 21         48 return;
33             }
34              
35             # Reduce duplicit blank space in string to one space.
36             # @param $string Reference to data string.
37             sub reduce_duplicit_ws {
38 3     3 1 1083 my $string_sr = shift;
39 3         3 ${$string_sr} =~ s/\s+/\ /gms;
  3         15  
40 3         6 return;
41             }
42              
43             # Remove blank characters in begin of string.
44             # @param $string Reference to data string.
45             sub remove_first_ws {
46 9     9 1 1120 my $string_sr = shift;
47 9         11 ${$string_sr} =~ s/^\s*//ms;
  9         28  
48 9         14 return;
49             }
50              
51             # Remove blank characters in end of string.
52             # @param $string Reference to data string.
53             sub remove_last_ws {
54 9     9 1 1144 my $string_sr = shift;
55 9         11 ${$string_sr} =~ s/\s*$//ms;
  9         52  
56 9         17 return;
57             }
58              
59             # Remove white characters in begin and end of string.
60             # @param $string reference to data string.
61             sub remove_ws {
62 6     6 1 2419 my $string_sr = shift;
63 6         15 remove_last_ws($string_sr);
64 6         10 remove_first_ws($string_sr);
65 6         7 return;
66             }
67              
68             # Gets length of string.
69             # @param $string Data string.
70             # @return $length_of_string Length of data string, when '\t' translate to
71             # $TAB_LENGTH x space.
72             sub string_len {
73 66     66 1 3844 my $string = shift;
74 66         112 my $tmp = $SPACE x $TAB_LENGTH;
75 66         104 $string =~ s/\t/$tmp/gms;
76 66         96 my $length_of_string = length $string;
77 66         172 return $length_of_string;
78             }
79              
80             1;
81              
82             __END__