blib/lib/PDL/Char.pm

filter: Statementstmt 96.9% 95 / 98 Branchbran 78.5% 33 / 42 Conditioncond 81.8% 9 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 90.9% 10 / 11 Podpod 80.0% 4 / 5 totaltotal 89.0% 155 / 174 CC 28 SCAR 18.4
CC
28
Cov
91%
_rcharpack
11.4
new
6.0
string
6.0
CRAP 6.3
« blib/lib/PDL/PP/CType.pm ↑ Summary blib/lib/PDL/Compression.pm »
1 package PDL::Char;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our @ISA = qw (PDL);
6 1 ▶use overload '""' => \&PDL::Char::string;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶sub import {} # override the PDL one to avoid the big import list
Subroutine import: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 PDL::Char -- PDL subclass which allows reading and writing of fixed-length character strings as byte PDLs
13  
14 =head1 SYNOPSIS
15  
16 use PDL;
17 use PDL::Char;
18  
19 my $pchar = PDL::Char->new( [['abc', 'def', 'ghi'],['jkl', 'mno', 'pqr']] );
20
21 $pchar->setstr(1,0,'foo');
22
23 print $pchar; # 'string' bound to "", perl stringify function
24 # Prints:
25 # [
26 # ['abc' 'foo' 'ghi']
27 # ['jkl' 'mno' 'pqr']
28 # ]
29  
30 print $pchar->atstr(2,0);
31 # Prints:
32 # ghi
33  
34 =head1 DESCRIPTION
35  
36 This subclass of PDL allows one to manipulate PDLs of 'byte' type as if they were made of fixed
37 length strings, not just numbers.
38  
39 This type of behavior is useful when you want to work with character grids. The indexing is done
40 on a string level and not a character level for the 'setstr' and 'atstr' commands.
41  
42 This module is in particular useful for writing NetCDF files that include character data using the
43 L<PDL::NetCDF> module.
44  
45 =head1 FUNCTIONS
46  
47 =head2 new
48  
49 =for ref
50  
51 Function to create a byte PDL from a string, list of strings, list of list of strings, etc.
52  
53 =for usage
54  
55 # create a new PDL::Char from a perl array of strings
56 $strpdl = PDL::Char->new( ['abc', 'def', 'ghij'] );
57  
58 # Convert a PDL of type 'byte' to a PDL::Char
59 $strpdl1 = PDL::Char->new (sequence (byte, 4, 5)+99);
60  
61 =for example
62  
63 $pdlchar3d = PDL::Char->new([['abc','def','ghi'],['jkl', 'mno', 'pqr']]);
64  
65 =cut
66  
67 5 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
68 5 my $type = shift;
69 5 ▶ my $value = (scalar(@_)>1 ? [@_] : shift); # ref thyself
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 1 4 5
70 # re-bless byte PDLs as PDL::Char
71 5 ▶ if (ref($value) =~ /PDL/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($value) =~ /PDL/) 1 4 5
72 1 ▶ PDL::Core::barf('Cannot convert a non-byte PDL to PDL::Char')
Branch: 1 branch
BranchTrueFalseTotal
if $value->get_datatype != $PDL::Types::PDL_B 0 1 1
73 if ($value->get_datatype != $PDL::Types::PDL_B);
74 1 return bless $value, $type;
75 }
76 4 my $ptype = $PDL::Types::PDL_B;
77 4 my $self = PDL->initialize();
78 4 $self->set_datatype($ptype);
79 4 ▶ $value = 0 if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
80 4 my $maxlength; # max length seen for all character strings
81 4 my $samelen = 1; # Flag = 1 if all character strings are the same length
82 # 1st Pass thru the perl array structure, assume all strings the same length
83 4 my @dims;
84 4 my $str = _rcharpack($value,\$maxlength,\$samelen,0,\@dims);
85 4 ▶ unless( $samelen){ # Strings weren't the same length, go thru again and null pad to
Branch: 1 branch
BranchTrueFalseTotal
unless ($samelen) 1 3 4
86 # the max length.
87 1 $str = _rcharpack2($value,$maxlength,0,\@dims);
88 }
89 4 $self->setdims([reverse @dims]);
90 4 $self->update_data_from($str);
91 4 return bless $self, $type;
92 }
93  
94 # Take an N-D perl array of strings and pack it into a single string,
95 # Used by the 'char' constructor
96 #
97 # References supplied so $maxlength and $samelen are updated along the way as well.
98 #
99 #
100 # This version (_rcharpack) is for the 1st pass thru the N-d string array.
101 # It assumes that all strings are the same length, but also checks to see if they aren't
102 44 ▶sub _rcharpack {
Subroutine _rcharpack: called CC=11 SCAR=24.3
103 44 my $w = shift; # Input string
104 44 my ($maxlenref, $samelenref, $level, $dims) = @_; # reference to $maxlength, $samelen
105 44 my ($ret,$type);
106 44 $ret = "";
107 44 ▶ if (ref($w) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $w eq 'ARRAY') { } 13 31 44
elsif (ref \$w eq 'SCALAR') { } 31 0 31
108 13 ▶ PDL::Core::barf('Array is not rectangular') if (defined($dims->[$level]) and
Branch: 1 branch
BranchTrueFalseTotal
if defined $dims->[$level] and $dims->[$level] != scalar @$w 0 13 13
Condition66%2/3
defined $dims->[$level] and $dims->[$level] != scalar @$w
!ll&&!rl&&r
760
MC/DC0%0/2
defined $dims->[$level] and $dims->[$level] != scalar @$w
defined $dims->[$level] $dims->[$level] != scalar @$w
Truth table66%2/3
A and B
A= defined $dims->[$level]B= $dims->[$level] != scalar @$w
ABresult
0X0
100
111
109 $dims->[$level] != scalar(@$w));
110 13 $dims->[$level] = scalar (@$w);
111 13 $level++;
112 13 $type = ref($$w[0]);
113 13 for(@$w) {
114 40 ▶ PDL::Core::barf('Array is not rectangular') unless $type eq ref($_); # Equal types
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq ref $_ 0 40 40
115 40 $ret .= _rcharpack($_,$maxlenref, $samelenref, $level, $dims);
116 }
117 }elsif (ref(\$w) eq "SCALAR") {
118 31 my $len = length($w);
119 # Check for this length being different then the others:
120 31 ▶ $$samelenref = 0 if( defined($$maxlenref) && ($len != $$maxlenref) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $$maxlenref and $len != $$maxlenref 5 26 31
Condition100%3/3
defined $$maxlenref and $len != $$maxlenref
!ll&&!rl&&r
4225
MC/DC100%2/2
defined $$maxlenref and $len != $$maxlenref
defined $$maxlenref $len != $$maxlenref
Truth table100%3/3
A and B
A= defined $$maxlenrefB= $len != $$maxlenref
ABresult
0X0
100
111
121 # Save the max length:
122 31 ▶ $$maxlenref = $len if( !defined($$maxlenref) || $len > $$maxlenref); # see if this is the max length seen so far
Branch: 1 branch
BranchTrueFalseTotal
if not defined $$maxlenref or $len > $$maxlenref 4 27 31
Condition66%2/3
not defined $$maxlenref or $len > $$maxlenref
l!l&&r!l&&!r
4027
MC/DC50%1/2
not defined $$maxlenref or $len > $$maxlenref
not defined $$maxlenref $len > $$maxlenref
Truth table66%2/3
not(A) or B
A= not defined $$maxlenrefB= $len > $$maxlenref
ABresult
1X1
011
000
123 31 $dims->[$level] = $len;
124 31 $ret = $w;
125 }else{
126 0 PDL::Core::barf("Don't know how to make a PDL object from passed argument");
127 }
128 44 return $ret;
129 }
130  
131 # This version (_rcharpack2) is for the 2nd pass (if required) thru the N-d string array.
132 # If the 1st pass thru (_rcharpack) finds that all strings were not the same length,
133 # this routine will go thru and null-pad all strings to the max length seen.
134 # Note: For efficiency, the error checking is not repeated here, because any errors will
135 # already be detected in the 1st pass.
136 #
137 9 ▶sub _rcharpack2 {
Subroutine _rcharpack2: called CC=4 SCAR=13.9
138 9 my $w = shift; # Input string
139 9 my ($maxlen, $level, $dims) = @_; # Length to pad strings to
140 9 my ($ret,$type);
141 9 $ret = "";
142 9 ▶ if (ref($w) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $w eq 'ARRAY') { } 3 6 9
elsif (ref \$w eq 'SCALAR') { } 6 0 6
143 # Checks not needed the second time thru (removed)
144 3 $dims->[$level] = scalar (@$w);
145 3 $level++;
146 3 $type = ref($$w[0]);
147 3 for(@$w) {
148 8 $ret .= _rcharpack2($_,$maxlen,$level,$dims);
149 }
150 }elsif (ref(\$w) eq "SCALAR") {
151 6 my $len = length($w);
152 6 $dims->[$level] = $maxlen;
153 6 $ret = $w.("\00" x ($maxlen - $len));
154 }
155 9 return $ret;
156 }
157  
158 =head2 string
159  
160 =for ref
161  
162 Function to print a character PDL (created by 'char') in a pretty format.
163  
164 =for usage
165  
166 $char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] );
167 print $char; # 'string' bound to "", perl stringify function
168 # Prints:
169 # [
170 # ['abc' 'def' 'ghi']
171 # ['jkl' 'mno' 'pqr']
172 # ]
173  
174 # 'string' is overloaded to the "" operator, so:
175 # print $char;
176 # should have the same effect.
177  
178 =cut
179  
180 38 ▶sub string {
Subroutine string: called CC=6 SCAR=17.9
Pod documented
181 38 my $self = shift;
182 38 ▶ my $level = shift || 0;
Condition100%2/2
shift() || 0
l!l
335
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
183 38 ▶ my $sep = $PDL::use_commas ? "," : " ";
Branch: 1 branch
BranchTrueFalseTotal
$PDL::use_commas ? : 0 38 38
184 38 ▶ if ($self->dims == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dims == 1) { } 25 13 38
185 25 my $str = ${$self->get_dataref}; # get copy of string
186 25 $str =~ s/\00+$//g; # get rid of any null padding
187 25 return "\'". $str. "\'". $sep;
188 } else {
189 13 my @dims = reverse $self->dims;
190 13 my $ret = '';
191 13 ▶ $ret .= (" " x $level) . '[' . ((@dims == 2) ? ' ' : "\n");
Branch: 1 branch
BranchTrueFalseTotal
@dims == 2 ? : 8 5 13
192 13 ▶ for (my $i=0;$i<$dims[0];$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $dims[0]) 33 13 46
193 33 my $slicestr = ":," x (scalar(@dims)-1) . "($i)";
194 33 my $substr = $self->slice($slicestr);
195 33 $ret .= $substr->string($level+1);
196 }
197 13 $ret .= (" " x $level) . ']' . $sep . "\n";
198 13 return $ret;
199 }
200 }
201  
202 =head2 setstr
203  
204 =for ref
205  
206 Function to set one string value in a character PDL. The input position is
207 the position of the string, not a character in the string. The first dimension
208 is assumed to be the length of the string.
209  
210 The input string will be null-padded if the string is shorter than the first
211 dimension of the PDL. It will be truncated if it is longer.
212  
213 =for usage
214  
215 $char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] );
216 $char->setstr(0,1, 'foobar');
217 print $char; # 'string' bound to "", perl stringify function
218 # Prints:
219 # [
220 # ['abc' 'def' 'ghi']
221 # ['foo' 'mno' 'pqr']
222 # ]
223 $char->setstr(2,1, 'f');
224 print $char; # 'string' bound to "", perl stringify function
225 # Prints:
226 # [
227 # ['abc' 'def' 'ghi']
228 # ['foo' 'mno' 'f'] -> note that this 'f' is stored "f\0\0"
229 # ]
230  
231 =cut
232  
233 3 ▶sub setstr { # Sets a particular single value to a string.
Subroutine setstr: called CC=4 SCAR=13.9
Pod documented
234 3 ▶ PDL::Core::barf('Usage: setstr($pdl, $x, $y,.., $value)') if $#_<2;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 2 0 3 3
235 3 my $self = shift;
236 3 my $val = pop;
237 3 my @dims = $self->dims;
238 3 my $n = $dims[0];
239 3 ▶ for (my $i=0;$i<$n;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 9 3 12
240 9 ▶ my $chr = ($i >= length($val)) ? 0 : unpack ("C", substr ($val, $i, 1));
Branch: 1 branch
BranchTrueFalseTotal
$i >= length $val ? : 2 7 9
241 9 PDL::Core::set_c ($self, [$i, @_], $chr);
242 }
243 }
244  
245 =head2 atstr
246  
247 =for ref
248  
249 Function to fetch one string value from a PDL::Char type PDL, given a position within the PDL.
250 The input position of the string, not a character in the string. The length of the input
251 string is the implied first dimension.
252  
253 =for usage
254  
255 $char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] );
256 print $char->atstr(0,1);
257 # Prints:
258 # jkl
259  
260 =cut
261  
262 3 ▶sub atstr { # Fetchs a string value from a PDL::Char
Subroutine atstr: called CC=2 SCAR=6.9
Pod documented
263 3 ▶ PDL::Core::barf('Usage: atstr($pdl, $x, $y,..,)') if (@_ < 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 3 3
264 3 my $self = shift;
265 3 my $str = ':,' . join (',', map {"($_)"} @_);
266 3 my $w = $self->slice($str);
267 3 my $val = ${$w->get_dataref}; # get the data
268 3 $val =~ s/\00+$//g; # get rid of any null padding
269 3 return $val;
270 }
271  
272 # yuck ;) this is a cool little accessor method
273 # rebless a slice into PDL; originally
274 # Marc's idea used in PDL::Complex
275 0 ▶sub numeric {
Subroutine numeric: not called CC=1 SCAR=6.9
Pod undocumented
276 0 my ($seq) = @_;
277 0 return bless $seq->slice(''), 'PDL';
278 }
279  
280 1 1;
« blib/lib/PDL/PP/CType.pm ↑ Summary blib/lib/PDL/Compression.pm »