Tagged.pm

filter: Statementstmt 93.4% 538 / 576 Branchbran 86.1% 255 / 296 Conditioncond 84.5% 82 / 97 MC/DCmcdc 74.1% 46 / 62 Subroutinesub 96.4% 54 / 56 Podpod 100% 34 / 34 totaltotal 90.0% 1009 / 1121 CC 196 SCAR 25.8
CC
196
Cov
90%
iter_extents_nooverlap
26.0
clone
25.5
set_substr
21.2
CRAP 13.2
↑ Summary Tagged/Extent.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2008-2023 -- leonerd@leonerd.org.uk
5  
6 package String::Tagged 0.24;
7  
8 21 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 21 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 21 require String::Tagged::Extent;
14  
15 21 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 FLAG_ANCHOR_BEFORE => 0x01,
17 FLAG_ANCHOR_AFTER => 0x02,
18 FLAG_ITERATING => 0x04,
19 FLAG_DELETED => 0x08,
20 21 };
21  
22 21 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # Since we're providing overloading, we should set fallback by default
25 21 ▶use overload fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =head1 NAME
28  
29 C<String::Tagged> - string buffers with value tags on extents
30  
31 =head1 SYNOPSIS
32  
33 use String::Tagged;
34  
35 my $st = String::Tagged->new( "An important message" );
36  
37 $st->apply_tag( 3, 9, bold => 1 );
38  
39 $st->iter_substr_nooverlap(
40 sub {
41 my ( $substring, %tags ) = @_;
42  
43 print $tags{bold} ? "<b>$substring</b>"
44 : $substring;
45 }
46 );
47  
48 =head1 DESCRIPTION
49  
50 This module implements an object class, instances of which store a (mutable)
51 string buffer that supports tags. A tag is a name/value pair that applies to
52 some extent of the underlying string.
53  
54 The types of tag names ought to be strings, or at least values that are
55 well-behaved as strings, as the names will often be used as the keys in hashes
56 or applied to the C<eq> operator.
57  
58 The types of tag values are not restricted - any scalar will do. This could be
59 a simple integer or string, ARRAY or HASH reference, or even a CODE reference
60 containing an event handler of some kind.
61  
62 Tags may be arbitrarily overlapped. Any given offset within the string has in
63 effect, a set of uniquely named tags. Tags of different names are independent.
64 For tags of the same name, only the latest, shortest tag takes effect.
65  
66 For example, consider a string with three tags represented here:
67  
68 Here is my string with tags
69 [-------------------------] foo => 1
70 [-------] foo => 2
71 [---] bar => 3
72  
73 Every character in this string has a tag named C<foo>. The value of this tag
74 is 2 for the words C<my> and C<string> and the space inbetween, and 1
75 elsewhere. Additionally, the words C<is> and C<my> and the space between them
76 also have the tag C<bar> with a value 3.
77  
78 Since C<String::Tagged> does not understand the significance of the tag values
79 it therefore cannot detect if two neighbouring tags really contain the same
80 semantic idea. Consider the following string:
81  
82 A string with words
83 [-------] type => "message"
84 [--------] type => "message"
85  
86 This string contains two tags. C<String::Tagged> will treat this as two
87 different tag values as far as C<iter_tags_nooverlap> is concerned, even
88 though C<get_tag_at> yields the same value for the C<type> tag at any position
89 in the string. The C<merge_tags> method may be used to merge tag extents of
90 tags that should be considered as equal.
91  
92 =head1 NAMING
93  
94 I spent a lot of time considering the name for this module. It seems that a
95 number of people across a number of languages all created similar
96 functionality, though named very differently. For the benefit of
97 keyword-based search tools and similar, here's a list of some other names this
98 sort of object might be known by:
99  
100 =over 4
101  
102 =item *
103  
104 Extents
105  
106 =item *
107  
108 Overlays
109  
110 =item *
111  
112 Attribute or attributed strings
113  
114 =item *
115  
116 Markup
117  
118 =item *
119  
120 Out-of-band data
121  
122 =back
123  
124 =cut
125  
126 *is_string_tagged =
127 # It would be nice if we could #ifdef HAVE_PERL_VERSION(...)
128 ( $] >= 5.034 ) ?
129 21 ▶ do { eval 'use experimental "isa"; sub { $_[0] isa __PACKAGE__ }' // die $@ } :
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition50%1/2
eval "use experimental \"isa\"; sub { \$_[0] isa __PACKAGE__ }" // die($@)
l!l
210
MC/DC0%0/1
eval "use experimental \"isa\"; sub { \$_[0] isa __PACKAGE__ }" // die($@)
eval "use experimental \"isa\"; sub { \$_[0] isa __PACKAGE__ }"
Truth table50%1/2
A
A= eval "use experimental \"isa\"; sub { \$_[0] isa __PACKAGE__ }"
Aresult
11
00
130 # We can't call ->isa as a method on 5.32 because of a bug in the isa
131 # operator implementation that breaks the isa cache on the package.
132 0 ▶ do { sub { blessed $_[0] and UNIVERSAL::isa( $_[0], __PACKAGE__ ) } };
Subroutine __ANON__: not called CC=2 SCAR=12.4
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($_[0]) and UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
$] >= 5.034 ? : 21 0 21
133  
134 =head1 CONSTRUCTOR
135  
136 =cut
137  
138 =head2 new
139  
140 $st = String::Tagged->new( $str );
141  
142 Returns a new instance of a C<String::Tagged> object. It will contain no tags.
143 If the optional C<$str> argument is supplied, the string buffer will be
144 initialised from this value.
145  
146 If C<$str> is a C<String::Tagged> object then it will be cloned, as if calling
147 the C<clone> method on it.
148  
149 =cut
150  
151 sub new
152 143 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod documented
153 143 my $class = shift;
154 143 my ( $str ) = @_;
155  
156 143 ▶ return $class->clone( $str ) if is_string_tagged( $str );
Branch: 1 branch
BranchTrueFalseTotal
if is_string_tagged($str) 21 122 143
157  
158 122 ▶ $str = "" unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 8 114 122
159  
160 122 return bless {
161 str => "$str",
162 tags => [],
163 }, $class;
164 }
165  
166 =head2 new_tagged
167  
168 $st = String::Tagged->new_tagged( $str, %tags );
169  
170 Shortcut for creating a new C<String::Tagged> object with the given tags
171 applied to the entire length. The tags will not be anchored at either end.
172  
173 =cut
174  
175 sub new_tagged
176 8 ▶{
Subroutine new_tagged: called CC=2 SCAR=6.9
Pod documented
177 8 my $class = shift;
178 8 my ( $str, %tags ) = @_;
179  
180 8 my $self = $class->new( $str );
181  
182 8 my $length = $self->length;
183 8 $self->apply_tag( 0, $length, $_ => $tags{$_} ) for keys %tags;
184  
185 8 return $self;
186 }
187  
188 =head2 clone (class)
189  
190 $new = String::Tagged->clone( $orig, %opts );
191  
192 Returns a new instance of C<String::Tagged> made by cloning the original,
193 subject to the options provided. The returned instance will be in the
194 requested class, which need not match the class of the original.
195  
196 The following options are recognised:
197  
198 =over 4
199  
200 =item only_tags => ARRAY
201  
202 If present, gives an ARRAY reference containing tag names. Only those tags
203 named here will be copied; others will be ignored.
204  
205 =item except_tags => ARRAY
206  
207 If present, gives an ARRAY reference containing tag names. All tags will be
208 copied except those named here.
209  
210 =item convert_tags => HASH
211  
212 If present, gives a HASH reference containing tag conversion functions. For
213 any tags in the original to be copied whose names appear in the hash, the
214 name and value are passed into the corresponding function, which should return
215 an even-sized key/value list giving a tag, or a list of tags, to apply to the
216 new clone.
217  
218 my @new_tags = $convert_tags->{$orig_name}->( $orig_name, $orig_value );
219 # Where @new_tags is ( $new_name, $new_value, $new_name_2, $new_value_2, ... );
220  
221 As a further convenience, if the value for a given tag name is a plain string
222 instead of a code reference, it gives the new name for the tag, and will be
223 applied with its existing value.
224  
225 If C<only_tags> is being used too, then the source names of any tags to be
226 converted must also be listed there, or they will not be copied.
227  
228 =item start => INT
229  
230 I<Since version 0.22.>
231  
232 Start at the given position; defaults to 0.
233  
234 =item end => INT
235  
236 I<Since version 0.22.>
237  
238 End after the given position; defaults to end of string. This option overrides
239 C<len>.
240  
241 =item len => INT
242  
243 End after the given length beyond the start position; defaults to end of
244 string. This option only applies if C<end> is not given.
245  
246 =back
247  
248 =head2 clone (instance)
249  
250 $new = $orig->clone( %args );
251  
252 Called as an instance (rather than a class) method, the newly-cloned instance
253 is returned in the same class as the original.
254  
255 =cut
256  
257 sub clone
258 57 ▶{
Subroutine clone: called CC=25 SCAR=32.4
Pod documented
259 57 ▶ my ( $class, $orig ) = blessed $_[0] ?
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 30 27 57
260 ( ref $_[0], shift ) :
261 ( shift, shift );
262 57 my %opts = @_;
263  
264 my $only = exists $opts{only_tags} ?
265 57 ▶ { map { $_ => 1 } @{ $opts{only_tags} } } :
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'only_tags'} ? : 1 56 57
266 undef;
267  
268 my $except = exists $opts{except_tags} ?
269 0 ▶ { map { $_ => 1 } @{ $opts{except_tags} } } :
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'except_tags'} ? : 0 57 57
270 undef;
271  
272 57 my $convert = $opts{convert_tags};
273  
274 57 my $origstr = $orig->str;
275  
276 57 ▶ my $start = $opts{start} // 0;
Condition100%2/2
$opts{"start"} // 0
l!l
3027
MC/DC100%1/1
$opts{"start"} // 0
$opts{"start"}
Truth table100%2/2
A
A= $opts{"start"}
Aresult
11
00
277 my $end = $opts{end} //
278 ( defined $opts{len} ? $start + $opts{len}
279 57 ▶ : length $origstr );
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'len'} ? : 28 27 55
Condition66%2/3
$opts{"end"} // (defined $opts{"len"} ? $start + $opts{"len"} : CORE::length $origstr)
l!l&&r!l&&!r
2550
MC/DC0%0/2
$opts{"end"} // (defined $opts{"len"} ? $start + $opts{"len"} : CORE::length $origstr)
$opts{"end"} (defined $opts{"len"} ? $start + $opts{"len"} : CORE::length $origstr)
Truth table66%2/3
A // B
A= $opts{"end"}B= (defined $opts{"len"} ? $start + $opts{"len"} : CORE::length $origstr)
ABresult
1X1
011
000
280  
281 57 my $len = $end - $start;
282  
283 57 my $new = $class->new( substr $origstr, $start, $end - $start );
284  
285 57 my $tags = $orig->{tags};
286  
287 # We know we're only looking
288 57 foreach my $t ( @$tags ) {
289 64 my ( $ts, $te, $tn, $tv, $tf ) = @$t;
290  
291 64 ▶ next if $te < $start;
Branch: 1 branch
BranchTrueFalseTotal
if $te < $start 5 59 64
292 59 ▶ last if $ts >= $end;
Branch: 1 branch
BranchTrueFalseTotal
if $ts >= $end 3 56 59
293  
294 56 ▶ next if $only and not $only->{$tn};
Branch: 1 branch
BranchTrueFalseTotal
if $only and not $only->{$tn} 1 55 56
Condition100%3/3
$only and not $only->{$tn}
!ll&&!rl&&r
5411
MC/DC100%2/2
$only and not $only->{$tn}
$only not $only->{$tn}
Truth table100%3/3
A and not(B)
A= $onlyB= not $only->{$tn}
ABresult
0X0
100
111
295 55 ▶ next if $except and $except->{$tn};
Branch: 1 branch
BranchTrueFalseTotal
if $except and $except->{$tn} 0 55 55
Condition33%1/3
$except and $except->{$tn}
!ll&&!rl&&r
5500
MC/DC0%0/2
$except and $except->{$tn}
$except $except->{$tn}
Truth table33%1/3
A and B
A= $exceptB= $except->{$tn}
ABresult
0X0
100
111
296  
297 55 my @tags;
298 55 ▶ if( $convert and my $c = $convert->{$tn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($convert and my $c = $convert->{$tn}) { } 2 53 55
Condition66%2/3
$convert and my $c = $convert->{$tn}
!ll&&!rl&&r
5302
MC/DC50%1/2
$convert and my $c = $convert->{$tn}
$convert my $c = $convert->{$tn}
Truth table66%2/3
A and B
A= $convertB= my $c = $convert->{$tn}
ABresult
0X0
100
111
299 2 ▶ if( ref $c eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'CODE') { } 1 1 2
300 1 @tags = $c->( $tn, $tv );
301 }
302 else {
303 1 @tags = ( $c, $tv );
304 }
305 }
306 else {
307 53 @tags = ( $tn, $tv );
308 }
309  
310 55 $_ -= $start for $ts, $te;
311  
312 55 ▶ my $tl = $te - ( $ts < 0 ? 0 : $ts );
Branch: 1 branch
BranchTrueFalseTotal
$ts < 0 ? : 8 47 55
313  
314 55 ▶ next if $te <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $te <= 0 1 54 55
315 54 ▶ $ts = -1 if $ts < 0 or $tf & FLAG_ANCHOR_BEFORE;
Branch: 1 branch
BranchTrueFalseTotal
if $ts < 0 or $tf & 1 23 31 54
Condition100%3/3
$ts < 0 or $tf & 1
l!l&&r!l&&!r
71631
MC/DC100%2/2
$ts < 0 or $tf & 1
$ts < 0 $tf & 1
Truth table100%3/3
A or B
A= $ts < 0B= $tf & 1
ABresult
1X1
011
000
316 54 ▶ $tl = -1 if $te > $len or $tf & FLAG_ANCHOR_AFTER;
Branch: 1 branch
BranchTrueFalseTotal
if $te > $len or $tf & 2 19 35 54
Condition100%3/3
$te > $len or $tf & 2
l!l&&r!l&&!r
61335
MC/DC100%2/2
$te > $len or $tf & 2
$te > $len $tf & 2
Truth table100%3/3
A or B
A= $te > $lenB= $tf & 2
ABresult
1X1
011
000
317  
318 54 ▶ while( @tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tags) 54 54 108
319 54 $new->apply_tag( $ts, $tl, shift @tags, shift @tags );
320 }
321 }
322  
323 57 return $new;
324 }
325  
326 sub _mkextent
327 196 ▶{
Subroutine _mkextent: called CC=1 SCAR=0.0
328 196 my $self = shift;
329 196 my ( $start, $end, $flags ) = @_;
330  
331 196 $flags &= (FLAG_ANCHOR_BEFORE|FLAG_ANCHOR_AFTER);
332  
333 196 return bless [ $self, $start, $end, $flags ], 'String::Tagged::Extent';
334 }
335  
336 =head2 from_sprintf
337  
338 $str = String::Tagged->from_sprintf( $format, @args );
339  
340 I<Since version 0.15.>
341  
342 Returns a new instance of a C<String::Tagged> object, initialised by
343 formatting the supplied arguments using the supplied format.
344  
345 The C<$format> string is similar to that supported by the core C<sprintf>
346 operator, though a few features such as out-of-order argument indexing and
347 vector formatting are missing. This format string may be a plain perl string,
348 or an instance of C<String::Tagged>. In the latter case, any tags within it
349 are preserved in the result.
350  
351 In the case of a C<%s> conversion, the value of the argument consumed may
352 itself be a C<String::Tagged> instance. In this case it will be appended to
353 the returned object, preserving any tags within it.
354  
355 All other conversions are handled individually by the core C<sprintf>
356 operator and appended to the result.
357  
358 =cut
359  
360 sub from_sprintf
361 12 ▶{
Subroutine from_sprintf: called CC=19 SCAR=29.8
Pod documented
362 12 my $class = shift;
363 12 my ( $format, @args ) = @_;
364  
365 # Clone the format string into the candidate return value, and then
366 # repeatedly replace %... expansions with their required value using
367 # ->set_substr, so that embedded tags in the format will behave sensibly.
368  
369 12 ▶ my $ret = ( is_string_tagged( $format ) ) ?
Branch: 1 branch
BranchTrueFalseTotal
is_string_tagged($format) ? : 2 10 12
370 $class->clone( $format ) :
371 $class->new( $format );
372  
373 12 my $pos = 0;
374  
375 12 ▶ while( $pos < length $ret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < CORE::length $ret) 22 12 34
376 22 my $str = "$ret";
377 22 pos( $str ) = $pos;
378  
379 22 my $replacement;
380  
381 22 ▶ if( $str =~ m/\G[^%]+/gc ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($str =~ /\G[^%]+/cgu) { } 9 13 22
elsif ($str =~ /\G%%/cgu) { } 1 12 13
elsif ($str =~ /\G%([-]?)(\d+|\*)?(?:\.(\d+|\*))?s/cgu) { } 10 2 12
elsif ($str =~ /\G%(.*?)([cduoxefgXEGbBpaAiDUOF])/cgu) { } 2 0 2
elsif ($str =~ /\G%(.*?)([a-zA-Z])/cgu) { } 0 0 0
382 # A literal span
383 9 $pos = $+[0];
384 9 next;
385 }
386 elsif( $str =~ m/\G%%/gc ) {
387 # A literal %% conversion
388 1 $replacement = "%";
389 }
390 elsif( $str =~ m/\G%([-]?)(\d+|\*)?(?:\.(\d+|\*))?s/gc ) {
391 # A string
392 10 my ( $flags, $width, $precision ) = ( $1, $2, $3 );
393 10 ▶ $width = shift @args if defined $width and $width eq "*";
Branch: 1 branch
BranchTrueFalseTotal
if defined $width and $width eq "*" 1 9 10
Condition100%3/3
defined $width and $width eq "*"
!ll&&!rl&&r
631
MC/DC100%2/2
defined $width and $width eq "*"
defined $width $width eq "*"
Truth table100%3/3
A and B
A= defined $widthB= $width eq "*"
ABresult
0X0
100
111
394 10 ▶ $precision = shift @args if defined $precision and $precision eq "*";
Branch: 1 branch
BranchTrueFalseTotal
if defined $precision and $precision eq "*" 1 9 10
Condition100%3/3
defined $precision and $precision eq "*"
!ll&&!rl&&r
811
MC/DC100%2/2
defined $precision and $precision eq "*"
defined $precision $precision eq "*"
Truth table100%3/3
A and B
A= defined $precisionB= $precision eq "*"
ABresult
0X0
100
111
395 10 my $arg = shift @args;
396  
397 10 ▶ defined $arg or do {
Branch: 1 branch
BranchTrueFalseTotal
defined $arg or do { warnings::warnif("uninitialized", "Use of ininitialized value in String::Tagged->from_sprintf"); $arg = "" } 10 0 10
398 0 warnings::warnif( uninitialized => "Use of ininitialized value in String::Tagged->from_sprintf" );
399 0 $arg = "";
400 };
401  
402 10 ▶ if( defined $precision ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $precision) 2 8 10
403 2 ▶ if( is_string_tagged( $arg ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_string_tagged($arg)) { } 0 2 2
404 0 $arg = $arg->substr( 0, $precision );
405 }
406 else {
407 2 $arg = substr $arg, 0, $precision;
408 }
409 }
410  
411 10 my $leftalign = $flags =~ m/-/;
412  
413 10 ▶ my $padding = defined $width ? $width - length $arg : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $width ? : 4 6 10
414 10 ▶ $padding = 0 if $padding < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $padding < 0 1 9 10
415  
416 10 $replacement = "";
417  
418 10 ▶ $replacement .= " " x $padding if !$leftalign;
Branch: 1 branch
BranchTrueFalseTotal
unless $leftalign 8 2 10
419  
420 10 $replacement .= $arg;
421  
422 10 ▶ $replacement .= " " x $padding if $leftalign;
Branch: 1 branch
BranchTrueFalseTotal
if $leftalign 2 8 10
423 }
424 elsif( $str =~ m/\G%(.*?)([cduoxefgXEGbBpaAiDUOF])/gc ) {
425 # Another conversion format
426 2 my ( $template, $flags ) = ( $2, $1 );
427 2 my $argc = 1;
428 2 $argc += ( () = $flags =~ m/\*/g );
429  
430 2 $replacement = sprintf "%$flags$template", @args[0..$argc-1];
431 2 splice @args, 0, $argc;
432 }
433 elsif( $str =~ m/\G%(.*?)([a-zA-Z])/gc ) {
434 0 warn "Unrecognised sprintf conversion %$2";
435 }
436 else {
437 # must be at EOF now
438 0 last;
439 }
440  
441 13 my $templatelen = $+[0] - $-[0];
442 13 $ret->set_substr( $-[0], $templatelen, $replacement );
443  
444 13 $pos += length( $replacement );
445 }
446  
447 12 return $ret;
448 }
449  
450 =head2 join
451  
452 $str = String::Tagged->join( $sep, @parts );
453  
454 I<Since version 0.17.>
455  
456 Returns a new instance of a C<String::Tagged> object, formed by concatenating
457 each of the component piece together, joined with the separator string.
458  
459 The result will be much like the core C<join> function, except that it will
460 preserve tags in the resulting string.
461  
462 =cut
463  
464 sub join
465 1 ▶{
Subroutine join: called CC=3 SCAR=11.0
Pod documented
466 1 my $class = shift;
467 1 my ( $sep, @parts ) = @_;
468  
469 1 ▶ is_string_tagged( $sep ) or
Branch: 1 branch
BranchTrueFalseTotal
is_string_tagged($sep) or $sep = $class->new($sep) 0 1 1
470 $sep = $class->new( $sep );
471  
472 1 my $ret = shift @parts;
473 1 $ret .= $sep . $_ for @parts;
474  
475 1 return $ret;
476 }
477  
478 =head1 METHODS
479  
480 =cut
481  
482 =head2 str
483  
484 $str = $st->str;
485  
486 $str = "$st";
487  
488 Returns the plain string contained within the object.
489  
490 This method is also called for stringification; so the C<String::Tagged>
491 object can be used in a plain string interpolation such as
492  
493 my $message = String::Tagged->new( "Hello world" );
494 print "My message is $message\n";
495  
496 =cut
497  
498 21 ▶use overload '""' => 'str';
Subroutine BEGIN: called CC=1 SCAR=0.0
499  
500 sub str
501 230 ▶{
Subroutine str: called CC=1 SCAR=0.0
Pod documented
502 230 my $self = shift;
503 230 return $self->{str};
504 }
505  
506 =head2 length
507  
508 $len = $st->length;
509  
510 $len = length( $st );
511  
512 Returns the length of the plain string. Because stringification works on this
513 object class, the normal core C<length> function works correctly on it.
514  
515 =cut
516  
517 sub length
518 422 ▶{
Subroutine length: called CC=1 SCAR=0.0
Pod documented
519 422 my $self = shift;
520 422 return CORE::length $self->{str};
521 }
522  
523 =head2 substr
524  
525 $str = $st->substr( $start, $len );
526  
527 Returns a C<String::Tagged> instance representing a section from within the
528 given string, containing all the same tags at the same conceptual positions.
529  
530 =cut
531  
532 sub substr
533 27 ▶{
Subroutine substr: called CC=1 SCAR=0.0
Pod documented
534 27 my $self = shift;
535 27 my ( $start, $len ) = @_;
536  
537 27 return $self->clone( start => $start, len => $len );
538 }
539  
540 =head2 plain_substr
541  
542 $str = $st->plain_substr( $start, $len );
543  
544 Returns as a plain perl string, the substring at the given position. This will
545 be the same string data as returned by C<substr>, only as a plain string
546 without the tags
547  
548 =cut
549  
550 sub plain_substr
551 23 ▶{
Subroutine plain_substr: called CC=1 SCAR=0.0
Pod documented
552 23 my $self = shift;
553 23 my ( $start, $len ) = @_;
554  
555 23 return CORE::substr( $self->{str}, $start, $len );
556 }
557  
558 sub _cmp_tags
559 154 ▶{
Subroutine _cmp_tags: called CC=2 SCAR=6.9
560 154 my ( $as, $ae ) = @$a;
561 154 my ( $bs, $be ) = @$b;
562  
563 # Sort by start first; shortest first
564 154 ▶ return $as <=> $bs ||
Condition100%3/3
$as <=> $bs || $ae <=> $be
l!l&&r!l&&!r
130177
MC/DC100%2/2
$as <=> $bs || $ae <=> $be
$as <=> $bs $ae <=> $be
Truth table100%3/3
A || B
A= $as <=> $bsB= $ae <=> $be
ABresult
1X1
011
000
565 $ae <=> $be;
566 }
567  
568 sub _assert_sorted
569 0 ▶{
Subroutine _assert_sorted: not called CC=4 SCAR=30.0
570 0 my $self = shift;
571  
572 0 my $tags = $self->{tags};
573 # If fewer than 2 tags, must be sorted
574 0 ▶ return if @$tags < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$tags < 2 0 0 0
575  
576 0 my $prev = $tags->[0];
577  
578 0 ▶ for( my $i = 1; $i < @$tags; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tags) 0 0 0
579 0 my $here = $tags->[$i];
580 0 local ( $a, $b ) = ( $prev, $here );
581 0 ▶ if( _cmp_tags() <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_cmp_tags() <= 0) 0 0 0
582 0 $prev = $here;
583 0 next;
584 }
585  
586 0 print STDERR "Tag order violation at i=$i\n";
587 0 print STDERR "[@{[ $i - 1 ]}] = [ $tags->[$i-1]->[0], $tags->[$i-1]->[1] ]\n";
588 0 print STDERR "[@{[ $i ]}] = [ $tags->[$i]->[0], $tags->[$i]->[1] ]\n";
589 0 die "Assert failure";
590 }
591 }
592  
593 sub _insert_tag
594 162 ▶{
Subroutine _insert_tag: called CC=11 SCAR=24.7
595 162 my $self = shift;
596 162 my ( $start, $end, $name, $value, $flags ) = @_;
597  
598 162 my $tags = $self->{tags};
599  
600 162 my $newtag = [ $start, $end, $name => $value, $flags ];
601  
602 # Specialcase - if there's no tags yet, just push it
603 162 ▶ if( @$tags == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tags == 0) 84 78 162
604 84 push @$tags, $newtag;
605 84 return;
606 }
607  
608 78 local $a = $newtag;
609  
610 # Two more special cases - it's quite likely we're either inserting an
611 # 'everywhere' tag, or appending one to the end. Check the endpoints first
612 78 local $b;
613  
614 78 $b = $tags->[0];
615 78 ▶ if( _cmp_tags() <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_cmp_tags() <= 0) 14 64 78
616 14 unshift @$tags, $newtag;
617 14 return;
618 }
619  
620 64 $b = $tags->[-1];
621 64 ▶ if( _cmp_tags() >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_cmp_tags() >= 0) 62 2 64
622 62 push @$tags, $newtag;
623 62 return;
624 }
625  
626 2 my $range_start = 0;
627 2 my $range_end = $#$tags;
628  
629 2 my $inspos;
630  
631 2 ▶ while( $range_end > $range_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range_end > $range_start) 2 0 2
632 2 my $i = int( ( $range_start + $range_end ) / 2 );
633  
634 2 $b = $tags->[$i];
635 2 my $cmp = _cmp_tags;
636  
637 2 ▶ if( $cmp > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp > 0) { } 2 0 2
elsif ($cmp < 0) { } 0 0 0
638 2 $range_start = $i + 1;
639 }
640 elsif( $cmp < 0 ) {
641 0 $range_end = $i; # open interval
642 }
643 else {
644 0 $inspos = $i;
645 0 last;
646 }
647  
648 2 ▶ if( $range_start == $range_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range_start == $range_end) 2 0 2
649 2 $inspos = $range_start;
650 2 last;
651 }
652 }
653  
654 2 ▶ $inspos = $range_end unless defined $inspos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inspos 0 2 2
655  
656 2 ▶ $inspos = 0 if $inspos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $inspos < 0 0 2 2
657 2 ▶ $inspos = @$tags if $inspos > @$tags;
Branch: 1 branch
BranchTrueFalseTotal
if $inspos > @$tags 0 2 2
658  
659 2 splice @$tags, $inspos, 0, $newtag;
660  
661 2 $self->_assert_sorted if DEBUG;
662 }
663  
664 =head2 apply_tag
665  
666 $st->apply_tag( $start, $len, $name, $value );
667  
668 Apply the named tag value to the given extent. The tag will start on the
669 character at the C<$start> index, and continue for the next C<$len>
670 characters.
671  
672 If C<$start> is given as -1, the tag will be considered to start "before" the
673 actual string. If C<$len> is given as -1, the tag will be considered to
674 end "after" end of the actual string. These special limits are used by
675 C<set_substr> when deciding whether to move a tag boundary. The start of any
676 tag that starts "before" the string is never moved, even if more text is
677 inserted at the beginning. Similarly, a tag which ends "after" the end of the
678 string, will continue to the end even if more text is appended.
679  
680 This method returns the C<$st> object.
681  
682 $st->apply_tag( $e, $name, $value )
683  
684 Alternatively, an existing L<String::Tagged::Extent> object can be passed as
685 the first argument instead of two integers. The new tag will apply at the
686 given extent.
687  
688 =cut
689  
690 sub apply_tag
691 159 ▶{
Subroutine apply_tag: called CC=7 SCAR=21.0
Pod documented
692 159 my $self = shift;
693 159 my ( $start, $end );
694 159 my $flags = 0;
695  
696 159 ▶ if( blessed $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 0 159 159
697 0 my $e = shift;
698 0 $start = $e->start;
699 0 $end = $e->end;
700  
701 0 ▶ $flags |= FLAG_ANCHOR_BEFORE if $e->anchor_before;
Branch: 1 branch
BranchTrueFalseTotal
if $e->anchor_before 0 0 0
702 0 ▶ $flags |= FLAG_ANCHOR_AFTER if $e->anchor_after;
Branch: 1 branch
BranchTrueFalseTotal
if $e->anchor_after 0 0 0
703 }
704 else {
705 159 $start = shift;
706 159 my $len = shift;
707  
708 159 my $strlen = $self->length;
709  
710 159 ▶ if( $start < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 36 123 159
711 36 $start = 0;
712 36 $flags |= FLAG_ANCHOR_BEFORE;
713 }
714  
715 159 ▶ if( $len == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == -1) { } 37 122 159
716 37 $end = $strlen;
717 37 $flags |= FLAG_ANCHOR_AFTER;
718 }
719 else {
720 122 $end = $start + $len;
721 122 ▶ $end = $strlen if $end > $strlen;
Branch: 1 branch
BranchTrueFalseTotal
if $end > $strlen 2 120 122
722 }
723 }
724  
725 159 my ( $name, $value ) = @_;
726  
727 159 $self->_insert_tag( $start, $end, $name, $value, $flags );
728  
729 159 return $self;
730 }
731  
732 sub _remove_tag
733 9 ▶{
Subroutine _remove_tag: called CC=15 SCAR=27.3
734 9 my $self = shift;
735 9 my $keepends = shift;
736 9 my ( $start, $end );
737  
738 9 ▶ if( blessed $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 5 4 9
739 5 my $e = shift;
740 5 $start = $e->start;
741 5 $end = $e->end;
742 }
743 else {
744 4 $start = shift;
745 4 $end = $start + shift;
746 }
747  
748 9 my ( $name ) = @_;
749  
750 9 ▶ if( my $t = $self->{iterating} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $self->{"iterating"}) 5 4 9
751 5 my ( $ts, $te, $tn ) = @$t;
752 5 ▶ if( $start == $ts and $end == $te and $name eq $tn ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == $ts and $end == $te and $name eq $tn) 5 0 5
Condition33%2/6
$start == $ts and $end == $te and $name eq $tn
!ll&&!rl&&r
005
$start == $ts and $end == $te
!ll&&!rl&&r
005
MC/DC0%0/3
$start == $ts and $end == $te and $name eq $tn
$start == $ts $end == $te $name eq $tn
Truth table25%1/4
A and B and C
A= $start == $tsB= $end == $teC= $name eq $tn
ABCresult
0XX0
10X0
1100
1111
753 5 $t->[4] |= FLAG_DELETED;
754 5 return;
755 }
756 }
757  
758 4 my $tags = $self->{tags};
759  
760 4 my $have_added = 0;
761  
762 # Can't foreach() because we modify $i
763 4 ▶ for( my $i = 0; $i < @$tags; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tags) 8 1 9
764 8 my ( $ts, $te, $tn, $tv, $tf ) = @{ $tags->[$i] };
765  
766 8 ▶ next if $te <= $start;
Branch: 1 branch
BranchTrueFalseTotal
if $te <= $start 1 7 8
767 7 ▶ last if $ts >= $end;
Branch: 1 branch
BranchTrueFalseTotal
if $ts >= $end 3 4 7
768  
769 4 ▶ next if $tn ne $name;
Branch: 1 branch
BranchTrueFalseTotal
if $tn ne $name 0 4 4
770  
771 4 ▶ if( $keepends and $end < $te ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keepends and $end < $te) 2 2 4
Condition100%3/3
$keepends and $end < $te
!ll&&!rl&&r
112
MC/DC100%2/2
$keepends and $end < $te
$keepends $end < $te
Truth table100%3/3
A and B
A= $keependsB= $end < $te
ABresult
0X0
100
111
772 2 $self->_insert_tag( $end, $te, $tn, $tv, $tf & ~(FLAG_ANCHOR_BEFORE|FLAG_ITERATING) );
773 2 $have_added = 1;
774 }
775  
776 4 ▶ if( $tf & FLAG_ITERATING ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tf & 4) 0 4 4
777 0 die "ARGH encountered FLAG_ITERATING while walking the list of tags during ->_remove_tag";
778 }
779  
780 4 splice @$tags, $i, 1;
781  
782 4 ▶ if( $keepends and $ts < $start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keepends and $ts < $start) { } 1 3 4
Condition100%3/3
$keepends and $ts < $start
!ll&&!rl&&r
121
MC/DC100%2/2
$keepends and $ts < $start
$keepends $ts < $start
Truth table100%3/3
A and B
A= $keependsB= $ts < $start
ABresult
0X0
100
111
783 1 $self->_insert_tag( $ts, $start, $tn, $tv, $tf & ~(FLAG_ANCHOR_AFTER|FLAG_ITERATING) );
784 1 $have_added = 1;
785 }
786 else {
787 3 $i--;
788 }
789 }
790  
791 4 if( DEBUG && $have_added ) {
792 $self->_assert_sorted;
793 }
794  
795 4 return $self;
796 }
797  
798 =head2 unapply_tag
799  
800 $st->unapply_tag( $start, $len, $name );
801  
802 Unapply the named tag value from the given extent. If the tag extends beyond
803 this extent, then any partial fragment of the tag will be left in the string.
804  
805 This method returns the C<$st> object.
806  
807 $st->unapply_tag( $e, $name );
808  
809 Alternatively, an existing L<String::Tagged::Extent> object can be passed as
810 the first argument instead of two integers.
811  
812 =cut
813  
814 sub unapply_tag
815 3 ▶{
Subroutine unapply_tag: called CC=1 SCAR=0.0
Pod documented
816 3 my $self = shift;
817 3 return $self->_remove_tag( 1, @_ );
818 }
819  
820 =head2 delete_tag
821  
822 $st->delete_tag( $start, $len, $name );
823  
824 Delete the named tag within the given extent. Entire tags are removed, even if
825 they extend beyond this extent.
826  
827 This method returns the C<$st> object.
828  
829 $st->delete_tag( $e, $name );
830  
831 Alternatively, an existing L<String::Tagged::Extent> object can be passed as
832 the first argument instead of two integers.
833  
834 =cut
835  
836 sub delete_tag
837 6 ▶{
Subroutine delete_tag: called CC=1 SCAR=0.0
Pod documented
838 6 my $self = shift;
839 6 return $self->_remove_tag( 0, @_ );
840 }
841  
842 =head2 delete_all_tag
843  
844 $st->delete_all_tag( $name );
845  
846 I<Since version 0.21.>
847  
848 Deletes every tag with the given name. This is more efficient than calling
849 C<iter_extents> to list the tags then C<delete_tag> on each one individually
850 in the case of a simple name match.
851  
852 This method returns the C<$st> object.
853  
854 =cut
855  
856 sub delete_all_tag
857 1 ▶{
Subroutine delete_all_tag: called CC=3 SCAR=11.0
Pod documented
858 1 my $self = shift;
859 1 my ( $name ) = @_;
860  
861 1 my $tags = $self->{tags};
862  
863 1 ▶ for( my $i = 0; $i < @$tags; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tags) 3 1 4
864 3 my ( $ts, $te, $tn, $tv, $tf ) = @{ $tags->[$i] };
865  
866 3 ▶ next if $tn ne $name;
Branch: 1 branch
BranchTrueFalseTotal
if $tn ne $name 1 2 3
867  
868 2 splice @$tags, $i, 1, ();
869 2 $i--;
870 }
871  
872 1 return $self;
873 }
874  
875 =head2 merge_tags
876  
877 $st->merge_tags( $eqsub );
878  
879 Merge neighbouring or overlapping tags of the same name and equal values.
880  
881 For each pair of tags of the same name that apply on neighbouring or
882 overlapping extents, the C<$eqsub> callback is called, as
883  
884 $equal = $eqsub->( $name, $value_a, $value_b );
885  
886 If this function returns true then the tags are merged.
887  
888 The equallity test function is free to perform any comparison of the values
889 that may be relevant to the application; for example it may deeply compare
890 referred structures and check for equivalence in some application-defined
891 manner. In this case, the first tag of a pair is retained, the second is
892 deleted. This may be relevant if the tag value is a reference to some object.
893  
894 =cut
895  
896 sub merge_tags
897 8 ▶{
Subroutine merge_tags: called CC=12 SCAR=25.0
Pod documented
898 8 my $self = shift;
899 8 my ( $eqsub ) = @_;
900  
901 8 my $tags = $self->{tags};
902  
903 # Can't foreach() because we modify @$tags
904 8 ▶ OUTER: for( my $i = 0; $i < @$tags; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tags) 12 8 20
905 13 my ( $ts, $te, $tn, $tv, $tf ) = @{ $tags->[$i] };
906  
907 13 ▶ for( my $j = $i+1; $j < @$tags; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$tags) 11 10 21
908 11 my ( $t2s, $t2e, $t2n, $t2v, $t2f ) = @{ $tags->[$j] };
909  
910 11 ▶ last if $t2s > $te;
Branch: 1 branch
BranchTrueFalseTotal
if $t2s > $te 1 10 11
911 10 ▶ next unless $t2s <= $te;
Branch: 1 branch
BranchTrueFalseTotal
unless $t2s <= $te 0 10 10
912 10 ▶ next unless $t2n eq $tn;
Branch: 1 branch
BranchTrueFalseTotal
unless $t2n eq $tn 3 7 10
913  
914 7 ▶ last unless $eqsub->( $tn, $tv, $t2v );
Branch: 1 branch
BranchTrueFalseTotal
unless &$eqsub($tn, $tv, $t2v) 1 6 7
915  
916 # Need to delete the tag at $j, extend the end of the tag at $i, and
917 # possibly move $i later
918 6 splice @$tags, $j, 1, ();
919 6 $j--;
920  
921 6 $te = $tags->[$i][1] = $t2e;
922  
923 6 ▶ $tags->[$i][4] |= FLAG_ANCHOR_AFTER if $t2f & FLAG_ANCHOR_AFTER;
Branch: 1 branch
BranchTrueFalseTotal
if $t2f & 2 1 5 6
924  
925 6 local $a = $tags->[$i];
926  
927 6 ▶ if( local $b = $tags->[$i+1] and _cmp_tags() > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (local $b = $$tags[$i + 1] and _cmp_tags() > 0) 1 5 6
Condition100%3/3
local $b = $$tags[$i + 1] and _cmp_tags() > 0
!ll&&!rl&&r
411
MC/DC100%2/2
local $b = $$tags[$i + 1] and _cmp_tags() > 0
local $b = $$tags[$i + 1] _cmp_tags() > 0
Truth table100%3/3
A and B
A= local $b = $$tags[$i + 1]B= _cmp_tags() > 0
ABresult
0X0
100
111
928 1 my $newpos = $i+1;
929 1 ▶ while( local $b = $tags->[$newpos ] and _cmp_tags() <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (local $b = $tags->[$newpos] and _cmp_tags() <= 0) 0 1 1
Condition33%1/3
local $b = $tags->[$newpos] and _cmp_tags() <= 0
!ll&&!rl&&r
010
MC/DC0%0/2
local $b = $tags->[$newpos] and _cmp_tags() <= 0
local $b = $tags->[$newpos] _cmp_tags() <= 0
Truth table33%1/3
A and B
A= local $b = $tags->[$newpos]B= _cmp_tags() <= 0
ABresult
0X0
100
111
930 0 $newpos++;
931 }
932  
933 1 splice @$tags, $newpos, 0, splice @$tags, $i, 1, ();
934  
935 1 redo OUTER;
936 }
937 }
938 }
939 }
940  
941 =head2 iter_extents
942  
943 $st->iter_extents( $callback, %opts );
944  
945 Iterate the tags stored in the string. For each tag, the CODE reference in
946 C<$callback> is invoked once, being passed a L<String::Tagged::Extent> object
947 that represents the extent of the tag.
948  
949 $callback->( $extent, $tagname, $tagvalue );
950  
951 Options passed in C<%opts> may include:
952  
953 =over 4
954  
955 =item start => INT
956  
957 Start at the given position; defaults to 0.
958  
959 =item end => INT
960  
961 End after the given position; defaults to end of string. This option overrides
962 C<len>.
963  
964 =item len => INT
965  
966 End after the given length beyond the start position; defaults to end of
967 string. This option only applies if C<end> is not given.
968  
969 =item only => ARRAY
970  
971 Select only the tags named in the given ARRAY reference.
972  
973 =item except => ARRAY
974  
975 Select all the tags except those named in the given ARRAY reference.
976  
977 =back
978  
979 I<Since version 0.21> it is safe to call C<delete_tag> from within the
980 callback function to remove the tag currently being iterated on.
981  
982 $str->iter_extents( sub {
983 my ( $e, $n, $v ) = @_;
984 $str->delete_tag( $e, $n ) if $n =~ m/^tmp_/;
985 } );
986  
987 Apart from this scenario, the tags in the string should not otherwise be added
988 or removed while the iteration is occurring.
989  
990 =cut
991  
992 sub iter_extents
993 52 ▶{
Subroutine iter_extents: called CC=14 SCAR=26.4
Pod documented
994 52 my $self = shift;
995 52 my ( $callback, %opts ) = @_;
996  
997 my $start = exists $opts{start} ? $opts{start} :
998 52 ▶ 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'start'} ? : 1 51 52
999  
1000 my $end = exists $opts{end} ? $opts{end} :
1001 exists $opts{len} ? $start + $opts{len} :
1002 52 ▶ $self->length + 1; # so as to include zerolen at end
Branch: 2 branches
BranchTrueFalseTotal
exists $opts{'end'} ? : 1 51 52
exists $opts{'len'} ? : 0 51 51
1003  
1004 52 ▶ my $only = exists $opts{only} ? { map { $_ => 1 } @{ $opts{only} } } :
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'only'} ? : 1 51 52
1005 undef;
1006  
1007 52 ▶ my $except = exists $opts{except} ? { map { $_ => 1 } @{ $opts{except} } } :
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'except'} ? : 1 51 52
1008 undef;
1009  
1010 52 my $tags = $self->{tags};
1011  
1012 52 ▶ for ( my $i = 0; $i < @$tags; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tags) 76 51 127
1013 76 my $t = $tags->[$i];
1014 76 my ( $ts, $te, $tn, $tv, $tf ) = @$t;
1015  
1016 76 ▶ next if $te < $start;
Branch: 1 branch
BranchTrueFalseTotal
if $te < $start 1 75 76
1017 75 ▶ last if $ts >= $end;
Branch: 1 branch
BranchTrueFalseTotal
if $ts >= $end 1 74 75
1018  
1019 74 ▶ next if $only and !$only->{$tn};
Branch: 1 branch
BranchTrueFalseTotal
if $only and not $only->{$tn} 2 72 74
Condition100%3/3
$only and not $only->{$tn}
!ll&&!rl&&r
7112
MC/DC100%2/2
$only and not $only->{$tn}
$only not $only->{$tn}
Truth table100%3/3
A and not(B)
A= $onlyB= not $only->{$tn}
ABresult
0X0
100
111
1020 72 ▶ next if $except and $except->{$tn};
Branch: 1 branch
BranchTrueFalseTotal
if $except and $except->{$tn} 1 71 72
Condition100%3/3
$except and $except->{$tn}
!ll&&!rl&&r
6921
MC/DC100%2/2
$except and $except->{$tn}
$except $except->{$tn}
Truth table100%3/3
A and B
A= $exceptB= $except->{$tn}
ABresult
0X0
100
111
1021  
1022 71 $t->[4] |= FLAG_ITERATING;
1023 71 local $self->{iterating} = $t;
1024  
1025 71 $callback->( $self->_mkextent( $ts, $te, $tf ), $tn, $tv );
1026  
1027 71 $t->[4] &= ~FLAG_ITERATING;
1028  
1029 71 ▶ if( $t->[4] & FLAG_DELETED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->[4] & 8) 5 66 71
1030 5 splice @$tags, $i, 1, ();
1031 5 $i--;
1032 }
1033 }
1034 }
1035  
1036 =head2 iter_tags
1037  
1038 $st->iter_tags( $callback, %opts );
1039  
1040 Iterate the tags stored in the string. For each tag, the CODE reference in
1041 C<$callback> is invoked once, being passed the start point and length of the
1042 tag.
1043  
1044 $callback->( $start, $length, $tagname, $tagvalue );
1045  
1046 Options passed in C<%opts> are the same as for C<iter_extents>.
1047  
1048 =cut
1049  
1050 sub iter_tags
1051 25 ▶{
Subroutine iter_tags: called CC=1 SCAR=0.0
Pod documented
1052 25 my $self = shift;
1053 25 my ( $callback, %opts ) = @_;
1054  
1055 $self->iter_extents(
1056 36 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1057 36 my ( $e, $tn, $tv ) = @_;
1058 36 $callback->( $e->start, $e->length, $tn, $tv );
1059 },
1060 25 %opts
1061 );
1062 }
1063  
1064 =head2 iter_extents_nooverlap
1065  
1066 $st->iter_extents_nooverlap( $callback, %opts );
1067  
1068 Iterate non-overlapping extents of tags stored in the string. The CODE
1069 reference in C<$callback> is invoked for each extent in the string where no
1070 tags change. The entire set of tags active in that extent is given to the
1071 callback. Because the extent covers possibly-multiple tags, it will not define
1072 the C<anchor_before> and C<anchor_after> flags.
1073  
1074 $callback->( $extent, %tags );
1075  
1076 The callback will be invoked over the entire length of the string, including
1077 any extents with no tags applied.
1078  
1079 Options may be passed in C<%opts> to control the range of the string iterated
1080 over, in the same way as the C<iter_extents> method.
1081  
1082 If the C<only> or C<except> filters are applied, then only the tags that
1083 survive filtering will be present in the C<%tags> hash. Tags that are excluded
1084 by the filtering will not be present, nor will their bounds be used to split
1085 the string into extents.
1086  
1087 =cut
1088  
1089 sub iter_extents_nooverlap
1090 36 ▶{
Subroutine iter_extents_nooverlap: called CC=26 SCAR=32.6
Pod documented
1091 36 my $self = shift;
1092 36 my ( $callback, %opts ) = @_;
1093  
1094 my $start = exists $opts{start} ? $opts{start} :
1095 36 ▶ 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'start'} ? : 2 34 36
1096  
1097 my $end = exists $opts{end} ? $opts{end} :
1098 exists $opts{len} ? $start + $opts{len} :
1099 36 ▶ $self->length;
Branch: 2 branches
BranchTrueFalseTotal
exists $opts{'end'} ? : 2 34 36
exists $opts{'len'} ? : 0 34 34
1100  
1101 36 ▶ my $only = exists $opts{only} ? { map { $_ => 1 } @{ $opts{only} } } :
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'only'} ? : 1 35 36
1102 undef;
1103  
1104 36 ▶ my $except = exists $opts{except} ? { map { $_ => 1 } @{ $opts{except} } } :
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'except'} ? : 1 35 36
1105 undef;
1106  
1107 36 my $tags = $self->{tags};
1108  
1109 36 my @active; # ARRAY of [ $ts, $te, $tn, $tv ]
1110 36 my $pos = $start;
1111  
1112 36 foreach my $t ( @$tags ) {
1113 78 my ( $ts, $te, $tn, $tv ) = @$t;
1114  
1115 78 ▶ next if $te < $start;
Branch: 1 branch
BranchTrueFalseTotal
if $te < $start 2 76 78
1116 76 ▶ last if $ts > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $ts > $end 2 74 76
1117  
1118 74 ▶ next if $only and !$only->{$tn};
Branch: 1 branch
BranchTrueFalseTotal
if $only and not $only->{$tn} 2 72 74
Condition100%3/3
$only and not $only->{$tn}
!ll&&!rl&&r
7112
MC/DC100%2/2
$only and not $only->{$tn}
$only not $only->{$tn}
Truth table100%3/3
A and not(B)
A= $onlyB= not $only->{$tn}
ABresult
0X0
100
111
1119 72 ▶ next if $except and $except->{$tn};
Branch: 1 branch
BranchTrueFalseTotal
if $except and $except->{$tn} 1 71 72
Condition100%3/3
$except and $except->{$tn}
!ll&&!rl&&r
6921
MC/DC100%2/2
$except and $except->{$tn}
$except $except->{$tn}
Truth table100%3/3
A and B
A= $exceptB= $except->{$tn}
ABresult
0X0
100
111
1120  
1121 71 ▶ while( $pos < $ts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $ts) 45 71 116
1122 45 my %activetags;
1123 my %tagends;
1124 45 my $rangeend = $ts;
1125  
1126 45 foreach ( @active ) {
1127 42 my ( undef, $e, $n, $v ) = @$_;
1128  
1129 42 ▶ $e < $rangeend and $rangeend = $e;
Branch: 1 branch
BranchTrueFalseTotal
$e < $rangeend and $rangeend = $e 14 28 42
1130 42 ▶ next if $tagends{$n} and $tagends{$n} < $e;
Branch: 1 branch
BranchTrueFalseTotal
if $tagends{$n} and $tagends{$n} < $e 1 41 42
Condition66%2/3
$tagends{$n} and $tagends{$n} < $e
!ll&&!rl&&r
4101
MC/DC50%1/2
$tagends{$n} and $tagends{$n} < $e
$tagends{$n} $tagends{$n} < $e
Truth table66%2/3
A and B
A= $tagends{$n}B= $tagends{$n} < $e
ABresult
0X0
100
111
1131  
1132 41 $activetags{$n} = $v;
1133 41 $tagends{$n} = $e;
1134 }
1135  
1136 45 $callback->( $self->_mkextent( $pos, $rangeend, 0 ), %activetags );
1137  
1138 45 $pos = $rangeend;
1139 45 @active = grep { $_->[1] > $pos } @active;
1140 }
1141  
1142 71 push @active, [ $ts, $te, $tn, $tv ];
1143 }
1144  
1145 36 ▶ while( $pos < $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $end) 56 36 92
1146 56 my %activetags;
1147 my %tagends;
1148 56 my $rangeend = $end;
1149  
1150 56 foreach ( @active ) {
1151 74 my ( undef, $e, $n, $v ) = @$_;
1152  
1153 74 ▶ $e < $rangeend and $rangeend = $e;
Branch: 1 branch
BranchTrueFalseTotal
$e < $rangeend and $rangeend = $e 21 53 74
1154 74 ▶ next if $tagends{$n} and $tagends{$n} < $e;
Branch: 1 branch
BranchTrueFalseTotal
if $tagends{$n} and $tagends{$n} < $e 1 73 74
Condition100%3/3
$tagends{$n} and $tagends{$n} < $e
!ll&&!rl&&r
7031
MC/DC100%2/2
$tagends{$n} and $tagends{$n} < $e
$tagends{$n} $tagends{$n} < $e
Truth table100%3/3
A and B
A= $tagends{$n}B= $tagends{$n} < $e
ABresult
0X0
100
111
1155  
1156 73 $activetags{$n} = $v;
1157 73 $tagends{$n} = $e;
1158 }
1159  
1160 56 $callback->( $self->_mkextent( $pos, $rangeend, 0 ), %activetags );
1161  
1162 56 $pos = $rangeend;
1163 56 @active = grep { $_->[1] > $pos } @active;
1164 }
1165  
1166 # We might have zero-length tags active at the very end of the range
1167 36 ▶ if( my @zerolen = grep { $_->[0] == $pos and $_->[1] == $pos } @active ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@zerolen) = grep({$_->[0] == $pos and $_->[1] == $pos;} @active)) 1 35 36
Condition66%2/3
$_->[0] == $pos && $_->[1] == $pos
!ll&&!rl&&r
201
MC/DC50%1/2
$_->[0] == $pos && $_->[1] == $pos
$_->[0] == $pos $_->[1] == $pos
Truth table66%2/3
A && B
A= $_->[0] == $posB= $_->[1] == $pos
ABresult
0X0
100
111
1168 1 my %activetags;
1169 1 foreach ( @active ) {
1170 1 my ( undef, undef, $n, $v ) = @$_;
1171  
1172 1 $activetags{$n} = $v;
1173 }
1174  
1175 1 $callback->( $self->_mkextent( $pos, $pos, 0 ), %activetags );
1176 }
1177 }
1178  
1179 =head2 iter_tags_nooverlap
1180  
1181 $st->iter_tags_nooverlap( $callback, %opts );
1182  
1183 Iterate extents of the string using C<iter_extents_nooverlap>, but passing
1184 the start and length of each extent to the callback instead of the extent
1185 object.
1186  
1187 $callback->( $start, $length, %tags );
1188  
1189 Options may be passed in C<%opts> to control the range of the string iterated
1190 over, in the same way as the C<iter_extents> method.
1191  
1192 =cut
1193  
1194 sub iter_tags_nooverlap
1195 30 ▶{
Subroutine iter_tags_nooverlap: called CC=1 SCAR=0.0
Pod documented
1196 30 my $self = shift;
1197 30 my ( $callback, %opts ) = @_;
1198  
1199 $self->iter_extents_nooverlap(
1200 85 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1201 85 my ( $e, %tags ) = @_;
1202 85 $callback->( $e->start, $e->length, %tags );
1203 },
1204 30 %opts
1205 );
1206 }
1207  
1208 =head2 iter_substr_nooverlap
1209  
1210 $st->iter_substr_nooverlap( $callback, %opts );
1211  
1212 Iterate extents of the string using C<iter_extents_nooverlap>, but passing the
1213 substring of data instead of the extent object.
1214  
1215 $callback->( $substr, %tags );
1216  
1217 Options may be passed in C<%opts> to control the range of the string iterated
1218 over, in the same way as the C<iter_extents> method.
1219  
1220 =cut
1221  
1222 sub iter_substr_nooverlap
1223 6 ▶{
Subroutine iter_substr_nooverlap: called CC=1 SCAR=0.0
Pod documented
1224 6 my $self = shift;
1225 6 my ( $callback, %opts ) = @_;
1226  
1227 $self->iter_extents_nooverlap(
1228 17 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1229 17 my ( $e, %tags ) = @_;
1230 17 $callback->( $e->plain_substr, %tags );
1231 },
1232 6 %opts,
1233 );
1234 }
1235  
1236 =head2 tagnames
1237  
1238 @names = $st->tagnames;
1239  
1240 Returns the set of tag names used in the string, in no particular order.
1241  
1242 =cut
1243  
1244 sub tagnames
1245 14 ▶{
Subroutine tagnames: called CC=2 SCAR=6.9
Pod documented
1246 14 my $self = shift;
1247  
1248 14 my $tags = $self->{tags};
1249  
1250 14 my %tags;
1251 14 foreach my $t ( @$tags ) {
1252 15 $tags{$t->[2]}++;
1253 }
1254  
1255 14 keys %tags;
1256 }
1257  
1258 =head2 get_tags_at
1259  
1260 $tags = $st->get_tags_at( $pos );
1261  
1262 Returns a HASH reference of all the tag values active at the given position.
1263  
1264 =cut
1265  
1266 sub get_tags_at
1267 11 ▶{
Subroutine get_tags_at: called CC=4 SCAR=13.9
Pod documented
1268 11 my $self = shift;
1269 11 my ( $pos ) = @_;
1270  
1271 11 my $tags = $self->{tags};
1272  
1273 11 my %tags;
1274  
1275 # TODO: turn this into a binary search
1276 11 foreach my $t ( @$tags ) {
1277 14 my ( $ts, $te, $tn, $tv ) = @$t;
1278  
1279 14 ▶ last if $ts > $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $ts > $pos 3 11 14
1280 11 ▶ next if $te <= $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $te <= $pos 1 10 11
1281  
1282 10 $tags{$tn} = $tv;
1283 }
1284  
1285 11 return \%tags;
1286 }
1287  
1288 =head2 get_tag_at
1289  
1290 $value = $st->get_tag_at( $pos, $name );
1291  
1292 Returns the value of the named tag at the given position, or C<undef> if the
1293 tag is not applied there.
1294  
1295 =cut
1296  
1297 sub get_tag_at
1298 6 ▶{
Subroutine get_tag_at: called CC=5 SCAR=16.1
Pod documented
1299 6 my $self = shift;
1300 6 my ( $pos, $name ) = @_;
1301  
1302 6 my $tags = $self->{tags};
1303  
1304 6 my $value;
1305  
1306 6 foreach my $t ( @$tags ) {
1307 15 my ( $ts, $te, $tn, $tv ) = @$t;
1308  
1309 15 ▶ last if $ts > $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $ts > $pos 4 11 15
1310 11 ▶ next if $te <= $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $te <= $pos 3 8 11
1311  
1312 8 ▶ $value = $tv if $tn eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $tn eq $name 6 2 8
1313 }
1314  
1315 6 return $value;
1316 }
1317  
1318 =head2 get_tag_extent
1319  
1320 $extent = $st->get_tag_extent( $pos, $name );
1321  
1322 If the named tag applies to the given position, returns a
1323 L<String::Tagged::Extent> object to represent the extent of the tag at that
1324 position. If it does not, C<undef> is returned. If an extent is returned it
1325 will define the C<anchor_before> and C<anchor_after> flags if appropriate.
1326  
1327 =cut
1328  
1329 sub get_tag_extent
1330 13 ▶{
Subroutine get_tag_extent: called CC=6 SCAR=17.9
Pod documented
1331 13 my $self = shift;
1332 13 my ( $pos, $name ) = @_;
1333  
1334 13 my $tags = $self->{tags};
1335  
1336 13 my ( $start, $end, $flags );
1337  
1338 13 foreach my $t ( @$tags ) {
1339 19 my ( $ts, $te, $tn, undef, $tf ) = @$t;
1340  
1341 19 ▶ last if $ts > $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $ts > $pos 3 16 19
1342 16 ▶ next if $te <= $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $te <= $pos 1 15 16
1343  
1344 15 ▶ next unless $tn eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $tn eq $name 3 12 15
1345  
1346 12 $start = $ts;
1347 12 $end = $te;
1348 12 $flags = $tf;
1349 }
1350  
1351 13 ▶ if( defined $start ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) { } 12 1 13
1352 12 return $self->_mkextent( $start, $end, $flags );
1353 }
1354 else {
1355 1 return undef;
1356 }
1357 }
1358  
1359 =head2 get_tag_missing_extent
1360  
1361 $extent = $st->get_tag_missing_extent( $pos, $name );
1362  
1363 If the named tag does not apply at the given position, returns the extent of
1364 the string around that position that does not have the tag. If it does exist,
1365 C<undef> is returned. If an extent is returned it will not define the
1366 C<anchor_before> and C<anchor_after> flags, as these do not make sense for the
1367 range in which a tag is absent.
1368  
1369 =cut
1370  
1371 sub get_tag_missing_extent
1372 3 ▶{
Subroutine get_tag_missing_extent: called CC=6 SCAR=17.9
Pod documented
1373 3 my $self = shift;
1374 3 my ( $pos, $name ) = @_;
1375  
1376 3 my $tags = $self->{tags};
1377  
1378 3 my $start = 0;
1379  
1380 3 foreach my $t ( @$tags ) {
1381 6 my ( $ts, $te, $tn ) = @$t;
1382  
1383 6 ▶ next unless $tn eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $tn eq $name 3 3 6
1384  
1385 3 ▶ if( $ts <= $pos and $te > $pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts <= $pos and $te > $pos) 1 2 3
Condition100%3/3
$ts <= $pos and $te > $pos
!ll&&!rl&&r
111
MC/DC100%2/2
$ts <= $pos and $te > $pos
$ts <= $pos $te > $pos
Truth table100%3/3
A and B
A= $ts <= $posB= $te > $pos
ABresult
0X0
100
111
1386 1 return undef;
1387 }
1388  
1389 2 ▶ if( $ts > $pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts > $pos) 1 1 2
1390 1 return $self->_mkextent( $start, $ts, 0 );
1391 }
1392  
1393 1 $start = $te;
1394 }
1395  
1396 1 return $self->_mkextent( $start, $self->length, 0 );
1397 }
1398  
1399 =head2 set_substr
1400  
1401 $st->set_substr( $start, $len, $newstr );
1402  
1403 Modifies a extent of the underlying plain string to that given. The extents of
1404 tags in the string are adjusted to cope with the modified region, and the
1405 adjustment in length.
1406  
1407 Tags entirely before the replaced extent remain unchanged.
1408  
1409 Tags entirely within the replaced extent are deleted.
1410  
1411 Tags entirely after the replaced extent are moved by appropriate amount to
1412 ensure they still apply to the same characters as before.
1413  
1414 Tags that start before and end after the extent remain, and have their lengths
1415 suitably adjusted.
1416  
1417 Tags that span just the start or end of the extent, but not both, are
1418 truncated, so as to remove the part of the tag applied on the modified extent
1419 but preserving that applied outside.
1420  
1421 If C<$newstr> is a C<String::Tagged> object, then its tags will be applied to
1422 C<$st> as appropriate. Edge-anchored tags in C<$newstr> will not be extended
1423 through C<$st>, though they will apply as edge-anchored if they now sit at the
1424 edge of the new string. If C<$newstr> is being appended to the end, then any
1425 existing edge-anchored tags at the end of C<$st> are I<not> extended through
1426 the string; they will instead become bounded to their end position before the
1427 append happened.
1428  
1429 If C<$newstr> is otherwise treated as a plain string, then any existing
1430 edge-anchored tags at the end of C<$st> I<are> extended through the newly
1431 added content and will continue to be edge-anchored in the result.
1432  
1433 =cut
1434  
1435 sub set_substr
1436 57 ▶{
Subroutine set_substr: called CC=21 SCAR=30.5
Pod documented
1437 57 my $self = shift;
1438 57 my ( $start, $len, $new ) = @_;
1439  
1440 57 my $new_is_st = is_string_tagged( $new );
1441  
1442 57 my $limit = $self->length;
1443  
1444 57 ▶ $start = $limit if $start > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $start > $limit 0 57 57
1445 57 ▶ $len = ( $limit - $start ) if $len > ( $limit - $start );
Branch: 1 branch
BranchTrueFalseTotal
if $len > $limit - $start 0 57 57
1446  
1447 57 CORE::substr( $self->{str}, $start, $len ) = $new;
1448  
1449 57 my $oldend = $start + $len;
1450 57 my $newend = $start + CORE::length( $new );
1451  
1452 57 my $delta = $newend - $oldend;
1453 # Positions after $oldend have now moved up $delta places
1454  
1455 57 my $tags = $self->{tags};
1456  
1457 57 my $i = 0;
1458  
1459 57 ▶ for( ; $i < @$tags; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tags) 41 45 86
1460 # In this loop we'll handle tags that start before the deleted section
1461  
1462 41 my $t = $tags->[$i];
1463 41 my ( $ts, $te, undef, undef, $tf ) = @$t;
1464  
1465 41 ▶ last if $ts >= $start and not( $tf & FLAG_ANCHOR_BEFORE );
Branch: 1 branch
BranchTrueFalseTotal
if $ts >= $start and not $tf & 1 12 29 41
Condition100%3/3
$ts >= $start and not $tf & 1
!ll&&!rl&&r
22712
MC/DC100%2/2
$ts >= $start and not $tf & 1
$ts >= $start not $tf & 1
Truth table100%3/3
A and not(B)
A= $ts >= $startB= not $tf & 1
ABresult
0X0
100
111
1466  
1467 # Two cases:
1468 # A: Tag spans entirely outside deleted section - stretch/compress it
1469 # We may have to collapse it to nothing, so delete it
1470 # B: Tag starts before but ends within deleted section - truncate it
1471 # Plus a case we don't care about
1472 # Tag starts and ends entirely before the deleted section - ignore it
1473  
1474 29 ▶ if( $te > $oldend or
Branch: 2 branches
BranchTrueFalseTotal
if ($te > $oldend or $te == $oldend and $tf & 2) { } 15 14 29
elsif ($te > $start) { } 1 13 14
Condition100%6/6
$te > $oldend or $te == $oldend and $tf & 2
l!l&&r!l&&!r
10514
$te == $oldend and $tf & 2
!ll&&!rl&&r
1135
MC/DC100%3/3
$te > $oldend or $te == $oldend and $tf & 2
$te > $oldend $te == $oldend $tf & 2
Truth table100%4/4
A or B and C
A= $te > $oldendB= $te == $oldendC= $tf & 2
ABCresult
1XX1
0111
00X0
0100
1475 ( $te == $oldend and $tf & FLAG_ANCHOR_AFTER ) ) {
1476 # Case A
1477 15 ▶ if( $tf & FLAG_ANCHOR_AFTER and $new_is_st ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tf & 2 and $new_is_st) 4 11 15
Condition100%3/3
$tf & 2 and $new_is_st
!ll&&!rl&&r
474
MC/DC100%2/2
$tf & 2 and $new_is_st
$tf & 2 $new_is_st
Truth table100%3/3
A and B
A= $tf & 2B= $new_is_st
ABresult
0X0
100
111
1478 # Do not extend anchor-after tags if we are appending a String::Tagged
1479 4 $t->[4] &= ~FLAG_ANCHOR_AFTER;
1480 4 next;
1481 }
1482  
1483 11 $t->[1] += $delta;
1484  
1485 11 ▶ if( $t->[0] == $t->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->[0] == $t->[1]) 0 11 11
1486 0 splice @$tags, $i, 1, ();
1487 0 $i--;
1488 0 next;
1489 }
1490 }
1491 elsif( $te > $start ) {
1492 # Case B
1493 1 $t->[1] = $start;
1494 }
1495 }
1496  
1497 57 ▶ for( ; $i < @$tags; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tags) 13 47 60
1498 13 my $t = $tags->[$i];
1499 13 my ( $ts, $te ) = @$t;
1500  
1501 # In this loop we'll handle tags that start within the deleted section
1502 13 ▶ last if $ts >= $oldend;
Branch: 1 branch
BranchTrueFalseTotal
if $ts >= $oldend 10 3 13
1503  
1504 # Two cases
1505 # C: Tag contained entirely within deleted section - delete it
1506 # D: Tag starts within but ends after the deleted section - truncate it
1507  
1508 3 ▶ if( $te <= $oldend ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($te <= $oldend) { } 2 1 3
1509 # Case C
1510 2 splice @$tags, $i, 1;
1511 2 $i--;
1512 2 next;
1513 }
1514 else {
1515 # Case D
1516 1 $t->[0] = $newend;
1517 1 $t->[1] += $delta;
1518 }
1519 }
1520  
1521 57 ▶ for( ; $i < @$tags; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tags) 12 57 69
1522 12 my $t = $tags->[$i];
1523 12 my ( $ts, $te, undef, undef, $tf ) = @$t;
1524  
1525 # In this loop we'll handle tags that start after the deleted section
1526  
1527 # One case
1528 # E: Tag starts and ends after the deleted section - move it
1529 12 ▶ $t->[0] += $delta unless $tf & FLAG_ANCHOR_BEFORE;
Branch: 1 branch
BranchTrueFalseTotal
unless $tf & 1 11 1 12
1530 12 $t->[1] += $delta;
1531  
1532 # If we've not moved the start (because it was FLAG_ANCHOR_BEFORE), we
1533 # might now have an ordering constraint violation. Better fix it.
1534 12 local $b = $t;
1535 12 foreach my $new_i ( reverse 0 .. $i-1 ) {
1536 7 local $a = $tags->[$new_i];
1537  
1538 7 ▶ last if _cmp_tags() <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if _cmp_tags() <= 0 6 1 7
1539  
1540 1 splice @$tags, $new_i, 0, splice @$tags, $i, 1, ();
1541  
1542 1 last;
1543 }
1544 }
1545  
1546 57 ▶ if( $new_is_st ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_is_st) 22 35 57
1547 22 my $atstart = $start == 0;
1548 22 my $atend = $newend == $self->length;
1549  
1550 21 ▶ $new->iter_extents( sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
1551 21 my ( $e, $tn, $tv ) = @_;
1552 21 ▶ $self->apply_tag(
Branch: 2 branches
BranchTrueFalseTotal
$atstart && $e->anchor_before ? : 0 21 21
$atend && $e->anchor_after ? : 5 16 21
Condition83%5/6
$atstart and $e->anchor_before
!ll&&!rl&&r
1830
$atend and $e->anchor_after
!ll&&!rl&&r
3135
MC/DC0%0/2
$atstart and $e->anchor_before
$atstart $e->anchor_before
MC/DC100%2/2
$atend and $e->anchor_after
$atend $e->anchor_after
Truth table66%2/3
A and B
A= $atstartB= $e->anchor_before
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $atendB= $e->anchor_after
ABresult
0X0
100
111
1553 ( $atstart && $e->anchor_before ) ? -1 : $start + $e->start,
1554 ( $atend && $e->anchor_after ) ? -1 : $e->length,
1555 $tn, $tv );
1556 22 } );
1557 }
1558  
1559 57 $self->_assert_sorted if DEBUG;
1560  
1561 57 return $self;
1562 }
1563  
1564 =head2 insert
1565  
1566 $st->insert( $start, $newstr );
1567  
1568 Insert the given string at the given position. A shortcut around
1569 C<set_substr>.
1570  
1571 If C<$newstr> is a C<String::Tagged> object, then its tags will be applied to
1572 C<$st> as appropriate. If C<$start> is 0, any before-anchored tags in will
1573 become before-anchored in C<$st>.
1574  
1575 =cut
1576  
1577 sub insert
1578 15 ▶{
Subroutine insert: called CC=1 SCAR=0.0
Pod documented
1579 15 my $self = shift;
1580 15 my ( $at, $new ) = @_;
1581 15 $self->set_substr( $at, 0, $new );
1582 }
1583  
1584 =head2 append
1585  
1586 $st->append( $newstr );
1587  
1588 $st .= $newstr;
1589  
1590 Append to the underlying plain string. A shortcut around C<set_substr>.
1591  
1592 If C<$newstr> is a C<String::Tagged> object, then its tags will be applied to
1593 C<$st> as appropriate. Any after-anchored tags in will become after-anchored
1594 in C<$st>.
1595  
1596 As per C<set_substr>, whether any existing edge-anchored tags are extended
1597 through the newly-added content or become bounded to their current limit
1598 depends on whether C<$newstr> is a C<String::Tagged> instance or not.
1599  
1600 =cut
1601  
1602 21 ▶use overload '.=' => 'append';
Subroutine BEGIN: called CC=1 SCAR=0.0
1603  
1604 sub append
1605 68 ▶{
Subroutine append: called CC=4 SCAR=13.9
Pod documented
1606 68 my $self = shift;
1607 68 my ( $new ) = @_;
1608  
1609 68 ▶ return $self->set_substr( $self->length, 0, $new ) if is_string_tagged( $new );
Branch: 1 branch
BranchTrueFalseTotal
if is_string_tagged($new) 19 49 68
1610  
1611 # Optimised version
1612 49 $self->{str} .= $new;
1613  
1614 49 my $newend = $self->length;
1615  
1616 49 my $tags = $self->{tags};
1617  
1618 49 my $i = 0;
1619  
1620 # Adjust boundaries of ANCHOR_AFTER tags
1621 49 ▶ for( ; $i < @$tags; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tags) 59 49 108
1622 59 my $t = $tags->[$i];
1623 59 ▶ $t->[1] = $newend if $t->[4] & FLAG_ANCHOR_AFTER;
Branch: 1 branch
BranchTrueFalseTotal
if $t->[4] & 2 12 47 59
1624 }
1625  
1626 49 return $self;
1627 }
1628  
1629 =head2 append_tagged
1630  
1631 $st->append_tagged( $newstr, %tags );
1632  
1633 Append to the underlying plain string, and apply the given tags to the
1634 newly-inserted extent.
1635  
1636 Returns C<$st> itself so that the method may be easily chained.
1637  
1638 =cut
1639  
1640 sub append_tagged
1641 19 ▶{
Subroutine append_tagged: called CC=2 SCAR=6.9
Pod documented
1642 19 my $self = shift;
1643 19 my ( $new, %tags ) = @_;
1644  
1645 19 my $start = $self->length;
1646 19 my $len = CORE::length( $new );
1647  
1648 19 $self->append( $new );
1649 19 $self->apply_tag( $start, $len, $_, $tags{$_} ) for keys %tags;
1650  
1651 19 return $self;
1652 }
1653  
1654 =head2 concat
1655  
1656 $ret = $st->concat( $other );
1657  
1658 $ret = $st . $other;
1659  
1660 Returns a new C<String::Tagged> containing the two strings concatenated
1661 together, preserving any tags present. This method overloads normal string
1662 concatenation operator, so expressions involving C<String::Tagged> values
1663 retain their tags.
1664  
1665 This method or operator tries to respect subclassing; preferring to return a
1666 new object of a subclass if either argument or operand is a subclass of
1667 C<String::Tagged>. If they are both subclasses, it will prefer the type of the
1668 invocant or first operand.
1669  
1670 =cut
1671  
1672 21 ▶use overload '.' => 'concat';
Subroutine BEGIN: called CC=1 SCAR=0.0
1673  
1674 sub concat
1675 17 ▶{
Subroutine concat: called CC=4 SCAR=13.9
Pod documented
1676 17 my $self = shift;
1677 17 my ( $other, $swap ) = @_;
1678  
1679 # Try to find the "higher" subclass
1680 17 ▶ my $class = ( ref $self eq __PACKAGE__ and is_string_tagged( $other ) )
Branch: 1 branch
BranchTrueFalseTotal
ref $self eq __PACKAGE__ && is_string_tagged($other) ? : 3 14 17
Condition100%3/3
ref $self eq __PACKAGE__ and is_string_tagged($other)
!ll&&!rl&&r
3113
MC/DC100%2/2
ref $self eq __PACKAGE__ and is_string_tagged($other)
ref $self eq __PACKAGE__ is_string_tagged($other)
Truth table100%3/3
A and B
A= ref $self eq __PACKAGE__B= is_string_tagged($other)
ABresult
0X0
100
111
1681 ? ref $other : ref $self;
1682  
1683 17 my $ret = $class->new( $self );
1684 17 ▶ return $ret->insert( 0, $other ) if $swap;
Branch: 1 branch
BranchTrueFalseTotal
if $swap 11 6 17
1685 6 return $ret->append( $other );
1686 }
1687  
1688 =head2 matches
1689  
1690 @subs = $st->matches( $regexp );
1691  
1692 Returns a list of substrings (as C<String::Tagged> instances) for every
1693 non-overlapping match of the given C<$regexp>.
1694  
1695 This could be used, for example, to build a formatted string from a formatted
1696 template containing variable expansions:
1697  
1698 my $template = ...
1699 my %vars = ...
1700  
1701 my $ret = String::Tagged->new;
1702 foreach my $m ( $template->matches( qr/\$\w+|[^$]+/ ) ) {
1703 if( $m =~ m/^\$(\w+)$/ ) {
1704 $ret->append_tagged( $vars{$1}, %{ $m->get_tags_at( 0 ) } );
1705 }
1706 else {
1707 $ret->append( $m );
1708 }
1709 }
1710  
1711 This iterates segments of the template containing variables expansions
1712 starting with a C<$> symbol, and replaces them with values from the C<%vars>
1713 hash, careful to preserve all the formatting tags from the original template
1714 string.
1715  
1716 =cut
1717  
1718 sub matches
1719 1 ▶{
Subroutine matches: called CC=2 SCAR=6.9
Pod documented
1720 1 my $self = shift;
1721 1 my ( $re ) = @_;
1722  
1723 1 my $plain = $self->str;
1724  
1725 1 my @ret;
1726 1 ▶ while( $plain =~ m/$re/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plain =~ /$re/gu) 5 1 6
1727 5 push @ret, $self->substr( $-[0], $+[0] - $-[0] );
1728 }
1729  
1730 1 return @ret;
1731 }
1732  
1733 =head2 match_extents
1734  
1735 @extents = $st->match_extents( $regexp );
1736  
1737 I<Since version 0.20.>
1738  
1739 Returns a list of extent objects for every non-overlapping match of the given
1740 C<$regexp>. This is similar to L</matches>, except that the results are
1741 returned as extent objects instead of substrings, allowing access to the
1742 position information as well.
1743  
1744 If using the result of this method to find regions of a string to modify,
1745 remember that any length alterations will not update positions in later extent
1746 objects. However, since the extents are non-overlapping and in position order,
1747 this can be handled by iterating them in reverse order so that the
1748 modifications done first are later in the string.
1749  
1750 foreach my $e ( reverse $st->match_extents( $pattern ) ) {
1751 $st->set_substr( $e->start, $e->length, $replacement );
1752 }
1753  
1754 =cut
1755  
1756 sub match_extents
1757 2 ▶{
Subroutine match_extents: called CC=2 SCAR=6.9
Pod documented
1758 2 my $self = shift;
1759 2 my ( $re ) = @_;
1760  
1761 2 my $plain = $self->str;
1762  
1763 2 my @ret;
1764 2 ▶ while( $plain =~ m/$re/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plain =~ /$re/gu) 9 2 11
1765 9 push @ret, $self->_mkextent( $-[0], $+[0], 0 );
1766 }
1767  
1768 2 return @ret;
1769 }
1770  
1771 =head2 split
1772  
1773 @parts = $st->split( $regexp, $limit );
1774  
1775 Returns a list of substrings by applying the regexp to the string content;
1776 similar to the core perl C<split> function. If C<$limit> is supplied, the
1777 method will stop at that number of elements, returning the entire remainder of
1778 the input string as the final element. If the C<$regexp> contains a capture
1779 group then the content of the first one will be added to the return list as
1780 well.
1781  
1782 =cut
1783  
1784 sub split
1785 4 ▶{
Subroutine split: called CC=6 SCAR=17.9
Pod documented
1786 4 my $self = shift;
1787 4 my ( $re, $limit ) = @_;
1788  
1789 4 my $plain = $self->str;
1790  
1791 4 my $prev = 0;
1792 4 my @ret;
1793 4 ▶ while( $plain =~ m/$re/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plain =~ /$re/gu) 5 3 8
1794 5 push @ret, $self->substr( $prev, $-[0]-$prev );
1795 5 ▶ push @ret, $self->substr( $-[1], $+[1]-$-[1] ) if @- > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @- > 1 2 3 5
1796  
1797 5 $prev = $+[0];
1798  
1799 5 ▶ last if defined $limit and @ret == $limit-1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit and @ret == $limit - 1 1 4 5
Condition66%2/3
defined $limit and @ret == $limit - 1
!ll&&!rl&&r
401
MC/DC50%1/2
defined $limit and @ret == $limit - 1
defined $limit @ret == $limit - 1
Truth table66%2/3
A and B
A= defined $limitB= @ret == $limit - 1
ABresult
0X0
100
111
1800 }
1801  
1802 4 ▶ if( CORE::length $plain > $prev ) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $plain > $prev) 3 1 4
1803 3 push @ret, $self->substr( $prev, CORE::length( $plain ) - $prev );
1804 }
1805  
1806 4 return @ret;
1807 }
1808  
1809 =head2 sprintf
1810  
1811 $ret = $st->sprintf( @args );
1812  
1813 I<Since version 0.15.>
1814  
1815 Returns a new string by using the given instance as the format string for a
1816 L</from_sprintf> constructor call. The returned instance will be of the same
1817 class as the invocant.
1818  
1819 =cut
1820  
1821 sub sprintf
1822 1 ▶{
Subroutine sprintf: called CC=1 SCAR=0.0
Pod documented
1823 1 my $self = shift;
1824  
1825 1 return ( ref $self )->from_sprintf( $self, @_ );
1826 }
1827  
1828 =head2 debug_sprintf
1829  
1830 $ret = $st->debug_sprintf;
1831  
1832 Returns a representation of the string data and all the tags, suitable for
1833 debug printing or other similar use. This is a format such as is given in the
1834 DESCRIPTION section above.
1835  
1836 The output will consist of a number of lines, the first containing the plain
1837 underlying string, then one line per tag. The line shows the extent of the tag
1838 given by C<[---]> markers, or a C<|> in the special case of a tag covering
1839 only a single character. Special markings of C<E<lt>> and C<E<gt>> indicate
1840 tags which are "before" or "after" anchored.
1841  
1842 For example:
1843  
1844 Hello, world
1845 [---] word => 1
1846 <[----------]> everywhere => 1
1847 | space => 1
1848  
1849 =cut
1850  
1851 sub debug_sprintf
1852 5 ▶{
Subroutine debug_sprintf: called CC=6 SCAR=17.9
Pod documented
1853 5 my $self = shift;
1854  
1855 5 my $str = $self->str;
1856 5 my $len = CORE::length( $str );
1857  
1858 5 my $maxnamelen = 0;
1859  
1860 5 my $ret = " " . ( $str =~ s/\n/./gr ) . "\n";
1861  
1862 7 ▶ $self->iter_tags( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1863 7 my ( undef, undef, $name, undef ) = @_;
1864 7 ▶ CORE::length( $name ) > $maxnamelen and $maxnamelen = CORE::length( $name );
Branch: 1 branch
BranchTrueFalseTotal
CORE::length $name > $maxnamelen and $maxnamelen = CORE::length $name 6 1 7
1865 5 } );
1866  
1867 5 foreach my $t ( @{ $self->{tags} } ) {
1868 7 my ( $ts, $te, $tn, $tv, $tf ) = @$t;
1869  
1870 7 ▶ $ret .= ( $tf & FLAG_ANCHOR_BEFORE ) ? " <" : " ";
Branch: 1 branch
BranchTrueFalseTotal
$tf & 1 ? : 1 6 7
1871  
1872 7 $ret .= " " x $ts;
1873  
1874 7 my $tl = $te - $ts;
1875  
1876 7 ▶ if( $tl == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tl == 0) { } 1 6 7
elsif ($tl == 1) { } 2 4 6
1877 1 $ret =~ s/ $/></;
1878 1 $te++; # account for extra printed width
1879 }
1880 elsif( $tl == 1 ) {
1881 2 $ret .= "|";
1882 }
1883 else {
1884 4 $ret .= "[" . ( "-" x ( $tl - 2 ) ) . "]";
1885 }
1886  
1887 7 $ret .= " " x ( $len - $te );
1888  
1889 7 ▶ $ret .= ( $tf & FLAG_ANCHOR_AFTER ) ? "> " : " ";
Branch: 1 branch
BranchTrueFalseTotal
$tf & 2 ? : 1 6 7
1890  
1891 7 $ret .= CORE::sprintf "%-*s => %s\n", $maxnamelen, $tn, $tv;
1892 }
1893  
1894 5 return $ret;
1895 }
1896  
1897 =head1 TODO
1898  
1899 =over 4
1900  
1901 =item *
1902  
1903 There are likely variations on the rules for C<set_substr> that could equally
1904 apply to some uses of tagged strings. Consider whether the behaviour of
1905 modification is chosen per-method, per-tag, or per-string.
1906  
1907 =item *
1908  
1909 Consider how to implement a clone from one tag format to another which wants
1910 to merge multiple different source tags together into a single new one.
1911  
1912 =back
1913  
1914 =head1 AUTHOR
1915  
1916 Paul Evans <leonerd@leonerd.org.uk>
1917  
1918 =cut
1919  
1920 21 0x55AA;
↑ Summary Tagged/Extent.pm »