encoded_id 1.0.0.rc4 → 1.0.0.rc6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,465 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rbs_inline: enabled
4
+
5
+ # Sqids (pronounced "squids") is a library that generates short, unique, non-sequential IDs
6
+ # from numbers. It's useful for obfuscating database IDs, creating URL-friendly identifiers,
7
+ # and generating human-readable codes.
8
+ #
9
+ # Key features:
10
+ # - Reversible: encoded IDs can be decoded back to the original numbers
11
+ # - Customizable: supports custom alphabets, minimum lengths, and blocklists
12
+ # - Collision-free: same input always produces the same output
13
+ # - Blocklist filtering: automatically regenerates IDs that contain blocked words
14
+ #
15
+ # The algorithm uses a shuffling mechanism based on the input numbers to select characters
16
+ # from a customized alphabet, ensuring that sequential numbers produce non-sequential IDs.
17
+ #
18
+ class MySqids
19
+ # @rbs @alphabet: Array[Integer]
20
+ # @rbs @min_length: Integer
21
+ # @rbs @blocklist: (Array[String] | Set[String])
22
+
23
+ # @rbs self.@DEFAULT_ALPHABET: String
24
+ DEFAULT_ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
25
+
26
+ # Default minimum length of 0 means no padding is applied to generated IDs
27
+ # @rbs self.@DEFAULT_MIN_LENGTH: Integer
28
+ DEFAULT_MIN_LENGTH = 0
29
+ # rubocop:disable Metrics/CollectionLiteralLength, Layout/LineLength
30
+ # Default blocklist containing words that should not appear in generated IDs
31
+ # The blocklist prevents offensive or inappropriate words from appearing in IDs by
32
+ # regenerating IDs that contain these patterns.
33
+ # @rbs self.@DEFAULT_BLOCKLIST: Array[String]
34
+ DEFAULT_BLOCKLIST = %w[0rgasm 1d10t 1d1ot 1di0t 1diot 1eccacu10 1eccacu1o 1eccacul0
35
+ 1eccaculo 1mbec11e 1mbec1le 1mbeci1e 1mbecile a11upat0 a11upato a1lupat0 a1lupato aand ah01e ah0le aho1e ahole al1upat0 al1upato allupat0 allupato ana1 ana1e anal anale anus arrapat0 arrapato arsch arse ass b00b b00be b01ata b0ceta b0iata b0ob b0obe b0sta b1tch b1te b1tte ba1atkar balatkar bastard0 bastardo batt0na battona bitch bite bitte bo0b bo0be bo1ata boceta boiata boob boobe bosta bran1age bran1er bran1ette bran1eur bran1euse branlage branler branlette branleur branleuse c0ck c0g110ne c0g11one c0g1i0ne c0g1ione c0gl10ne c0gl1one c0gli0ne c0glione c0na c0nnard c0nnasse c0nne c0u111es c0u11les c0u1l1es c0u1lles c0ui11es c0ui1les c0uil1es c0uilles c11t c11t0 c11to c1it c1it0 c1ito cabr0n cabra0 cabrao cabron caca cacca cacete cagante cagar cagare cagna cara1h0 cara1ho caracu10 caracu1o caracul0 caraculo caralh0 caralho cazz0 cazz1mma cazzata cazzimma cazzo ch00t1a ch00t1ya ch00tia ch00tiya ch0d ch0ot1a ch0ot1ya ch0otia ch0otiya ch1asse ch1avata ch1er ch1ng0 ch1ngadaz0s ch1ngadazos ch1ngader1ta ch1ngaderita ch1ngar ch1ngo ch1ngues ch1nk chatte chiasse chiavata chier ching0 chingadaz0s chingadazos chingader1ta chingaderita chingar chingo chingues chink cho0t1a cho0t1ya cho0tia cho0tiya chod choot1a choot1ya chootia chootiya cl1t cl1t0 cl1to clit clit0 clito cock cog110ne cog11one cog1i0ne cog1ione cogl10ne cogl1one cogli0ne coglione cona connard connasse conne cou111es cou11les cou1l1es cou1lles coui11es coui1les couil1es couilles cracker crap cu10 cu1att0ne cu1attone cu1er0 cu1ero cu1o cul0 culatt0ne culattone culer0 culero culo cum cunt d11d0 d11do d1ck d1ld0 d1ldo damn de1ch deich depp di1d0 di1do dick dild0 dildo dyke encu1e encule enema enf01re enf0ire enfo1re enfoire estup1d0 estup1do estupid0 estupido etr0n etron f0da f0der f0ttere f0tters1 f0ttersi f0tze f0utre f1ca f1cker f1ga fag fica ficker figa foda foder fottere fotters1 fottersi fotze foutre fr0c10 fr0c1o fr0ci0 fr0cio fr0sc10 fr0sc1o fr0sci0 fr0scio froc10 froc1o froci0 frocio frosc10 frosc1o frosci0 froscio fuck g00 g0o g0u1ne g0uine gandu go0 goo gou1ne gouine gr0gnasse grognasse haram1 harami haramzade hund1n hundin id10t id1ot idi0t idiot imbec11e imbec1le imbeci1e imbecile j1zz jerk jizz k1ke kam1ne kamine kike leccacu10 leccacu1o leccacul0 leccaculo m1erda m1gn0tta m1gnotta m1nch1a m1nchia m1st mam0n mamahuev0 mamahuevo mamon masturbat10n masturbat1on masturbate masturbati0n masturbation merd0s0 merd0so merda merde merdos0 merdoso mierda mign0tta mignotta minch1a minchia mist musch1 muschi n1gger neger negr0 negre negro nerch1a nerchia nigger orgasm p00p p011a p01la p0l1a p0lla p0mp1n0 p0mp1no p0mpin0 p0mpino p0op p0rca p0rn p0rra p0uff1asse p0uffiasse p1p1 p1pi p1r1a p1rla p1sc10 p1sc1o p1sci0 p1scio p1sser pa11e pa1le pal1e palle pane1e1r0 pane1e1ro pane1eir0 pane1eiro panele1r0 panele1ro paneleir0 paneleiro patakha pec0r1na pec0rina pecor1na pecorina pen1s pendej0 pendejo penis pip1 pipi pir1a pirla pisc10 pisc1o pisci0 piscio pisser po0p po11a po1la pol1a polla pomp1n0 pomp1no pompin0 pompino poop porca porn porra pouff1asse pouffiasse pr1ck prick pussy put1za puta puta1n putain pute putiza puttana queca r0mp1ba11e r0mp1ba1le r0mp1bal1e r0mp1balle r0mpiba11e r0mpiba1le r0mpibal1e r0mpiballe rand1 randi rape recch10ne recch1one recchi0ne recchione retard romp1ba11e romp1ba1le romp1bal1e romp1balle rompiba11e rompiba1le rompibal1e rompiballe ruff1an0 ruff1ano ruffian0 ruffiano s1ut sa10pe sa1aud sa1ope sacanagem sal0pe salaud salope saugnapf sb0rr0ne sb0rra sb0rrone sbattere sbatters1 sbattersi sborr0ne sborra sborrone sc0pare sc0pata sch1ampe sche1se sche1sse scheise scheisse schlampe schwachs1nn1g schwachs1nnig schwachsinn1g schwachsinnig schwanz scopare scopata sexy sh1t shit slut sp0mp1nare sp0mpinare spomp1nare spompinare str0nz0 str0nza str0nzo stronz0 stronza stronzo stup1d stupid succh1am1 succh1ami succhiam1 succhiami sucker t0pa tapette test1c1e test1cle testic1e testicle tette topa tr01a tr0ia tr0mbare tr1ng1er tr1ngler tring1er tringler tro1a troia trombare turd twat vaffancu10 vaffancu1o vaffancul0 vaffanculo vag1na vagina verdammt verga w1chsen wank wichsen x0ch0ta x0chota xana xoch0ta xochota z0cc01a z0cc0la z0cco1a z0ccola z1z1 z1zi ziz1 zizi zocc01a zocc0la zocco1a zoccola].freeze
36
+ # rubocop:enable Metrics/CollectionLiteralLength, Layout/LineLength
37
+
38
+ # Maximum integer value that can be encoded
39
+ # Uses Integer::MAX if available (Ruby 2.4+), otherwise calculates the max fixnum value
40
+ # based on the platform's word size
41
+ MAX_INT = defined?(Integer::MAX) ? Integer::MAX : ((2**((0.size * 8) - 2)) - 1)
42
+
43
+ # Returns the maximum integer value that can be safely encoded
44
+ # @rbs () -> Integer
45
+ def self.max_value
46
+ MAX_INT
47
+ end
48
+
49
+ # Initializes a new MySqids encoder with custom options
50
+ #
51
+ # @param options [Hash] Configuration options
52
+ # @option options [String, Array<String>] :alphabet Custom alphabet to use for encoding
53
+ # (default: DEFAULT_ALPHABET). Must be at least 3 characters and contain only single-byte chars.
54
+ # @option options [Integer] :min_length Minimum length for generated IDs (default: 0).
55
+ # IDs shorter than this will be padded. Must be between 0 and 255.
56
+ # @option options [Array<String>, Set<String>] :blocklist Words to exclude from generated IDs
57
+ # (default: DEFAULT_BLOCKLIST). Words must be at least 3 characters long.
58
+ #
59
+ # @raise [ArgumentError] If alphabet contains multibyte characters
60
+ # @raise [ArgumentError] If alphabet is shorter than 3 characters
61
+ # @raise [ArgumentError] If alphabet contains duplicate characters
62
+ # @raise [TypeError] If min_length is not between 0 and 255
63
+ #
64
+ # @rbs (?Hash[Symbol, untyped] options) -> void
65
+ def initialize(options = {})
66
+ alphabet = options[:alphabet] || DEFAULT_ALPHABET
67
+ min_length = options[:min_length] || DEFAULT_MIN_LENGTH
68
+ blocklist = options[:blocklist] || DEFAULT_BLOCKLIST
69
+
70
+ raise ArgumentError, "Alphabet cannot contain multibyte characters" if contains_multibyte_chars?(alphabet)
71
+ raise ArgumentError, "Alphabet length must be at least 3" if alphabet.length < 3
72
+
73
+ alphabet = alphabet.chars unless alphabet.is_a?(Array)
74
+
75
+ if alphabet.uniq.size != alphabet.length
76
+ raise ArgumentError,
77
+ "Alphabet must contain unique characters"
78
+ end
79
+
80
+ min_length_limit = 255
81
+ unless min_length.is_a?(Integer) && min_length >= 0 && min_length <= min_length_limit
82
+ raise TypeError,
83
+ "Minimum length has to be between 0 and #{min_length_limit}"
84
+ end
85
+
86
+ # Filter the blocklist to only include words that:
87
+ # 1. Are at least 3 characters long
88
+ # 2. Only contain characters that exist in the alphabet (case-insensitive)
89
+ # This ensures we don't try to block words that could never appear in generated IDs
90
+ filtered_blocklist = if options[:blocklist].nil? && options[:alphabet].nil?
91
+ # If using default blocklist and alphabet, skip filtering since we know it's valid
92
+ blocklist
93
+ else
94
+ downcased_alphabet = alphabet.map(&:downcase)
95
+ # Only keep words that can be formed from the alphabet
96
+ blocklist.select do |word|
97
+ word.length >= 3 && (word.downcase.chars - downcased_alphabet).empty?
98
+ end.to_set(&:downcase)
99
+ end
100
+
101
+ # Store the alphabet as an array of integer codepoints after shuffling
102
+ # Shuffling ensures the alphabet order is unique to this instance
103
+ @alphabet = shuffle(alphabet.map(&:ord))
104
+ @min_length = min_length
105
+ @blocklist = filtered_blocklist
106
+ end
107
+
108
+ # Encodes an array of integers into a single Sqids string
109
+ #
110
+ # The encoding process:
111
+ # 1. Validates all numbers are in valid range (0 to MAX_INT)
112
+ # 2. Generates a prefix character based on the numbers and alphabet
113
+ # 3. Encodes each number using a shuffled alphabet
114
+ # 4. Separates encoded numbers with the first character of the shuffled alphabet
115
+ # 5. Pads the result if it's shorter than min_length
116
+ # 6. Regenerates if the result contains blocklisted words
117
+ #
118
+ # @param numbers [Array<Integer>] Array of non-negative integers to encode
119
+ # @return [String] The encoded Sqids string
120
+ # @raise [ArgumentError] If any number is outside the valid range (0 to MAX_INT)
121
+ #
122
+ # @example
123
+ # sqids.encode([1, 2, 3]) # => "86Rf07"
124
+ # sqids.encode([]) # => ""
125
+ #
126
+ # @rbs (Array[Integer] numbers) -> String
127
+ def encode(numbers)
128
+ return "" if numbers.empty?
129
+
130
+ # Validate that all numbers are within the acceptable range
131
+ in_range_numbers = numbers.filter_map { |n| i = n.to_i; i if i.between?(0, MAX_INT) }
132
+ unless in_range_numbers.length == numbers.length
133
+ raise ArgumentError,
134
+ "Encoding supports numbers between 0 and #{MAX_INT}"
135
+ end
136
+
137
+ encode_numbers(in_range_numbers)
138
+ end
139
+
140
+ # Decodes a Sqids string back into the original array of integers
141
+ #
142
+ # The decoding process mirrors the encoding:
143
+ # 1. Validates all characters exist in the alphabet
144
+ # 2. Extracts the prefix to determine the alphabet offset
145
+ # 3. Rotates and reverses the alphabet based on the offset
146
+ # 4. Splits the ID by separator characters (first char of shuffled alphabet)
147
+ # 5. Converts each chunk back to its original number
148
+ # 6. Re-shuffles the alphabet between chunks
149
+ #
150
+ # @param id [String] The Sqids string to decode
151
+ # @return [Array<Integer>] Array of integers that were encoded, or empty array if invalid
152
+ #
153
+ # @example
154
+ # sqids.decode("86Rf07") # => [1, 2, 3]
155
+ # sqids.decode("") # => []
156
+ # sqids.decode("xyz") # => [] (if 'xyz' contains invalid chars)
157
+ #
158
+ # @rbs (String id) -> Array[Integer]
159
+ def decode(id)
160
+ ret = [] #: Array[Integer]
161
+
162
+ return ret if id.empty?
163
+
164
+ # Convert string to array of character codepoints for processing
165
+ id = id.chars.map(&:ord)
166
+
167
+ # Validate that all characters in the ID exist in our alphabet
168
+ # If any character is invalid, return empty array
169
+ id.each do |c|
170
+ return ret unless @alphabet.include?(c)
171
+ end
172
+
173
+ # Extract the prefix (first character) which determines the alphabet transformation
174
+ prefix = id[0]
175
+ offset = @alphabet.index(prefix)
176
+ # If prefix not found in alphabet, return empty (should never happen after validation)
177
+ return [] if offset.nil?
178
+
179
+ # Reconstruct the alphabet used during encoding
180
+ alphabet = rotate_and_reverse_alphabet(@alphabet, offset)
181
+
182
+ # Remove the prefix, leaving only the encoded number segments
183
+ id = id[1, id.length] || [] #: Array[Integer]
184
+
185
+ # Decode each segment separated by the separator character
186
+ while id.length.positive?
187
+ separator = alphabet[0]
188
+ chunks = split_array(id, separator)
189
+ if chunks.any?
190
+ # Empty chunk indicates invalid ID structure
191
+ return ret if chunks[0] == []
192
+
193
+ # Convert the chunk back to its original number
194
+ ret.push(to_number(chunks[0], alphabet))
195
+ # Re-shuffle alphabet before processing next segment (matches encoding)
196
+ alphabet = shuffle(alphabet) if chunks.length > 1
197
+ end
198
+
199
+ # Continue with the next segment, or empty array if no more segments
200
+ id = (chunks.length > 1) ? chunks[1] : []
201
+ end
202
+
203
+ ret
204
+ end
205
+
206
+ private
207
+
208
+ # Splits an array into two parts at the first occurrence of a separator
209
+ #
210
+ # This is used during decoding to split the encoded ID at separator characters,
211
+ # which mark the boundaries between encoded numbers.
212
+ #
213
+ # @param arr [Array<Integer>] The array to split (character codepoints)
214
+ # @param separator [Integer] The separator character codepoint to split on
215
+ # @return [Array<Array<Integer>>] An array containing the left part (before separator)
216
+ # and right part (after separator). If separator not found, returns [arr].
217
+ #
218
+ # @example
219
+ # split_array([1, 2, 3, 4, 5], 3) # => [[1, 2], [4, 5]]
220
+ # split_array([1, 2, 3], 9) # => [[1, 2, 3]]
221
+ #
222
+ # @rbs (Array[Integer] arr, Integer separator) -> Array[Array[Integer]]
223
+ def split_array(arr, separator)
224
+ index = arr.index(separator)
225
+ return [arr] if index.nil?
226
+
227
+ left = arr[0...index] #: Array[Integer]
228
+ right = arr[index + 1..] #: Array[Integer]
229
+
230
+ [left, right]
231
+ end
232
+
233
+ # Shuffles an array of character codepoints using a consistent, deterministic algorithm
234
+ #
235
+ # This is a key part of the Sqids algorithm. The shuffle is deterministic and reversible,
236
+ # meaning the same input always produces the same output. The algorithm performs a series
237
+ # of swaps based on the current index and character values.
238
+ #
239
+ # The shuffle ensures that:
240
+ # - Sequential numbers don't produce sequential IDs
241
+ # - The same alphabet configuration always produces the same shuffle
242
+ # - The transformation is reversible
243
+ #
244
+ # @param chars [Array<Integer>] Array of character codepoints to shuffle
245
+ # @return [Array<Integer>] The shuffled array (modifies in place and returns)
246
+ #
247
+ # @rbs (Array[Integer] chars) -> Array[Integer]
248
+ def shuffle(chars)
249
+ i = 0
250
+ length = chars.length
251
+ j = length - 1
252
+ while j > 0
253
+ r = ((i * j) + chars[i] + chars[j]) % length
254
+ chars[i], chars[r] = chars[r], chars[i]
255
+ i += 1
256
+ j -= 1
257
+ end
258
+
259
+ chars
260
+ end
261
+
262
+ # Internal method to encode an array of numbers into a Sqids string
263
+ #
264
+ # This is the core encoding logic. The process:
265
+ # 1. Calculates an offset based on the numbers and alphabet (ensures uniqueness)
266
+ # 2. Applies an increment if this is a retry (for blocklist filtering)
267
+ # 3. Selects a prefix character from the alphabet at the offset position
268
+ # 4. Rotates and reverses the alphabet based on the offset
269
+ # 5. Encodes each number and separates them with the first shuffled alphabet character
270
+ # 6. Pads to minimum length if needed
271
+ # 7. Checks against blocklist and retries with incremented offset if needed
272
+ #
273
+ # @param numbers [Array<Integer>] Array of integers to encode
274
+ # @param increment [Integer] Retry counter for blocklist collision avoidance (default: 0)
275
+ # @return [String] The encoded Sqids string
276
+ # @raise [ArgumentError] If max retry attempts (alphabet length) is exceeded
277
+ #
278
+ # @rbs (Array[Integer] numbers, ?increment: Integer) -> String
279
+ def encode_numbers(numbers, increment: 0)
280
+ alphabet_length = @alphabet.length
281
+ raise ArgumentError, "Reached max attempts to re-generate the ID" if increment > alphabet_length
282
+
283
+ numbers_length = numbers.length
284
+ offset = numbers_length
285
+ i = 0
286
+ while i < numbers_length
287
+ offset += @alphabet[numbers[i] % alphabet_length] + i
288
+ i += 1
289
+ end
290
+ offset %= alphabet_length
291
+ offset = (offset + increment) % alphabet_length
292
+
293
+ prefix = @alphabet[offset]
294
+ # Now working with modified alphabet
295
+ alphabet = rotate_and_reverse_alphabet(@alphabet, offset)
296
+ id = [prefix]
297
+
298
+ i = 0
299
+ while i < numbers_length
300
+ to_id(id, numbers[i], alphabet)
301
+
302
+ if i < numbers_length - 1
303
+ id.push(alphabet[0])
304
+ alphabet = shuffle(alphabet)
305
+ end
306
+
307
+ i += 1
308
+ end
309
+
310
+ if @min_length > id.length
311
+ id << alphabet[0]
312
+
313
+ while (@min_length - id.length) > 0
314
+ alphabet = shuffle(alphabet)
315
+ slice_length = [@min_length - id.length, alphabet.length].min
316
+ alphabet_slice = alphabet.slice(0, slice_length) #: Array[Integer]
317
+ id.concat alphabet_slice
318
+ end
319
+ end
320
+
321
+ id = id.pack("U*")
322
+
323
+ id = encode_numbers(numbers, increment: increment + 1) if blocked_id?(id)
324
+
325
+ id
326
+ end
327
+
328
+ # Converts a single number into its encoded representation and appends to the ID
329
+ #
330
+ # This implements a base conversion algorithm where:
331
+ # - The base is (alphabet_length - 1) because the first character is reserved as separator
332
+ # - Characters are added at the start_index position (building the number representation)
333
+ # - The process continues until the number is fully converted
334
+ #
335
+ # The algorithm repeatedly:
336
+ # 1. Takes the remainder (mod alphabet_length - 1) to get the next character index
337
+ # 2. Adds 1 to skip the first character (reserved as separator)
338
+ # 3. Inserts the character into the ID
339
+ # 4. Divides the number by the base to continue with the quotient
340
+ #
341
+ # @param id [Array<Integer>] The ID array being built (modified in place)
342
+ # @param num [Integer] The number to encode
343
+ # @param alphabet [Array<Integer>] The alphabet to use for encoding
344
+ # @return [void] Modifies id in place
345
+ #
346
+ # @rbs (Array[Integer] id, Integer num, Array[Integer] alphabet) -> void
347
+ def to_id(id, num, alphabet)
348
+ result = num
349
+ start_index = id.length
350
+ # We are effectively removing the first character of the alphabet, hence the -1 on length and the +1 on the index
351
+ alphabet_length = alphabet.length - 1
352
+ while true # rubocop:disable Style/InfiniteLoop
353
+ new_char_index = (result % alphabet_length) + 1
354
+ new_char = alphabet[new_char_index]
355
+ # id is an array, we want to insert the new char at the start_index position.
356
+ id.insert(start_index, new_char)
357
+ result /= alphabet_length
358
+ break if result <= 0
359
+ end
360
+ end
361
+
362
+ # Converts an encoded ID chunk back into its original number
363
+ #
364
+ # This is the inverse of to_id, implementing base conversion from the custom alphabet
365
+ # back to a decimal integer. It processes each character in the ID chunk, treating it
366
+ # as a digit in a positional number system with base (alphabet_length - 1).
367
+ #
368
+ # The algorithm:
369
+ # 1. Finds each character's index in the alphabet
370
+ # 2. Subtracts 1 (because we added 1 during encoding to skip separator)
371
+ # 3. Multiplies accumulator by base and adds the digit value
372
+ #
373
+ # @param id [Array<Integer>] The encoded ID chunk (character codepoints)
374
+ # @param alphabet [Array<Integer>] The alphabet used during encoding
375
+ # @return [Integer] The decoded number
376
+ # @raise [RuntimeError] If a character is not found in the alphabet
377
+ #
378
+ # @rbs (Array[Integer] id, Array[Integer] alphabet) -> Integer
379
+ def to_number(id, alphabet)
380
+ # We are effectively removing the first character of the alphabet, hence the -1 on length and the -1 on the index
381
+ alphabet_length = alphabet.length - 1
382
+ id.reduce(0) do |a, v|
383
+ v_index = alphabet.index(v)
384
+ raise "Character #{v} not found in alphabet" if v_index.nil?
385
+ (a * alphabet_length) + v_index - 1
386
+ end
387
+ end
388
+
389
+ # Checks if a generated ID contains any blocklisted words
390
+ #
391
+ # The filtering rules vary by word and ID length:
392
+ # - For very short IDs/words (≤3 chars): requires exact match
393
+ # - For words containing digits: checks if ID starts or ends with the word
394
+ # - For other words: checks if word appears anywhere in the ID
395
+ #
396
+ # This helps prevent offensive or inappropriate words from appearing in generated IDs
397
+ # while minimizing false positives.
398
+ #
399
+ # @param id [String] The generated ID to check
400
+ # @return [Boolean] true if the ID contains a blocklisted word, false otherwise
401
+ #
402
+ # @rbs (String id) -> bool
403
+ def blocked_id?(id)
404
+ id = id.downcase
405
+
406
+ @blocklist.any? do |word|
407
+ if word.length <= id.length
408
+ if id.length <= 3 || word.length <= 3
409
+ id == word
410
+ elsif word.match?(/\d/)
411
+ id.start_with?(word) || id.end_with?(word)
412
+ else
413
+ id.include?(word)
414
+ end
415
+ end
416
+ end
417
+ end
418
+
419
+ # Checks if a string contains any multibyte (non-ASCII) characters
420
+ #
421
+ # Sqids requires single-byte characters only because:
422
+ # - The algorithm uses character codepoints (ord) for shuffling and encoding
423
+ # - Multibyte characters would complicate the mathematical operations
424
+ # - Single-byte ensures consistent behavior across different Ruby versions/platforms
425
+ #
426
+ # @param input_str [String] The string to check
427
+ # @return [Boolean] true if any character requires multiple bytes, false otherwise
428
+ #
429
+ # @rbs (String input_str) -> bool
430
+ def contains_multibyte_chars?(input_str)
431
+ input_str.each_char do |char|
432
+ return true if char.bytesize > 1
433
+ end
434
+
435
+ false
436
+ end
437
+
438
+ # Rotates and reverses the alphabet based on an offset
439
+ #
440
+ # This transformation is a crucial part of the Sqids algorithm:
441
+ # - Rotation: moves elements from the start to the end by 'offset' positions
442
+ # - Reversal: reverses the entire array order
443
+ #
444
+ # These operations ensure that:
445
+ # - Different input numbers produce different alphabet arrangements
446
+ # - The transformation is deterministic and reproducible during decoding
447
+ # - Sequential numbers don't produce predictable patterns
448
+ #
449
+ # Both encoder and decoder use this to synchronize their alphabet state.
450
+ #
451
+ # @param alphabet [Array<Integer>] The alphabet to transform (character codepoints)
452
+ # @param offset [Integer] Number of positions to rotate
453
+ # @return [Array<Integer>] A new rotated and reversed alphabet
454
+ #
455
+ # @example
456
+ # rotate_and_reverse_alphabet([1,2,3,4,5], 2)
457
+ # # => [5, 4, 1, 2, 3] (rotated by 2: [3,4,5,1,2], then reversed)
458
+ #
459
+ # @rbs (Array[Integer] alphabet, Integer offset) -> Array[Integer]
460
+ def rotate_and_reverse_alphabet(alphabet, offset)
461
+ rotated_alphabet = alphabet.dup
462
+ rotated_alphabet.rotate!(offset)
463
+ rotated_alphabet.reverse!
464
+ end
465
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rbs_inline: enabled
4
+
5
+ module EncodedId
6
+ module Encoders
7
+ class Sqids < Base
8
+ # @rbs @sqids: untyped
9
+
10
+ # @rbs (String salt, ?Integer min_hash_length, ?Alphabet alphabet, ?Blocklist blocklist) -> void
11
+ def initialize(salt, min_hash_length = 0, alphabet = Alphabet.alphanum, blocklist = Blocklist.empty)
12
+ super
13
+ @sqids = ::MySqids.new(
14
+ {
15
+ min_length: min_hash_length,
16
+ alphabet: alphabet.characters,
17
+ blocklist: blocklist
18
+ }
19
+ )
20
+ rescue TypeError, ArgumentError => e
21
+ raise InvalidInputError, "unable to create sqids instance: #{e.message}"
22
+ end
23
+
24
+ # @rbs (Array[Integer] numbers) -> String
25
+ def encode(numbers)
26
+ numbers.all? { |n| Integer(n) } # raises if conversion fails
27
+ return "" if numbers.empty? || numbers.any? { |n| n < 0 }
28
+
29
+ @sqids.encode(numbers)
30
+ end
31
+
32
+ # @rbs (String hash) -> Array[Integer]
33
+ def decode(hash)
34
+ return [] if hash.nil? || hash.empty?
35
+
36
+ @sqids.decode(hash)
37
+ rescue
38
+ raise InvalidInputError, "unable to unhash"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,15 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # rbs_inline: enabled
4
+
3
5
  module EncodedId
6
+ # @rbs!
7
+ # type encodeableHexValue = Array[String] | String
8
+
4
9
  class HexRepresentation
10
+ # @rbs @hex_digit_encoding_group_size: Integer
11
+ # @rbs @hex_string_separator: Integer
12
+
13
+ # @rbs (Integer hex_digit_encoding_group_size) -> void
5
14
  def initialize(hex_digit_encoding_group_size)
6
15
  @hex_digit_encoding_group_size = validate_hex_digit_encoding_group_size(hex_digit_encoding_group_size)
7
16
  end
8
17
 
18
+ # @rbs (encodeableHexValue hexs) -> Array[Integer]
9
19
  def hex_as_integers(hexs)
10
20
  integer_representation(hexs)
11
21
  end
12
22
 
23
+ # @rbs (Array[Integer] integers) -> Array[String]
13
24
  def integers_as_hex(integers)
14
25
  integers_to_hex_strings(integers)
15
26
  end
@@ -18,6 +29,7 @@ module EncodedId
18
29
 
19
30
  # Number of hex digits to encode in each group, larger values will result in shorter hashes for longer inputs.
20
31
  # Vice versa for smaller values, ie a smaller value will result in smaller hashes for small inputs.
32
+ # @rbs (Integer hex_digit_encoding_group_size) -> Integer
21
33
  def validate_hex_digit_encoding_group_size(hex_digit_encoding_group_size)
22
34
  if !hex_digit_encoding_group_size.is_a?(Integer) || hex_digit_encoding_group_size < 1 || hex_digit_encoding_group_size > 32
23
35
  raise InvalidConfigurationError, "hex_digit_encoding_group_size must be > 0 and <= 32"
@@ -26,9 +38,10 @@ module EncodedId
26
38
  end
27
39
 
28
40
  # Convert hex strings to integer representations
41
+ # @rbs (encodeableHexValue hexs) -> Array[Integer]
29
42
  def integer_representation(hexs)
30
43
  inputs = Array(hexs).map(&:to_s)
31
- digits_to_encode = []
44
+ digits_to_encode = [] #: Array[Integer]
32
45
 
33
46
  inputs.map { |hex_string| hex_string_as_integer_representation(hex_string) }.each do |integer_groups|
34
47
  digits_to_encode.concat(integer_groups)
@@ -41,9 +54,10 @@ module EncodedId
41
54
  end
42
55
 
43
56
  # Convert integer representations to hex strings
57
+ # @rbs (Array[Integer] integers) -> Array[String]
44
58
  def integers_to_hex_strings(integers)
45
- hex_strings = []
46
- hex_string = []
59
+ hex_strings = [] #: Array[String]
60
+ hex_string = [] #: Array[String]
47
61
  add_leading = false
48
62
 
49
63
  integers.reverse_each do |integer|
@@ -62,22 +76,26 @@ module EncodedId
62
76
  hex_strings.reverse
63
77
  end
64
78
 
79
+ # @rbs (String hex_string) -> Array[Integer]
65
80
  def hex_string_as_integer_representation(hex_string)
66
81
  cleaned = remove_non_hex_characters(hex_string)
67
82
  convert_to_integer_groups(cleaned)
68
83
  end
69
84
 
70
85
  # Marker to separate hex strings, must be greater than largest value encoded
86
+ # @rbs return: Integer
71
87
  def hex_string_separator
72
- @hex_string_separator ||= 2.pow(@hex_digit_encoding_group_size * 4)
88
+ @hex_string_separator ||= 2.pow(@hex_digit_encoding_group_size * 4).to_i
73
89
  end
74
90
 
91
+ # @rbs (String hex_string) -> String
75
92
  def remove_non_hex_characters(hex_string)
76
93
  hex_string.gsub(/[^0-9a-f]/i, "")
77
94
  end
78
95
 
96
+ # @rbs (String hex_string_cleaned) -> Array[Integer]
79
97
  def convert_to_integer_groups(hex_string_cleaned)
80
- groups = []
98
+ groups = [] #: Array[Array[String]]
81
99
  hex_string_cleaned.chars.reverse.each_with_index do |char, i|
82
100
  group_id = i / @hex_digit_encoding_group_size
83
101
  groups[group_id] ||= []