| 1 | package App::Greple::xlate::Mask; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | use v5.24; | ||||||||||||||||||||||||||||
| 4 | use warnings; | ||||||||||||||||||||||||||||
| 5 | use Data::Dumper; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | use Hash::Util qw(lock_keys); | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | my %default = ( | ||||||||||||||||||||||||||||
| 10 | TAG => 'm', | ||||||||||||||||||||||||||||
| 11 | INDEX => 'id', | ||||||||||||||||||||||||||||
| 12 | NUMBER => 0, | ||||||||||||||||||||||||||||
| 13 | PATTERN => [], | ||||||||||||||||||||||||||||
| 14 | TABLE => [], | ||||||||||||||||||||||||||||
| 15 | AUTORESET => 0, | ||||||||||||||||||||||||||||
| 16 | # --- stable (anonymization) path --- | ||||||||||||||||||||||||||||
| 17 | STABLE => 0, # same (tag, string) -> same tag | ||||||||||||||||||||||||||||
| 18 | RULES => undef, # permanent [ [tag, pattern], ... ] | ||||||||||||||||||||||||||||
| 19 | FILERULES => undef, # per-document rules, replaced via file_rules() | ||||||||||||||||||||||||||||
| 20 | COUNTER => undef, # per-tag counters | ||||||||||||||||||||||||||||
| 21 | ASSIGNED => undef, # "tag\0string" -> tag string | ||||||||||||||||||||||||||||
| 22 | ASSIGN_ORDER => undef, # tag strings in assignment order | ||||||||||||||||||||||||||||
| 23 | ORIGIN => undef, # tag string -> original string | ||||||||||||||||||||||||||||
| 24 | TRACK => undef, # tag string -> expected response occurrence count | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub new { | ||||||||||||||||||||||||||||
| 28 | 67 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 67 | my $obj = bless { %default }, $class; | |||||||||||||||||||||||||||
| 30 | # NOTE: reference-valued defaults must get fresh copies here | ||||||||||||||||||||||||||||
| 31 | 67 | $obj->{PATTERN} = []; | |||||||||||||||||||||||||||
| 32 | 67 | $obj->{TABLE} = []; | |||||||||||||||||||||||||||
| 33 | 67 | $obj->{RULES} = []; | |||||||||||||||||||||||||||
| 34 | 67 | $obj->{FILERULES} = []; | |||||||||||||||||||||||||||
| 35 | 67 | $obj->{COUNTER} = {}; | |||||||||||||||||||||||||||
| 36 | 67 | $obj->{ASSIGNED} = {}; | |||||||||||||||||||||||||||
| 37 | 67 | $obj->{ASSIGN_ORDER} = []; | |||||||||||||||||||||||||||
| 38 | 67 | $obj->{ORIGIN} = {}; | |||||||||||||||||||||||||||
| 39 | 67 | $obj->{TRACK} = {}; | |||||||||||||||||||||||||||
| 40 | 67 | lock_keys %{$obj}; | |||||||||||||||||||||||||||
| 41 | 67 | $obj->configure(@_); | |||||||||||||||||||||||||||
| 42 | 25 | $obj; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub reset { | ||||||||||||||||||||||||||||
| 46 | 25 | ▶ | my $obj = shift; | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 25 | $obj->{NUMBER} = 0; | |||||||||||||||||||||||||||
| 48 | 25 | $obj->{TABLE} = []; | |||||||||||||||||||||||||||
| 49 | 25 | $obj->{TRACK} = {}; | |||||||||||||||||||||||||||
| 50 | 25 | $obj; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub configure { | ||||||||||||||||||||||||||||
| 54 | 25 | ▶ | my $obj = shift; | ||||||||||||||||||||||||||
|
Subroutine
configure: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 25 | ▶ | while (my($a, $b) = splice @_, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 25 | ▶ | if ($a eq 'pattern') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 57 | 25 | ▶ | my @pattern = ref $b ? @$b : $b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 25 | push @{$obj->{PATTERN}}, @pattern; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | elsif ($a eq 'file') { | ||||||||||||||||||||||||||||
| 61 | 25 | ▶ | open my $fh, '<:encoding(utf8)', $b or die "$b: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 25 | my @p = map s/\\(?=\n)//gr, split /(?<!\\)\n/, do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 63 | 24 | push @{$obj->{PATTERN}}, @p; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||
| 66 | 24 | $obj->{$a} = $b; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub add_rule { | ||||||||||||||||||||||||||||
| 72 | 24 | ▶ | my($obj, $tag, $pattern) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_rule: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 25 | ▶ | $tag =~ /\A[a-z][a-z0-9_]*\z/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | or die "$tag: invalid category name.\n"; | ||||||||||||||||||||||||||||
| 75 | 25 | push @{$obj->{RULES}}, [ $tag, $pattern ]; | |||||||||||||||||||||||||||
| 76 | 1 | $obj; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub file_rules { | ||||||||||||||||||||||||||||
| 80 | 1 | ▶ | my($obj, $rules) = @_; | ||||||||||||||||||||||||||
|
Subroutine
file_rules: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 1 | $obj->{FILERULES} = [ @$rules ]; | |||||||||||||||||||||||||||
| 82 | 1 | $obj; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | ## | ||||||||||||||||||||||||||||
| 86 | ## Escape rule: hide pre-existing tag-shaped literals so that every | ||||||||||||||||||||||||||||
| 87 | ## tag in the working text is one of ours. Must be the first rule; | ||||||||||||||||||||||||||||
| 88 | ## restored last (rules are restored in reverse order of the TABLE). | ||||||||||||||||||||||||||||
| 89 | ## | ||||||||||||||||||||||||||||
| 90 | sub add_escape_rule { | ||||||||||||||||||||||||||||
| 91 | 1 | ▶ | my $obj = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_escape_rule: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 1 | unshift @{$obj->{RULES}}, [ | |||||||||||||||||||||||||||
| 93 | 'lit', | ||||||||||||||||||||||||||||
| 94 | q{<[a-z][a-z0-9_]* [a-z0-9_]+=(?:"\d+"|'\d+'|\d+) */>}, | ||||||||||||||||||||||||||||
| 95 | ]; | ||||||||||||||||||||||||||||
| 96 | 1 | $obj; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | use JSON; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | our $DEFAULT_MARK = | ||||||||||||||||||||||||||||
| 102 | q[\{\{\s*(?<category>[a-z][a-z0-9_]*)\(\s*(?<q>["'])(?<text>.+?)\k<q>\s*\)\s*\}\}]; | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub _check_category { | ||||||||||||||||||||||||||||
| 105 | 1 | ▶ | my $tag = shift; | ||||||||||||||||||||||||||
|
Subroutine
_check_category: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 106 | 23 | ▶ | die "lit: reserved category name.\n" if $tag eq 'lit'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 35 | ▶ | $tag =~ /\A[a-z][a-z0-9_]*\z/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | or die "$tag: invalid category name.\n"; | ||||||||||||||||||||||||||||
| 109 | 35 | $tag; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | sub load_anonymize_file { | ||||||||||||||||||||||||||||
| 113 | 35 | ▶ | my($obj, $path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load_anonymize_file: called CC=16 SCAR=27.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 35 | ▶ | open my $fh, '<:encoding(utf8)', $path or die "$path: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 35 | my $data = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 116 | 1 | $data =~ s/\A\x{FEFF}//; # tolerate a UTF-8 BOM | |||||||||||||||||||||||||||
| 117 | 13 | ▶ | if ($data =~ /\A\s*\[/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 13 | my $list = JSON->new->decode($data); | |||||||||||||||||||||||||||
| 119 | 13 | ▶ | ref $list eq 'ARRAY' or die "$path: JSON array expected.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 13 | for my $e (@$list) { | |||||||||||||||||||||||||||
| 121 | 31 | ▶ | ref $e eq 'HASH' or die "$path: object expected.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | my $cat = _check_category($e->{category} | ||||||||||||||||||||||||||||
| 123 | 31 | ▶ | // die "$path: category missing.\n"); | ||||||||||||||||||||||||||
|
Condition50%1/2
$e->{"category"} // die("${path}: category missing.\n")
MC/DC0%0/1
$e->{"category"} // die("${path}: category missing.\n")
$e->{"category"}
Truth table50%1/2
A
A= $e->{"category"}
| |||||||||||||||||||||||||||||
| 124 | 29 | my $has_text = defined $e->{text}; | |||||||||||||||||||||||||||
| 125 | 28 | my $has_regex = defined $e->{regex}; | |||||||||||||||||||||||||||
| 126 | 15 | ▶ | die "$path: both text and regex given.\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$has_text and $has_regex
MC/DC100%2/2
$has_text and $has_regex
$has_text
$has_regex
Truth table100%3/3
A and B
A= $has_textB= $has_regex
| |||||||||||||||||||||||||||||
| 127 | if $has_text and $has_regex; | ||||||||||||||||||||||||||||
| 128 | 15 | ▶ | die "$path: either text or regex required.\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$has_text or $has_regex
MC/DC100%2/2
$has_text or $has_regex
$has_text
$has_regex
Truth table100%3/3
A or B
A= $has_textB= $has_regex
| |||||||||||||||||||||||||||||
| 129 | unless $has_text or $has_regex; | ||||||||||||||||||||||||||||
| 130 | $obj->add_rule($cat, | ||||||||||||||||||||||||||||
| 131 | 15 | ▶ | $has_text ? quotemeta($e->{text}) : $e->{regex}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||
| 134 | 15 | my @lines = map s/\\(?=\n)//gr, split /(?<!\\)\n/, $data; | |||||||||||||||||||||||||||
| 135 | 15 | for my $line (@lines) { | |||||||||||||||||||||||||||
| 136 | 15 | ▶ | next if $line =~ /^\s*(#|$)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 15 | ▶ | my($cat, $pat) = $line =~ /\A\s*(\S+)\s+(.*?)\s*\z/s | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | or die "$path: unparsable line: $line\n"; | ||||||||||||||||||||||||||||
| 139 | 13 | _check_category($cat); | |||||||||||||||||||||||||||
| 140 | 13 | ▶ | if ($pat =~ m{\A/(.*)/\z}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 13 | $obj->add_rule($cat, $1); | |||||||||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||||||||||
| 143 | 21 | $obj->add_rule($cat, quotemeta($pat)); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 21 | $obj; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | sub extract_marks { | ||||||||||||||||||||||||||||
| 151 | 19 | ▶ | my($text, $regex) = @_; | ||||||||||||||||||||||||||
|
Subroutine
extract_marks: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | 19 | ▶ | index($regex, '(?<category>') >= 0 and index($regex, '(?<text>') >= 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
index($regex, "(?<category>") != -1 and index($regex, "(?<text>") != -1
MC/DC50%1/2
index($regex, "(?<category>") != -1 and index($regex, "(?<text>") != -1
index($regex, "(?<category>") != -1
index($regex, "(?<text>") != -1
Truth table66%2/3
A and B
A= index($regex, "(?<category>") != -1B= index($regex, "(?<text>") != -1
| |||||||||||||||||||||||||||||
| 153 | or die "mark regex needs (?<category>...) and (?<text>...) named captures.\n"; | ||||||||||||||||||||||||||||
| 154 | 19 | my(%cat, @rules); | |||||||||||||||||||||||||||
| 155 | 18 | ▶ | while ($text =~ /$regex/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 17 | my($cat, $str) = ($+{category}, $+{text}); | |||||||||||||||||||||||||||
| 157 | 2 | _check_category($cat); | |||||||||||||||||||||||||||
| 158 | 2 | ▶ | if (exists $cat{$str}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 4 | ▶ | $cat{$str} eq $cat | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | or die "\"$str\": conflicting categories ($cat{$str} vs $cat).\n"; | ||||||||||||||||||||||||||||
| 161 | 3 | next; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 3 | $cat{$str} = $cat; | |||||||||||||||||||||||||||
| 164 | 3 | push @rules, [ $cat, quotemeta($str) ]; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 1 | \@rules; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub _all_rules { | ||||||||||||||||||||||||||||
| 170 | 2 | ▶ | my $obj = shift; | ||||||||||||||||||||||||||
|
Subroutine
_all_rules: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 171 | 11 | (@{$obj->{RULES}}, @{$obj->{FILERULES}}); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub _stable_tag { | ||||||||||||||||||||||||||||
| 175 | 4 | ▶ | my($obj, $tag, $matched) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_stable_tag: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 176 | 4 | my $key = "$tag\0$matched"; | |||||||||||||||||||||||||||
| 177 | 7 | ▶ | $obj->{ASSIGNED}{$key} //= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$obj->{"ASSIGNED"}{$key} //= do {
my $t = sprintf("<%s %s="%d" />", $tag, $obj->{"INDEX"}, ++$obj->{"COUNTER"}{$tag});
$obj->{"ORIGIN"}{$t} = $matched;
push @{$$obj{"ASSIGN_ORDER"};}, $t;
$t
}
MC/DC0%0/2
$obj->{"ASSIGNED"}{$key} //= do {
my $t = sprintf("<%s %s="%d" />", $tag, $obj->{"INDEX"}, ++$obj->{"COUNTER"}{$tag});
$obj->{"ORIGIN"}{$t} = $matched;
push @{$$obj{"ASSIGN_ORDER"};}, $t;
$t
}
$obj->{"ASSIGNED"}{$key}
do {
my $t = sprintf("<%s %s="%d" />", $tag, $obj->{"INDEX"}, ++$obj->{"COUNTER"}{$tag});
$obj->{"ORIGIN"}{$t} = $matched;
push @{$$obj{"ASSIGN_ORDER"};}, $t;
$t
}
Truth table66%2/3
A //= B
A= $obj->{"ASSIGNED"}{$key}B= do {
my $t = sprintf("<%s %s="%d" />", $tag, $obj->{"INDEX"}, ++$obj->{"COUNTER"}{$tag});
$obj->{"ORIGIN"}{$t} = $matched;
push @{$$obj{"ASSIGN_ORDER"};}, $t;
$t
}
| |||||||||||||||||||||||||||||
| 178 | my $t = sprintf('<%s %s="%d" />', | ||||||||||||||||||||||||||||
| 179 | 7 | $tag, $obj->{INDEX}, ++$obj->{COUNTER}{$tag}); | |||||||||||||||||||||||||||
| 180 | 6 | $obj->{ORIGIN}{$t} = $matched; | |||||||||||||||||||||||||||
| 181 | 2 | push @{$obj->{ASSIGN_ORDER}}, $t; | |||||||||||||||||||||||||||
| 182 | 4 | $t; | |||||||||||||||||||||||||||
| 183 | }; | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | sub _mask_stable { | ||||||||||||||||||||||||||||
| 187 | 4 | ▶ | my($obj, $track) = splice @_, 0, 2; | ||||||||||||||||||||||||||
|
Subroutine
_mask_stable: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 188 | 2 | for (@_) { | |||||||||||||||||||||||||||
| 189 | 55 | for my $rule ($obj->_all_rules) { | |||||||||||||||||||||||||||
| 190 | 55 | my($tag, $pat) = @$rule; | |||||||||||||||||||||||||||
| 191 | 55 | s{$pat}{ | |||||||||||||||||||||||||||
| 192 | 55 | my $t = $obj->_stable_tag($tag, ${^MATCH}); | |||||||||||||||||||||||||||
| 193 | 51 | ▶ | $obj->{TRACK}{$t}++ if $track; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 51 | $t; | |||||||||||||||||||||||||||
| 195 | }gpe; | ||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 51 | return $obj; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | sub mask { | ||||||||||||||||||||||||||||
| 202 | 32 | ▶ | my $obj = shift; | ||||||||||||||||||||||||||
|
Subroutine
mask: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 203 | 32 | ▶ | if ($obj->{STABLE}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 32 | return $obj->_mask_stable(1, @_); | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 32 | ▶ | my $pattern = $obj->{PATTERN} // die; | ||||||||||||||||||||||||||
|
Condition50%1/2
$obj->{"PATTERN"} // die()
MC/DC0%0/1
$obj->{"PATTERN"} // die()
$obj->{"PATTERN"}
Truth table50%1/2
A
A= $obj->{"PATTERN"}
| |||||||||||||||||||||||||||||
| 207 | 32 | ▶ | my @patterns = ref $pattern ? @$pattern : $pattern; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 37 | my $fromto = $obj->{TABLE}; | |||||||||||||||||||||||||||
| 209 | # edit parameters in place | ||||||||||||||||||||||||||||
| 210 | 37 | for (@_) { | |||||||||||||||||||||||||||
| 211 | 55 | for my $pat (@patterns) { | |||||||||||||||||||||||||||
| 212 | 148 | ▶ | next if $pat =~ /^\s*(#|$)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 148 | s{$pat}{ | |||||||||||||||||||||||||||
| 214 | my $tag = sprintf('<%s %s="%d" />', | ||||||||||||||||||||||||||||
| 215 | 51 | $obj->{TAG}, $obj->{INDEX}, ++$obj->{NUMBER}); | |||||||||||||||||||||||||||
| 216 | 51 | push @$fromto, [ $tag, ${^MATCH} ]; | |||||||||||||||||||||||||||
| 217 | 51 | $tag; | |||||||||||||||||||||||||||
| 218 | }gpe; | ||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 37 | return $obj; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | sub mask_reference { | ||||||||||||||||||||||||||||
| 225 | 26 | ▶ | my $obj = shift; | ||||||||||||||||||||||||||
|
Subroutine
mask_reference: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 226 | 26 | ▶ | $obj->{STABLE} or die "mask_reference requires STABLE mode.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 24 | $obj->_mask_stable(0, @_); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | sub unmask { | ||||||||||||||||||||||||||||
| 231 | 2 | ▶ | my $obj = shift; | ||||||||||||||||||||||||||
|
Subroutine
unmask: called CC=14 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 232 | 2 | ▶ | if ($obj->{STABLE}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | # Verify the complete placeholder multiset before restoring anything. | ||||||||||||||||||||||||||||
| 234 | # ASSIGN_ORDER also includes reference-only tags; those must not leak | ||||||||||||||||||||||||||||
| 235 | # into the response unless the same stable tag occurs in the payload. | ||||||||||||||||||||||||||||
| 236 | 2 | for my $t (@{$obj->{ASSIGN_ORDER}}) { | |||||||||||||||||||||||||||
| 237 | 2 | ▶ | my $want = $obj->{TRACK}{$t} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$obj->{"TRACK"}{$t} // 0
MC/DC100%1/1
$obj->{"TRACK"}{$t} // 0
$obj->{"TRACK"}{$t}
Truth table100%2/2
A
A= $obj->{"TRACK"}{$t}
| |||||||||||||||||||||||||||||
| 238 | 2 | my $got = 0; | |||||||||||||||||||||||||||
| 239 | 2 | $got += () = /\Q$t\E/g for @_; | |||||||||||||||||||||||||||
| 240 | 3 | ▶ | $got == $want or die sprintf( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | "Masking error: \"%s\" count mismatch (expected %d, got %d).\n", | ||||||||||||||||||||||||||||
| 242 | $t, $want, $got); | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | 3 | for (@_) { | |||||||||||||||||||||||||||
| 245 | # Restore in REVERSE assignment order: the escape rule runs | ||||||||||||||||||||||||||||
| 246 | # first, so its tags are assigned first and must be restored | ||||||||||||||||||||||||||||
| 247 | # last -- an escaped literal may itself look like one of our | ||||||||||||||||||||||||||||
| 248 | # later tags, and restoring it earlier would let a later | ||||||||||||||||||||||||||||
| 249 | # substitution corrupt it. | ||||||||||||||||||||||||||||
| 250 | 3 | for my $t (reverse @{$obj->{ASSIGN_ORDER}}) { | |||||||||||||||||||||||||||
| 251 | 13 | my $orig = $obj->{ORIGIN}{$t}; | |||||||||||||||||||||||||||
| 252 | 13 | s/\Q$t/$orig/g; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | 13 | return $obj; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | 24 | my @tags = map $_->[0], @{$obj->{TABLE}}; | |||||||||||||||||||||||||||
| 258 | 22 | for my $t (@tags) { | |||||||||||||||||||||||||||
| 259 | 22 | my $got = 0; | |||||||||||||||||||||||||||
| 260 | 30 | $got += () = /\Q$t\E/g for @_; | |||||||||||||||||||||||||||
| 261 | 30 | ▶ | $got == 1 or die sprintf( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | "Masking error: \"%s\" count mismatch (expected 1, got %d).\n", | ||||||||||||||||||||||||||||
| 263 | $t, $got); | ||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | # edit parameters in place | ||||||||||||||||||||||||||||
| 266 | 30 | for (@_) { | |||||||||||||||||||||||||||
| 267 | 30 | for my $fromto (reverse @{$obj->{TABLE}}) { | |||||||||||||||||||||||||||
| 268 | 28 | my($from, $to) = @$fromto; | |||||||||||||||||||||||||||
| 269 | 28 | s/\Q$from/$to/g; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | 41 | ▶ | $obj->reset if $obj->{AUTORESET}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 41 | return $obj; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 1; | ||||||||||||||||||||||||||||