lib/ULID/Tiny.pm

filter: Statementstmt 99.0% 103 / 104 Branchbran 95.8% 23 / 24 Conditioncond 84.6% 11 / 13 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 96.3% 159 / 165 CC 22 SCAR 13.9
CC
22
Cov
97%
ulid
7.0
_crockford_increment
6.0
_crockford_decode_int
4.0
CRAP 4.0
« blib/lib/ULID/example.pl ↑ Summary
1 package ULID::Tiny;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Crypt::SysRandom qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Fcntl qw(O_RDONLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '1.0.0';
14  
15 1 our @EXPORT = qw(ulid ulid_date);
16 1 our @EXPORT_OK = qw(ulid ulid_date);
17  
18 1 my @CROCKFORD_CHARS = split //, '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
19  
20 ###############################################################################
21 # Public API
22 ###############################################################################
23  
24 1026 ▶sub ulid {
Subroutine ulid: called CC=7 SCAR=19.5
Pod documented
25 1026 my (%opts) = @_;
26  
27 1026 my $ts;
28 1026 ▶ if (defined($opts{time})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'time'}) { } 23 1003 1026
29 23 $ts = _encode_timestamp($opts{time});
30 } else {
31 1003 $ts = _unixtime_ms_48bit();
32 }
33  
34 1026 my $rand = random_bytes(10);
35 1026 my $ret = '';
36  
37 1026 state $prev_ts = 0;
38 1026 state $prev_ulid = "";
39  
40 1026 ▶ if (!$opts{unique} && $prev_ts && ($ts eq $prev_ts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{'unique'} and $prev_ts and $ts eq $prev_ts) { } 989 37 1026
Condition100%6/6
not $opts{"unique"} and $prev_ts and $ts eq $prev_ts
!ll&&!rl&&r
235989
not $opts{"unique"} and $prev_ts
!ll&&!rl&&r
111024
MC/DC100%3/3
not $opts{"unique"} and $prev_ts and $ts eq $prev_ts
not $opts{"unique"} $prev_ts $ts eq $prev_ts
Truth table100%4/4
not(A) and B and C
A= not $opts{"unique"}B= $prev_tsC= $ts eq $prev_ts
ABCresult
0XX0
10X0
1100
1111
41 989 $ret = _crockford_increment($prev_ulid);
42 } else {
43 # 48 bits of timestamp + 80 bits of randomness
44 37 my $raw = $ts . $rand;
45  
46 37 $ret = _crockford_encode($raw);
47 }
48  
49 1026 ▶ if (!$opts{unique}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"unique"}) 1025 1 1026
50 1025 $prev_ts = $ts;
51 1025 $prev_ulid = $ret;
52 }
53  
54 1026 ▶ if ($opts{binary}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"binary"}) 2 1024 1026
55 2 my $bits = _crockford_decode_bits($ret);
56 2 $bits = substr($bits, 0, 128);
57 2 return pack("B*", $bits);
58 }
59  
60 1024 return $ret;
61 }
62  
63 # Extract the millisecond epoch timestamp from a ULID string
64 7 ▶sub ulid_date {
Subroutine ulid_date: called CC=3 SCAR=11.0
Pod documented
65 7 my ($ulid_str) = @_;
66  
67 7 ▶ if (!defined $ulid_str || length($ulid_str) != 26) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ulid_str or length $ulid_str != 26) 2 5 7
Condition100%3/3
not defined $ulid_str or length $ulid_str != 26
l!l&&r!l&&!r
115
MC/DC100%2/2
not defined $ulid_str or length $ulid_str != 26
not defined $ulid_str length $ulid_str != 26
Truth table100%3/3
not(A) or B
A= not defined $ulid_strB= length $ulid_str != 26
ABresult
1X1
011
000
68 2 die "Invalid ULID: must be exactly 26 characters";
69 }
70  
71 # The first 10 characters of a ULID encode the 48-bit timestamp.
72 # 10 Crockford chars = 50 bits, but only the top 48 are the timestamp
73 # (the encoder right-pads 2 zero bits to reach a multiple of 5).
74 5 my $time_part = substr($ulid_str, 0, 10);
75 5 my $raw = _crockford_decode_int($time_part);
76 5 my $ms = $raw >> 2; # discard the 2 padding bits
77  
78 5 return $ms;
79 }
80  
81 ###############################################################################
82 # Internal functions
83 ###############################################################################
84  
85 989 ▶sub _crockford_increment {
Subroutine _crockford_increment: called CC=6 SCAR=17.9
86 989 my ($str) = @_;
87  
88 989 state %val;
89  
90 # Build the reverse mapping table (once)
91 989 ▶ if (!scalar(%val)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar %val) 1 988 989
92 1 @val{@CROCKFORD_CHARS} = (0..$#CROCKFORD_CHARS);
93 }
94  
95 989 my @out = reverse split //, uc($str);
96 989 my $carry = 1;
97  
98 989 for my $i (0 .. $#out) {
99 2012 ▶ last unless $carry;
Branch: 1 branch
BranchTrueFalseTotal
unless $carry 989 1023 2012
100  
101 1023 my $v = $val{$out[$i]};
102 1023 $v += $carry;
103  
104 1023 ▶ if ($v >= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v >= 32) { } 34 989 1023
105 34 $out[$i] = $CROCKFORD_CHARS[0];
106 34 $carry = 1;
107 } else {
108 989 $out[$i] = $CROCKFORD_CHARS[$v];
109 989 $carry = 0;
110 }
111 }
112  
113 989 ▶ if ($carry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($carry) 0 989 989
114 0 push(@out, '1');
115 }
116  
117 989 return join('', reverse @out);
118 }
119  
120 37 ▶sub _crockford_encode {
Subroutine _crockford_encode: called CC=3 SCAR=11.0
121 37 my ($bytes) = @_;
122 37 my $bits = unpack("B*", $bytes);
123 37 my $result = '';
124  
125 # Pad bits to multiple of 5
126 37 my $pad = (5 - (length($bits) % 5)) % 5;
127 37 $bits .= '0' x $pad;
128  
129 37 ▶ for (my $i = 0; $i < length($bits); $i += 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $bits) 962 37 999
130 962 my $chunk = substr($bits, $i, 5);
131 962 my $index = 0;
132 962 for my $bit (split //, $chunk) {
133 4810 $index = ($index << 1) | $bit;
134 }
135  
136 962 $result .= $CROCKFORD_CHARS[$index];
137 }
138  
139 37 return $result;
140 }
141  
142 # Decode a Crockford Base32 string to a decimal integer (for timestamps)
143 5 ▶sub _crockford_decode_int {
Subroutine _crockford_decode_int: called CC=4 SCAR=13.9
144 5 my ($str) = @_;
145  
146 5 state %val;
147 5 ▶ if (!scalar(%val)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar %val) 1 4 5
148 1 @val{@CROCKFORD_CHARS} = (0..$#CROCKFORD_CHARS);
149 }
150  
151 5 my $n = 0;
152 5 for my $ch (split //, uc($str)) {
153 50 ▶ $n = $n * 32 + ($val{$ch} // die "Invalid Crockford character: $ch");
Condition50%1/2
$val{$ch} // die("Invalid Crockford character: $ch")
l!l
500
MC/DC0%0/1
$val{$ch} // die("Invalid Crockford character: $ch")
$val{$ch}
Truth table50%1/2
A
A= $val{$ch}
Aresult
11
00
154 }
155  
156 5 return $n;
157 }
158  
159 # Decode a Crockford Base32 string to a binary bit string
160 2 ▶sub _crockford_decode_bits {
Subroutine _crockford_decode_bits: called CC=4 SCAR=13.9
161 2 my ($str) = @_;
162  
163 2 state %val;
164 2 ▶ if (!scalar(%val)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar %val) 1 1 2
165 1 @val{@CROCKFORD_CHARS} = (0..$#CROCKFORD_CHARS);
166 }
167  
168 2 my $bits = '';
169 2 for my $ch (split //, uc($str)) {
170 52 ▶ my $v = $val{$ch} // die "Invalid Crockford character: $ch";
Condition50%1/2
$val{$ch} // die("Invalid Crockford character: $ch")
l!l
520
MC/DC0%0/1
$val{$ch} // die("Invalid Crockford character: $ch")
$val{$ch}
Truth table50%1/2
A
A= $val{$ch}
Aresult
11
00
171 52 $bits .= sprintf("%05b", $v);
172 }
173  
174 2 return $bits;
175 }
176  
177 1003 ▶sub _unixtime_ms_48bit {
Subroutine _unixtime_ms_48bit: called CC=1 SCAR=0.0
178 1003 my $ms = int(time() * 1000);
179  
180 1003 return pack("H*", sprintf("%012X", $ms));
181 }
182  
183 23 ▶sub _encode_timestamp {
Subroutine _encode_timestamp: called CC=1 SCAR=0.0
184 23 my ($epoch_ms) = @_;
185  
186 23 return pack("H*", sprintf("%012X", int($epoch_ms)));
187 }
188  
189 1 1;
190  
191 __END__
« blib/lib/ULID/example.pl ↑ Summary