line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::MakeMethods::Utility::TextBuilder; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
$VERSION = 1.008; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
@EXPORT_OK = qw( text_builder ); |
6
|
2
|
50
|
|
2
|
|
1929
|
sub import { require Exporter and goto &Exporter::import } # lazy Exporter |
7
|
|
|
|
|
|
|
|
8
|
88
|
|
|
88
|
|
8358
|
use strict; |
|
88
|
|
|
|
|
168
|
|
|
88
|
|
|
|
|
5464
|
|
9
|
88
|
|
|
88
|
|
599
|
use Carp; |
|
88
|
|
|
|
|
160
|
|
|
88
|
|
|
|
|
67866
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# $expanded_text = text_builder( $base_text, @exprs ) |
12
|
|
|
|
|
|
|
sub text_builder { |
13
|
429
|
|
|
429
|
0
|
1714
|
my ( $text, @mod_exprs ) = @_; |
14
|
|
|
|
|
|
|
|
15
|
429
|
|
|
|
|
727
|
my @code_exprs; |
16
|
429
|
|
|
|
|
1120
|
while ( scalar @mod_exprs ) { |
17
|
1874
|
|
|
|
|
2874
|
my $mod_expr = shift @mod_exprs; |
18
|
1874
|
100
|
|
|
|
10284
|
if ( ref $mod_expr eq 'HASH' ) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
19
|
433
|
|
|
|
|
5302
|
push @code_exprs, %$mod_expr; |
20
|
|
|
|
|
|
|
} elsif ( ref $mod_expr eq 'ARRAY' ) { |
21
|
262
|
|
|
|
|
841
|
unshift @mod_exprs, @$mod_expr; |
22
|
|
|
|
|
|
|
} elsif ( ref $mod_expr eq 'CODE' ) { |
23
|
0
|
|
|
|
|
0
|
$text = &$mod_expr( $text ); |
24
|
|
|
|
|
|
|
} elsif ( ! ref $_ ) { |
25
|
1179
|
|
|
|
|
4052
|
$mod_expr =~ s{\*}{$text}g; |
26
|
1179
|
|
|
|
|
3518
|
$text = $mod_expr; |
27
|
|
|
|
|
|
|
} else { |
28
|
0
|
|
|
|
|
0
|
Carp::confess "Wierd contents of modifier array."; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
429
|
|
|
|
|
3514
|
my %rules = @code_exprs; |
32
|
|
|
|
|
|
|
|
33
|
429
|
|
|
|
|
600
|
my @exprs; |
34
|
|
|
|
|
|
|
my @blocks; |
35
|
429
|
|
|
|
|
3307
|
foreach ( sort { length($b) <=> length($a) } keys %rules ) { |
|
32505
|
|
|
|
|
42223
|
|
36
|
9629
|
100
|
|
|
|
38395
|
if ( s/\{\}\Z// ) { |
37
|
3441
|
|
|
|
|
6415
|
push @blocks, $_; |
38
|
|
|
|
|
|
|
} else { |
39
|
6188
|
|
|
|
|
9847
|
push @exprs, $_; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
429
|
50
|
|
|
|
1713
|
push @blocks, 'UNUSED_CONSTANT' if ( ! scalar @blocks ); |
43
|
429
|
50
|
|
|
|
981
|
push @exprs, 'UNUSED_CONSTANT' if ( ! scalar @exprs ); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# There has *got* to be a better way to regex matched brackets... Right? |
46
|
|
|
|
|
|
|
# Erm, well, no. It looks like Text::Balanced would do the trick, with the |
47
|
|
|
|
|
|
|
# requirement that the below bit get re-written to not be regex-based. |
48
|
429
|
|
|
|
|
6121
|
my $expr_expr = '\b(' . join('|', map "\Q$_\E", @exprs ) . ')\b'; |
49
|
429
|
|
|
|
|
4369
|
my $block_expr = '\b(' . join('|', map "\Q$_\E", @blocks ) . ') \{ |
50
|
|
|
|
|
|
|
( [^\{\}]* |
51
|
|
|
|
|
|
|
(?: \{ |
52
|
|
|
|
|
|
|
[^\{\}]* |
53
|
|
|
|
|
|
|
(?: \{ [^\{\}]* \} [^\{\}]* )*? |
54
|
|
|
|
|
|
|
\} [^\{\}]* )*? |
55
|
|
|
|
|
|
|
) |
56
|
|
|
|
|
|
|
\}'; |
57
|
|
|
|
|
|
|
|
58
|
429
|
|
66
|
|
|
45296
|
1 while ( |
|
|
|
100
|
|
|
|
|
59
|
|
|
|
|
|
|
length $text and $text =~ s/ $expr_expr / |
60
|
3673
|
|
|
|
|
7595
|
my $substitute = $rules{ $1 }; |
61
|
3673
|
50
|
|
|
|
6593
|
if ( ! ref $substitute ) { |
|
|
0
|
|
|
|
|
|
62
|
3673
|
|
|
|
|
51356
|
$substitute; |
63
|
|
|
|
|
|
|
} elsif ( ref $substitute eq 'CODE' ) { |
64
|
0
|
|
|
|
|
0
|
&{ $substitute }(); |
|
0
|
|
|
|
|
0
|
|
65
|
|
|
|
|
|
|
} else { |
66
|
0
|
|
|
|
|
0
|
croak "Unknown type of substitution rule: '$substitute'"; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
/gesx or $text =~ s/ $block_expr / |
69
|
2224
|
|
|
|
|
18673
|
my $substitute = $rules{ $1 . '{}' }; |
70
|
2224
|
|
|
|
|
3396
|
my $contents = $2; |
71
|
2224
|
100
|
|
|
|
4473
|
if ( ! ref $substitute ) { |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
72
|
2137
|
|
|
|
|
25154
|
$substitute =~ s{\*}{$contents}g; |
73
|
2137
|
|
|
|
|
25667
|
$substitute; |
74
|
|
|
|
|
|
|
} elsif ( ref $substitute eq 'HASH' ) { |
75
|
87
|
|
|
|
|
1059
|
$substitute->{$contents}; |
76
|
|
|
|
|
|
|
} elsif ( ref $substitute eq 'CODE' ) { |
77
|
0
|
|
|
|
|
0
|
&{ $substitute }( $contents ); |
|
0
|
|
|
|
|
0
|
|
78
|
|
|
|
|
|
|
} else { |
79
|
0
|
|
|
|
|
0
|
croak "Unknown type of substitution rule: '$substitute'"; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
/gesx |
82
|
|
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
|
84
|
429
|
|
|
|
|
6885
|
return $text; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
1; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
__END__ |