@@ -51,7 +51,13 @@ class Hashids(
51
51
}
52
52
}
53
53
54
- def encode (numbers : Long * ): String = if (numbers.isEmpty) " " else _encode(numbers:_* )
54
+ def encode (numbers : Long * ): String = {
55
+ if (numbers.isEmpty) {
56
+ " "
57
+ } else {
58
+ org.hashids.impl.encode(numbers:_* )(effectiveAlphabet, minHashLength, salt, seps, guards)
59
+ }
60
+ }
55
61
56
62
def encodeHex (in : String ): String = {
57
63
require(in.matches(" ^[0-9a-fA-F]+$" ), " Not a HEX string" )
@@ -66,76 +72,13 @@ class Hashids(
66
72
result
67
73
}
68
74
69
- _encode(doSplit(Nil ):_* )
70
- }
71
-
72
- private def _encode (numbers : Long * ): String = {
73
- val indexedNumbers = numbers.zipWithIndex
74
- val numberHash = indexedNumbers
75
- .foldLeft[Int ](0 ){ case (acc, (x, i)) =>
76
- acc + (x % (i+ 100 )).toInt
77
- }
78
- val lottery = effectiveAlphabet.charAt(numberHash % effectiveAlphabet.length).toString
79
-
80
- val (tmpResult, tmpAlpha) =
81
- indexedNumbers.foldLeft[(String , String )]((lottery, effectiveAlphabet)) {
82
- case ((result, alpha), (x, i)) =>
83
- val buffer = lottery + salt + alpha
84
- val newAlpha = consistentShuffle(alpha, buffer.substring(0 , alpha.length))
85
- val last = hash(x, newAlpha)
86
- val newResult = result + last
87
-
88
- if (i + 1 < numbers.size) {
89
- val num = x % (last.codePointAt(0 ) + i)
90
- val sepsIndex = (num % seps.length).toInt
91
- (newResult + seps.charAt((num % seps.length).toInt), newAlpha)
92
- } else {
93
- (newResult, newAlpha)
94
- }
95
- }
96
-
97
- val provisionalResult = {
98
- if (tmpResult.length < minHashLength) {
99
- val guardIndex = (numberHash + tmpResult.codePointAt(0 )) % guards.length
100
- val guard = guards.charAt(guardIndex)
101
-
102
- val provResult = guard + tmpResult
103
-
104
- if (provResult.length < minHashLength) {
105
- val guardIndex = (numberHash + provResult.codePointAt(2 )) % guards.length
106
- val guard = guards.charAt(guardIndex)
107
- provResult + guard
108
- } else {
109
- provResult
110
- }
111
- } else tmpResult
112
- }
113
-
114
- val halfLen = tmpAlpha.length / 2
115
-
116
- @ tailrec
117
- def respectMinHashLength (alpha : String , res : String ): String = {
118
- if (res.length >= minHashLength) {
119
- res
120
- } else {
121
- val newAlpha = consistentShuffle(alpha, alpha);
122
- val tmpRes = newAlpha.substring(halfLen) + res + newAlpha.substring(0 , halfLen);
123
- val excess = tmpRes.length - minHashLength
124
- val newRes = if (excess > 0 ) {
125
- val startPos = excess / 2
126
- tmpRes.substring(startPos, startPos + minHashLength)
127
- } else tmpRes
128
- respectMinHashLength(newAlpha, newRes)
129
- }
130
- }
131
-
132
- respectMinHashLength(tmpAlpha, provisionalResult)
75
+ org.hashids.impl.encode(doSplit(Nil ):_* )(effectiveAlphabet, minHashLength, salt, seps, guards)
133
76
}
134
77
135
78
def decode (hash : String ): List [Long ] = hash match {
136
79
case " " => Nil
137
80
case x =>
138
- val res = org.hashids.impl.decode(x, effectiveAlphabet, salt, seps, guards)
81
+ val res = org.hashids.impl.decode(x)( effectiveAlphabet, salt, seps, guards)
139
82
if (encode(res:_* ) == hash) res else Nil
140
83
}
141
84
0 commit comments