blib/lib/Data/ULID.pm

filter: Statementstmt 90.0% 100 / 111 Branchbran 60.0% 18 / 30 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 92.5% 25 / 27 Podpod 0.0% 0 / 6 totaltotal 78.0% 146 / 187 CC 19 SCAR 12.6
CC
19
Cov
81%
ulid_date
12.0
_ulid
8.2
_zero_pad
4.0
CRAP 3.5
↑ Summary
1 package Data::ULID;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '1.4';
7  
8 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 our @EXPORT_OK = qw/ulid binary_ulid ulid_date ulid_to_uuid uuid_to_ulid/;
10 2 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
11  
12 2 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Bytes::Random::Secure::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 my $rng = Bytes::Random::Secure::Tiny->new;
16  
17 2 my $datetime_status;
18 sub HAS_DATETIME
19 1 ▶{
Subroutine HAS_DATETIME: called CC=2 SCAR=7.4
Pod undocumented
20 1 ▶ if (!defined $datetime_status) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $datetime_status) 1 0 1
21 0 $datetime_status = eval { require DateTime; 1 };
22 }
23  
24 1 return $datetime_status;
25 }
26  
27 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶ use constant CAN_SKIP_BIGINTS => $Config{ivsize} >= 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 if (!CAN_SKIP_BIGINTS) {
32 require Math::BigInt;
33 Math::BigInt->VERSION(1.999808);
34 Math::BigInt->import(try => 'GMP,LTM');
35 }
36 }
37  
38 ### EXPORTED ULID FUNCTIONS
39  
40 4 ▶sub ulid {
Subroutine ulid: called CC=1 SCAR=0.0
Pod undocumented
41 4 return _encode(_ulid(shift));
42 }
43  
44 4 ▶sub binary_ulid {
Subroutine binary_ulid: called CC=1 SCAR=0.0
Pod undocumented
45 4 return _pack(_ulid(shift));
46 }
47  
48 0 ▶sub ulid_date {
Subroutine ulid_date: not called CC=3 SCAR=24.8
Pod undocumented
49 0 my $ulid = shift;
50  
51 0 ▶ die "ulid_date() requires DateTime module" unless HAS_DATETIME;
Branch: 1 branch
BranchTrueFalseTotal
unless HAS_DATETIME() 0 0 0
52 0 ▶ die "ulid_date() needs a normal or binary ULID as parameter" unless $ulid;
Branch: 1 branch
BranchTrueFalseTotal
unless $ulid 0 0 0
53  
54 0 my ($ts, $rand) = _ulid($ulid);
55  
56 0 return DateTime->from_epoch(epoch => _unfix_ts($ts));
57 }
58  
59 1 ▶sub ulid_to_uuid {
Subroutine ulid_to_uuid: called CC=2 SCAR=7.1
Pod undocumented
60 1 ▶ my $ulid = shift or die "Need ULID to convert";
Branch: 1 branch
BranchTrueFalseTotal
my $ulid = shift() or die "Need ULID to convert" 1 0 1
61 1 my $bin = _pack(_ulid($ulid));
62 1 return _uuid_bin2str($bin)
63 }
64  
65 3 ▶sub uuid_to_ulid {
Subroutine uuid_to_ulid: called CC=2 SCAR=7.1
Pod undocumented
66 3 ▶ my $uuid = shift or die "Need UUID to convert";
Branch: 1 branch
BranchTrueFalseTotal
my $uuid = shift() or die "Need UUID to convert" 3 0 3
67 3 my $bin_uuid = _uuid_str2bin($uuid);
68 3 return _encode(_ulid($bin_uuid));
69 }
70  
71 ### HELPER FUNCTIONS
72  
73 1 ▶sub _uuid_bin2str {
Subroutine _uuid_bin2str: called CC=3 SCAR=11.2
74 1 my $uuid = shift;
75  
76 1 ▶ return $uuid if length($uuid) == 36;
Branch: 1 branch
BranchTrueFalseTotal
if length $uuid == 36 0 1 1
77 1 ▶ die "Invalid uuid" unless length $uuid == 16;
Branch: 1 branch
BranchTrueFalseTotal
unless length $uuid == 16 0 1 1
78 1 my @offsets = (4, 2, 2, 2, 6);
79  
80 return join(
81 '-',
82 5 map { unpack 'H*', $_ }
83 1 map { substr $uuid, 0, $_, ''}
84 @offsets);
85 }
86  
87 3 ▶sub _uuid_str2bin {
Subroutine _uuid_str2bin: called CC=2 SCAR=7.0
88 3 my $uuid = shift;
89  
90 3 ▶ return $uuid if length $uuid == 16;
Branch: 1 branch
BranchTrueFalseTotal
if length $uuid == 16 0 3 3
91 3 $uuid =~ s/-//g;
92  
93 3 return pack 'H*', $uuid;
94 }
95  
96 12 ▶sub _ulid {
Subroutine _ulid: called CC=7 SCAR=21.1
97 12 my $arg = shift;
98 12 my $ts;
99  
100 12 ▶ if ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 11 1 12
101 11 ▶ if (ref $arg && $arg->isa('DateTime')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg and $arg->isa('DateTime')) { } 0 11 11
elsif (length $arg == 16) { } 6 5 11
Condition33%1/3
ref $arg and $arg->isa("DateTime")
!ll&&!rl&&r
1100
MC/DC0%0/2
ref $arg and $arg->isa("DateTime")
ref $arg $arg->isa("DateTime")
Truth table33%1/3
A and B
A= ref $argB= $arg->isa("DateTime")
ABresult
0X0
100
111
102 0 $ts = $arg->hires_epoch;
103 }
104 elsif (length($arg) == 16) {
105 6 return _unpack($arg);
106 }
107 else {
108 5 $arg = _normalize($arg);
109 5 ▶ die "Invalid ULID supplied: wrong length" unless length($arg) == 26;
Branch: 1 branch
BranchTrueFalseTotal
unless length $arg == 26 0 5 5
110 5 return _decode($arg);
111 }
112 }
113  
114 1 ▶ return (_fix_ts($ts || time()), $rng->bytes(10));
Condition33%1/3
$ts || &Data::ULID::time()
l!l&&r!l&&!r
010
MC/DC0%0/2
$ts || &Data::ULID::time()
$ts &Data::ULID::time()
Truth table33%1/3
A || B
A= $tsB= &Data::ULID::time()
ABresult
1X1
011
000
115 }
116  
117 5 ▶sub _pack {
Subroutine _pack: called CC=1 SCAR=0.0
118 5 my ($ts, $rand) = @_;
119 5 return _zero_pad($ts, 6, "\x00") . _zero_pad($rand, 10, "\x00");
120 }
121  
122 6 ▶sub _unpack {
Subroutine _unpack: called CC=1 SCAR=0.0
123 6 my ($ts, $rand) = unpack 'a6a10', shift;
124 6 return ($ts, $rand);
125 }
126  
127 1 ▶sub _fix_ts {
Subroutine _fix_ts: called CC=1 SCAR=0.0
128 1 my $ts = shift;
129  
130 1 if (CAN_SKIP_BIGINTS) {
131 1 $ts = int($ts * 1000);
132 1 return pack 'Nn', $ts >> 16, $ts & 0xffff;
133 } else {
134 $ts .= '000';
135 $ts =~ s/\.(\d{3}).*$/$1/;
136 return Math::BigInt->new($ts)->to_bytes;
137 }
138 }
139  
140 0 ▶sub _unfix_ts {
Subroutine _unfix_ts: not called CC=1 SCAR=6.9
141 0 my $ts = shift;
142  
143 0 if (CAN_SKIP_BIGINTS) {
144 0 my ($high, $low) = unpack 'Nn', $ts;
145 0 return (($high << 16) + $low) / 1000;
146 } else {
147 $ts = Math::BigInt->from_bytes($ts);
148 $ts =~ s/(\d{3})$/.$1/;
149 return $ts;
150 }
151 }
152  
153 7 ▶sub _encode {
Subroutine _encode: called CC=1 SCAR=0.0
154 7 my ($ts, $rand) = @_;
155 7 return sprintf('%010s%016s', _encode_b32($ts), _encode_b32($rand));
156 }
157  
158 5 ▶sub _decode {
Subroutine _decode: called CC=1 SCAR=0.0
159 5 my ($ts, $rand) = map { _decode_b32($_) } unpack 'A10A16', shift;
160 5 return ($ts, $rand);
161 }
162  
163 34 ▶sub _zero_pad {
Subroutine _zero_pad: called CC=4 SCAR=13.9
164 # this function is used a lot. Keep it as lean as possible
165 # my ($value, $character_multiplier, $padding_character) = @_;
166 34 my $value = shift;
167  
168 34 my $padded = length($value) % $_[0];
169 34 ▶ return $value if $padded == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $padded == 0 22 12 34
170  
171 12 ▶ $_[1] ||= 0;
Condition50%1/2
$_[1] ||= 0
l!l
012
MC/DC0%0/1
$_[1] ||= 0
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
172 12 my $padding = substr $value, 0, $padded, '';
173  
174 12 ▶ return $value if $padding eq $_[1] x $padded;
Branch: 1 branch
BranchTrueFalseTotal
if $padding eq $_[1] x $padded 10 2 12
175 2 return $_[1] x ($_[0] - $padded) . $padding . $value;
176 }
177  
178 ### BASE32 ENCODER / DECODER
179  
180 2 my $ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
181  
182 2 my %ALPHABET_MAP = do {
183 2 my $num = 0;
184 2 map { $_ => substr sprintf('0000%b', $num++), -5 } split //, $ALPHABET;
185 };
186  
187 2 my %ALPHABET_MAP_REVERSE = map { $ALPHABET_MAP{$_} => $_ } keys %ALPHABET_MAP;
188  
189 5 ▶sub _normalize {
Subroutine _normalize: called CC=1 SCAR=0.0
190 5 my $s = uc(shift);
191 5 my $re = "[^$ALPHABET]";
192  
193 5 $s =~ s/$re//g;
194 5 return $s;
195 }
196  
197 14 ▶sub _encode_b32 {
Subroutine _encode_b32: called CC=2 SCAR=6.9
198 14 my $bits = _zero_pad(unpack('B*', shift), 5);
199 14 my $len = length $bits;
200  
201 14 my $result = '';
202 14 ▶ for (my $i = 0; $i < $len; $i += 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 177 14 191
203 177 $result .= $ALPHABET_MAP_REVERSE{substr $bits, $i, 5};
204 }
205 14 return $result;
206 }
207  
208 10 ▶sub _decode_b32 {
Subroutine _decode_b32: called CC=1 SCAR=0.0
209 10 my $encoded = join '', map { $ALPHABET_MAP{$_} } split //, uc shift;
210 10 return pack 'B*', _zero_pad($encoded, 8);
211 }
212  
213 2 1;
214  
215 __END__
↑ Summary