| 1 | package Sort::Key::Types; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | our $VERSION = '1.30'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | require Exporter; | |||||||||||||||||||||||||||
| 10 | 5 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 11 | 5 | our @EXPORT_OK = qw(register_type); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | our $DEBUG; | |||||||||||||||||||||||||||
| 14 | 5 | ▶ | $DEBUG ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$Sort::Key::Types::DEBUG ||= 0
MC/DC0%0/1
$Sort::Key::Types::DEBUG ||= 0
$Sort::Key::Types::DEBUG
Truth table50%1/2
A
A= $Sort::Key::Types::DEBUG
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # this hash is also used from Sort::Key::Multi to find out which | ||||||||||||||||||||||||||||
| 17 | # letters can be used as types: | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | our %mktypes = ( s => 0, | |||||||||||||||||||||||||||
| 20 | l => 1, | ||||||||||||||||||||||||||||
| 21 | n => 2, | ||||||||||||||||||||||||||||
| 22 | i => 3, | ||||||||||||||||||||||||||||
| 23 | u => 4 ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 33 | ▶ | sub _mks2n { | ||||||||||||||||||||||||||
|
Subroutine
_mks2n: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 26 | 33 | ▶ | if (my ($rev, $key)=$_[0]=~/^([-+]?)(.)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 33 | ▶ | exists $mktypes{$key} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | or croak "invalid multi-key type '$_[0]'"; | ||||||||||||||||||||||||||||
| 29 | 33 | my $n = $mktypes{$key}; | |||||||||||||||||||||||||||
| 30 | 33 | ▶ | $n+=128 if $rev eq '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 33 | return $n | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 0 | die "internal error, bad key '$_[0]'"; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 5 | our %mkmap = qw(str s | |||||||||||||||||||||||||||
| 37 | string s | ||||||||||||||||||||||||||||
| 38 | locale l | ||||||||||||||||||||||||||||
| 39 | loc l | ||||||||||||||||||||||||||||
| 40 | lstr l | ||||||||||||||||||||||||||||
| 41 | int i | ||||||||||||||||||||||||||||
| 42 | integer i | ||||||||||||||||||||||||||||
| 43 | uint u | ||||||||||||||||||||||||||||
| 44 | unsigned_integer u | ||||||||||||||||||||||||||||
| 45 | number n | ||||||||||||||||||||||||||||
| 46 | num n); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 5 | $_ = [$_] for (values %mkmap); | |||||||||||||||||||||||||||
| 49 | 5 | our %mksub = map { $_ => undef } keys %mkmap; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 50 | ▶ | sub _get_map { | ||||||||||||||||||||||||||
|
Subroutine
_get_map: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 52 | 50 | my ($rev, $name) = $_[0]=~/^([+-]?)(.*)$/; | |||||||||||||||||||||||||||
| 53 | 50 | ▶ | exists $mkmap{$name} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | or croak "unknown key type '$name'\n"; | ||||||||||||||||||||||||||||
| 55 | 50 | ▶ | if ($rev eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 15 | ▶ | return map { /^-(.*)$/ ? $1 : "-$_" } @{$mkmap{$name}} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 35 | @{$mkmap{$name}} | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 36 | ▶ | sub _get_sub { | ||||||||||||||||||||||||||
|
Subroutine
_get_sub: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 62 | 36 | $_[0]=~/^[+-]?(.*)$/; | |||||||||||||||||||||||||||
| 63 | 36 | ▶ | exists $mksub{$1} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | or croak "unknown key type '$1'\n"; | ||||||||||||||||||||||||||||
| 65 | 36 | return $mksub{$1} | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 44 | ▶ | sub _combine_map { map { _get_map $_ } @_ } | ||||||||||||||||||||||||||
|
Subroutine
_combine_map: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | ▶ | use constant _nl => "\n"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 24 | ▶ | sub combine_types { pack('C*', (map { _mks2n $_ } _combine_map(@_))) } | ||||||||||||||||||||||||||
|
Subroutine
combine_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 31 | ▶ | sub combine_sub { | ||||||||||||||||||||||||||
|
Subroutine
combine_sub: called CC=18 SCAR=30.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 31 | my $sub = shift; | |||||||||||||||||||||||||||
| 76 | 31 | my $for = shift; | |||||||||||||||||||||||||||
| 77 | 31 | ▶ | $for = defined $for ? " for $for" : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 31 | my @subs = map { _get_sub $_ } @_; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 31 | ▶ | if ($sub) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 31 | my $code = 'sub { '._nl; | |||||||||||||||||||||||||||
| 83 | 31 | ▶ | if (ref $sub eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 19 | ▶ | unless (grep { defined $_ } @subs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 16 | return $sub | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 3 | $code.= 'my @keys = &{$sub};'._nl; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||
| 90 | 12 | ▶ | if ($sub eq '@_') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 12 | ▶ | return undef unless grep {defined $_} @subs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 10 | $code.= 'my @keys = '.$sub.';'._nl; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 13 | ▶ | $code.= 'print "in: |@keys|\n";'._nl if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 13 | $code.= '@keys == '.scalar(@_) | |||||||||||||||||||||||||||
| 98 | . ' or croak "wrong number of keys generated$for ' | ||||||||||||||||||||||||||||
| 99 | . '(expected '.scalar(@_).', returned ".scalar(@keys).")";'._nl; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | { # new scope so @map doesn't get captured | ||||||||||||||||||||||||||||
| 102 | 13 | my @map = _combine_map @_; | |||||||||||||||||||||||||||
| 103 | 13 | ▶ | if (@map==@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 10 | for my $i (0..$#_) { | |||||||||||||||||||||||||||
| 105 | 10 | ▶ | if (defined $subs[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 10 | $code.= '{ local $_ = $keys['.$i.']; ($keys['.$i.']) = &{$subs['.$i.']}() }'._nl; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 10 | ▶ | $code.='print "out: |@keys|\n";'._nl if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 10 | $code.='return @keys'._nl; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||
| 113 | 3 | $code.='my @keys1;'._nl; | |||||||||||||||||||||||||||
| 114 | 3 | for my $i (0..$#_) { | |||||||||||||||||||||||||||
| 115 | 4 | ▶ | if (defined $subs[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 4 | $code.= '{ local $_ = shift @keys; push @keys1, &{$subs['.$i.']}() }'._nl; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||
| 119 | 0 | $code.= 'push @keys1, shift @keys;'._nl; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 3 | ▶ | $code.='print "out: |@keys1|\n";'._nl if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 3 | $code.='return @keys1'._nl; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 13 | $code.='}'._nl; | |||||||||||||||||||||||||||
| 127 | 13 | ▶ | print "CODE$for:\n$code----\n" if $DEBUG >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 13 | my $map = eval $code; | |||||||||||||||||||||||||||
| 129 | 13 | ▶ | $@ and die "internal error: code generation failed ($@)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 13 | return $map; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | @_==1 or croak "too many keys or keygen subroutine undefined$for"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | return @subs; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 7 | ▶ | sub register_type { | ||||||||||||||||||||||||||
|
Subroutine
register_type: called CC=5 SCAR=17.3
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 7 | my $name = shift; | |||||||||||||||||||||||||||
| 140 | 7 | my $sub = shift; | |||||||||||||||||||||||||||
| 141 | 7 | ▶ | $name=~/^\w+(?:::\w+)*$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | or croak "invalid type name '$name'"; | ||||||||||||||||||||||||||||
| 143 | 7 | ▶ | @_ or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | croak "too few keys"; | ||||||||||||||||||||||||||||
| 145 | 7 | ▶ | (exists $mkmap{$name} or exists $mktypes{$name}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $Sort::Key::Types::mkmap{$name} or exists $Sort::Key::Types::mktypes{$name}
MC/DC0%0/2
exists $Sort::Key::Types::mkmap{$name} or exists $Sort::Key::Types::mktypes{$name}
exists $Sort::Key::Types::mkmap{$name}
exists $Sort::Key::Types::mktypes{$name}
Truth table33%1/3
A or B
A= exists $Sort::Key::Types::mkmap{$name}B= exists $Sort::Key::Types::mktypes{$name}
| |||||||||||||||||||||||||||||
| 146 | and croak "type '$name' already registered or reserved in ".__PACKAGE__; | ||||||||||||||||||||||||||||
| 147 | 7 | $mkmap{$name} = [ _combine_map @_ ]; | |||||||||||||||||||||||||||
| 148 | 7 | $mksub{$name} = combine_sub $sub, $name, @_; | |||||||||||||||||||||||||||
| 149 | () | ||||||||||||||||||||||||||||
| 150 | 7 | } | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 5 | 1; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||||||||||