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   167871 use base qw(Exporter);
  22         46  
  22         4973  
4 22     22   192 use strict;
  22         1522  
  22         681  
5 22     22   124 use warnings;
  22         46  
  22         1265  
6              
7 22     22   4580 use Error::Pure qw(err);
  22         108049  
  22         3167  
8 22     22   837 use Readonly;
  22         48  
  22         13318  
9              
10             # Constants.
11             Readonly::Scalar my $DEFAULT_TAB_LENGTH => 8;
12             Readonly::Scalar my $SPACE => q{ };
13              
14             our $VERSION = 0.09;
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 28     28 1 408149 my $self = shift;
26 28 100 100     292 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         18 'line_size', $self->{'line_size'};
31             }
32 24         53 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 386799 my $string_sr = shift;
39 3         5 ${$string_sr} =~ s/\s+/\ /gms;
  3         23  
40 3         10 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 417771 my $string_sr = shift;
47 9         15 ${$string_sr} =~ s/^\s*//ms;
  9         49  
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 366459 my $string_sr = shift;
55 9         16 ${$string_sr} =~ s/\s*$//ms;
  9         77  
56 9         20 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 344116 my $string_sr = shift;
63 6         26 remove_last_ws($string_sr);
64 6         16 remove_first_ws($string_sr);
65 6         39 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 332622 my $string = shift;
74 66         166 my $tmp = $SPACE x $TAB_LENGTH;
75 66         148 $string =~ s/\t/$tmp/gms;
76 66         103 my $length_of_string = length $string;
77 66         220 return $length_of_string;
78             }
79              
80             1;
81              
82             __END__
83              
84             =pod
85              
86             =encoding utf8
87              
88             =head1 NAME
89              
90             Indent::Utils - Utilities for Indent classes.
91              
92             =head1 SYNOPSIS
93              
94             use Indent::Utils qw(line_size_check reduce_duplicit_ws remove_first_ws
95             remove_last_ws remove_ws string_len);
96              
97             line_size_check($object_with_line_size_parameter);
98             reduce_duplicit_ws(\$string);
99             remove_first_ws(\$string);
100             remove_last_ws(\$string);
101             remove_ws(\$string);
102             my $length_of_string = string_len($string);
103              
104             =head1 GLOBAL VARIABLES
105              
106             =over 8
107              
108             =item C<TAB_LENGTH>
109              
110             Default length of tabelator is 8 chars.
111              
112             =back
113              
114             =head1 SUBROUTINES
115              
116             =head2 C<line_size_check>
117              
118             line_size_check($object_with_line_size_parameter);
119              
120             Line size 'line_size' parameter check.
121              
122             =head2 C<reduce_duplicit_ws>
123              
124             reduce_duplicit_ws(\$string);
125              
126             Reduce duplicit blank space in string to one space.
127              
128             =head2 C<remove_first_ws>
129              
130             remove_first_ws(\$string);
131              
132             Remove blank characters in begin of string.
133              
134             =head2 C<remove_last_ws>
135              
136             remove_last_ws(\$string);
137              
138             Remove blank characters in end of string.
139              
140             =head2 C<remove_ws>
141              
142             remove_ws(\$string);
143              
144             Remove white characters in begin and end of string.
145              
146             =head2 C<string_len>
147              
148             my $length_of_string = string_len($string);
149              
150             Gets length of string.
151              
152             =head1 ERRORS
153              
154             line_size_check():
155             'line_size' parameter must be a positive number.
156             'line_size', %s
157              
158             =head1 EXAMPLE1
159              
160             use strict;
161             use warnings;
162              
163             use Indent::Utils qw(reduce_duplicit_ws);
164              
165             my $input = 'a b';
166             reduce_duplicit_ws(\$input);
167             print "$input|\n";
168              
169             # Output:
170             # a b|
171              
172             =head1 EXAMPLE2
173              
174             use strict;
175             use warnings;
176              
177             use Indent::Utils qw(remove_first_ws);
178              
179             my $input = ' a';
180             remove_first_ws(\$input);
181             print "$input|\n";
182              
183             # Output:
184             # a|
185              
186             =head1 EXAMPLE3
187              
188             use strict;
189             use warnings;
190              
191             use Indent::Utils qw(remove_last_ws);
192              
193             my $input = 'a ';
194             remove_last_ws(\$input);
195             print "$input|\n";
196              
197             # Output:
198             # a|
199              
200             =head1 EXAMPLE4
201              
202             use strict;
203             use warnings;
204              
205             use Indent::Utils qw(remove_ws);
206              
207             my $input = ' a ';
208             remove_ws(\$input);
209             print "$input|\n";
210              
211             # Output:
212             # a|
213              
214             =head1 EXAMPLE5
215              
216             use strict;
217             use warnings;
218              
219             use Indent::Utils qw(string_len);
220              
221             # Print string length.
222             print string_len("\tab\t")."\n";
223              
224             # Output:
225             # 18
226              
227             =head1 DEPENDENCIES
228              
229             L<Error::Pure>,
230             L<Exporter>,
231             L<Readonly>.
232              
233             =head1 SEE ALSO
234              
235             =over
236              
237             =item L<Indent>
238              
239             Class for indent handling.
240              
241             =item L<Indent::Block>
242              
243             Class for block indenting.
244              
245             =item L<Indent::Data>
246              
247             Class for data indenting.
248              
249             =item L<Indent::String>
250              
251             Class for text indenting.
252              
253             =item L<Indent::Word>
254              
255             Class for word indenting.
256              
257             =back
258              
259             =head1 REPOSITORY
260              
261             L<https://github.com/michal-josef-spacek/Indent>
262              
263             =head1 AUTHOR
264              
265             Michal Josef Špaček L<mailto:skim@cpan.org>
266              
267             L<http://skim.cz>
268              
269             =head1 LICENSE AND COPYRIGHT
270              
271             © 2005-2024 Michal Josef Špaček
272              
273             BSD 2-Clause License
274              
275             =head1 VERSION
276              
277             0.09
278              
279             =cut