blib/lib/Data/KSUID.pm

filter: Statementstmt 100% 158 / 158 Branchbran 85.1% 46 / 54 Conditioncond 86.8% 33 / 38 MC/DCmcdc 70.0% 14 / 20 Subroutinesub 100% 38 / 38 Podpod 100% 23 / 23 totaltotal 94.2% 312 / 331 CC 53 SCAR 13.8
CC
53
Cov
95%
create_ksuid
9.0
string_to_ksuid
8.1
__ANON__
6.0
CRAP 4.0
↑ Summary
1 # ABSTRACT: K-Sortable Unique IDentifiers
2 package Data::KSUID;
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.001';
8  
9 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our @EXPORT_OK = qw(
12 create_ksuid
13 create_ksuid_string
14 is_ksuid
15 is_ksuid_string
16 ksuid_to_string
17 next_ksuid
18 payload_of_ksuid
19 previous_ksuid
20 string_to_ksuid
21 time_of_ksuid
22 );
23  
24 4 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
25  
26 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use Crypt::URandom ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use Sub::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # KSUID's epoch starts more recently so that the 32-bit
32 # number space gives a significantly higher useful lifetime
33 # of around 136 years from March 2017. This number (14e8)
34 # was picked to be easy to remember.
35 4 ▶use constant EPOCH => 1_400_000_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
38 # For base-62 encoding
39 4 KSUID_BASE => 4_294_967_296, # 0x100_000_000
40 STRING_BASE => 62,
41  
42 MAX_TIME => EPOCH + unpack( 'N', "\xff" x 4 ),
43 4 };
44  
45 # Public constants
46 # The ones defined above are for internal use only
47 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 MAX => "\xff" x 20,
49 MIN => "\x00" x 20,
50  
51 # Math::BigInt->from_bytes("\xff" x 20)->to_base(62)
52 MAX_STRING => 'aWgEPTl1tmebfsQzFP4bxwgy80V',
53 MIN_STRING => '000000000000000000000000000',
54 4 };
55  
56 # Trusting private functions
57  
58 36 ▶my $safely_printed = Sub::Util::set_subname( safely_printed => sub {
Subroutine safely_printed: called CC=2 SCAR=6.9
59 36 require B;
60 36 ▶ defined $_[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 24 12 36
61 ? B::perlstring($_[0])
62 : 'an undefined value';
63 4 });
64  
65 my %value62 = map {
66 4 ▶ $_ =~ /[A-Z]/ ? ( $_ => ord($_) - ord('A') + 10 ) :
Branch: 2 branches
BranchTrueFalseTotal
$_ =~ /[A-Z]/ ? : 104 144 248
$_ =~ /[a-z]/ ? : 104 40 144
67 $_ =~ /[a-z]/ ? ( $_ => ord($_) - ord('a') + 36 ) :
68 ( $_ => $_ );
69 } 0 .. 9, 'A' .. 'Z', 'a' .. 'z';
70  
71 4 my %digit62 = reverse %value62;
72  
73 19 ▶my $ksuid_to_string = Sub::Util::set_subname( ksuid_to_string => sub {
Subroutine ksuid_to_string: called CC=6 SCAR=17.9
Subroutine __ANON__: called CC=6 SCAR=17.9
Pod documented
74 19 my @parts = unpack 'N*', $_[0];
75 19 my @digits = (0) x 27;
76  
77 19 for ( 0 .. $#digits ) {
78 487 my @quotient;
79 487 my $remainder = 0;
80  
81 487 for (@parts) {
82 1520 my $value = int($_) + int($remainder) * KSUID_BASE;
83 1520 my $digit = $value / STRING_BASE;
84 1520 $remainder = $value % STRING_BASE;
85  
86 1520 ▶ push @quotient, $digit
Branch: 2 branches
BranchTrueFalseTotal
if @quotient or $digit 1443 77 1520
if @quotient or $digit 400 20 420
Condition100%6/6
@quotient or $digit
l!l&&r!l&&!r
95748677
@quotient or $digit
l!l&&r!l&&!r
26513520
MC/DC100%2/2
@quotient or $digit
@quotient $digit
MC/DC100%2/2
@quotient or $digit
@quotient $digit
Truth table100%3/3
A or B
A= @quotientB= $digit
ABresult
1X1
011
000
Truth table100%3/3
A or B
A= @quotientB= $digit
ABresult
1X1
011
000
87 if @quotient || $digit;
88 }
89  
90 # We push into this in reverse order for convenience
91 487 $digits[$_] = $remainder;
92  
93 487 ▶ @parts = @quotient or last;
Branch: 2 branches
BranchTrueFalseTotal
@parts = @quotient or last 486 1 487
@parts = @quotient or last 135 0 135
94 }
95  
96 19 join '', @digit62{ reverse @digits };
97 4 });
98  
99 4 ▶my $string_to_ksuid = Sub::Util::set_subname( string_to_ksuid => sub {
Subroutine string_to_ksuid: called CC=8 SCAR=20.9
Pod documented
100 4 my @digits = (0) x 20;
101 4 my @parts = @value62{ split //, $_[0] };
102  
103 4 ▶ die unless @parts == 27;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts == 27 0 4 4
104  
105 4 my $n = 0;
106 4 ▶ while ( @parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 20 0 20
107 20 my @quotient;
108 20 my $remainder = 0;
109  
110 20 for (@parts) {
111 408 my $value = int($_) + int($remainder) * STRING_BASE;
112 408 my $digit = $value / KSUID_BASE;
113 408 $remainder = $value % KSUID_BASE;
114  
115 408 ▶ push @quotient, $digit % 256
Branch: 1 branch
BranchTrueFalseTotal
if @quotient or $digit 320 88 408
Condition100%3/3
@quotient or $digit
l!l&&r!l&&!r
3002088
MC/DC100%2/2
@quotient or $digit
@quotient $digit
Truth table100%3/3
A or B
A= @quotientB= $digit
ABresult
1X1
011
000
116 if @quotient || $digit;
117 }
118  
119 20 $digits[$n++] = ( $remainder ) % 256;
120 20 $digits[$n++] = ( $remainder >> 8 ) % 256;
121 20 $digits[$n++] = ( $remainder >> 16 ) % 256;
122 20 $digits[$n++] = ( $remainder >> 24 ) % 256;
123  
124 20 ▶ @parts = @quotient or last;
Branch: 1 branch
BranchTrueFalseTotal
@parts = @quotient or last 20 0 20
125 20 ▶ last if $n == @digits;
Branch: 1 branch
BranchTrueFalseTotal
if $n == @digits 4 16 20
126 }
127  
128 4 pack 'C*', reverse @digits;
129 4 });
130  
131 7 ▶my $time_of_ksuid = Sub::Util::set_subname( time_of_ksuid => sub {
Subroutine time_of_ksuid: called CC=1 SCAR=0.0
Pod documented
132 7 EPOCH + unpack 'N', substr( $_[0], 0, 4 );
133 4 });
134  
135 11 ▶my $payload_of_ksuid = Sub::Util::set_subname( payload_of_ksuid => sub {
Subroutine payload_of_ksuid: called CC=1 SCAR=0.0
Pod documented
136 11 substr $_[0], 4, 20;
137 4 });
138  
139 2 ▶my $next_ksuid = Sub::Util::set_subname( next_ksuid => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
140 2 my $k = shift;
141  
142 2 my $time = $k->$time_of_ksuid;
143 2 my $data = $k->$payload_of_ksuid;
144  
145 # Overflow
146 2 ▶ return create_ksuid( $time + 1, "\x00" x 16 )
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" 0 2 2
147 if $data eq ( "\xff" x 16 );
148  
149 2 my @parts = reverse $data =~ /[\w\W]{4}/g;
150 2 for (@parts) {
151 2 $_ = pack 'N', unpack('N', $_) + 1;
152 2 ▶ last unless $_ eq "\x00" x 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq "\0\0\0\0" 2 0 2
153 }
154  
155 2 create_ksuid( $time, join '', reverse @parts );
156 4 });
157  
158 2 ▶my $previous_ksuid = Sub::Util::set_subname( previous_ksuid => sub {
Subroutine previous_ksuid: called CC=4 SCAR=14.0
Pod documented
159 2 my $k = shift;
160  
161 2 my $time = $k->$time_of_ksuid;
162 2 my $data = $k->$payload_of_ksuid;
163  
164 # Overflow
165 2 ▶ return create_ksuid( $time - 1, "\xff" x 16 )
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" 0 2 2
166 if $data eq ( "\x00" x 16 );
167  
168 2 my @parts = reverse $data =~ /[\w\W]{4}/g;
169 2 for (@parts) {
170 2 $_ = pack 'N', unpack('N', $_) - 1;
171 2 ▶ last unless $_ eq "\xff" x 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq "\377\377\377\377" 2 0 2
172 }
173  
174 2 create_ksuid( $time, join '', reverse @parts );
175 4 });
176  
177 # Distrustful user-facing functions
178  
179 34 ▶sub create_ksuid {
Subroutine create_ksuid: called CC=9 SCAR=22.0
Pod documented
180 34 my ( $time, $payload ) = @_;
181  
182 34 ▶ if ( $time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time) 18 16 34
183 18 ▶ Carp::croak 'Timestamp must be numeric'
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::looks_like_number($time) 3 15 18
184 unless Scalar::Util::looks_like_number($time);
185  
186 15 ▶ Carp::croak "Timestamp must be between 0 and "
Branch: 1 branch
BranchTrueFalseTotal
if $time < 0 or $time > 5694967295 6 9 15
Condition100%3/3
$time < 0 or $time > 5694967295
l!l&&r!l&&!r
339
MC/DC100%2/2
$time < 0 or $time > 5694967295
$time < 0 $time > 5694967295
Truth table100%3/3
A or B
A= $time < 0B= $time > 5694967295
ABresult
1X1
011
000
187 . MAX_TIME . ", got $time instead"
188 if $time < 0 || $time > MAX_TIME;
189 }
190  
191 25 ▶ if ( $payload ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload) 15 10 25
192 15 my $length = length $payload;
193 15 ▶ Carp::croak "KSUID payloads must have 16 bytes, got instead $length"
Branch: 1 branch
BranchTrueFalseTotal
if $length != 16 6 9 15
194 if $length != 16;
195 }
196  
197 19 ▶ $time ||= time;
Condition100%2/2
$time ||= CORE::time
l!l
910
MC/DC100%1/1
$time ||= CORE::time
$time
Truth table100%2/2
A
A= $time
Aresult
11
00
198 19 ▶ $payload ||= Crypt::URandom::urandom(16);
Condition66%2/3
$payload ||= Crypt::URandom::urandom(16)
l!l&&r!l&&!r
9100
MC/DC0%0/2
$payload ||= Crypt::URandom::urandom(16)
$payload Crypt::URandom::urandom(16)
Truth table66%2/3
A ||= B
A= $payloadB= Crypt::URandom::urandom(16)
ABresult
1X1
011
000
199  
200 19 pack( 'N', $time - EPOCH ) . $payload;
201 }
202  
203 8 ▶sub create_ksuid_string {
Subroutine create_ksuid_string: called CC=1 SCAR=0.0
Pod documented
204 8 create_ksuid(@_)->$ksuid_to_string
205 }
206  
207 10 ▶sub ksuid_to_string {
Subroutine ksuid_to_string: called CC=2 SCAR=6.9
Pod documented
208 10 ▶ Carp::croak 'Expected a valid KSUID, got instead '
Branch: 1 branch
BranchTrueFalseTotal
unless is_ksuid($_[0]) 6 4 10
209 . $_[0]->$safely_printed
210 unless is_ksuid($_[0]);
211  
212 4 goto $ksuid_to_string;
213 }
214  
215 10 ▶sub string_to_ksuid {
Subroutine string_to_ksuid: called CC=2 SCAR=6.9
Pod documented
216 10 ▶ Carp::croak 'Expected a string KSUID, got instead '
Branch: 1 branch
BranchTrueFalseTotal
unless is_ksuid_string($_[0]) 6 4 10
217 . $_[0]->$safely_printed
218 unless is_ksuid_string($_[0]);
219  
220 4 goto $string_to_ksuid;
221 }
222  
223 8 ▶sub time_of_ksuid {
Subroutine time_of_ksuid: called CC=2 SCAR=6.9
Pod documented
224 8 ▶ Carp::croak 'Expected a valid KSUID, got instead '
Branch: 1 branch
BranchTrueFalseTotal
unless is_ksuid($_[0]) 6 2 8
225 . $_[0]->$safely_printed
226 unless is_ksuid($_[0]);
227  
228 2 goto $time_of_ksuid;
229 }
230  
231 12 ▶sub payload_of_ksuid {
Subroutine payload_of_ksuid: called CC=2 SCAR=6.9
Pod documented
232 12 ▶ Carp::croak 'Expected a valid KSUID, got instead '
Branch: 1 branch
BranchTrueFalseTotal
unless is_ksuid($_[0]) 6 6 12
233 . $_[0]->$safely_printed
234 unless is_ksuid($_[0]);
235  
236 6 goto $payload_of_ksuid;
237 }
238  
239 7 ▶sub next_ksuid {
Subroutine next_ksuid: called CC=2 SCAR=6.9
Pod documented
240 7 ▶ Carp::croak 'Expected a valid KSUID, got instead '
Branch: 1 branch
BranchTrueFalseTotal
unless is_ksuid($_[0]) 6 1 7
241 . $_[0]->$safely_printed
242 unless is_ksuid($_[0]);
243  
244 1 goto $next_ksuid;
245 }
246  
247 7 ▶sub previous_ksuid {
Subroutine previous_ksuid: called CC=2 SCAR=6.9
Pod documented
248 7 ▶ Carp::croak 'Expected a valid KSUID, got instead '
Branch: 1 branch
BranchTrueFalseTotal
unless is_ksuid($_[0]) 6 1 7
249 . $_[0]->$safely_printed
250 unless is_ksuid($_[0]);
251  
252 1 goto $previous_ksuid;
253 }
254  
255 50 ▶sub is_ksuid {
Subroutine is_ksuid: called CC=4 SCAR=14.2
Pod documented
256 50 ▶ return defined $_[0]
Condition77%7/9
defined $_[0] && length $_[0] == 20 && $_[0] ge "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" && $_[0] le "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
!ll&&!rl&&r
36014
defined $_[0] and length $_[0] == 20 and $_[0] ge "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
!ll&&!rl&&r
36014
defined $_[0] and length $_[0] == 20
!ll&&!rl&&r
122414
MC/DC50%2/4
defined $_[0] && length $_[0] == 20 && $_[0] ge "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" && $_[0] le "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
defined $_[0] length $_[0] == 20 $_[0] ge "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" $_[0] le "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
Truth table60%3/5
A and B and C && D
A= defined $_[0]B= length $_[0] == 20C= $_[0] ge "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"D= $_[0] le "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
257 && length $_[0] == 20
258 && $_[0] ge MIN
259 && $_[0] le MAX;
260 }
261  
262 16 ▶sub is_ksuid_string {
Subroutine is_ksuid_string: called CC=5 SCAR=16.3
Pod documented
263 16 ▶ return defined $_[0]
Condition83%10/12
defined $_[0] && length $_[0] == 27 && $_[0] ge '000000000000000000000000000' && $_[0] le 'aWgEPTl1tmebfsQzFP4bxwgy80V' && !($_[0] =~ /[^0-9A-Za-z]/)
!ll&&!rl&&r
1204
defined $_[0] and length $_[0] == 27 and $_[0] ge "000000000000000000000000000" and $_[0] le "aWgEPTl1tmebfsQzFP4bxwgy80V"
!ll&&!rl&&r
1024
defined $_[0] and length $_[0] == 27 and $_[0] ge "000000000000000000000000000"
!ll&&!rl&&r
1006
defined $_[0] and length $_[0] == 27
!ll&&!rl&&r
466
MC/DC60%3/5
defined $_[0] && length $_[0] == 27 && $_[0] ge '000000000000000000000000000' && $_[0] le 'aWgEPTl1tmebfsQzFP4bxwgy80V' && !($_[0] =~ /[^0-9A-Za-z]/)
defined $_[0] length $_[0] == 27 $_[0] ge "000000000000000000000000000" $_[0] le "aWgEPTl1tmebfsQzFP4bxwgy80V" !($_[0] =~ /[^0-9A-Za-z]/)
Truth table66%4/6
A and B and C and D && not(E)
A= defined $_[0]B= length $_[0] == 27C= $_[0] ge "000000000000000000000000000"D= $_[0] le "aWgEPTl1tmebfsQzFP4bxwgy80V"E= !($_[0] =~ /[^0-9A-Za-z]/)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
264 && length $_[0] == 27
265 && $_[0] ge MIN_STRING
266 && $_[0] le MAX_STRING
267 && $_[0] !~ /[^0-9A-Za-z]/;
268 }
269  
270 ## OO interface
271  
272 use overload
273 4 ▶ '""' => \&string,
Subroutine BEGIN: called CC=1 SCAR=0.0
274 3 ▶ 'cmp' => Sub::Util::set_subname( cmp => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
275 3 $_[0]->bytes cmp $_[1]->bytes
276 4 }),
277 4 ;
278  
279 11 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
280 11 my $class = shift;
281 11 my $self = create_ksuid(@_);
282 6 bless \$self, $class;
283 }
284  
285 1 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
286 1 my $class = shift;
287 1 my $self = string_to_ksuid(@_);
288 1 bless \$self, $class;
289 }
290  
291 31 ▶sub bytes { ${ $_[0] } }
Subroutine bytes: called CC=1 SCAR=0.0
Pod documented
292 1 ▶sub payload { $_[0]->bytes->$payload_of_ksuid }
Subroutine payload: called CC=1 SCAR=0.0
Pod documented
293 17 ▶sub string { $_[0]->bytes->$ksuid_to_string }
Subroutine string: called CC=1 SCAR=0.0
Pod documented
294 1 ▶sub time { $_[0]->bytes->$time_of_ksuid }
Subroutine time: called CC=1 SCAR=0.0
Pod documented
295  
296 1 ▶sub next {
Subroutine next: called CC=1 SCAR=0.0
Pod documented
297 1 my $self = $_[0];
298 1 my $next = $self->bytes->$next_ksuid;
299 1 bless \$next, ref $self;
300 }
301  
302 1 ▶sub previous {
Subroutine previous: called CC=1 SCAR=0.0
Pod documented
303 1 my $self = $_[0];
304 1 my $prev = $self->bytes->$previous_ksuid;
305 1 bless \$prev, ref $self;
306 }
307  
308 # Clean our namespace
309 4 delete @Data::KSUID::{qw(
310 MAX_TIME
311 EPOCH
312 KSUID_BASE
313 STRING_BASE
314 )};
315  
316 4 1;
↑ Summary