Buffer.pm

filter: Statementstmt 84.5% 269 / 318 Branchbran 64.4% 67 / 104 Conditioncond 44.7% 34 / 76 MC/DCmcdc 13.6% 6 / 44 Subroutinesub 91.1% 41 / 45 Podpod 100% 34 / 34 totaltotal 72.6% 451 / 621 CC 84 SCAR 22.0
CC
84
Cov
74%
setAutoNewline
44.7
new
13.3
findNext
12.2
CRAP 9.0
« Modify/Rule.pm ↑ Summary Modify.pm »
1 package Text::Buffer;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use vars qw($VERSION $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 $VERSION = '0.4';
10 4 $DEBUG = 1;
11 }
12  
13 4 ▶sub new {
Subroutine new: called CC=9 SCAR=25.9
Pod documented
14 4 my $proto = shift;
15 4 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
16 4 my $self = {
17 _debug => 0,
18 _buffer => [],
19 _currline => 0,
20 _modified => 0,
21 _autonewline => "unix",
22 _newline => "\n"
23 };
24  
25 4 bless( $self, $class );
26  
27 4 my %opts = @_;
28 0 ▶ if ($opts{debug}) { $self->{_debug} = $opts{debug}}
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"debug"}) 0 4 4
29 4 $self->_debug("Instantiated new object $class");
30 4 ▶ if ( $opts{file} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'file'}) { } 3 1 4
elsif ($opts{'array'}) { } 0 1 1
31 3 $self->{file} = $opts{file};
32 3 $self->load();
33 }
34 elsif ( $opts{array} ) {
35 0 ▶ if ( ref( $opts{array} ) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"array"} eq "ARRAY") 0 0 0
36 0 foreach ( @{ $opts{array} } ) {
37 0 $self->append($_);
38 }
39 }
40 0 $self->setModified(1);
41 }
42 4 foreach (qw(autonewline)) {
43 4 ▶ $self->{"_$_"} = $opts{$_} if exists($opts{$_});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{$_} 1 3 4
44 }
45  
46 4 return $self;
47 }
48  
49 3 ▶sub load {
Subroutine load: called CC=5 SCAR=18.0
Pod documented
50 3 my $self = shift;
51 3 ▶ my $file = shift || $self->{file};
Condition33%1/3
shift() || $self->{'file'}
l!l&&r!l&&!r
030
MC/DC0%0/2
shift() || $self->{'file'}
shift() $self->{'file'}
Truth table33%1/3
A || B
A= shift()B= $self->{'file'}
ABresult
1X1
011
000
52 3 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 3 3
53 0 $self->_setError("No file to load specified");
54 0 return undef;
55 }
56 3 $self->_debug("Loading file $file");
57 3 ▶ if ( open( FIL, $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open FIL, $file) { } 3 0 3
58 3 $self->_debug("clearing buffer and adding $file to buffer");
59 3 $self->clear();
60 3 ▶ while (<FIL>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FIL)) 19 3 22
61 19 $self->append($_);
62 }
63 3 close(FIL);
64 3 $self->_clearModified();
65 3 return 1;
66 }
67 else {
68 0 $self->_setError("Failed to load file $file");
69 0 return undef;
70 }
71 0 return 0;
72 }
73  
74 3 ▶sub save {
Subroutine save: called CC=8 SCAR=24.1
Pod documented
75 3 my $self = shift;
76 3 ▶ my $file = shift || $self->{file};
Condition33%1/3
shift() || $self->{'file'}
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || $self->{'file'}
shift() $self->{'file'}
Truth table33%1/3
A || B
A= shift()B= $self->{'file'}
ABresult
1X1
011
000
77 3 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 3 3
78 0 $self->_setError("No file to save to specified");
79 0 return undef;
80 }
81  
82 3 ▶ if ( $self->{file} && $file eq $self->{file} && !$self->isModified() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'file'} and $file eq $self->{'file'} and not $self->isModified) { } 0 3 3
Condition50%3/6
$self->{"file"} and $file eq $self->{"file"} and not $self->isModified
!ll&&!rl&&r
300
$self->{"file"} and $file eq $self->{"file"}
!ll&&!rl&&r
120
MC/DC0%0/3
$self->{"file"} and $file eq $self->{"file"} and not $self->isModified
$self->{"file"} $file eq $self->{"file"} not $self->isModified
Truth table50%2/4
A and B and not(C)
A= $self->{"file"}B= $file eq $self->{"file"}C= not $self->isModified
ABCresult
0XX0
10X0
1100
1111
83 0 $self->_debug("Buffer not modified, not saving to file $file");
84 0 return 1;
85 }
86 else {
87 3 $self->_debug(
88 "Saving " . $self->getLineCount() . " lines to file $file" );
89 }
90  
91 3 ▶ if ( open( FIL, ">$file" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open FIL, ">$file") { } 3 0 3
92 3 $self->_debug("saving buffer to $file");
93 3 $self->goto('top');
94 3 my $str = $self->get();
95 3 my $cnt = 0;
96 3 ▶ while ( defined($str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 20 3 23
97 20 $self->_debug("saving: '$str'");
98 20 $cnt++;
99 20 print FIL $str;
100 20 $str = $self->next();
101 }
102 3 close(FIL);
103 3 return $cnt;
104 }
105 else {
106 0 $self->_setError("Failed to load file $file");
107 0 return undef;
108 }
109  
110 0 return 0;
111 }
112  
113 3 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
114 3 my $self = shift;
115 3 @{ $self->{_buffer} } = ();
116 3 $self->{_currline} = 0;
117 3 return 1;
118 }
119  
120 #=============================================================
121 # Public Methods
122 #=============================================================
123 # Navigation methods
124 #-------------------------------------------------------------
125  
126 # Internal method returning the resulting array position (starting at 0)
127 151 ▶sub _translateLinePos {
Subroutine _translateLinePos: called CC=8 SCAR=20.8
128 151 my $self = shift;
129 151 ▶ my $linenum = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
1510
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
130 151 my $curr = $self->{_currline}; # Resulting line to return
131 151 ▶ if ( $linenum =~ /^[0-9]+$/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($linenum =~ /^[0-9]+$/) { } 8 143 151
elsif ($linenum =~ /^[+-]\d+$/) { } 122 21 143
elsif ($linenum =~ /^(start|top|first)$/) { } 17 4 21
elsif ($linenum =~ /^(end|bottom|last)$/) { } 3 1 4
132 8 $curr = $linenum - 1;
133 }
134 elsif ( $linenum =~ /^[+-]\d+$/ ) {
135 122 eval "\$curr=$curr$linenum";
136 }
137 elsif ( $linenum =~ /^(start|top|first)$/ ) {
138 17 $curr = 0;
139 }
140 elsif ( $linenum =~ /^(end|bottom|last)$/ ) {
141 3 $curr = $self->getLineCount() - 1;
142 }
143 else {
144 1 $self->_debug("Could not translate: $linenum");
145 1 return undef;
146 }
147  
148 # do sanity check now
149 150 ▶ if ( $curr < 0 || $curr >= $self->getLineCount() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr < 0 or $curr >= $self->getLineCount) 20 130 150
Condition100%3/3
$curr < 0 or $curr >= $self->getLineCount
l!l&&r!l&&!r
119130
MC/DC100%2/2
$curr < 0 or $curr >= $self->getLineCount
$curr < 0 $curr >= $self->getLineCount
Truth table100%3/3
A or B
A= $curr < 0B= $curr >= $self->getLineCount
ABresult
1X1
011
000
150 20 $self->_debug(
151 "Failed sanity check, current line would be out of bounds");
152 20 return undef;
153 }
154  
155 130 return $curr;
156 }
157  
158 151 ▶sub goto {
Subroutine goto: called CC=2 SCAR=6.9
Pod documented
159 151 my $self = shift;
160 151 my $goto = shift;
161 151 my $curr = $self->_translateLinePos($goto);
162  
163 151 ▶ if ( !defined($curr) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $curr) 21 130 151
164 21 $self->_setError("Invalid line position: $goto");
165 21 return undef;
166 }
167  
168 $self->_debug( "goto $goto succeeded from array pos "
169 . $self->{_currline}
170 130 . " to $curr" );
171 130 $self->{_currline} = $curr;
172 130 return $self->getLineNumber();
173 }
174  
175 191 ▶sub getLineCount {
Subroutine getLineCount: called CC=1 SCAR=0.0
Pod documented
176 191 my $self = shift;
177 191 return ( $#{ $self->{_buffer} } + 1 );
178 }
179  
180 137 ▶sub getLineNumber {
Subroutine getLineNumber: called CC=1 SCAR=0.0
Pod documented
181 137 my $self = shift;
182 $self->_debug( "line is "
183 137 . ( $self->{_currline} + 1 )
184 . ", array pos is $self->{_currline}" );
185 137 return ( $self->{_currline} + 1 );
186 }
187  
188 20 ▶sub isEOF { return shift->isEndOfBuffer() }
Subroutine isEOF: called CC=1 SCAR=0.0
Pod documented
189  
190 20 ▶sub isEndOfBuffer {
Subroutine isEndOfBuffer: called CC=1 SCAR=0.0
Pod documented
191 20 my $self = shift;
192 20 return ( $self->{_currline} >= $self->getLineCount() );
193 }
194 1 ▶sub isEmpty { return ( shift->getLineCount() == 0 ) }
Subroutine isEmpty: called CC=1 SCAR=0.0
Pod documented
195  
196 0 ▶sub isModified { return shift->{_modified}; }
Subroutine isModified: not called CC=1 SCAR=6.9
Pod documented
197 25 ▶sub setModified { my $self = shift; $self->_debug("Marking buffer modified"); $self->{_modified} = 1; }
Subroutine setModified: called CC=1 SCAR=0.0
Pod documented
198 3 ▶sub _clearModified { my $self = shift; $self->_debug("Marking buffer unmodified"); $self->{_modified} = 0; }
Subroutine _clearModified: called CC=1 SCAR=0.0
199  
200 1 ▶sub setAutoNewline {
Subroutine setAutoNewline: called CC=10 SCAR=38.0
Pod documented
201 1 my $self = shift;
202 1 my $newline = shift;
203 1 ▶ if (!$newline || $newline eq "off" || $newline eq "none") {
Branch: 4 branches
BranchTrueFalseTotal
if (not $newline or $newline eq 'off' or $newline eq 'none') { } 0 1 1
elsif ($newline eq "\n" or lc $newline eq 'unix') { } 1 0 1
elsif ($newline eq "\r" or lc $newline eq 'mac') { } 0 0 0
elsif ($newline eq "\r\n" or lc $newline eq 'windows') { } 0 0 0
Condition33%2/6
not $newline or $newline eq "off" or $newline eq "none"
l!l&&r!l&&!r
001
not $newline or $newline eq "off"
l!l&&r!l&&!r
001
MC/DC0%0/3
not $newline or $newline eq "off" or $newline eq "none"
not $newline $newline eq "off" $newline eq "none"
Truth table25%1/4
not(A) or B or C
A= not $newlineB= $newline eq "off"C= $newline eq "none"
ABCresult
1XX1
01X1
0011
0000
204 0 $self->{_autonewline} = ""; $self->{_newline} = "";
205 }
206 ▶ elsif ($newline eq "\n" || lc($newline) eq "unix") {
Condition33%1/3
$newline eq "\n" or lc $newline eq "unix"
l!l&&r!l&&!r
010
MC/DC0%0/2
$newline eq "\n" or lc $newline eq "unix"
$newline eq "\n" lc $newline eq "unix"
Truth table33%1/3
A or B
A= $newline eq "\n"B= lc $newline eq "unix"
ABresult
1X1
011
000
207 1 $self->{_autonewline} = "unix"; $self->{_newline} = "\n";
208 }
209 ▶ elsif ($newline eq "\r" || lc($newline) eq "mac") {
Condition0%0/3
$newline eq "\r" or lc $newline eq "mac"
l!l&&r!l&&!r
000
MC/DC0%0/2
$newline eq "\r" or lc $newline eq "mac"
$newline eq "\r" lc $newline eq "mac"
Truth table0%0/3
A or B
A= $newline eq "\r"B= lc $newline eq "mac"
ABresult
1X1
011
000
210 0 $self->{_autonewline} = "mac"; $self->{_newline} = "\r";
211 }
212 ▶ elsif ($newline eq "\r\n" || lc($newline) eq "windows") {
Condition0%0/3
$newline eq "\r\n" or lc $newline eq "windows"
l!l&&r!l&&!r
000
MC/DC0%0/2
$newline eq "\r\n" or lc $newline eq "windows"
$newline eq "\r\n" lc $newline eq "windows"
Truth table0%0/3
A or B
A= $newline eq "\r\n"B= lc $newline eq "windows"
ABresult
1X1
011
000
213 0 $self->{_autonewline} = "windows"; $self->{_newline} = "\r\n";
214 }
215 else {
216 0 $self->{_autonewline} = "other"; $self->{_newline} = "$newline";
217 }
218 1 return 1;
219 }
220  
221 1 ▶sub getAutoNewline {
Subroutine getAutoNewline: called CC=1 SCAR=0.0
Pod documented
222 1 my $self = shift;
223 1 return $self->{_newline};
224 }
225  
226 118 ▶sub next {
Subroutine next: called CC=3 SCAR=11.0
Pod documented
227 118 my $self = shift;
228 118 ▶ my $num = shift || 1;
Condition50%1/2
shift() || 1
l!l
0118
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
229  
230 #FIXME should return all lines as array in array context
231 118 ▶ if ( !$self->goto("+$num") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->goto("+$num")) 18 100 118
232 18 return undef;
233 }
234 100 return $self->get();
235 }
236  
237 1 ▶sub previous {
Subroutine previous: called CC=3 SCAR=12.0
Pod documented
238 1 my $self = shift;
239 1 ▶ my $num = shift || 1;
Condition50%1/2
shift() || 1
l!l
10
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
240  
241 #FIXME should return all lines as array in array context
242 1 ▶ if ( !$self->goto("-$num") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->goto("-$num")) 0 1 1
243 0 return undef;
244 }
245 1 return $self->get();
246 }
247  
248 #-------------------------------------------------------------
249 # Searching methods
250 #-------------------------------------------------------------
251 4 ▶sub find {
Subroutine find: called CC=2 SCAR=6.9
Pod documented
252 4 my $self = shift;
253 4 ▶ my $match = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
40
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
254 # TODO Add a more sophisticated interface, like
255 # find(regex => "\d+", startat => 'top', wrap => 1)
256 4 my $wrap = shift;
257 4 $match = $self->escapeRegexString($match);
258 4 $self->{_findstart} = 1; # Start at top, unless startline is defined
259 4 $self->{_findlast} = undef;
260 4 $self->{_findregex} = $match;
261 4 $self->{_findwrap} = $wrap;
262 4 $self->goto($self->{_findstart});
263 4 return $self->findNext();
264 }
265  
266 6 ▶sub findNext {
Subroutine findNext: called CC=10 SCAR=25.0
Pod documented
267 6 my $self = shift;
268 6 my $match = $self->{_findregex};
269 6 ▶ return undef if !$match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 6 6
270 # Continue from current-line + 1 (avoid matchloop)
271 6 ▶ if (defined($self->{_findlast})) { $self->goto($self->{_findlast} + 1); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_findlast"}) 1 5 6
272 6 my $line = $self->get();
273 6 my $MAXCOUNT = $self->getLineCount();
274 6 my $count = 0;
275 6 ▶ while (defined($line) && $count++ <= $MAXCOUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and $count++ <= $MAXCOUNT) 21 4 25
Condition66%2/3
defined $line and $count++ <= $MAXCOUNT
!ll&&!rl&&r
4021
MC/DC50%1/2
defined $line and $count++ <= $MAXCOUNT
defined $line $count++ <= $MAXCOUNT
Truth table66%2/3
A and B
A= defined $lineB= $count++ <= $MAXCOUNT
ABresult
0X0
100
111
276 21 $self->_debug("Finding $match in line: '$line'");
277 21 ▶ if ($line =~ /$match/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$match/) 2 19 21
278 2 ▶ if (defined($self->{_findlast}) && $self->{_currline} eq $self->{_findlast}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_findlast"} and $self->{"_currline"} eq $self->{"_findlast"}) 0 2 2
Condition33%1/3
defined $self->{"_findlast"} and $self->{"_currline"} eq $self->{"_findlast"}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $self->{"_findlast"} and $self->{"_currline"} eq $self->{"_findlast"}
defined $self->{"_findlast"} $self->{"_currline"} eq $self->{"_findlast"}
Truth table33%1/3
A and B
A= defined $self->{"_findlast"}B= $self->{"_currline"} eq $self->{"_findlast"}
ABresult
0X0
100
111
279 0 $self->_debug("Ohoh, should not have found same match again");
280 0 return undef;
281 }
282 2 $self->{_findlast} = $self->getLineNumber();
283 2 $self->_debug("Found match $match in line $self->{_findlast}");
284 2 return $self->getLineNumber();
285 }
286 19 $line = $self->next();
287 19 ▶ if ($self->isEOF() && $self->{_findwrap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isEOF and $self->{"_findwrap"}) 0 19 19
Condition33%1/3
$self->isEOF and $self->{"_findwrap"}
!ll&&!rl&&r
1900
MC/DC0%0/2
$self->isEOF and $self->{"_findwrap"}
$self->isEOF $self->{"_findwrap"}
Truth table33%1/3
A and B
A= $self->isEOFB= $self->{"_findwrap"}
ABresult
0X0
100
111
288 0 $self->goto('top');
289 0 $line = $self->get();
290 }
291 }
292 4 return undef;
293 }
294  
295 0 ▶sub findPrevious {
Subroutine findPrevious: not called CC=1 SCAR=6.9
Pod documented
296 0 return undef;
297 }
298  
299 #-------------------------------------------------------------
300 # Viewing/Editing methods
301 #-------------------------------------------------------------
302 137 ▶sub get {
Subroutine get: called CC=4 SCAR=15.2
Pod documented
303 137 my $self = shift;
304 137 my $linenum = shift;
305 0 ▶ if ( defined($linenum) ) { $linenum = $self->_translateLinePos($linenum) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $linenum) { } 0 137 137
306 137 else { $linenum = $self->{_currline} }
307 137 ▶ if ( !defined($linenum) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $linenum) 0 137 137
308 0 $self->_setError("Invalid line position");
309 0 return undef;
310 }
311 137 my $line = $self->_appendAutoNewline(${ $self->{_buffer} }[$linenum]);
312 137 ▶ $self->_debug( "get line $linenum in array: "
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 137 0 137
313 . ( defined($line) ? $line : "*undef*" ) );
314 137 return $line;
315 }
316  
317 23 ▶sub set {
Subroutine set: called CC=5 SCAR=17.5
Pod documented
318 23 my $self = shift;
319 23 my $line = shift;
320 23 my $linenum = shift;
321 0 ▶ if ( defined($linenum) ) { $linenum = $self->translateLinePos($linenum) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $linenum) { } 0 23 23
322 23 else { $linenum = $self->{_currline} }
323 23 ▶ if ( !defined($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 23 23
324 0 $self->_setError("Cannot set undefined data for line $linenum");
325 0 return undef;
326 }
327 23 $self->_debug("set line $linenum in array: $line");
328 23 ▶ if ( !defined( ${ $self->{_buffer} }[$linenum] )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined do { $$self{"_buffer"} }->[$linenum] or do { $$self{"_buffer"} }->[$linenum] ne $line) 23 0 23
Condition33%1/3
not defined do { $$self{"_buffer"} }->[$linenum] or do { $$self{"_buffer"} }->[$linenum] ne $line
l!l&&r!l&&!r
0230
MC/DC0%0/2
not defined do { $$self{"_buffer"} }->[$linenum] or do { $$self{"_buffer"} }->[$linenum] ne $line
not defined do { $$self{"_buffer"} }->[$linenum] do { $$self{"_buffer"} }->[$linenum] ne $line
Truth table33%1/3
not(A) or B
A= not defined do { $$self{"_buffer"} }->[$linenum]B= do { $$self{"_buffer"} }->[$linenum] ne $line
ABresult
1X1
011
000
329 23 || ${ $self->{_buffer} }[$linenum] ne $line )
330 {
331 23 $self->setModified();
332 }
333  
334 23 ${ $self->{_buffer} }[$linenum] = $line;
335 23 return 1;
336 }
337  
338 # Insert before start of buffer
339 2 ▶sub insert {
Subroutine insert: called CC=1 SCAR=0.0
Pod documented
340 2 my $self = shift;
341 2 unshift( @{ $self->{_buffer} }, @_ );
342 2 return 1;
343 }
344  
345 21 ▶sub append {
Subroutine append: called CC=1 SCAR=0.0
Pod documented
346 21 my $self = shift;
347 21 push( @{ $self->{_buffer} }, @_ );
348 21 return 1;
349 }
350  
351 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
352 0 my $self = shift;
353 0 splice( @{ $self->{_buffer} }, $self->{_currline}, 1 );
354 0 return $self->get();
355 }
356  
357 5 ▶sub dumpAsString {
Subroutine dumpAsString: called CC=5 SCAR=19.6
Pod documented
358 5 my $self = shift;
359 return
360 30 ▶ join( "", map { ( defined($_) ? $_ : "*undef*" ) } @{ $self->{_buffer} } )
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 30 0 30
361 if ( $self->{_buffer} )
362 && ( ref( $self->{_buffer} ) eq "ARRAY" )
363 5 ▶ && $#{ $self->{_buffer} } >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_buffer"} and ref $self->{"_buffer"} eq "ARRAY" and $#{$$self{"_buffer"};} >= 0 5 0 5
Condition33%2/6
$self->{"_buffer"} and ref $self->{"_buffer"} eq "ARRAY" and $#{$$self{"_buffer"};} >= 0
!ll&&!rl&&r
005
$self->{"_buffer"} and ref $self->{"_buffer"} eq "ARRAY"
!ll&&!rl&&r
005
MC/DC0%0/3
$self->{"_buffer"} and ref $self->{"_buffer"} eq "ARRAY" and $#{$$self{"_buffer"};} >= 0
$self->{"_buffer"} ref $self->{"_buffer"} eq "ARRAY" $#{$$self{"_buffer"};} >= 0
Truth table25%1/4
A and B and C
A= $self->{"_buffer"}B= ref $self->{"_buffer"} eq "ARRAY"C= $#{$$self{"_buffer"};} >= 0
ABCresult
0XX0
10X0
1100
1111
364 0 return "";
365 }
366  
367 1 ▶sub replaceString {
Subroutine replaceString: called CC=7 SCAR=20.1
Pod documented
368 1 my $self = shift;
369 1 my ($match,$with) = @_;
370 1 my $str = $self->get();
371 1 ▶ return undef if !defined($str);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 1 1
372 1 $self->_debug( "Doing string replacement of '$match' with '$with' on string: $str" );
373 1 my $pos = 0;
374 1 my $index = 0;
375 1 my $count = 0;
376 1 my $MAXCOUNT = 1000;
377 1 ▶ while ($pos < length($str) && ($index = index($str,$match,$pos)) >= $pos && $count++ < $MAXCOUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $str and ($index = index($str, $match, $pos)) >= $pos and $count++ < $MAXCOUNT) 2 1 3
Condition66%4/6
$pos < length $str and ($index = index($str, $match, $pos)) >= $pos and $count++ < $MAXCOUNT
!ll&&!rl&&r
102
$pos < length $str and ($index = index($str, $match, $pos)) >= $pos
!ll&&!rl&&r
012
MC/DC33%1/3
$pos < length $str and ($index = index($str, $match, $pos)) >= $pos and $count++ < $MAXCOUNT
$pos < length $str ($index = index($str, $match, $pos)) >= $pos $count++ < $MAXCOUNT
Truth table50%2/4
A and B and C
A= $pos < length $strB= ($index = index($str, $match, $pos)) >= $posC= $count++ < $MAXCOUNT
ABCresult
0XX0
10X0
1100
1111
378 # myfoobar, foo is at index 2, foo is replaced by bar,
379 2 $self->_debug("Found $match at $index (pos was $pos)");
380 2 $str = substr($str,0,$index) . $with . substr($str,$index + length($match));
381 2 $pos = $index + length($with);
382 2 $self->_debug("String is now $str, pos is $pos");
383 }
384 0 ▶ if ($count == $MAXCOUNT) { $self->setError("Maximum loopcount reached"); return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($count == $MAXCOUNT) 0 1 1
385 1 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 1 0 1
386 1 $self->set($str);
387 }
388 1 return $count;
389 }
390  
391 2 ▶sub replaceWildcard {
Subroutine replaceWildcard: called CC=1 SCAR=0.0
Pod documented
392 2 my $self = shift;
393 2 my ($match,$with,$opts) = @_;
394 # Replace wildcards with apropriate regex terms
395 # map '*' to '.*?'
396 # map '?' to '.'
397 # leave other things the same, but escape / and ()
398 2 $self->_debug("Doing wildcard replacement of '$match' with '$with'" );
399 2 $match = $self->escapeRegexString($match,"*.");
400 2 $match =~ s/\?/./g;
401 2 $match =~ s/\*/\\S*/g;
402 2 $self->_debug("After wildcard expansion: $match (was $_[1])");
403 2 return $self->replaceRegex($match, $with, $opts);
404 }
405  
406 4 ▶sub replaceRegex {
Subroutine replaceRegex: called CC=4 SCAR=14.0
Pod documented
407 4 my $self = shift;
408 4 my $match = shift;
409 4 my $with = shift;
410 4 my $opts = shift;
411 4 ▶ if ( !defined($opts) ) { $opts = "g"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts) 4 0 4
412 4 my $count;
413 4 my $str = $self->get();
414 4 ▶ return undef if !defined($str);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 4 4
415 # Be sure to escape our used seperation char for s//
416 4 $with =~ s?(^|[^\\])/?$1\\/?g;
417 4 $self->_debug(
418 "Doing regex replacement of '$match' with '$with' (opts: $opts) on string: $str" );
419 4 eval "\$count = (\$str =~ s/$match/$with/$opts)";
420  
421 4 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 4 0 4
422 4 $self->set($str);
423 }
424 4 return $count;
425 }
426  
427 # replace is an alias for replaceRegex
428 1 ▶sub replace { shift->replaceRegex(@_); }
Subroutine replace: called CC=1 SCAR=0.0
Pod documented
429  
430 #=============================================================
431 # Utility functions / class methods
432 #=============================================================
433 2 ▶sub convertWildcardToRegex {
Subroutine convertWildcardToRegex: called CC=2 SCAR=7.0
Pod documented
434 2 my $self = shift;
435 2 ▶ my $string = shift || return "";
Condition50%1/2
shift() || (return '')
l!l
20
MC/DC0%0/1
shift() || (return '')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
436 2 $self->_debug("convert wildcard '$string'");
437 2 $string = $self->escapeRegexString($string,"?*");
438 2 $string =~ s/\?/./g;
439 2 $string =~ s/\*/.*/g;
440 # $string =~ s/([\(\)\/])/\\$1/g;
441 2 $self->_debug("converted to regex: $string");
442 2 return $string;
443 }
444  
445 12 ▶sub escapeRegexString {
Subroutine escapeRegexString: called CC=5 SCAR=16.1
Pod documented
446 # We need to escape all regex specific chars
447 # ignore chars will not be escaped
448 12 my $self = shift;
449 12 ▶ my $string = shift || return "";
Condition50%1/2
shift() || (return '')
l!l
120
MC/DC0%0/1
shift() || (return '')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
450 12 ▶ my $ignorechars = shift || "";
Condition100%2/2
shift() || ''
l!l
48
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
451 12 my $regexchars = '\\/()[]{}+.*?'; #'
452 12 my $escapechars = "";
453 12 $self->_debug("escape string: '$string' ignoring: '$ignorechars' regex: '$regexchars'\n");
454 # Build a hash of chars to ignore and
455 12 my %chars = (map { $_ => 1 } split(//,$ignorechars));
456 # Now remove all unused ignored chars
457 12 ▶ foreach (split(//,$regexchars)) { $escapechars .= '\\' . $_ if !$chars{$_} }
Branch: 1 branch
BranchTrueFalseTotal
unless $chars{$_} 136 8 144
458 12 $string =~ s/([$escapechars])/\\$1/g;
459 12 $self->_debug("escape: '$escapechars', string: '$string'");
460 12 return $string;
461 }
462  
463 2 ▶sub convertStringToRegex {
Subroutine convertStringToRegex: called CC=1 SCAR=0.0
Pod documented
464 2 return shift->escapeRegexString(@_);
465 }
466  
467 #-------------------------------------------------------------
468 # ErrorHandling Methods
469 #-------------------------------------------------------------
470 21 ▶sub _setError { my $self = shift; $self->{error} = shift; }
Subroutine _setError: called CC=1 SCAR=0.0
471 0 ▶sub isError { return ( shift->{'error'} ? 1 : 0 ); }
Subroutine isError: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{'error'} ? : 0 0 0
472  
473 4 ▶sub getError {
Subroutine getError: called CC=1 SCAR=0.0
Pod documented
474 4 my $self = shift;
475 4 my $error = $self->{error};
476 4 $self->_clearError();
477 4 return $error;
478 }
479 4 ▶sub _clearError { shift->{error} = ""; }
Subroutine _clearError: called CC=1 SCAR=0.0
480  
481 #=============================================================
482 # Private Methods
483 #=============================================================
484 # Only internal function for debug output
485 137 ▶sub _appendAutoNewline {
Subroutine _appendAutoNewline: called CC=4 SCAR=14.0
486 137 my $self = shift;
487 137 my $text = shift;
488 137 ▶ return $text if (!$self->{_autonewline} || !$text);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_autonewline"} and $text 39 98 137
Condition66%2/3
$self->{"_autonewline"} and $text
!ll&&!rl&&r
39098
MC/DC50%1/2
$self->{"_autonewline"} and $text
$self->{"_autonewline"} $text
Truth table66%2/3
A and B
A= $self->{"_autonewline"}B= $text
ABresult
0X0
100
111
489 98 ▶ my $newline = $self->{_newline} || "";
Condition50%1/2
$self->{'_newline'} || ''
l!l
980
MC/DC0%0/1
$self->{'_newline'} || ''
$self->{'_newline'}
Truth table50%1/2
A
A= $self->{'_newline'}
Aresult
11
00
490 98 $text =~ s/[\r\n]+$//;
491 98 $self->_debug("appended autonewline " . $self->{_autonewline}. "'$newline' to '$text'");
492 98 return "$text$newline";
493 }
494  
495 1348 ▶sub _debuglevel {
Subroutine _debuglevel: called CC=4 SCAR=14.5
496 1348 my $self = shift;
497 1348 my $level = shift;
498 1348 ▶ if (ref($self) eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq __PACKAGE__) { } 1316 32 1348
499 0 ▶ if (defined($level)) { $self->{_debug} = $level; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $level) 0 1316 1316
500 1316 $level = $self->{_debug};
501 # print "Object debug is $level (" . ref($self) . ")\n";
502 } else {
503 0 ▶ if (defined($level)) { $DEBUG = $level; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $level) 0 32 32
504 32 $level = $DEBUG;
505 # print "Class debug is $level (" . ref($self) . ")\n";
506 }
507 1348 return $level;
508 }
509  
510 674 ▶sub _debug {
Subroutine _debug: called CC=2 SCAR=6.9
511 674 my $self = shift;
512 674 my $lvl = $self->_debuglevel();
513 674 ▶ if ( $self->_debuglevel() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_debuglevel) 16 658 674
514 16 print "[DEBUG$lvl] @_\n";
515 }
516 }
517  
518 4 1;
519 __END__
« Modify/Rule.pm ↑ Summary Modify.pm »