use Carp;
-our @EXPORT_OK = qw(_hashify camelify snakify);
+our @EXPORT_OK = qw(_hashify camelify snakify trim);
sub _hashify {
my $keep = shift;
lc $str;
}
+sub trim {
+ my $value = shift;
+ $value =~ s{^ \p{WSpace}+ | \p{WSpace}+ $}{}xg if defined($value);
+ return $value;
+}
+
1;
__END__
L</camilify> does the reverse.
+=item C<trim $string>
+
+Removes all leading and trailing whitespaces from C<$string> and
+returns it. Whitespaces within the string won't be changed.
+
+This function considers everything matching the Unicode character
+property "Whitespace" (C<WSpace>) to be a whitespace.
+
=back
=head1 BUGS
--- /dev/null
+use Test::More tests => 11;
+
+use strict;
+use utf8;
+
+use lib 't';
+
+use SL::Util qw(trim);
+
+is(trim("hello"), "hello", "hello");
+is(trim("hello "), "hello", "hello ");
+is(trim(" hello"), "hello", " hello");
+is(trim(" hello "), "hello", " hello ");
+is(trim(" h el lo "), "h el lo", " h el lo ");
+is(trim("\n\t\rh\nello"), "h\nello", "line feed, horizontal tab, carriage return; line feed within word");
+is(trim("\x{a0}h\nello"), "h\nello", "non-breaking space");
+is(trim("h\nello\n\t\r"), "h\nello", "line feed, horizontal tab, carriage return; line feed within word");
+is(trim("h\nello\x{a0}"), "h\nello", "non-breaking space");
+is(trim("h\ne\x{a0}llo\x{a0}"), "h\ne\x{a0}llo", "non-breaking space within word");
+is(trim(undef), undef, "undef");