Ryu/Buffer.pm

filter: Statementstmt 89.9% 116 / 129 Branchbran 68.4% 26 / 38 Conditioncond 57.1% 4 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.3% 21 / 23 Podpod 92.3% 12 / 13 totaltotal 83.6% 179 / 214 CC 24 SCAR 11.7
CC
24
Cov
84%
__ANON__
12.0
read_packed
4.0
__ANON__
4.0
CRAP 3.2
« Ryu/Source.pm ↑ Summary Ryu/Exception.pm »
1 package Ryu::Buffer;
2  
3 44 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 44 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 44 our $VERSION = '4.001'; # VERSION
7 44 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8  
9 44 ▶use parent qw(Ryu::Node);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =encoding utf8
12  
13 =head1 NAME
14  
15 Ryu::Buffer - accumulate data
16  
17 =head1 DESCRIPTION
18  
19 Provides a simple way to push bytes or characters into a buffer,
20 and get them back out again.
21  
22 Typically of use for delimiter-based or fixed-size protocols.
23  
24 See also L<Future::Buffer>, if you're dealing exclusively with L<Future>
25 instances and don't need the L<Ryu::Source> functionality then that's
26 likely to be a better option.
27  
28 =cut
29  
30 44 ▶use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 44 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 =head1 METHODS
34  
35 =cut
36  
37 =head2 new
38  
39 Instantiates a new, empty L<Ryu::Buffer>.
40  
41 =cut
42  
43 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
44 5 my ($class, %args) = @_;
45 5 ▶ $args{data} //= '';
Condition50%1/2
$args{"data"} //= ""
l!l
05
MC/DC0%0/1
$args{"data"} //= ""
$args{"data"}
Truth table50%1/2
A
A= $args{"data"}
Aresult
11
00
46 5 ▶ $args{ops} //= [];
Condition50%1/2
$args{"ops"} //= []
l!l
05
MC/DC0%0/1
$args{"ops"} //= []
$args{"ops"}
Truth table50%1/2
A
A= $args{"ops"}
Aresult
11
00
47 5 my $self = $class->next::method(%args);
48 5 return $self;
49 }
50  
51 =head1 METHODS - Reading data
52  
53 These methods provide ways of accessing the buffer either
54 destructively (C<read*>) or non-destructively (C<peek*>).
55  
56 =cut
57  
58 =head2 read_exactly
59  
60 Reads exactly the given number of bytes or characters.
61  
62 Takes the following parameters:
63  
64 =over 4
65  
66 =item * C<$size> - number of characters or bytes to return
67  
68 =back
69  
70 Returns a L<Future> which will resolve to a scalar containing the requested data.
71  
72 =cut
73  
74 5 ▶sub read_exactly {
Subroutine read_exactly: called CC=1 SCAR=0.0
Pod documented
75 5 my ($self, $size) = @_;
76 5 my $f = $self->new_future;
77 5 ▶ push @{$self->{ops}}, $self->$curry::weak(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
78 7 my ($self) = @_;
79 7 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 7 7
80 7 ▶ return $f unless $size <= length($self->{data});
Branch: 1 branch
BranchTrueFalseTotal
unless $size <= length $self->{"data"} 2 5 7
81 5 my $data = substr($self->{data}, 0, $size, '');
82 5 $f->done($data);
83 5 $self->on_change;
84 5 return $f;
85 5 });
86 5 $self->process_pending;
87 5 $f;
88 }
89  
90 =head2 read_atmost
91  
92 Reads up to the given number of bytes or characters - if
93 we have at least one byte or character in the buffer, we'll
94 return that even if it's shorter than the requested C<$size>.
95 This method is guaranteed not to return B<more> than the
96 C<$size>.
97  
98 Takes the following parameters:
99  
100 =over 4
101  
102 =item * C<$size> - maximum number of characters or bytes to return
103  
104 =back
105  
106 Returns a L<Future> which will resolve to a scalar containing the requested data.
107  
108 =cut
109  
110 0 ▶sub read_atmost {
Subroutine read_atmost: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($self, $size) = @_;
112 0 my $f = $self->new_future;
113 0 ▶ push @{$self->{ops}}, $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
114 0 my ($self) = @_;
115 0 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 0 0
116 0 ▶ return $f unless length($self->{data});
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"data"} 0 0 0
117 0 my $data = substr($self->{data}, 0, min($size, length($self->{data})), '');
118 0 $f->done($data);
119 0 $self->on_change;
120 0 return $f;
121 0 });
122 0 $self->process_pending;
123 0 $f;
124 }
125  
126 =head2 read_atleast
127  
128 Reads at least the given number of bytes or characters - if
129 we have a buffer that's the given size or larger, we'll
130 return everything available, even if it's larger than the
131 requested C<$size>.
132  
133 Takes the following parameters:
134  
135 =over 4
136  
137 =item * C<$size> - minimum number of characters or bytes to return
138  
139 =back
140  
141 Returns a L<Future> which will resolve to a scalar containing the requested data.
142  
143 =cut
144  
145 2 ▶sub read_atleast {
Subroutine read_atleast: called CC=1 SCAR=0.0
Pod documented
146 2 my ($self, $size) = @_;
147 2 my $f = $self->new_future;
148 2 ▶ push @{$self->{ops}}, $self->$curry::weak(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
149 2 my ($self) = @_;
150 2 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 2 2
151 2 ▶ return $f unless length($self->{data}) >= $size;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"data"} >= $size 0 2 2
152 2 my $data = substr($self->{data}, 0, max($size, length($self->{data})), '');
153 2 $f->done($data);
154 2 $self->on_change;
155 2 return $f;
156 2 });
157 2 $self->process_pending;
158 2 $f;
159 }
160  
161 =head2 read_until
162  
163 Reads up to the given string or regex match.
164  
165 Pass a C<< qr// >> instance if you want to use a regular expression to match,
166 or a plain string if you want exact-string matching behaviour.
167  
168 The data returned will B<include> the match.
169  
170 Takes the following parameters:
171  
172 =over 4
173  
174 =item * C<$match> - the string or regex to match against
175  
176 =back
177  
178 Returns a L<Future> which will resolve to the requested bytes or characters.
179  
180 =cut
181  
182 1 ▶sub read_until {
Subroutine read_until: called CC=2 SCAR=6.9
Pod documented
183 1 my ($self, $match) = @_;
184 1 ▶ $match = qr/\Q$match/ unless ref($match) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $match eq "Regexp" 1 0 1
185 1 my $f = $self->new_future;
186 1 ▶ push @{$self->{ops}}, $self->$curry::weak(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
187 5 my ($self) = @_;
188 5 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 5 5
189 5 ▶ return $f unless length($self->{data});
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"data"} 1 4 5
190 4 ▶ return $f unless $self->{data} =~ /$match/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"} =~ /$match/g 3 1 4
191 1 my $data = substr($self->{data}, 0, pos($self->{data}), '');
192 1 $f->done($data);
193 1 $self->on_change;
194 1 return $f;
195 1 });
196 1 $self->process_pending;
197 1 $f;
198 }
199  
200 44 my $pack_characters = q{aAZbBhHcCWsSlLqQiInNvVjJfdFpPUwx};
201 my %character_sizes = map {
202 44 $_ => length(pack("x[$_]", ""))
203 } split //, $pack_characters;
204  
205 =head2 read_packed
206  
207 Uses L<pack> template notation to define a pattern to extract.
208 Will attempt to accumulate enough bytes to fulfill the request,
209 then unpack and extract from the buffer.
210  
211 This method only supports a B<very limited> subset of the
212 full L<pack> functionality - currently, this includes
213 sequences such as C<A4> or C<N1n1>, but does B<not> handle
214 multi-stage templates such as C<N/a*>.
215  
216 These would need to parse the initial C<N1> bytes to
217 determine the full extent of the data to be processed, and
218 the logic for handling this is not yet implemented.
219  
220 Takes the following parameters:
221  
222 =over 4
223  
224 =item * C<$format> - a L<pack>-style format string
225  
226 =back
227  
228 Returns a L<Future> which will resolve to the requested items,
229 of which there can be more than one depending on the format string.
230  
231 =cut
232  
233 3 ▶sub read_packed {
Subroutine read_packed: called CC=4 SCAR=13.9
Pod documented
234 3 my ($self, $format) = @_;
235 3 my $f = $self->new_future;
236 3 my @handler;
237 3 my $simple_format = $format;
238  
239 # Might as well avoid too much complexity
240 # in the parser
241 3 $simple_format =~ s{\[([0-9]+)\]}{$1}g;
242 3 $simple_format =~ s{\s+}{}g;
243 PARSER:
244 3 while(1) {
245 3 for($simple_format) {
246 3 ▶ if(my ($char, $count) = /\G([$pack_characters])[!><]?([0-9]*)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = /\G([$pack_characters])[!><]?([0-9]*)/cg) 3 0 3
247 3 $count *= $character_sizes{$char};
248 3 push @handler, {
249 regex => qr/(.{$count})/,
250 }
251 }
252 3 ▶ last PARSER unless pos($_) < length($_);
Branch: 1 branch
BranchTrueFalseTotal
unless pos $_ < length $_ 3 0 3
253 }
254 }
255 3 my $re = join '', map { $_->{regex} } @handler;
256 3 ▶ push @{$self->{ops}}, $self->$curry::weak(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
257 9 my ($self) = @_;
258 9 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 9 9
259 9 ▶ return $f unless length($self->{data});
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"data"} 3 6 9
260  
261 6 ▶ return $f unless $self->{data} =~ m{^$re};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"} =~ /^$re/ 3 3 6
262 3 my @items = unpack $format, $self->{data};
263 3 $self->{data} =~ s{^$re}{};
264 3 $f->done(@items);
265 3 $self->on_change;
266 3 return $f;
267 3 });
268 3 $self->process_pending;
269 3 $f;
270 }
271  
272 =head2 write
273  
274 Add more data to the buffer.
275  
276 Call this with a single scalar, and the results will be appended
277 to the internal buffer, triggering any callbacks for read activity
278 as required.
279  
280 =cut
281  
282 19 ▶sub write {
Subroutine write: called CC=2 SCAR=6.9
Pod documented
283 19 my ($self, $data) = @_;
284 19 $self->{data} .= $data;
285 19 ▶ $self->process_pending if @{$self->{ops}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"ops"};} 12 7 19
286 19 return $self;
287 }
288  
289 =head2 size
290  
291 Returns the current buffer size.
292  
293 =cut
294  
295 17 ▶sub size { length(shift->{data}) }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
296  
297 =head2 is_empty
298  
299 Returns true if the buffer is currently empty (size = 0), false otherwise.
300  
301 =cut
302  
303 5 ▶sub is_empty { !length(shift->{data}) }
Subroutine is_empty: called CC=1 SCAR=0.0
Pod documented
304  
305 =head1 METHODS - Internal
306  
307 These are documented for convenience, but generally not recommended
308 to call any of these directly.
309  
310 =head2 data
311  
312 Accessor for the internal buffer. Not recommended to use this,
313 but if you break it you get to keep all the pieces.
314  
315 =cut
316  
317 2 ▶sub data { shift->{data} }
Subroutine data: called CC=1 SCAR=0.0
Pod documented
318  
319 =head2 process_pending
320  
321 Used internally to trigger callbacks once L</write> has been called.
322  
323 =cut
324  
325 23 ▶sub process_pending {
Subroutine process_pending: called CC=3 SCAR=11.0
Pod documented
326 23 my ($self) = @_;
327 23 while(1) {
328 34 ▶ my ($op) = @{$self->{ops}} or return;
Branch: 1 branch
BranchTrueFalseTotal
my($op) = @{$self->{"ops"};} or return 23 11 34
329 23 my $f = $op->();
330 23 ▶ return unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 12 11 23
331 11 shift @{$self->{ops}};
332 }
333 }
334  
335 11 ▶sub on_change {
Subroutine on_change: called CC=2 SCAR=6.9
Pod undocumented
336 11 my ($self) = @_;
337 11 ▶ $self->{on_change}->($self) if $self->{on_change};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_change"} 2 9 11
338 11 return;
339 }
340  
341 =head2 new_future
342  
343 Instantiates a new L<Future>, used to ensure we get something awaitable.
344  
345 Can be overridden using C<$Ryu::FUTURE_FACTORY>.
346  
347 =cut
348  
349 11 ▶sub new_future {
Subroutine new_future: called CC=2 SCAR=7.0
Pod documented
350 11 my $self = shift;
351 11 require Ryu;
352 (
353 11 ▶ $self->{new_future} //= $Ryu::FUTURE_FACTORY
Condition66%2/3
$self->{"new_future"} //= $Ryu::FUTURE_FACTORY
l!l&&r!l&&!r
830
MC/DC0%0/2
$self->{"new_future"} //= $Ryu::FUTURE_FACTORY
$self->{"new_future"} $Ryu::FUTURE_FACTORY
Truth table66%2/3
A //= B
A= $self->{"new_future"}B= $Ryu::FUTURE_FACTORY
ABresult
1X1
011
000
354 )->($self, @_)
355 }
356  
357 44 1;
358  
359 __END__
« Ryu/Source.pm ↑ Summary Ryu/Exception.pm »