Sort.pm

filter: Statementstmt 91.5% 54 / 59 Branchbran 66.6% 8 / 12 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 7 totaltotal 76.2% 74 / 97 CC 10 SCAR 16.8
CC
10
Cov
86%
no_sort
9.4
no_xfrm
2.0
no_aa_xfrm
2.0
CRAP 5.4
« Dato.pm ↑ Summary
1 package No::Sort;
2  
3 1 require 5.002;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 require Exporter;
7 1 @ISA=qw(Exporter);
8 1 @EXPORT=qw(no_sort);
9 1 @EXPORT_OK=qw(no_xfrm no_aa_xfrm
10 latin1_uc latin1_lc latin1_ucfirst latin1_lcfirst);
11  
12 1 $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
13  
14  
15 =head1 NAME
16  
17 No::Sort - Norwegian sorting
18  
19 =head1 SYNOPSIS
20  
21 use No::Sort;
22 @sortert = no_sort @norske_ord;
23  
24 =head1 DESCRIPTION
25  
26 This module provde the function no_sort() which sort a ISO-8859/1
27 encoded string according to Norwegian practice. The routine works
28 like the normal perl sort routine, but the optional first argument is
29 special. It can either be a reference to the strxfrm() function to
30 use while sorting or a reference to a hash used to transform the words
31 while sorting.
32  
33 You can also import the no_xfrm() function which is used for standard
34 sorting. It can be useful to base your custom transformation function
35 on it. If we for instance would like to sort "Aa" as "Å" we could
36 implement it like this:
37  
38 use No::Sort qw(no_sort no_xfrm);
39 sub my_xfrm {
40 my $word = shift;
41 $word =~ s/A[aA]/Å/g;
42 $word =~ s/aa/å/g;
43 no_xfrm($word);
44 }
45 @sorted = no_sort \&my_xfrm, @names;
46  
47 By the way, the my_xfrm shown in this example can be imported from
48 this module under the name 'no_aa_xfrm':
49  
50 use No::Sort qw(no_sort no_aa_xfrm);
51 @sorted = no_sort \&no_aa_xfrm, @names;
52  
53 If you set the $No::Sort::DEBUG variable to a TRUE value, then we will
54 make some extra noise on STDOUT while sorting.
55  
56 The module can also export functions for up/down casing ISO-8859/1
57 strings. These functions are called latin1_uc(), latin1_lc(),
58 latin1_ucfirst(), latin1_lcfirst().
59  
60 =head1 SEE ALSO
61  
62 L<perllocale>
63  
64 =head1 AUTHORS
65  
66 Hallvard B Furuseth <h.b.furuseth@usit.uio.no>, Gisle Aas <gisle@aas.no>
67  
68 =cut
69  
70 3 ▶sub no_sort {
Subroutine no_sort: called CC=9 SCAR=22.4
Pod undocumented
71 3 my $xfrm; # ref to sort hash
72 3 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 1 2 3
73 1 ▶ if (ref($_[0]) eq "CODE") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'CODE') { } 1 0 1
elsif (ref $_[0] eq 'HASH') { } 0 0 0
74 1 my $code = shift;
75 1 @{$xfrm}{@_} = map &$code($_), @_;
76 } elsif (ref($_[0]) eq "HASH") {
77 0 $xfrm = shift;
78 }
79 }
80 3 ▶ @{$xfrm}{@_} = map no_xfrm($_), @_ unless $xfrm;
Branch: 1 branch
BranchTrueFalseTotal
unless $xfrm 2 1 3
81  
82 3 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($No::Sort::DEBUG) 2 1 3
83 2 ▶ my @s = sort { $xfrm->{$a} cmp $xfrm->{$b} || $a cmp $b } @_;
Condition66%2/3
$xfrm->{$a} cmp $xfrm->{$b} || $a cmp $b
l!l&&r!l&&!r
10330
MC/DC0%0/2
$xfrm->{$a} cmp $xfrm->{$b} || $a cmp $b
$xfrm->{$a} cmp $xfrm->{$b} $a cmp $b
Truth table66%2/3
A || B
A= $xfrm->{$a} cmp $xfrm->{$b}B= $a cmp $b
ABresult
1X1
011
000
84 2 printf STDERR "%-20s %s\n", "ORD", "SORTERES SOM";
85 2 print STDERR "-" x 20, " ", "-" x 40, "\n";
86 2 for (@s) {
87 34 printf STDERR "%-20s %s\n", $_, $xfrm->{$_};
88 }
89 2 return @s;
90 }
91  
92 1 ▶ sort { $xfrm->{$a} cmp $xfrm->{$b} || $a cmp $b } @_;
Condition66%2/3
$xfrm->{$a} cmp $xfrm->{$b} || $a cmp $b
l!l&&r!l&&!r
8230
MC/DC0%0/2
$xfrm->{$a} cmp $xfrm->{$b} || $a cmp $b
$xfrm->{$a} cmp $xfrm->{$b} $a cmp $b
Truth table66%2/3
A || B
A= $xfrm->{$a} cmp $xfrm->{$b}B= $a cmp $b
ABresult
1X1
011
000
93 }
94  
95 59 ▶sub no_xfrm {
Subroutine no_xfrm: called CC=2 SCAR=6.9
Pod undocumented
96 59 my $p1 = shift;
97  
98 # Ikke-alfanumeriske tegn regnes som en enkelt blank
99 # (eller sikkert litt mer komplisert, f.eks whitespace -> blank,
100 # punktum o.l -> et annet "lite" tegn, med blanke fjernet på begge
101 # sider, osv...
102 59 $p1 =~ tr/\0-\040\177\200-\240/ /s;
103 59 ▶ $p1 =~ tr/ 0-9_A-Za-zÀ-ÖØ-ßà-öø-ÿ/,/cs
Branch: 1 branch
BranchTrueFalseTotal
$p1 =~ tr/ 0-9A-Z_a-z\300-\326\330-\366\370-\377/,/cs and $p1 =~ s/,[ ,]+/,/g 0 59 59
104 and $p1 =~ s/,[ ,]+/,/g;
105  
106 # Plasser æøå i riktig rekkefølge. Tar med svensk äø også.
107 # (Egentlig burde *alle* tegn transformeres slik at ting kommer i
108 # riktig rekkefølge her, men da blir resten av programmet så uleselig...)
109 59 $p1 =~ tr[æäøöåÆÄØÖÅ]
110 [ååææøÅÅÆÆØ];
111  
112 # Aksenter telles bare hvis uaksentede tegn er like
113 59 my $p2 = $p1;
114 59 $p2 =~ tr[ÀÁÂÃÄÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝÞßàáâãäçèéêëìíîïðñòóôõöùúûüýþÿ]
115 [AAAAÆCEEEEIIIIDNOOOOØUUUUYTSaaaaæceeeeiiiidnooooøuuuuyty];
116  
117 # Store & små bokstaver er bare forskjellig hvis alt annet er likt
118 59 my $p3 = $p2;
119 59 $p3 =~ tr[A-ZÆØÅ]
120 [a-zæøå];
121  
122 59 join("\1", $p3, $p2, $p1);
123 }
124  
125 0 ▶sub no_aa_xfrm {
Subroutine no_aa_xfrm: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my $word = shift;
127 0 $word =~ s/A[aA]/Å/g;
128 0 $word =~ s/aa/å/g;
129 0 no_xfrm($word);
130 }
131  
132 # Some additional case convertion routines that does not really have
133 # much to do with sorting.
134  
135 sub latin1_lc
136 2 ▶{
Subroutine latin1_lc: called CC=1 SCAR=0.0
Pod undocumented
137 2 my $str = shift;
138 2 $str =~ tr[A-ZÀ-ÖØ-Þ]
139 [a-zà-öø-þ];
140 2 $str;
141 }
142  
143 sub latin1_uc
144 2 ▶{
Subroutine latin1_uc: called CC=1 SCAR=0.0
Pod undocumented
145 2 my $str = shift;
146 2 $str =~ tr[a-zà-öø-þ]
147 [A-ZÀ-ÖØ-Þ];
148 2 $str;
149 }
150  
151 sub latin1_ucfirst
152 1 ▶{
Subroutine latin1_ucfirst: called CC=1 SCAR=0.0
Pod undocumented
153 1 my $str = shift;
154 1 $str =~ s/(.)/latin1_uc($1)/es;
155 1 $str;
156 }
157  
158 sub latin1_lcfirst
159 1 ▶{
Subroutine latin1_lcfirst: called CC=1 SCAR=0.0
Pod undocumented
160 1 my $str = shift;
161 1 $str =~ s/(.)/latin1_lc($1)/es;
162 1 $str;
163 }
164  
165 1 1;
« Dato.pm ↑ Summary