blib/lib/Sort/Key/Natural.pm

filter: Statementstmt 77.6% 66 / 85 Branchbran 25.0% 4 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.4% 17 / 18 Podpod 100% 2 / 2 totaltotal 70.6% 89 / 126 CC 12 SCAR 28.3
CC
12
Cov
67%
mkkey_natural_with_floats
56.5
mkkey_natural
5.1
BEGIN
1.0
CRAP 17.0
↑ Summary blib/lib/Sort/Key/Types.pm »
1 package Sort::Key::Natural;
2  
3 1 our $VERSION = '0.04';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require Exporter;
9  
10 1 our @ISA = qw( Exporter );
11 1 our @EXPORT_OK = qw( natkeysort
12 natkeysort_inplace
13 rnatkeysort
14 rnatkeysort_inplace
15 mkkey_natural
16 natsort
17 rnatsort
18 natsort_inplace
19 rnatsort_inplace
20  
21 natwfkeysort
22 natwfkeysort_inplace
23 rnatwfkeysort
24 rnatwfkeysort_inplace
25 mkkey_natural_with_floats
26 natwfsort
27 rnatwfsort
28 natwfsort_inplace
29 rnatwfsort_inplace );
30  
31  
32 1 require locale;
33  
34 117 ▶sub mkkey_natural {
Subroutine mkkey_natural: called CC=5 SCAR=16.2
Pod documented
35 117 ▶ my $nat = @_ ? shift : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 117 117
36 117 my @parts = do {
37 117 ▶ if ((caller 0)[8] & $locale::hint_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if ((caller 0)[8] & $locale::hint_bits) { } 0 117 117
38 1 ▶ use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 0 $nat =~ /\d+|\p{IsAlpha}+/g;
40 }
41 else {
42 117 $nat =~ /\d+|\p{IsAlpha}+/g;
43 }
44 };
45 117 for (@parts) {
46 216 ▶ if (/^\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\d/) 81 135 216
47 81 s/^0+//;
48 81 my $len = length;
49 81 my $nines = int ($len / 9);
50 81 my $rest = $len - 9 * $nines;
51 81 $_ = ('9' x $nines) . $rest . $_;
52 }
53 }
54 117 return join("\0", @parts);
55 }
56  
57 1 ▶use Sort::Key::Register natural => \&mkkey_natural, 'string';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use Sort::Key::Register nat => \&mkkey_natural, 'string';
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 1 ▶use Sort::Key::Maker natkeysort => 'nat';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use Sort::Key::Maker rnatkeysort => '-nat';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use Sort::Key::Maker natsort => \&mkkey_natural, 'str';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Sort::Key::Maker rnatsort => \&mkkey_natural, '-str';
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 0 ▶sub mkkey_natural_with_floats {
Subroutine mkkey_natural_with_floats: not called CC=8 SCAR=40.3
Pod documented
66 0 ▶ my $nat = @_ ? shift : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
67 0 my @parts = do {
68 0 ▶ if ((caller 0)[8] & $locale::hint_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if ((caller 0)[8] & $locale::hint_bits) { } 0 0 0
69 1 ▶ use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 0 $nat =~ /[+\-]?\d+(?:\.\d*)?|\p{IsAlpha}+/g;
71 }
72 else {
73 0 $nat =~ /[+\-]?\d+(?:\.\d*)?|\p{IsAlpha}+/g;
74 }
75 };
76 0 for (@parts) {
77 0 ▶ if (my ($sign, $number, $dec) = /^([+-]?)(\d+)(?:\.(\d*))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = /^([+-]?)(\d+)(?:\.(\d*))?$/) 0 0 0
78 0 $number =~ s/^0+//;
79 0 ▶ $dec = '' unless defined $dec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dec 0 0 0
80 0 $dec =~ s/0+$//;
81 0 my $len = length $number;
82 0 my $nines = int ($len / 9);
83 0 my $rest = $len - 9 * $nines;
84 0 $_ = ('9' x $nines) . $rest . $number . $dec;
85 0 ▶ if ($sign eq '-' and $_ ne '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign eq "-" and $_ ne 0) 0 0 0
Condition0%0/3
$sign eq "-" and $_ ne 0
!ll&&!rl&&r
000
MC/DC0%0/2
$sign eq "-" and $_ ne 0
$sign eq "-" $_ ne 0
Truth table0%0/3
A and B
A= $sign eq "-"B= $_ ne 0
ABresult
0X0
100
111
86 0 tr/0123456789/9876543210/;
87 0 $_ = "-$_";
88 }
89 }
90 }
91 0 return join("\0", @parts);
92 }
93  
94 1 ▶use Sort::Key::Register natural_with_floats => \&mkkey_natural_with_floats, 'string';
Subroutine BEGIN: called CC=1 SCAR=0.0
95 1 ▶use Sort::Key::Register natwf => \&mkkey_natural_with_floats, 'string';
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 1 ▶use Sort::Key::Maker natwfkeysort => 'natwf';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶use Sort::Key::Maker rnatwfkeysort => '-natwf';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 1 ▶use Sort::Key::Maker natwfsort => \&mkkey_natural_with_floats, 'str';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶use Sort::Key::Maker rnatwfsort => \&mkkey_natural_with_floats, '-str';
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102  
103 1 1;
104  
105 =head1 NAME
106  
107 Sort::Key::Natural - fast natural sorting
108  
109 =head1 SYNOPSIS
110  
111 use Sort::Key::Natural qw(natsort);
112  
113 my @data = qw(foo1 foo23 foo6 bar12 bar1
114 foo bar2 bar-45 foomatic b-a-r-45);
115  
116 my @sorted = natsort @data;
117  
118 print "@sorted\n";
119 # prints:
120 # b-a-r-45 bar1 bar2 bar12 bar-45 foo foo1 foo6 foo23 foomatic
121  
122 use Sort::Key::Natural qw(natkeysort);
123  
124 my @objects = (...);
125 my @sorted = natkeysort { $_->get_id } @objects;
126  
127  
128 =head1 DESCRIPTION
129  
130 This module extends the L<Sort::Key> family of modules to support
131 natural sorting.
132  
133 Under natural sorting, strings are split at word and number
134 boundaries, and the resulting substrings are compared as follows:
135  
136 =over 4
137  
138 =item *
139  
140 numeric substrings are compared numerically
141  
142 =item *
143  
144 alphabetic substrings are compared lexically
145  
146 =item *
147  
148 numeric substrings come always before alphabetic substrings
149  
150 =back
151  
152 Spaces, symbols and non-printable characters are only considered for
153 splitting the string into its parts but not for sorting. For instance
154 C<foo-bar-42> is broken in three substrings C<foo>, C<bar> and C<42>
155 and after that the dashes are ignored.
156  
157 Note, that the sorting is case sensitive. To do a case insensitive
158 sort you have to convert the keys explicitly:
159  
160 my @sorted = natkeysort { lc $_ } @data
161  
162 Also, once this module is loaded, the new type C<natural> (or C<nat>) will
163 be available from L<Sort::Key::Maker>. For instance:
164  
165 use Sort::Key::Natural;
166 use Sort::Key::Maker i_rnat_keysort => qw(integer -natural);
167  
168 creates a multi-key sorter C<i_rnat_keysort> accepting two keys, the
169 first to be compared as an integer and the second in natural
170 descending order.
171  
172 There is also an alternative set of natural sorting functions that
173 recognize floating point numbers. They use the key type C<natwf>
174 (abbreviation of C<natural_with_floats>).
175  
176 =head2 FUNCTIONS
177  
178 the functions that can be imported from this module are:
179  
180 =over 4
181  
182 =item natsort @data
183  
184 returns the elements of C<@data> sorted in natural order.
185  
186 =item rnatsort @data
187  
188 returns the elements of C<@data> sorted in natural descending order.
189  
190 =item natkeysort { CALC_KEY($_) } @data
191  
192 returns the elements on C<@array> naturally sorted by the keys
193 resulting from applying them C<CALC_KEY>.
194  
195 =item rnatkeysort { CALC_KEY($_) } @data
196  
197 is similar to C<natkeysort> but sorts the elements in descending
198 order.
199  
200 =item natsort_inplace @data
201  
202 =item rnatsort_inplace @data
203  
204 =item natkeysort_inplace { CALC_KEY($_) } @data
205  
206 =item rnatkeysort_inplace { CALC_KEY($_) } @data
207  
208 these functions are similar respectively to C<natsort>, C<rnatsort>,
209 C<natsortkey> and C<rnatsortkey>, but they sort the array C<@data> in
210 place.
211  
212 =item $key = mkkey_natural $string
213  
214 given C<$string>, returns a key that can be compared lexicographically
215 to another key obtained in the same manner, results in the same order
216 as comparing the former strings as in the natural order.
217  
218 If the argument C<$key> is not provided it defaults to C<$_>.
219  
220 =item natwfsort @data
221  
222 =item rnatwfsort @data
223  
224 =item natwfkeysort { CALC_KEY($_) } @data
225  
226 =item rnatwfkeysort { CALC_KEY($_) } @data
227  
228 =item natwfsort_inplace @data
229  
230 =item rnatwfsort_inplace @data
231  
232 =item natwfkeysort_inplace { CALC_KEY($_) } @data
233  
234 =item rnatwfkeysort_inplace { CALC_KEY($_) } @data
235  
236 =item mkkey_natural_with_floats $key
237  
238 this ugly named set of functions perform in the same way as its
239 s/natwf/nat/ counterpart with the difference that they honor floating
240 point numbers embedded inside the strings.
241  
242 In this context a floating point number is a string matching the
243 regular expression C</[+\-]?\d+(\.\d*)?/>. Note that numbers with an
244 exponent part (i.e. C<1.12E-12>) are not recognized as such.
245  
246 Note also that numbers without an integer part (i.e. C<.2> or C<-.12>)
247 are not supported either.
248  
249 =back
250  
251 =head1 SEE ALSO
252  
253 L<Sort::Key>, L<Sort::Key::Maker>.
254  
255 Other module providing similar functionality is L<Sort::Naturally>.
256  
257 =head1 COPYRIGHT AND LICENSE
258  
259 Copyright (C) 2006, 2012, 2014 by Salvador FandiE<ntilde>o,
260 E<lt>sfandino@yahoo.comE<gt>.
261  
262 This library is free software; you can redistribute it and/or modify
263 it under the same terms as Perl itself, either Perl version 5.8.4 or,
264 at your option, any later version of Perl 5 you may have available.
265  
266 =cut
↑ Summary blib/lib/Sort/Key/Types.pm »