File tree Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -251,7 +251,10 @@ impl Sqids {
251
251
252
252
let alphabet_without_separator: Vec < char > =
253
253
alphabet. iter ( ) . copied ( ) . skip ( 1 ) . collect ( ) ;
254
- ret. push ( self . to_number ( chunks[ 0 ] , & alphabet_without_separator) ) ;
254
+ match self . to_number ( chunks[ 0 ] , & alphabet_without_separator) {
255
+ Some ( value) => ret. push ( value) ,
256
+ None => ( ) ,
257
+ }
255
258
256
259
if chunks. len ( ) > 1 {
257
260
alphabet = Self :: shuffle ( & alphabet) ;
@@ -332,15 +335,19 @@ impl Sqids {
332
335
id. into_iter ( ) . collect ( )
333
336
}
334
337
335
- fn to_number ( & self , id : & str , alphabet : & [ char ] ) -> u64 {
338
+ fn to_number ( & self , id : & str , alphabet : & [ char ] ) -> Option < u64 > {
336
339
let mut result = 0 ;
337
340
338
341
for c in id. chars ( ) {
339
342
let idx = alphabet. iter ( ) . position ( |& x| x == c) . unwrap ( ) ;
340
- result = result * alphabet. len ( ) as u64 + idx as u64 ;
343
+ result = result * alphabet. len ( ) as u128 + idx as u128 ;
341
344
}
342
345
343
- result
346
+ if result <= u64:: MAX . into ( ) {
347
+ Some ( result. try_into ( ) . unwrap ( ) )
348
+ } else {
349
+ None
350
+ }
344
351
}
345
352
346
353
fn shuffle ( alphabet : & [ char ] ) -> Vec < char > {
Original file line number Diff line number Diff line change
1
+ use sqids:: * ;
2
+
3
+ #[ test]
4
+ fn decode_number_maximum_value ( ) {
5
+ let sqids = Sqids :: default ( ) ;
6
+ let numbers = sqids. decode ( "ABARpJzdz9" ) ;
7
+ assert_eq ! ( numbers, [ 9_007_199_254_740_991 ] ) ; // 2 ^ 53
8
+ }
9
+
10
+ #[ test]
11
+ fn decode_number_overflows ( ) {
12
+ let sqids = Sqids :: default ( ) ;
13
+ let numbers = sqids. decode ( "0J4AEXRN106Z0" ) ;
14
+ assert_eq ! ( numbers, Vec :: <u64 >:: new( ) ) ;
15
+ }
You can’t perform that action at this time.
0 commit comments