| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package IO::Buffered::Regexp; |
|
2
|
8
|
|
|
8
|
|
92
|
use strict; |
|
|
8
|
|
|
|
|
17
|
|
|
|
8
|
|
|
|
|
256
|
|
|
3
|
8
|
|
|
8
|
|
37
|
use warnings; |
|
|
8
|
|
|
|
|
64
|
|
|
|
8
|
|
|
|
|
169
|
|
|
4
|
8
|
|
|
8
|
|
36
|
use Carp; |
|
|
8
|
|
|
|
|
14
|
|
|
|
8
|
|
|
|
|
436
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
37
|
use base ("IO::Buffered"); |
|
|
8
|
|
|
|
|
17
|
|
|
|
8
|
|
|
|
|
913
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '1.00'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# FIXME: Write documentation |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
IO::Buffered::Regexp - Regular expression buffering |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 METHODS |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=over |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
|
25
|
|
|
|
|
|
|
|
|
26
|
8
|
|
|
8
|
|
48
|
use base "Exporter"; |
|
|
8
|
|
|
|
|
13
|
|
|
|
8
|
|
|
|
|
5737
|
|
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our @EXPORT_OK = qw(); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=item new($regexp, MaxSize => 0..Inf, Double => 0|1, ReturnsLast => 0|1) |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
The C buffer type takes a regular expression as input and |
|
33
|
|
|
|
|
|
|
splits records based on that. Only the match defined in the () is returned and |
|
34
|
|
|
|
|
|
|
not the complete match. |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
The options C and C are optional. |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
C provides a limit on how big a buffer can grow, when the limit is hit |
|
39
|
|
|
|
|
|
|
an exception is thrown. |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
C defines the how the regexp is used to extract new items and remove |
|
42
|
|
|
|
|
|
|
old data from the buffer. By default a while loop is used that grabs and |
|
43
|
|
|
|
|
|
|
removes one item at a time from the buffer. If C is defined another |
|
44
|
|
|
|
|
|
|
method is used: |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my @records = ($self->{buffer} =~ /$regexp/g); |
|
47
|
|
|
|
|
|
|
$self->{buffer} =~ s/$regexp//g; |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
This might be faster in some cases and slower in others. |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
C sets what the C returns, this is used by the |
|
52
|
|
|
|
|
|
|
"Split" buffer type to return the correct value as it is just a wrapper. |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub new { |
|
57
|
6
|
|
|
6
|
1
|
14
|
my ($class, $regexp, %opts) = @_; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Check that $regexp is a Regexp or a non empty string |
|
60
|
6
|
50
|
66
|
|
|
679
|
croak "Regexp should be a string or regexp" if !(defined $regexp and |
|
|
|
|
66
|
|
|
|
|
|
61
|
|
|
|
|
|
|
(ref $regexp eq 'Regexp' or (ref $regexp eq '' and $regexp ne ''))); |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Check that $regexp is a Regexp or a non empty string |
|
64
|
2
|
50
|
33
|
|
|
19
|
croak "Option MaxSize should be a positiv integer" if $opts{MaxSize} and !( |
|
|
|
|
66
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$opts{MaxSize} =~ /^\d+$/ and $opts{MaxSize} > 0); |
|
66
|
|
|
|
|
|
|
|
|
67
|
2
|
100
|
50
|
|
|
40
|
my %self = ( |
|
68
|
|
|
|
|
|
|
buffer => '', |
|
69
|
|
|
|
|
|
|
regexp => qr/$regexp/, |
|
70
|
|
|
|
|
|
|
maxsize => $opts{MaxSize}, |
|
71
|
|
|
|
|
|
|
double => ($opts{Double} or 0), |
|
72
|
|
|
|
|
|
|
returns_last => (exists $opts{ReturnsLast} ? $opts{ReturnsLast} : 1), |
|
73
|
|
|
|
|
|
|
); |
|
74
|
|
|
|
|
|
|
|
|
75
|
2
|
|
33
|
|
|
53
|
return bless \%self, (ref $class || $class); |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item flush($str, ...) |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub flush { |
|
83
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
84
|
0
|
|
|
|
|
0
|
$self->{buffer} = join ('', @_); |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item buffer() |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub buffer { |
|
92
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
93
|
0
|
|
|
|
|
0
|
return $self->{buffer}; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item write($str, ...) |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub write { |
|
101
|
4
|
|
|
4
|
1
|
2095
|
my $self = shift; |
|
102
|
4
|
|
|
|
|
13
|
my $str = join ('', @_); |
|
103
|
|
|
|
|
|
|
|
|
104
|
4
|
100
|
|
|
|
21
|
if(my $maxsize = $self->{maxsize}) { |
|
105
|
2
|
|
|
|
|
5
|
my $length = length($str) + length($self->{buffer}); |
|
106
|
2
|
50
|
|
|
|
6
|
if($length > $maxsize) { |
|
107
|
0
|
|
|
|
|
0
|
croak "Buffer overrun"; |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
4
|
|
|
|
|
12
|
$self->{buffer} .= $str; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=item read() |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub read { |
|
119
|
8
|
|
|
8
|
1
|
24
|
my ($self) = @_; |
|
120
|
|
|
|
|
|
|
|
|
121
|
8
|
50
|
|
|
|
18
|
if($self->{double}) { |
|
122
|
0
|
|
|
|
|
0
|
my $regexp = $self->{regexp}; |
|
123
|
0
|
|
|
|
|
0
|
my @records = ($self->{buffer} =~ /$regexp/g); |
|
124
|
0
|
|
|
|
|
0
|
$self->{buffer} =~ s/$regexp//g; |
|
125
|
0
|
|
|
|
|
0
|
return @records; |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
} else { |
|
128
|
8
|
|
|
|
|
11
|
my $regexp = $self->{regexp}; |
|
129
|
8
|
|
|
|
|
10
|
my @records; |
|
130
|
8
|
|
|
|
|
54
|
while ($self->{buffer} =~ s/$regexp//) { |
|
131
|
6
|
50
|
|
|
|
17
|
if($1 ne '') { |
|
132
|
6
|
|
|
|
|
74
|
push(@records, $1); |
|
133
|
|
|
|
|
|
|
} else { |
|
134
|
0
|
|
|
|
|
0
|
last; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
} |
|
137
|
8
|
|
|
|
|
26
|
return @records; |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item returns_last() |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=cut |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub returns_last { |
|
146
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
147
|
0
|
|
|
|
|
0
|
return $self->{returns_last}; |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item read_last() |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=cut |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub read_last { |
|
155
|
4
|
|
|
4
|
1
|
1245
|
my ($self) = @_; |
|
156
|
4
|
|
|
|
|
11
|
my @results = $self->read(); |
|
157
|
4
|
100
|
|
|
|
15
|
push(@results, $self->{buffer}) if $self->{buffer} ne ''; |
|
158
|
4
|
|
|
|
|
6
|
$self->{buffer} = ''; |
|
159
|
4
|
|
|
|
|
28
|
return @results; |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=back |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 AUTHOR |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Troels Liebe Bentsen |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Copyright(C) 2008 Troels Liebe Bentsen |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
173
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
1; |
|
178
|
|
|
|
|
|
|
|