Skip to content

Commit

Permalink
added trim method to Mojo::ByteStream
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 30, 2010
1 parent 65d7f91 commit 26af459
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Changes
Expand Up @@ -18,7 +18,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Added XML namespace support to Mojo::DOM.
- Added is_xhr method to Mojo::Message::Request.
- Added detect_proxy method to Mojo::Client. (DaTa)
- Added say method to Mojo::ByteStream.
- Added say and trim methods to Mojo::ByteStream.
- Added custom socket support to Mojo::Client.
- Added SHA1 support to Mojo::ByteStream. (vti)
- Renamed attributes method in Mojo::DOM to attrs.
Expand Down
14 changes: 14 additions & 0 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -732,6 +732,13 @@ sub size { length shift->{bytestream} }

sub to_string { shift->{bytestream} }

sub trim {
my $self = shift;
$self->{bytestream} =~ s/^\s*//;
$self->{bytestream} =~ s/\s*$//;
return $self;
}

sub unquote {
my $self = shift;

Expand Down Expand Up @@ -897,6 +904,7 @@ Mojo::ByteStream - ByteStream
$stream->quote;
$stream->sha1_bytes;
$stream->sha1_sum;
$stream->trim;
$stream->unquote;
$stream->url_escape;
$stream->url_sanitize;
Expand Down Expand Up @@ -1136,6 +1144,12 @@ Size of bytestream.
Stringify bytestream.
=head2 C<trim>
$stream = $stream->trim;
Trim whitespace characters from both ends of bytestream.
=head2 C<unquote>
$stream = $stream->unquote;
Expand Down
12 changes: 11 additions & 1 deletion t/mojo/bytestream.t
Expand Up @@ -14,7 +14,7 @@ use Test::More;

plan skip_all => 'Perl 5.10 required for this test!'
unless eval { require Digest::SHA; 1 };
plan tests => 74;
plan tests => 78;

use_ok('Mojo::ByteStream', 'b');

Expand Down Expand Up @@ -301,6 +301,16 @@ is("$stream", 'bcher-kva', 'right punycode encoded result');
$stream = b('bcher-kva')->punycode_decode;
is("$stream", 'bücher', 'right punycode decoded result');

# trim
$stream = b(' la la la ')->trim;
is("$stream", 'la la la', 'right trimmed result');
$stream = b(" \n la la la \n ")->trim;
is("$stream", 'la la la', 'right trimmed result');
$stream = b("\n la\nla la \n")->trim;
is("$stream", "la\nla la", 'right trimmed result');
$stream = b(" \nla\nla\nla\n ")->trim;
is("$stream", "la\nla\nla", 'right trimmed result');

# say and autojoin
$buffer = '';
open my $handle, '>', \$buffer;
Expand Down

0 comments on commit 26af459

Please sign in to comment.