blib/lib/Convert/GeekCode.pm

filter: Statementstmt 70.5% 72 / 102 Branchbran 48.0% 25 / 52 Conditioncond 66.6% 8 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 8 totaltotal 60.8% 118 / 194 CC 36 SCAR 26.5
CC
36
Cov
63%
encode
42.0
calcv
30.0
decode
12.5
CRAP 14.1
↑ Summary
1 package Convert::GeekCode;
2 1 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw/@ISA @EXPORT $VERSION $DELIMITER/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 $Convert::GeekCode::VERSION = '0.63';
7  
8 1 ▶use YAML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =encoding utf8
12  
13 =head1 NAME
14  
15 Convert::GeekCode - Convert and generate geek code sequences
16  
17 =head1 SYNOPSIS
18  
19 use Convert::GeekCode; # exports geek_decode()
20  
21 my @out = geek_decode(q(
22 -----BEGIN GEEK CODE BLOCK-----
23 Version: 3.12
24 GB/C/CM/CS/CC/ED/H/IT/L/M/MU/P/SS/TW/AT d---x s+: a-- C++++ UB++++$
25 P++++$ L+ E--->+ W+++$ N++ !o K w--(++) O-- M-@ !V PS+++ PE Y+>++
26 PGP++ t+ 5? X+ R+++ !tv b++++ DI+++@ D++ G++++ e-(--) h* r++(+) z++*
27 ------END GEEK CODE BLOCK------
28 )); # yes, that's the author's geek code
29  
30 my ($key, $val);
31 print "[$key]\n$val\n\n" while (($key, $val) = splice(@out, 0, 2));
32  
33 =head1 DESCRIPTION
34  
35 B<Convert::GeekCode> converts and generates Geek Code sequences (cf.
36 L<http://geekcode.com/>). It supports different langugage codes and
37 user-customizable codesets.
38  
39 Since version 0.5, this module uses B<YAML> to represent the geek code
40 tables, for greater readability and ease of deserialization. Please
41 refer to L<http://www.yaml.org/> for more related information.
42  
43 The F<geekgen> and F<geekdec> utilities are installed by default,
44 and may be used to generate / decode geek code blocks, respectively.
45  
46 =cut
47  
48 1 @ISA = qw/Exporter/;
49 1 @EXPORT = qw/geek_encode geek_decode/;
50 1 $DELIMITER = " ";
51  
52 1 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod undocumented
53 1 my $class = shift;
54 1 ▶ my $id = shift || 'geekcode';
Condition50%1/2
shift() || 'geekcode'
l!l
10
MC/DC0%0/1
shift() || 'geekcode'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
55 1 ▶ my $version = shift || '3.12';
Condition50%1/2
shift() || '3.12'
l!l
10
MC/DC0%0/1
shift() || '3.12'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
56 1 ▶ my $lang = shift || 'en_us';
Condition50%1/2
shift() || 'en_us'
l!l
10
MC/DC0%0/1
shift() || 'en_us'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
57 1 my ( $cursec, $curcode, $curval );
58  
59 1 $lang =~ tr/-/_/; # paranoia
60  
61 1 ▶ my $file = locate("$id/$version/$lang.yml")
Branch: 1 branch
BranchTrueFalseTotal
my $file = locate("$id/$version/$lang.yml") or die "cannot locate $id/$version/$lang.yml in @INC" 1 0 1
62 or die "cannot locate $id/$version/$lang.yml in @INC";
63  
64 1 my $self = YAML::LoadFile($file);
65 1 return bless( $self, $class );
66 }
67  
68 1 ▶sub decode {
Subroutine decode: called CC=12 SCAR=25.3
Pod undocumented
69 1 my ( $self, $code ) = @_;
70  
71 1 ▶ die "can't find geek code block; stop."
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /\Q$self->{"Head"}\E([\x00-\xff]+)\Q$self->{"Tail"}\E/ 0 1 1
72 unless $code =~ m|\Q$self->{Head}\E([\x00-\xff]+)\Q$self->{Tail}\E|;
73  
74 1 $code = $1;
75 1 $code =~ s|^\s+||mg; # Strip leading spaces
76 1 ▶ $code =~ s|[\x00-\xff]*?^$self->{Begin}|_|m or die "Sorry, could not decode your Geekcode. Please check if it has the right syntax: http://geekcode.com/\n$code\n";
Branch: 1 branch
BranchTrueFalseTotal
$code =~ s/[\x00-\xff]*?^$self->{"Begin"}/_/m or die "Sorry, could not decode your Geekcode. Please check if it has the right syntax: http://geekcode.com/\n$code\n" 1 0 1
77  
78 1 my @ret;
79  
80 1 foreach my $chunk ( split( /[\s\t\n\r]+/, $code ) ) {
81 34 ▶ next unless $chunk =~ m|^(\!?\w+)\b|;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk =~ /^(\!?\w+)\b/ 0 34 34
82  
83 34 my $head = $1;
84 34 ▶ while ($head) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head) 38 1 39
85 38 ▶ if ( exists( $self->{_}{$head} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_"}{$head}) 33 5 38
86 33 my $sec = $self->{_}{$head};
87 33 my $out;
88  
89 33 push @ret, $sec->{_};
90 33 $chunk = substr( $chunk, length($head) );
91 33 ▶ $out = $sec->{''} . $DELIMITER
Branch: 1 branch
BranchTrueFalseTotal
if not $chunk or $chunk =~ /^[\>\(]/ 2 31 33
Condition66%2/3
not $chunk or $chunk =~ /^[\>\(]/
l!l&&r!l&&!r
2031
MC/DC50%1/2
not $chunk or $chunk =~ /^[\>\(]/
not $chunk $chunk =~ /^[\>\(]/
Truth table66%2/3
not(A) or B
A= not $chunkB= $chunk =~ /^[\>\(]/
ABresult
1X1
011
000
92 if !$chunk
93 or $chunk =~ /^[\>\(]/;
94  
95 33 ▶ while ($chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk) 68 33 101
96 68 ▶ next if $self->tokenize( $sec, \$chunk, \$out );
Branch: 1 branch
BranchTrueFalseTotal
if $self->tokenize($sec, \$chunk, \$out) 47 21 68
97 21 ▶ next if $self->tokenize( $self->{_}{''}, \$chunk, \$out );
Branch: 1 branch
BranchTrueFalseTotal
if $self->tokenize($self->{"_"}{""}, \$chunk, \$out) 21 0 21
98  
99 0 warn "parse error: ", substr( $chunk, 0, 1 );
100 0 $chunk = substr( $chunk, 1 );
101 }
102  
103 33 push @ret, $out;
104 33 last;
105 }
106  
107 5 $head = substr( $head, 0, -1 );
108 }
109 }
110  
111 1 return @ret;
112 }
113  
114 0 ▶sub encode {
Subroutine encode: not called CC=6 SCAR=37.4
Pod undocumented
115 0 my ( $self, $code ) = @_;
116  
117 0 my @out;
118 0 foreach my $sec ( split( /[\s\t\n\r]+/, $self->{Sequence} ) ) {
119 0 ▶ my $secref = $self->{_}{$sec} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $secref = $self->{"_"}{$sec} or next 0 0 0
120 0 ▶ $sec = $self->{Begin} if $sec eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $sec eq "_" 0 0 0
121 push @out, $code->(
122 $secref->{_},
123 map {
124 0 my $sym = $secref->{$_};
125 0 my $code = $_;
126 0 $code =~ s/[\x27\/]//g;
127 (
128 (
129 0 ▶ ( index( $code, $sec ) > -1 )
Branch: 2 branches
BranchTrueFalseTotal
(index($code, $sec) != -1) ? : 0 0 0
$code eq '!' ? : 0 0 0
130 ? $code
131 : ( $code eq '!' ? "$code$sec" : "$sec$code" )
132 ),
133 $sym
134 );
135 0 } grep { $_ ne '_' }
136 0 sort { calcv($a) cmp calcv($b); } keys( %{$secref} )
137 );
138  
139 0 $out[-1] =~ s|\s+|/|g;
140 0 $out[-1] =~ s|/+$||;
141 0 $out[-1] =~ s|(?<=.)$sec||g;
142 }
143  
144 return join( "\n",
145 $self->{Head},
146 $self->{Ver} . $self->{Version},
147 join( ' ', @out ),
148 0 $self->{Tail}, '', );
149 }
150  
151 0 ▶sub calcv {
Subroutine calcv: not called CC=5 SCAR=34.0
Pod undocumented
152 0 ▶ my $sym = shift or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $sym = shift() or return "" 0 0 0
153  
154 0 ▶ return chr(0) x ( 10 - length($sym) ) if substr( $sym, 0, 1 ) eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if substr($sym, 0, 1) eq "+" 0 0 0
155 0 ▶ return chr(2) x length($sym) if substr( $sym, 0, 1 ) eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if substr($sym, 0, 1) eq "-" 0 0 0
156 0 ▶ return chr(1) if $sym eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $sym eq "" 0 0 0
157 0 return $sym;
158 }
159  
160 89 ▶sub tokenize {
Subroutine tokenize: called CC=7 SCAR=20.6
Pod undocumented
161 89 my ( $self, $sec, $chunk, $out ) = @_;
162  
163 89 foreach my $key ( sort { length($b) <=> length($a) } keys( %{$sec} ) ) {
164 926 ▶ next if $key eq '_' or !$key;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "_" or not $key 41 885 926
Condition100%3/3
$key eq "_" or not $key
l!l&&r!l&&!r
347885
MC/DC100%2/2
$key eq "_" or not $key
$key eq "_" not $key
Truth table100%3/3
A or not(B)
A= $key eq "_"B= not $key
ABresult
1X1
011
000
165  
166 885 ▶ if ( $key =~ m|/(.+)/| ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ m[/(.+)/]) { } 0 885 885
elsif ($$chunk =~ s/^\Q$key\E//) { } 68 817 885
167 0 ▶ if ( $$chunk =~ s|^$1|| ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$chunk =~ s/^$1//) 0 0 0
168 0 $$out .= $sec->{$key} . $DELIMITER;
169 0 return 1;
170 }
171 }
172 elsif ( $$chunk =~ s/^\Q$key\E// ) {
173 68 $$out .= $sec->{$key} . $DELIMITER;
174 68 return 1;
175 }
176 }
177  
178 21 return;
179 }
180  
181 1 ▶sub locate {
Subroutine locate: called CC=6 SCAR=18.2
Pod undocumented
182 1 my $path = (caller)[0];
183 1 my $file = $_[0];
184  
185 1 $path =~ s|::|/|g;
186 1 $path =~ s|\w+\$||;
187  
188 1 ▶ unless ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 1 0 1
189 1 foreach my $inc (@INC) {
190 2 ▶ last if -e ( $file = join( '/', $inc, $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if -e ($file = join("/", $inc, $_[0])) 0 2 2
191 2 ▶ last if -e ( $file = join( '/', $inc, $path, $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if -e ($file = join("/", $inc, $path, $_[0])) 1 1 2
192 }
193 }
194  
195 1 ▶ return -e $file ? $file : undef;
Branch: 1 branch
BranchTrueFalseTotal
-e $file ? : 1 0 1
196 }
197  
198 1 ▶sub geek_decode {
Subroutine geek_decode: called CC=1 SCAR=0.0
Pod undocumented
199 1 my $code = shift;
200 1 my $obj = __PACKAGE__->new(@_); # XXX should auto-detect version
201  
202 1 return $obj->decode($code);
203 }
204  
205 0 ▶sub geek_encode {
Subroutine geek_encode: not called CC=1 SCAR=6.9
Pod undocumented
206 0 my $code = shift;
207 0 my $obj = __PACKAGE__->new(@_); # XXX should auto-detect version
208  
209 0 return $obj->encode($code);
210 }
211  
212 1 1;
213  
214 __END__
↑ Summary