@@ -69,9 +69,9 @@ _hash() {
6969 local len_alphabet=" ${# alphabet} "
7070
7171 while true ; do
72- hashed=" ${alphabet: $(( $ number % $ len_alphabet )): 1}${hashed} "
73- number=$(( $ number / $ len_alphabet ))
74- if (( $ number == 0 )) ; then
72+ hashed=" ${alphabet: $(( number % len_alphabet )): 1}${hashed} "
73+ number=$(( number / len_alphabet ))
74+ if (( number == 0 )) ; then
7575 __RETURN=" $hashed "
7676 break
7777 fi
@@ -85,18 +85,17 @@ _unhash() {
8585
8686 local char
8787 local number=0
88- local position
8988 local len_hash=" ${# hashed} "
9089 local len_alphabet=" ${# alphabet} "
9190
92- for (( i= 0 ; i < $ len_hash ; i++ )) ; do
91+ for (( i= 0 ; i < len_hash; i++ )) ; do
9392 char=" ${hashed: $i : 1} "
9493 _indexof " $alphabet " " $char "
95- if (( $ __RETURN == - 1 )) ; then
94+ if (( __RETURN == - 1 )) ; then
9695 echo " error: invalid hashid" >&2
9796 exit 1
9897 fi
99- let number+=$(( $ __RETURN * $ len_alphabet ** $(( $ len_hash - $ i - 1 )) ))
98+ let number+=$(( __RETURN * len_alphabet ** $(( len_hash - i - 1 )) ))
10099 done
101100
102101 __RETURN=" $number "
@@ -116,27 +115,25 @@ _reorder() {
116115 local j
117116 local index=0
118117 local integer_sum=0
119- local salt_index
120118 local temp
121119 local trailer
122-
123120
124- while (( $ i > 0 )) ; do
125- index=$(( $ index % ${# salt} ))
121+ while (( i > 0 )) ; do
122+ index=$(( index % ${# salt} ))
126123 _ordinal " ${salt: $index : 1} "
127124 let integer_sum+=$__RETURN
128- j=$(( $(( $ __RETURN + $ index + $ integer_sum )) % $ i ))
125+ j=$(( $(( __RETURN + index + integer_sum )) % i ))
129126
130127 temp=" ${string: $j : 1} "
131128 trailer=" "
132- if (( $ j + 1 < ${# string} )) ; then
133- trailer=" ${string: $(( $ j + 1 ))} "
129+ if (( j + 1 < ${# string} )) ; then
130+ trailer=" ${string: $(( j + 1 ))} "
134131 fi
135132
136133 string=" ${string: 0: $j }${string: $i : 1}${trailer} "
137- string=" ${string: 0: $i }${temp}${string: $(( $ i + 1 ))} "
134+ string=" ${string: 0: $i }${temp}${string: $(( i + 1 ))} "
138135
139- i=$(( $ i - 1 ))
136+ i=$(( i - 1 ))
140137 let index+=1
141138 done
142139
@@ -153,30 +150,30 @@ _ensure_length() {
153150
154151 local len_guards=" ${# guards} "
155152 _ordinal " ${encoded: 0: 1} "
156- local guard_index=$(( $(( $ values_hash + $ __RETURN )) % $ len_guards ))
153+ local guard_index=$(( $(( values_hash + __RETURN )) % len_guards ))
157154 encoded=" ${guards: ${guard_index} : 1}${encoded} "
158155
159- if (( "${# encoded} " < $ min_length )) ; then
156+ if (( "${# encoded} " < min_length )) ; then
160157 _ordinal " ${encoded: 2: 1} "
161- guard_index=$(( $(( $ values_hash + $ __RETURN )) % $ len_guards ))
158+ guard_index=$(( $(( values_hash + __RETURN )) % len_guards ))
162159 encoded=" ${encoded}${guards: ${guard_index} : 1} "
163160 fi
164161
165162 local len_alphabet=" ${# alphabet} "
166- local split_at=$(( $ len_alphabet / 2 ))
163+ local split_at=$(( len_alphabet / 2 ))
167164 local excess
168165 local from_index
169166 local encoded_len
170167
171- while (( "${# encoded} " < $ min_length )) ; do
168+ while (( "${# encoded} " < min_length )) ; do
172169 _reorder " $alphabet " " $alphabet " > /dev/null
173170 alphabet=" $__RETURN "
174171 encoded=" ${alphabet: ${split_at} }${encoded}${alphabet: 0: ${split_at} } "
175172
176173 encoded_len=" ${# encoded} "
177- excess=$(( $ encoded_len - $ min_length ))
178- if (( $ excess > 0 )) ; then
179- from_index=$(( $ excess / 2 ))
174+ excess=$(( encoded_len - min_length ))
175+ if (( excess > 0 )) ; then
176+ from_index=$(( excess / 2 ))
180177 encoded=" ${encoded: ${from_index} : ${min_length} } "
181178 fi
182179 done
@@ -199,13 +196,13 @@ _encode() {
199196 local hashed
200197
201198 for (( i= 0 ; i < ${# values[@]} ; i++ )) ; do
202- hashed=$(( ${values[$i]} % $(( $ i + 100 )) ))
203- if (( $ hashed > 0 )) ; then
199+ hashed=$(( ${values[$i]} % $(( i + 100 )) ))
200+ if (( hashed > 0 )) ; then
204201 let values_hash+=$hashed
205202 fi
206203 done
207204
208- local encoded=" ${alphabet: $(( $ values_hash % $ len_alphabet )): 1} "
205+ local encoded=" ${alphabet: $(( values_hash % len_alphabet )): 1} "
209206 local lottery=" $encoded "
210207
211208 local last
@@ -221,13 +218,13 @@ _encode() {
221218 last=" $__RETURN "
222219 encoded=" ${encoded}${last} "
223220 _ordinal " ${last: 0: 1} "
224- value=$(( $ value % $(( $ __RETURN + $ i )) ))
225- encoded=" ${encoded}${separators: $(( $ value % $ len_separators )): 1} "
221+ value=$(( value % $(( __RETURN + i )) ))
222+ encoded=" ${encoded}${separators: $(( value % len_separators )): 1} "
226223 done
227224
228225 encoded=" ${encoded: 0: $(( ${# encoded} - 1 ))} "
229226
230- if (( ${# encoded} >= $ min_length )) ; then
227+ if (( ${# encoded} >= min_length )) ; then
231228 echo " $encoded "
232229 else
233230 _ensure_length " $encoded " " $min_length " " $alphabet " " $guards " " $values_hash "
@@ -263,7 +260,7 @@ _decode() {
263260 local unhashed
264261
265262 _split " $hashid " " $separators "
266- for part in ${__RETURN[@]} ; do
263+ for part in " ${__RETURN[@]} " ; do
267264 alphabet_salt=" ${lottery_char}${salt}${alphabet} "
268265 alphabet_salt=" ${alphabet_salt: 0: ${# alphabet} } "
269266 _reorder " $alphabet " " $alphabet_salt "
@@ -286,7 +283,7 @@ _getparams() {
286283 local separators=" "
287284 for (( i= 0 ; i < "${# seps} "; i++ )) ; do
288285 _indexof " $alphabet " " ${seps: $i : 1} "
289- if (( $ __RETURN >= 0 )) ; then
286+ if (( __RETURN >= 0 )) ; then
290287 separators=" ${separators}${seps: $i : 1} "
291288 fi
292289 done
@@ -299,7 +296,7 @@ _getparams() {
299296 local ret1=$__RETURN
300297 _indexof " $separators " " $x "
301298 local ret2=$__RETURN
302- if (( $ ret1 == $ i )) && (( $ ret2 == - 1 )) ; then
299+ if (( ret1 == i )) && (( ret2 == - 1 )) ; then
303300 _alphabet=" ${_alphabet}${x} "
304301 fi
305302 done
@@ -309,23 +306,23 @@ _getparams() {
309306 local len_separators=" ${# separators} "
310307
311308
312- if (( $ len_alphabet + $ len_separators < 16 )) ; then
309+ if (( len_alphabet + len_separators < 16 )) ; then
313310 echo " error: alphabet must contain at least 16 unique characters" >&2
314311 exit 1
315312 fi
316313
317314 _reorder " $separators " " $salt "
318315 separators=" $__RETURN "
319- _ceil $len_alphabet $RATIO_SEPARATORS
320- local min_seperators=$__RETURN
316+ _ceil " $len_alphabet " " $RATIO_SEPARATORS "
317+ local min_seperators=" $__RETURN "
321318
322- if [[ -z " $separators " ]] || (( $ len_separators < $ min_seperators )) ; then
323- if (( $ min_seperators == 1 )) ; then
319+ if [[ -z " $separators " ]] || (( len_separators < min_seperators )) ; then
320+ if (( min_seperators == 1 )) ; then
324321 min_seperators=2
325322 fi
326323
327- if (( $ min_seperators > $ len_separators )) ; then
328- local split_at=$(( $ min_seperators - $ len_separators ))
324+ if (( min_seperators > len_separators )) ; then
325+ local split_at=$(( min_seperators - len_separators ))
329326 separators=" ${separators}${alphabet: 0: ${split_at} } "
330327 alphabet=" ${alphabet: ${split_at} } "
331328 len_alphabet=" ${# alphabet} "
@@ -334,9 +331,9 @@ _getparams() {
334331
335332 _reorder " $alphabet " " $salt "
336333 alphabet=" $__RETURN "
337- _ceil $len_alphabet $RATIO_GUARDS
334+ _ceil " $len_alphabet " " $RATIO_GUARDS "
338335 local num_guards=$__RETURN
339- if (( $ len_alphabet < 3 )) ; then
336+ if (( len_alphabet < 3 )) ; then
340337 guards=" ${separators: 0: ${num_guards} } "
341338 separators=" ${separators: ${num_guards} } "
342339 else
@@ -370,10 +367,10 @@ encode() {
370367 esac
371368 done
372369
373- shift $(( $ OPTIND - 1 ))
370+ shift $(( OPTIND - 1 ))
374371
375372 _getparams " $salt " " $min_length " " $alphabet "
376- local params=(${__RETURN[@]} )
373+ local params=(" ${__RETURN[@]} " )
377374 alphabet=" ${params[0]} "
378375 local separators=" ${params[1]} "
379376 local guards=" ${params[2]} "
@@ -402,7 +399,7 @@ decode() {
402399 l) min_length=" $OPTARG " ;;
403400 esac
404401 done
405- shift $(( $ OPTIND - 1 ))
402+ shift $(( OPTIND - 1 ))
406403
407404 local hashid=" $1 "
408405
@@ -412,7 +409,7 @@ decode() {
412409 fi
413410
414411 _getparams " $salt " " $min_length " " $alphabet "
415- local params=(${__RETURN[@]} )
412+ local params=(" ${__RETURN[@]} " )
416413 alphabet=" ${params[0]} "
417414 local separators=" ${params[1]} "
418415 local guards=" ${params[2]} "
@@ -468,20 +465,20 @@ main() {
468465 esac
469466 done
470467
471- if (( $ OPTIND == 1 )) ; then
468+ if (( OPTIND == 1 )) ; then
472469 usage
473470 fi
474471
475- shift $(( $ OPTIND - 1 ))
472+ shift $(( OPTIND - 1 ))
476473
477474 if [[ -z " $mode " ]]; then
478475 echo " error: need a mode. either -e (encode) or -d (decode)" >&2
479476 exit 1
480477 fi
481478
482479 case " $mode " in
483- encode) encode -s " $salt " -a " $alphabet " -l " $min_length " $* ;;
484- decode) decode -s " $salt " -a " $alphabet " -l " $min_length " $* ;;
480+ encode) encode -s " $salt " -a " $alphabet " -l " $min_length " " $@ " ;;
481+ decode) decode -s " $salt " -a " $alphabet " -l " $min_length " " $@ " ;;
485482 esac
486483}
487484
0 commit comments