Toolkit/Case.pm

filter: Statementstmt 89.4% 51 / 57 Branchbran 78.5% 11 / 14 Conditioncond 90.0% 9 / 10 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 89.1% 90 / 101 CC 11 SCAR 14.1
CC
11
Cov
88%
lc_irc
6.2
uc_irc
6.1
BEGIN
1.0
CRAP 4.1
« Toolkit/Parser.pm ↑ Summary Toolkit.pm »
1 package IRC::Toolkit::Case;
2 5 $IRC::Toolkit::Case::VERSION = '0.092002';
3 5 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp 'carp';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 our @EXPORT = qw/
9 lc_irc
10 uc_irc
11 eq_irc
12  
13 rfc1459
14  
15 irc_str
16 /;
17  
18  
19 5 ▶use Sub::Infix;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶*rfc1459 = &infix(sub { eq_irc( $_[0], $_[1], 'rfc1459' ) });
Subroutine __ANON__: called CC=1 SCAR=0.0
21  
22  
23 ## The prototypes are unfortunate, but I pulled these out of an old
24 ## and very large bot project ... and was too scared to remove them.
25  
26 6 ▶sub lc_irc ($;$) {
Subroutine lc_irc: called CC=6 SCAR=18.3
Pod documented
27 6 my ($string, $casemap) = @_;
28 6 ▶ $casemap = lc( $casemap || 'rfc1459' );
Condition100%2/2
$casemap || 'rfc1459'
l!l
51
MC/DC100%1/1
$casemap || 'rfc1459'
$casemap
Truth table100%2/2
A
A= $casemap
Aresult
11
00
29  
30 CASE: {
31 6 ▶ if ($casemap eq 'rfc1459') {
Branch: 1 branch
BranchTrueFalseTotal
if ($casemap eq "rfc1459") 1 5 6
32 1 $string =~ tr/A-Z[]\\~/a-z{}|^/;
33 last CASE
34 1 }
35  
36 5 ▶ if ($casemap eq 'strict-rfc1459' || $casemap eq 'strict') {
Branch: 1 branch
BranchTrueFalseTotal
if ($casemap eq "strict-rfc1459" or $casemap eq "strict") 1 4 5
Condition66%2/3
$casemap eq "strict-rfc1459" or $casemap eq "strict"
l!l&&r!l&&!r
104
MC/DC50%1/2
$casemap eq "strict-rfc1459" or $casemap eq "strict"
$casemap eq "strict-rfc1459" $casemap eq "strict"
Truth table66%2/3
A or B
A= $casemap eq "strict-rfc1459"B= $casemap eq "strict"
ABresult
1X1
011
000
37 1 $string =~ tr/A-Z[]\\/a-z{}|/;
38 last CASE
39 1 }
40  
41 4 ▶ if ($casemap eq 'ascii') {
Branch: 1 branch
BranchTrueFalseTotal
if ($casemap eq "ascii") 4 0 4
42 4 $string =~ tr/A-Z/a-z/;
43 last CASE
44 4 }
45  
46 0 carp "Unknown CASEMAP $casemap, defaulted to rfc1459";
47 0 $casemap = 'rfc1459';
48 redo CASE
49 0 }
50  
51 $string
52 6 }
53  
54 36 ▶sub uc_irc ($;$) {
Subroutine uc_irc: called CC=6 SCAR=18.1
Pod documented
55 36 my ($string, $casemap) = @_;
56 36 ▶ $casemap = lc( $casemap || 'rfc1459' );
Condition100%2/2
$casemap || 'rfc1459'
l!l
297
MC/DC100%1/1
$casemap || 'rfc1459'
$casemap
Truth table100%2/2
A
A= $casemap
Aresult
11
00
57  
58 CASE: {
59 36 ▶ if ($casemap eq 'rfc1459') {
Branch: 1 branch
BranchTrueFalseTotal
if ($casemap eq "rfc1459") 15 21 36
60 15 $string =~ tr/a-z{}|^/A-Z[]\\~/;
61 last CASE
62 15 }
63  
64 21 ▶ if ($casemap eq 'strict-rfc1459' || $casemap eq 'strict') {
Branch: 1 branch
BranchTrueFalseTotal
if ($casemap eq "strict-rfc1459" or $casemap eq "strict") 5 16 21
Condition100%3/3
$casemap eq "strict-rfc1459" or $casemap eq "strict"
l!l&&r!l&&!r
1416
MC/DC100%2/2
$casemap eq "strict-rfc1459" or $casemap eq "strict"
$casemap eq "strict-rfc1459" $casemap eq "strict"
Truth table100%3/3
A or B
A= $casemap eq "strict-rfc1459"B= $casemap eq "strict"
ABresult
1X1
011
000
65 5 $string =~ tr/a-z{}|/A-Z[]\\/;
66 last CASE
67 5 }
68  
69 16 ▶ if ($casemap eq 'ascii') {
Branch: 1 branch
BranchTrueFalseTotal
if ($casemap eq "ascii") 16 0 16
70 16 $string =~ tr/a-z/A-Z/;
71 last CASE
72 16 }
73  
74 0 carp "Unknown CASEMAP $casemap, defaulted to rfc1459";
75 0 $casemap = 'rfc1459';
76 redo CASE
77 0 }
78  
79 $string
80 36 }
81  
82 10 ▶sub eq_irc ($$;$) {
Subroutine eq_irc: called CC=1 SCAR=0.0
Pod documented
83 10 my ($first, $second, $casemap) = @_;
84 10 uc_irc($first, $casemap) eq uc_irc($second, $casemap);
85 }
86  
87 3 ▶sub irc_str {
Subroutine irc_str: called CC=1 SCAR=0.0
Pod documented
88 3 require IRC::Toolkit::Case::MappedString;
89 3 IRC::Toolkit::Case::MappedString->new(@_)
90 }
91  
92 5 ▶print
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 5 5
93 qq[<Gilded> Also, every now and then I talk about a game I've enjoyed],
94 qq[ and rofer doesn't have time for it, Capn v1.02 doesn't understand],
95 qq[ the human concept of fun/entertainment, c[_] only plays retro-games],
96 qq[ made in his native homeland of Moria and avenj would rather shoot],
97 qq[ rifles while vaping\n]
98 5 unless caller; 1;
99  
100 =pod
101  
102 =head1 NAME
103  
104 IRC::Toolkit::Case - IRC case-folding utilities
105  
106 =head1 SYNOPSIS
107  
108 use IRC::Toolkit::Case;
109  
110 my $lower = lc_irc( $string, 'rfc1459' );
111  
112 my $upper = uc_irc( $string, 'ascii' );
113  
114 if (eq_irc($first, $second, 'strict-rfc1459')) {
115 ...
116 }
117  
118 # Or use the '|rfc1459|' operator if using RFC1459 rules:
119 if ($first |rfc1459| $second) {
120  
121 }
122  
123 =head1 DESCRIPTION
124  
125 IRC case-folding utilities.
126  
127 IRC daemons typically announce their casemap in B<ISUPPORT> (via the
128 B<CASEMAPPING> directive). This should be one of C<rfc1459>,
129 C<strict-rfc1459>, or C<ascii>:
130  
131 'ascii' a-z --> A-Z
132 'rfc1459' a-z{}|^ --> A-Z[]\~ (default)
133 'strict-rfc1459' a-z{}| --> A-Z[]\
134  
135 If told to convert/compare an unknown casemap, these functions will warn and
136 default to RFC1459 rules.
137  
138 If you're building a class that tracks an IRC casemapping and manipulates
139 strings accordingly, you may also want to see L<IRC::Toolkit::Role::CaseMap>.
140  
141 =head2 rfc1459 operator
142  
143 The infix operator C<|rfc1459|> is provided as a convenience for string
144 comparison (using RFC1459 rules):
145  
146 if ($first |rfc1459| $second) { ... }
147 # Same as:
148 if (eq_irc($first, $second)) { ... }
149  
150 =head2 lc_irc
151  
152 Takes a string and an optional casemap.
153  
154 Returns the lowercased string.
155  
156 =head2 uc_irc
157  
158 Takes a string and an optional casemap.
159  
160 Returns the uppercased string.
161  
162 =head2 eq_irc
163  
164 Takes a pair of strings and an optional casemap.
165  
166 Returns boolean true if the strings are equal
167 (per the rules specified by the given casemap).
168  
169 =head2 irc_str
170  
171 my $str = irc_str( strict => 'Nick^[Abc]' );
172 if ( $str eq 'nick^{abc}' ) {
173 # true
174 }
175  
176 Takes a casemap and string; if only one argument is provided, it is taken to
177 be the string and a C<rfc1459> casemap is assumed.
178  
179 Produces overloaded objects (see L<IRC::Toolkit::Case::MappedString>) that can
180 be stringified or compared; string comparison operators use the specified
181 casemap.
182  
183 =head1 AUTHOR
184  
185 Jon Portnoy <avenj@cobaltirc.org>
186  
187 Inspired by L<IRC::Utils>, copyright Chris Williams, Hinrik et al
188  
189 Licensed under the same terms as Perl.
190  
191 =cut
« Toolkit/Parser.pm ↑ Summary Toolkit.pm »