biblesearch / mojo / lib / Mojo / ByteStream.pm /
Newer Older
347 lines | 7.149kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojo::ByteStream;
2
use Mojo::Base -strict;
3
use overload '""' => sub { shift->to_string }, fallback => 1;
4

            
5
use Exporter 'import';
6
use Mojo::Collection;
7
use Mojo::Util;
8

            
9
our @EXPORT_OK = ('b');
10

            
11
# Turn most functions from Mojo::Util into methods
12
my @UTILS = (
13
  qw(b64_decode b64_encode camelize decamelize hmac_sha1_sum html_unescape),
14
  qw(md5_bytes md5_sum punycode_decode punycode_encode quote sha1_bytes),
15
  qw(sha1_sum slurp spurt squish trim unquote url_escape url_unescape),
16
  qw(xml_escape xor_encode)
17
);
18
for my $name (@UTILS) {
19
  my $sub = Mojo::Util->can($name);
20
  Mojo::Util::monkey_patch __PACKAGE__, $name, sub {
21
    my $self = shift;
22
    $$self = $sub->($$self, @_);
23
    return $self;
24
  };
25
}
26

            
27
sub new {
28
  my $class = shift;
29
  return bless \(my $dummy = join '', @_), ref $class || $class;
30
}
31

            
32
sub b { __PACKAGE__->new(@_) }
33

            
34
sub clone { $_[0]->new(${$_[0]}) }
35

            
36
sub decode {
37
  my $self = shift;
38
  $$self = Mojo::Util::decode shift || 'UTF-8', $$self;
39
  return $self;
40
}
41

            
42
sub encode {
43
  my $self = shift;
44
  $$self = Mojo::Util::encode shift || 'UTF-8', $$self;
45
  return $self;
46
}
47

            
48
sub say {
49
  my ($self, $handle) = @_;
50
  $handle ||= \*STDOUT;
51
  say $handle $$self;
52
}
53

            
54
sub secure_compare { Mojo::Util::secure_compare ${shift()}, @_ }
55

            
56
sub size { length ${$_[0]} }
57

            
58
sub split {
59
  my ($self, $pattern) = @_;
60
  return Mojo::Collection->new(map { $self->new($_) } split $pattern, $$self);
61
}
62

            
63
sub tap { shift->Mojo::Base::tap(@_) }
64

            
65
sub to_string { ${$_[0]} }
66

            
67
1;
68

            
69
=encoding utf8
70

            
71
=head1 NAME
72

            
73
Mojo::ByteStream - ByteStream
74

            
75
=head1 SYNOPSIS
76

            
77
  # Manipulate bytestreams
78
  use Mojo::ByteStream;
79
  my $stream = Mojo::ByteStream->new('foo_bar_baz');
80
  say $stream->camelize;
81

            
82
  # Chain methods
83
  my $stream = Mojo::ByteStream->new('foo bar baz')->quote;
84
  $stream = $stream->unquote->encode('UTF-8')->b64_encode('');
85
  say "$stream";
86

            
87
  # Use the alternative constructor
88
  use Mojo::ByteStream 'b';
89
  my $stream = b('foobarbaz')->b64_encode('')->say;
90

            
91
=head1 DESCRIPTION
92

            
93
L<Mojo::ByteStream> provides a more friendly API for the bytestream
94
manipulation functions in L<Mojo::Util>.
95

            
96
=head1 FUNCTIONS
97

            
98
L<Mojo::ByteStream> implements the following functions.
99

            
100
=head2 b
101

            
102
  my $stream = b('test123');
103

            
104
Construct a new scalar-based L<Mojo::ByteStream> object.
105

            
106
=head1 METHODS
107

            
108
L<Mojo::ByteStream> implements the following methods.
109

            
110
=head2 new
111

            
112
  my $stream = Mojo::ByteStream->new('test123');
113

            
114
Construct a new scalar-based L<Mojo::ByteStream> object.
115

            
116
=head2 b64_decode
117

            
118
  $stream = $stream->b64_decode;
119

            
120
Base64 decode bytestream with L<Mojo::Util/"b64_decode">.
121

            
122
=head2 b64_encode
123

            
124
  $stream = $stream->b64_encode;
125
  $stream = $stream->b64_encode("\n");
126

            
127
Base64 encode bytestream with L<Mojo::Util/"b64_encode">.
128

            
129
  b('foo bar baz')->b64_encode('')->say;
130

            
131
=head2 camelize
132

            
133
  $stream = $stream->camelize;
134

            
135
Camelize bytestream with L<Mojo::Util/"camelize">.
136

            
137
=head2 clone
138

            
139
  my $stream2 = $stream->clone;
140

            
141
Clone bytestream.
142

            
143
=head2 decamelize
144

            
145
  $stream = $stream->decamelize;
146

            
147
Decamelize bytestream with L<Mojo::Util/"decamelize">.
148

            
149
=head2 decode
150

            
151
  $stream = $stream->decode;
152
  $stream = $stream->decode('iso-8859-1');
153

            
154
Decode bytestream with L<Mojo::Util/"decode">, defaults to C<UTF-8>.
155

            
156
  $stream->decode('UTF-16LE')->unquote->trim->say;
157

            
158
=head2 encode
159

            
160
  $stream = $stream->encode;
161
  $stream = $stream->encode('iso-8859-1');
162

            
163
Encode bytestream with L<Mojo::Util/"encode">, defaults to C<UTF-8>.
164

            
165
  $stream->trim->quote->encode->say;
166

            
167
=head2 hmac_sha1_sum
168

            
169
  $stream = $stream->hmac_sha1_sum('passw0rd');
170

            
171
Generate HMAC-SHA1 checksum for bytestream with L<Mojo::Util/"hmac_sha1_sum">.
172

            
173
  b('foo bar baz')->hmac_sha1_sum('secr3t')->quote->say;
174

            
175
=head2 html_unescape
176

            
177
  $stream = $stream->html_unescape;
178

            
179
Unescape all HTML entities in bytestream with L<Mojo::Util/"html_unescape">.
180

            
181
  b('&lt;html&gt;')->html_unescape->url_escape->say;
182

            
183
=head2 md5_bytes
184

            
185
  $stream = $stream->md5_bytes;
186

            
187
Generate binary MD5 checksum for bytestream with L<Mojo::Util/"md5_bytes">.
188

            
189
=head2 md5_sum
190

            
191
  $stream = $stream->md5_sum;
192

            
193
Generate MD5 checksum for bytestream with L<Mojo::Util/"md5_sum">.
194

            
195
=head2 punycode_decode
196

            
197
  $stream = $stream->punycode_decode;
198

            
199
Punycode decode bytestream with L<Mojo::Util/"punycode_decode">.
200

            
201
=head2 punycode_encode
202

            
203
  $stream = $stream->punycode_encode;
204

            
205
Punycode encode bytestream with L<Mojo::Util/"punycode_encode">.
206

            
207
=head2 quote
208

            
209
  $stream = $stream->quote;
210

            
211
Quote bytestream with L<Mojo::Util/"quote">.
212

            
213
=head2 say
214

            
215
  $stream->say;
216
  $stream->say(*STDERR);
217

            
218
Print bytestream to handle and append a newline, defaults to C<STDOUT>.
219

            
220
=head2 secure_compare
221

            
222
  my $bool = $stream->secure_compare($str);
223

            
224
Compare bytestream with L<Mojo::Util/"secure_compare">.
225

            
226
  say 'Match!' if b('foo')->secure_compare('foo');
227

            
228
=head2 sha1_bytes
229

            
230
  $stream = $stream->sha1_bytes;
231

            
232
Generate binary SHA1 checksum for bytestream with L<Mojo::Util/"sha1_bytes">.
233

            
234
=head2 sha1_sum
235

            
236
  $stream = $stream->sha1_sum;
237

            
238
Generate SHA1 checksum for bytestream with L<Mojo::Util/"sha1_sum">.
239

            
240
=head2 size
241

            
242
  my $size = $stream->size;
243

            
244
Size of bytestream.
245

            
246
=head2 slurp
247

            
248
  $stream = $stream->slurp;
249

            
250
Read all data at once from file into bytestream with L<Mojo::Util/"slurp">.
251

            
252
  b('/home/sri/myapp.pl')->slurp->split("\n")->shuffle->join("\n")->say;
253

            
254
=head2 spurt
255

            
256
  $stream = $stream->spurt('/home/sri/myapp.pl');
257

            
258
Write all data from bytestream at once to file with L<Mojo::Util/"spurt">.
259

            
260
  b('/home/sri/foo.txt')->slurp->squish->spurt('/home/sri/bar.txt');
261

            
262
=head2 split
263

            
264
  my $collection = $stream->split(',');
265

            
266
Turn bytestream into L<Mojo::Collection> object containing L<Mojo::ByteStream>
267
objects.
268

            
269
  b('a,b,c')->split(',')->quote->join(',')->say;
270

            
271
=head2 squish
272

            
273
  $stream = $stream->squish;
274

            
275
Trim whitespace characters from both ends of bytestream and then change all
276
consecutive groups of whitespace into one space each with
277
L<Mojo::Util/"squish">.
278

            
279
=head2 tap
280

            
281
  $stream = $stream->tap(sub {...});
282

            
283
Alias for L<Mojo::Base/"tap">.
284

            
285
=head2 to_string
286

            
287
  my $str = $stream->to_string;
288
  my $str = "$stream";
289

            
290
Stringify bytestream.
291

            
292
=head2 trim
293

            
294
  $stream = $stream->trim;
295

            
296
Trim whitespace characters from both ends of bytestream with
297
L<Mojo::Util/"trim">.
298

            
299
=head2 unquote
300

            
301
  $stream = $stream->unquote;
302

            
303
Unquote bytestream with L<Mojo::Util/"unquote">.
304

            
305
=head2 url_escape
306

            
307
  $stream = $stream->url_escape;
308
  $stream = $stream->url_escape('^A-Za-z0-9\-._~');
309

            
310
Percent encode all unsafe characters in bytestream with
311
L<Mojo::Util/"url_escape">.
312

            
313
  b('foo bar baz')->url_escape->say;
314

            
315
=head2 url_unescape
316

            
317
  $stream = $stream->url_unescape;
318

            
319
Decode percent encoded characters in bytestream with
320
L<Mojo::Util/"url_unescape">.
321

            
322
  b('%3Chtml%3E')->url_unescape->xml_escape->say;
323

            
324
=head2 xml_escape
325

            
326
  $stream = $stream->xml_escape;
327

            
328
Escape only the characters C<&>, C<E<lt>>, C<E<gt>>, C<"> and C<'> in
329
bytestream with L<Mojo::Util/"xml_escape">.
330

            
331
=head2 xor_encode
332

            
333
  $stream = $stream->xor_encode($key);
334

            
335
XOR encode bytestream with L<Mojo::Util/"xor_encode">.
336

            
337
=head1 BYTESTREAM
338

            
339
Direct scalar reference access to the bytestream is also possible.
340

            
341
  $$stream .= 'foo';
342

            
343
=head1 SEE ALSO
344

            
345
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
346

            
347
=cut