@@ -54,14 +54,10 @@ public Hashids(
5454 string alphabet = DEFAULT_ALPHABET ,
5555 string seps = DEFAULT_SEPS )
5656 {
57- if ( salt == null )
58- throw new ArgumentNullException ( nameof ( salt ) ) ;
59- if ( string . IsNullOrWhiteSpace ( alphabet ) )
60- throw new ArgumentNullException ( nameof ( alphabet ) ) ;
61- if ( minHashLength < 0 )
62- throw new ArgumentOutOfRangeException ( nameof ( minHashLength ) , "Value must be zero or greater." ) ;
63- if ( string . IsNullOrWhiteSpace ( seps ) )
64- throw new ArgumentNullException ( nameof ( seps ) ) ;
57+ if ( salt == null ) throw new ArgumentNullException ( nameof ( salt ) ) ;
58+ if ( minHashLength < 0 ) throw new ArgumentOutOfRangeException ( nameof ( minHashLength ) , "Value must be zero or greater." ) ;
59+ if ( string . IsNullOrWhiteSpace ( alphabet ) ) throw new ArgumentNullException ( nameof ( alphabet ) ) ;
60+ if ( string . IsNullOrWhiteSpace ( seps ) ) throw new ArgumentNullException ( nameof ( seps ) ) ;
6561
6662 _salt = salt . Trim ( ) . ToCharArray ( ) ;
6763 _minHashLength = minHashLength ;
@@ -75,26 +71,18 @@ private static (char[] alphabetChars, char[] sepChars, char[] guardChars) InitCh
7571 var guardChars = new char [ 0 ] ;
7672
7773 if ( alphabetChars . Length < MIN_ALPHABET_LENGTH )
78- {
7974 throw new ArgumentException ( $ "Alphabet must contain at least { MIN_ALPHABET_LENGTH : N0} unique characters.", paramName : nameof ( alphabet ) ) ;
80- }
8175
8276 // seperator characters can only be chosen from the characters in the alphabet
8377 if ( sepChars . Length > 0 )
84- {
8578 sepChars = sepChars . Intersect ( alphabetChars ) . ToArray ( ) ;
86- }
8779
8880 // once separator characters are chosen, they must be removed from the alphabet availble for hash generation
8981 if ( sepChars . Length > 0 )
90- {
9182 alphabetChars = alphabetChars . Except ( sepChars ) . ToArray ( ) ;
92- }
9383
9484 if ( alphabetChars . Length < ( MIN_ALPHABET_LENGTH - 6 ) )
95- {
9685 throw new ArgumentException ( $ "Alphabet must contain at least { MIN_ALPHABET_LENGTH : N0} unique characters that are also not present in .", paramName : nameof ( alphabet ) ) ;
97- }
9886
9987 ConsistentShuffle ( alphabet : sepChars , alphabetLength : sepChars . Length , salt : salt , saltLength : salt . Length ) ;
10088
@@ -103,9 +91,7 @@ private static (char[] alphabetChars, char[] sepChars, char[] guardChars) InitCh
10391 var sepsLength = ( int ) Math . Ceiling ( ( float ) alphabetChars . Length / SEP_DIV ) ;
10492
10593 if ( sepsLength == 1 )
106- {
10794 sepsLength = 2 ;
108- }
10995
11096 if ( sepsLength > sepChars . Length )
11197 {
@@ -268,7 +254,8 @@ public virtual string EncodeHex(string hex)
268254 return string . Empty ;
269255
270256 var matches = hexSplitter . Value . Matches ( hex ) ;
271- if ( matches . Count == 0 ) return string . Empty ;
257+ if ( matches . Count == 0 )
258+ return string . Empty ;
272259
273260 var numbers = new long [ matches . Count ] ;
274261 for ( int i = 0 ; i < numbers . Length ; i ++ )
@@ -295,11 +282,8 @@ public virtual string DecodeHex(string hash)
295282 foreach ( var number in numbers )
296283 {
297284 var s = number . ToString ( "X" ) ;
298-
299285 for ( var i = 1 ; i < s . Length ; i ++ )
300- {
301286 builder . Append ( s [ i ] ) ;
302- }
303287 }
304288
305289 var result = builder . ToString ( ) ;
@@ -312,21 +296,19 @@ private string GenerateHashFrom(ReadOnlySpan<long> numbers)
312296 if ( numbers . Length == 0 )
313297 return string . Empty ;
314298
315- foreach ( var item in numbers )
316- if ( item < 0 )
299+ foreach ( var num in numbers )
300+ if ( num < 0 )
317301 return string . Empty ;
318302
319303 long numbersHashInt = 0 ;
320304 for ( var i = 0 ; i < numbers . Length ; i ++ )
321- {
322305 numbersHashInt += numbers [ i ] % ( i + 100 ) ;
323- }
324306
325307 var builder = _sbPool . Get ( ) ;
326-
327308 char [ ] shuffleBuffer = null ;
328- var alphabet = _alphabet . CopyPooled ( ) ;
329309 var hashBuffer = ArrayPool < char > . Shared . Rent ( MaxNumberHashLength ) ;
310+ var alphabet = _alphabet . CopyPooled ( ) ;
311+
330312 try
331313 {
332314 var lottery = alphabet [ numbersHashInt % _alphabet . Length ] ;
@@ -341,17 +323,13 @@ private string GenerateHashFrom(ReadOnlySpan<long> numbers)
341323 var number = numbers [ i ] ;
342324
343325 if ( length > 0 )
344- {
345326 Array . Copy ( alphabet , 0 , shuffleBuffer , startIndex , length ) ;
346- }
347327
348328 ConsistentShuffle ( alphabet , _alphabet . Length , shuffleBuffer , _alphabet . Length ) ;
349329 var hashLength = BuildReversedHash ( number , alphabet , hashBuffer ) ;
350330
351331 for ( var j = hashLength - 1 ; j > - 1 ; j -- )
352- {
353332 builder . Append ( hashBuffer [ j ] ) ;
354- }
355333
356334 if ( i + 1 < numbers . Length )
357335 {
@@ -486,9 +464,7 @@ private long[] GetNumbersFrom(string hash)
486464 }
487465
488466 if ( EncodeLong ( result ) == hash )
489- {
490467 return result ;
491- }
492468
493469 return Array . Empty < long > ( ) ;
494470 }
0 commit comments