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   80676 use base qw(Exporter);
  22         92  
  22         2596  
4 22     22   154 use strict;
  22         61  
  22         515  
5 22     22   123 use warnings;
  22         83  
  22         810  
6              
7 22     22   3834 use Error::Pure qw(err);
  22         90397  
  22         820  
8 22     22   569 use Readonly;
  22         41  
  22         9843  
9              
10             # Constants.
11             Readonly::Scalar my $DEFAULT_TAB_LENGTH => 8;
12             Readonly::Scalar my $SPACE => q{ };
13              
14             our $VERSION = 0.06;
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 21539 my $self = shift;
26 25 100 100     233 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         19 'line_size', $self->{'line_size'};
31             }
32 21         51 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 1429 my $string_sr = shift;
39 3         7 ${$string_sr} =~ s/\s+/\ /gms;
  3         20  
40 3         8 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 1465 my $string_sr = shift;
47 9         15 ${$string_sr} =~ s/^\s*//ms;
  9         31  
48 9         20 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 1510 my $string_sr = shift;
55 9         15 ${$string_sr} =~ s/\s*$//ms;
  9         63  
56 9         19 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 2990 my $string_sr = shift;
63 6         20 remove_last_ws($string_sr);
64 6         14 remove_first_ws($string_sr);
65 6         9 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 4732 my $string = shift;
74 66         113 my $tmp = $SPACE x $TAB_LENGTH;
75 66         111 $string =~ s/\t/$tmp/gms;
76 66         91 my $length_of_string = length $string;
77 66         166 return $length_of_string;
78             }
79              
80             1;
81              
82             __END__