File Coverage

blib/lib/String/Trim/NonRegex.pm
Criterion Covered Total %
statement 43 43 100.0
branch 13 16 81.2
condition 24 24 100.0
subroutine 7 7 100.0
pod 3 3 100.0
total 90 93 96.7


line stmt bran cond sub pod time code
1             package String::Trim::NonRegex;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2021-06-23'; # DATE
5             our $DIST = 'String-Trim-NonRegex'; # DIST
6             our $VERSION = '0.002'; # VERSION
7              
8 1     1   56920 use 5.010001;
  1         12  
9 1     1   4 use strict;
  1         2  
  1         17  
10 1     1   5 use warnings;
  1         1  
  1         46  
11              
12 1     1   5 use Exporter;
  1         2  
  1         367  
13             our @ISA = qw(Exporter);
14             our @EXPORT_OK = qw(
15             ltrim
16             rtrim
17             trim
18             ltrim_lines
19             rtrim_lines
20             trim_lines
21             trim_blank_lines
22              
23             ellipsis
24             );
25              
26             sub ltrim {
27 6     6 1 1071 my $str = shift;
28 6         11 my $len = length $str;
29 6         7 my $i = 0;
30 6         7 while (1) {
31 13         21 my $c = substr($str, $i, 1);
32 13 100 100     47 last unless $c eq ' ' || $c eq "\t" || $c eq "\n";
      100        
33 7         9 $i++;
34 7 50       11 last if $i >= $len;
35             }
36 6         23 substr($str, $i);
37             }
38              
39             sub rtrim {
40 6     6 1 2242 my $str = shift;
41 6         8 my $i = length($str)-1;
42 6         10 while (1) {
43 15         30 my $c = substr($str, $i, 1);
44 15 100 100     57 last unless $c eq ' ' || $c eq "\t" || $c eq "\n";
      100        
45 9         12 $i--;
46 9 50       17 last if $i < 0;
47             }
48 6         22 substr($str, 0, $i+1);
49             }
50              
51             sub trim {
52 6     6 1 2097 my $str = shift;
53 6         9 my $len = length $str;
54              
55 6         11 my $i = $len-1;
56 6         7 while (1) {
57 15         22 my $c = substr($str, $i, 1);
58 15 100 100     53 last unless $c eq ' ' || $c eq "\t" || $c eq "\n";
      100        
59 9         10 $i--;
60 9 50       16 last if $i < 0;
61             }
62              
63 6         8 my $j = 0;
64 6         7 while (1) {
65 11         15 my $c = substr($str, $j, 1);
66 11 100 100     37 last unless $c eq ' ' || $c eq "\t" || $c eq "\n";
      100        
67 7         7 $j++;
68 7 100       12 last if $j >= $i;
69             }
70 6         24 substr($str, $j, $i-$j+1);
71             }
72              
73             #sub ltrim_lines {
74             # my $str = shift;
75             # $str =~ s/^[ \t]+//mg; # XXX other unicode non-newline spaces
76             # $str;
77             #}
78              
79             #sub rtrim_lines {
80             # my $str = shift;
81             # $str =~ s/[ \t]+$//mg;
82             # $str;
83             #}
84              
85             #sub trim_lines {
86             # my $str = shift;
87             # $str =~ s/^[ \t]+//mg;
88             # $str =~ s/[ \t]+$//mg;
89             # $str;
90             #}
91              
92             #sub trim_blank_lines {
93             # local $_ = shift;
94             # return $_ unless defined;
95             # s/\A(?:\n\s*)+//;
96             # s/(?:\n\s*){2,}\z/\n/;
97             # $_;
98             #}
99              
100             #sub ellipsis {
101             # my ($str, $maxlen, $ellipsis) = @_;
102             # $maxlen //= 80;
103             # $ellipsis //= "...";
104             #
105             # if (length($str) <= $maxlen) {
106             # return $str;
107             # } else {
108             # return substr($str, 0, $maxlen-length($ellipsis)) . $ellipsis;
109             # }
110             #}
111              
112             1;
113             # ABSTRACT: String trimming functions that do not use regex
114              
115             __END__