1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
exception Alphabet_contains_multibyte_characters of string
exception Alphabet_too_short of string
exception Alphabet_contains_repeated_characters of string
exception Minimum_length_outside_limits of string
exception Encode_max_attempts of string
module String_set = Utils.String_set
type t = { alphabet : Bytes.t; min_length : int; blocklist : String_set.t }
module Defaults = struct
let alphabet =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let min_length = 0
let blocklist = Blocklist.blocklist
end
let to_id num alphabet =
let alphabet_len = Bytes.length alphabet in
let rec loop result id_chars =
let alphabet_idx = result mod alphabet_len in
let alphabet_char = Bytes.get alphabet alphabet_idx in
let id_chars' = Bytes.cat (Bytes.make 1 alphabet_char) id_chars in
let result' = result / alphabet_len in
if Int.equal result' 0 then id_chars' else loop result' id_chars'
in
loop num Bytes.empty
let to_number id alphabet =
Utils.string_fold_left
(fun acc c ->
let i = Bytes.index alphabet c in
(acc * Bytes.length alphabet) + i)
0 id
let is_blocked_id t ~id =
let id = String.lowercase_ascii id in
let id_len = String.length id in
let exception Blocked_word in
try
String_set.iter
(fun word ->
let word_len = String.length word in
if word_len <= id_len then
if id_len <= 3 || word_len <= 3 then (
if String.equal id word then raise_notrace Blocked_word)
else if Utils.string_for_all Utils.char_is_digit id then (
if
Utils.string_starts_with ~prefix:word id
|| Utils.string_ends_with ~suffix:word id
then raise_notrace Blocked_word)
else if Utils.string_is_infix ~affix:word id then
raise_notrace Blocked_word)
t.blocklist;
false
with Blocked_word -> true
let make ?(alphabet = Defaults.alphabet) ?(min_length = Defaults.min_length)
?(blocklist = Defaults.blocklist) () =
if String.length alphabet < 3 then
raise (Alphabet_too_short "Alphabet length must be at least 3");
if Utils.string_has_dups alphabet then
raise
(Alphabet_contains_repeated_characters
"Alphabet must contain unique characters");
let min_length_limit = 255 in
if min_length < 0 || min_length > min_length_limit then
raise
(Minimum_length_outside_limits
(Printf.sprintf "Minimum length has to be between 0 and %d"
min_length_limit));
String.to_seq alphabet
|> Seq.iter (fun char ->
if Char.code char > 127 then
raise
(Alphabet_contains_multibyte_characters
"Alphabet cannot contain multibyte characters"));
let blocklist_set = List.to_seq blocklist |> String_set.of_seq in
let filtered_blocklist =
let alphabet_lower = String.lowercase_ascii alphabet in
String_set.filter_map
(fun word ->
let word = String.lowercase_ascii word in
if
String.length word >= 3
&& Utils.string_for_all (String.contains alphabet_lower) word
then Some word
else None)
blocklist_set
in
let alphabet = Bytes.of_string alphabet in
Utils.bytes_shuffle_inplace alphabet;
{ alphabet; min_length; blocklist = filtered_blocklist }
let rec encode_numbers t ~numbers ?(increment = 0) () =
let alphabet = t.alphabet in
let alphabet_len = Bytes.length alphabet in
let numbers_len = List.length numbers in
if increment > alphabet_len then
raise (Encode_max_attempts "Reached max attempts to re-generate the ID");
let offset =
List.fold_left
(fun a (idx, v) ->
let alphabet_idx = v mod alphabet_len in
let alphabet_v = Bytes.get_uint8 alphabet alphabet_idx in
alphabet_v + idx + a)
numbers_len
(List.mapi (fun i v -> (i, v)) numbers)
in
let offset = (offset + increment) mod alphabet_len in
let alphabet = Utils.bytes_rotate alphabet offset in
let prefix = Bytes.get alphabet 0 in
Utils.bytes_rev_inplace alphabet;
let id_buf = Buffer.create 4 in
Buffer.add_char id_buf prefix;
List.iteri
(fun idx num ->
let alphabet_no_sep = Bytes.sub alphabet 1 (Bytes.length alphabet - 1) in
Buffer.add_bytes id_buf (to_id num alphabet_no_sep);
if idx < numbers_len - 1 then (
Buffer.add_bytes id_buf (Bytes.sub alphabet 0 1);
Utils.bytes_shuffle_inplace alphabet))
numbers;
if t.min_length > Buffer.length id_buf then (
Buffer.add_bytes id_buf (Bytes.sub alphabet 0 1);
while t.min_length - Buffer.length id_buf > 0 do
Utils.bytes_shuffle_inplace alphabet;
let slice_len =
min (t.min_length - Buffer.length id_buf) (Bytes.length alphabet)
in
Buffer.add_subbytes id_buf alphabet 0 slice_len
done);
let id = Buffer.contents id_buf in
match is_blocked_id t ~id with
| true -> encode_numbers t ~numbers ~increment:(increment + 1) ()
| false -> id
let encode t (numbers : int list) : string =
match numbers with
| [] -> ""
| numbers ->
if List.exists (fun nr -> nr < 0) numbers then
raise
(Invalid_argument "Encoding supports numbers between 0 and max_int.");
encode_numbers t ~numbers ()
let decode t id0 =
if
String.equal id0 ""
|| not (Utils.string_for_all (Bytes.contains t.alphabet) id0)
then []
else
let alphabet0 =
let offset =
let prefix = String.get id0 0 in
Bytes.index t.alphabet prefix
in
Utils.bytes_rotate t.alphabet offset
in
Utils.bytes_rev_inplace alphabet0;
let id1 = String.sub id0 1 (String.length id0 - 1) in
let rec loop id alphabet acc =
if String.equal id "" then List.rev acc
else
let sep = Bytes.get alphabet 0 in
match String.split_on_char sep id with
| [] -> List.rev acc
| "" :: _chunks -> List.rev acc
| chunks_hd :: chunks_tl ->
let acc' =
let alphabet_without_sep =
Bytes.sub alphabet 1 (Bytes.length alphabet - 1)
in
let n = to_number chunks_hd alphabet_without_sep in
n :: acc
in
if not (Utils.list_is_empty chunks_tl) then
Utils.bytes_shuffle_inplace alphabet;
let id' = String.concat (String.make 1 sep) chunks_tl in
loop id' alphabet acc'
in
loop id1 alphabet0 []