Revision 98c23539
Von Moritz Bunkus vor fast 9 Jahren hinzugefügt
SL/Util.pm | ||
---|---|---|
6 | 6 |
|
7 | 7 |
use Carp; |
8 | 8 |
|
9 |
our @EXPORT_OK = qw(_hashify camelify snakify); |
|
9 |
our @EXPORT_OK = qw(_hashify camelify snakify trim);
|
|
10 | 10 |
|
11 | 11 |
sub _hashify { |
12 | 12 |
my $keep = shift; |
... | ... | |
31 | 31 |
lc $str; |
32 | 32 |
} |
33 | 33 |
|
34 |
sub trim { |
|
35 |
my $value = shift; |
|
36 |
$value =~ s{^ \p{WSpace}+ | \p{WSpace}+ $}{}xg if defined($value); |
|
37 |
return $value; |
|
38 |
} |
|
39 |
|
|
34 | 40 |
1; |
35 | 41 |
__END__ |
36 | 42 |
|
... | ... | |
90 | 96 |
|
91 | 97 |
L</camilify> does the reverse. |
92 | 98 |
|
99 |
=item C<trim $string> |
|
100 |
|
|
101 |
Removes all leading and trailing whitespaces from C<$string> and |
|
102 |
returns it. Whitespaces within the string won't be changed. |
|
103 |
|
|
104 |
This function considers everything matching the Unicode character |
|
105 |
property "Whitespace" (C<WSpace>) to be a whitespace. |
|
106 |
|
|
93 | 107 |
=back |
94 | 108 |
|
95 | 109 |
=head1 BUGS |
t/helper/trim.t | ||
---|---|---|
1 |
use Test::More tests => 11; |
|
2 |
|
|
3 |
use strict; |
|
4 |
use utf8; |
|
5 |
|
|
6 |
use lib 't'; |
|
7 |
|
|
8 |
use SL::Util qw(trim); |
|
9 |
|
|
10 |
is(trim("hello"), "hello", "hello"); |
|
11 |
is(trim("hello "), "hello", "hello "); |
|
12 |
is(trim(" hello"), "hello", " hello"); |
|
13 |
is(trim(" hello "), "hello", " hello "); |
|
14 |
is(trim(" h el lo "), "h el lo", " h el lo "); |
|
15 |
is(trim("\n\t\rh\nello"), "h\nello", "line feed, horizontal tab, carriage return; line feed within word"); |
|
16 |
is(trim("\x{a0}h\nello"), "h\nello", "non-breaking space"); |
|
17 |
is(trim("h\nello\n\t\r"), "h\nello", "line feed, horizontal tab, carriage return; line feed within word"); |
|
18 |
is(trim("h\nello\x{a0}"), "h\nello", "non-breaking space"); |
|
19 |
is(trim("h\ne\x{a0}llo\x{a0}"), "h\ne\x{a0}llo", "non-breaking space within word"); |
|
20 |
is(trim(undef), undef, "undef"); |
Auch abrufbar als: Unified diff
trim-Funktion zum Entfernen führender und anhängender Whitespaces