blib/lib/String/Scanf.pm

filter: Statementstmt 94.8% 74 / 78 Branchbran 88.4% 46 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 5 totaltotal 87.5% 127 / 145 CC 27 SCAR 25.5
CC
27
Cov
92%
_format_to_re
20.3
_match
6.0
sscanf
3.0
CRAP 12.8
↑ Summary
1 package String::Scanf;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $VERSION = '2.1';
8  
9 1 require Exporter;
10 1 @ISA = qw(Exporter);
11 1 @EXPORT = qw(sscanf);
12  
13 =pod
14  
15 =head1 NAME
16  
17 String::Scanf - emulate sscanf() of the C library
18  
19 =head1 SYNOPSIS
20  
21 use String::Scanf; # imports sscanf()
22  
23 ($a, $b, $c, $d) = sscanf("%d+%d %f-%s", $input);
24 ($e, $f, $g, $h) = sscanf("%x %o %s:%3c"); # input defaults to $_
25  
26 $r = String::Scanf::format_to_re($f);
27  
28 or
29  
30 # works only for Perl 5.005 or later
31 use String::Scanf qw(); # import nothing
32  
33 my $s1 = String::Scanf->new("%d+%d %f-%s");
34 my $s2 = String::Scanf->new("%x %o %s:%3c");
35  
36 ($a, $b, $c, $d) = $s1->sscanf($input);
37 ($e, $f, $g, $h) = $s2->sscanf(); # input defaults to $_
38  
39 =head1 DESCRIPTION
40  
41 String::Scanf supports scanning strings for data using formats
42 similar to the libc/stdio sscanf().
43  
44 The supported sscanf() formats are as follows:
45  
46 =over 4
47  
48 =item %d
49  
50 Decimal integer, with optional plus or minus sign.
51  
52 =item %u
53  
54 Decimal unsigned integer, with optional plus sign.
55  
56 =item %x
57  
58 Hexadecimal unsigned integer, with optional "0x" or "0x" in front.
59  
60 =item %o
61  
62 Octal unsigned integer.
63  
64 =item %e %f %g
65  
66 (The [efg] work identically.)
67  
68 Decimal floating point number, with optional plus or minus sign,
69 in any of these formats:
70  
71 1
72 1.
73 1.23
74 .23
75 1e45
76 1.e45
77 1.23e45
78 .23e45
79  
80 The exponent has an optional plus or minus sign, and the C<e> may also be C<E>.
81  
82 The various borderline cases like C<Inf> and C<Nan> are not recognized.
83  
84 =item %s
85  
86 A non-whitespace string.
87  
88 =item %c
89  
90 A string of characters. An array reference is returned containing
91 the numerical values of the characters.
92  
93 =item %%
94  
95 A literal C<%>.
96  
97 =back
98  
99 The sscanf() formats [pnSC] are not supported.
100  
101 The C<%s> and C<%c> have an optional maximum width, e.g. C<%4s>,
102 in which case at most so many characters are consumed (but fewer
103 characters are also accecpted).
104  
105 The numeric formats may also have such a width but it is ignored.
106  
107 The numeric formats may have C<[hl]> before the main option, e.g. C<%hd>,
108 but since such widths have no meaning in Perl, they are ignored.
109  
110 Non-format parts of the parameter string are matched literally
111 (e.g. C<:> matches as C<:>),
112 expect that any whitespace is matched as any whitespace
113 (e.g. C< > matches as C<\s+>).
114  
115 =head1 WARNING
116  
117 The numeric formats match only something that looks like a number,
118 they do not care whether it fits into the numbers of Perl. In other
119 words, C<123e456789> is valid for C<sscanf()>, but quite probably it
120 won't fit into your Perl's numbers. Consider using the various
121 Math::* modules instead.
122  
123 =head1 AUTHOR, COPYRIGHT AND LICENSE
124  
125 Jarkko Hietaniemi <jhi@iki.fi>
126  
127 Copyright (c) 2002,2004 Jarkko Hietaniemi. All rights reserved.
128  
129 This program is free software; you can redistribute it and/or modify
130 it under the same terms as Perl itself.
131  
132 =cut
133  
134 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
135  
136 120 ▶sub _format_to_re {
Subroutine _format_to_re: called CC=20 SCAR=30.1
137 120 my $format = shift;
138  
139 120 my $re = '';
140 120 my $ix = 0;
141 120 my @fmt;
142 my @reo;
143 120 my $dx = '\d+(?:_\d+)*';
144  
145 120 ▶ while ($format =~
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /(%(?:(?:(\d+)\$)?(\d*)([hl]?[diuoxefg]|[pnsScC%]))|%(\d*)(\[.+?\])|(.+?))/g) 213 120 333
146 /(%(?:(?:(\d+)\$)?(\d*)([hl]?[diuoxefg]|[pnsScC%]))|%(\d*)(\[.+?\])|(.+?))/g) {
147 213 ▶ if (defined $2) { # Reordering.
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) { } 2 211 213
148 2 $reo[$ix] = $2 - 1;
149 } else {
150 211 $reo[$ix] = $ix;
151 }
152 213 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) 213 0 213
153 213 ▶ if (defined $4) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $4) { } 175 38 213
elsif (defined $6) { } 1 37 38
elsif (defined $7) { } 37 0 37
154 175 my $e;
155 175 my ($w, $f) = ($3, $4);
156 175 $f =~ s/^[hl]//;
157 175 ▶ if ($f =~ /^[pnSC]$/) {
Branch: 8 branches
BranchTrueFalseTotal
if ($f =~ /^[pnSC]$/) { } 0 175 175
elsif ($f =~ /^[di]$/) { } 28 147 175
elsif ($f eq 'x') { } 12 135 147
elsif ($f eq 'o') { } 8 127 135
elsif ($f =~ /^[efg]$/) { } 102 25 127
elsif ($f eq 'u') { } 8 17 25
elsif ($f eq 's') { } 16 1 17
elsif ($f eq 'c') { } 1 0 1
158 0 croak "'$f' not supported";
159 } elsif ($f =~ /^[di]$/) {
160 28 $e = "[-+]?$dx";
161 } elsif ($f eq 'x') {
162 12 $e = '(?:0[xX])?[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*';
163 } elsif ($f eq 'o') {
164 8 $e = '[0-7]+(?:_[0-7]+)*';
165 } elsif ($f =~ /^[efg]$/) {
166 102 $e = "[-+]?(?:(?:$dx(?:\\.(?:$dx)?)?|\\.$dx)(?:[eE][-+]?$dx)?)";
167 } elsif ($f eq 'u') {
168 8 $e = "\\+?$dx";
169 } elsif ($f eq 's') {
170 16 ▶ $e = $w ? "\\S{0,$w}" : "\\S*";
Branch: 1 branch
BranchTrueFalseTotal
$w ? : 7 9 16
171 } elsif ($f eq 'c') {
172 1 ▶ $e = $w ? ".{0,$w}" : ".*";
Branch: 1 branch
BranchTrueFalseTotal
$w ? : 1 0 1
173 }
174 175 ▶ if ($f !~ /^[cC%]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f =~ /^[cC%]$/) 174 1 175
175 174 $re .= '\s*';
176 }
177 175 $re .= "($e)";
178 175 $fmt[$ix++] = $f;
179 } elsif (defined $6) { # [...]
180 1 ▶ $re .= $5 ? "(${6}{0,$5})" : "($6+)";
Branch: 1 branch
BranchTrueFalseTotal
$5 ? : 1 0 1
181 1 $fmt[$ix++] = '[';
182 } elsif (defined $7) { # Literal.
183 37 my $lit = $7;
184 37 ▶ if ($lit =~ /^\s+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lit =~ /^\s+$/) { } 21 16 37
185 21 $re .= '\s+';
186 } else {
187 16 $lit =~ s/(\W)/\\$1/g;
188 16 $re .= $lit;
189 }
190 }
191 }
192 }
193  
194 120 $re =~ s/\\s\*\\s\+/\\s+/g;
195 120 $re =~ s/\\s\+\\s\*/\\s+/g;
196  
197 120 return ($re, \@fmt, \@reo);
198 }
199  
200 0 ▶sub format_to_re {
Subroutine format_to_re: not called CC=1 SCAR=6.9
Pod undocumented
201 0 my ($re) = _format_to_re $_[0];
202 0 return $re;
203 }
204  
205 121 ▶sub _match {
Subroutine _match: called CC=6 SCAR=17.9
206 121 my ($format, $re, $fmt, $reo, $data) = @_;
207 121 my @matches = ($data =~ /$re/);
208  
209 121 my $ix;
210 121 ▶ for ($ix = 0; $ix < @matches; $ix++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @matches) 177 121 298
211 177 ▶ if ($fmt->[$ix] eq 'c') {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt->[$ix] eq 'c') { } 1 176 177
elsif ($fmt->[$ix] =~ /^[diuoxefg]$/) { } 159 17 176
212 1 $matches[$ix] = [ map { ord } split //, $matches[$ix] ];
213 } elsif ($fmt->[$ix] =~ /^[diuoxefg]$/) {
214 159 $matches[$ix] =~ tr/_//d;
215 }
216 177 ▶ if ($fmt->[$ix] eq 'x') {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt->[$ix] eq 'x') { } 12 165 177
elsif ($fmt->[$ix] eq 'o') { } 8 157 165
217 12 $matches[$ix] =~ s/^0[xX]//;
218 12 $matches[$ix] = hex $matches[$ix];
219 } elsif ($fmt->[$ix] eq 'o') {
220 8 $matches[$ix] = oct $matches[$ix];
221 }
222 }
223 121 @matches = @matches[@$reo];
224  
225 121 return @matches;
226 }
227  
228 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
229 1 ▶ require 5.005; sub qr {}
Subroutine qr: not called CC=1 SCAR=0.0
Pod undocumented
230 1 my ($class, $format) = @_;
231 1 my ($re, $fmt, $reo) = _format_to_re $format;
232 1 bless [ $format, qr/$re/, $fmt, $reo ], $class;
233 }
234  
235 0 ▶sub format {
Subroutine format: not called CC=1 SCAR=6.9
Pod undocumented
236 0 $_[0]->[0];
237 }
238  
239 121 ▶sub sscanf {
Subroutine sscanf: called CC=3 SCAR=11.0
Pod undocumented
240 121 my $self = shift;
241 121 ▶ my $data = @_ ? shift : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 120 1 121
242 121 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 2 119 121
243 2 return _match(@{ $self }, $data);
244 }
245 119 _match($self, _format_to_re($self), $data);
246 }
247  
248 1 1;
↑ Summary