Commits

Miki Tebeka committed 0d4d732

encode and 0.2.0

Comments (0)

Files changed (3)

+2014-01-10 version 0.2.2
-# base62 - base62 encoding for Go
+# base62 - base62 encoding/decoding for Go
 
 
 ## About
 
 ## License
 
-    Copyright (C) 2011,2012,2013 Miki Tebeka <miki.tebeka@gmail.com>
+    Copyright (C) 2011-2014 Miki Tebeka <miki.tebeka@gmail.com>
 
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"), to
-/* base62 encoding
+/* base62 encoding/decoding
 
 base62 is usually used to encode short URLs.
 */
 )
 
 const (
-	alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+	Alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 	Version  = "0.2.0"
 )
 
-var base = uint64(len(alphabet))
+var base = uint64(len(Alphabet))
 
 // Encode encodes num to base62 string.
 func Encode(num uint64) string {
 	for num > 0 {
 		rem := num % base
 		num = num / base
-		arr = append(arr, alphabet[rem])
+		arr = append(arr, Alphabet[rem])
 	}
 
 	// Reverse the result array
 
 // Decode decodes base62 string to a number.
 func Decode(b62 string) uint64 {
-	//loc = chars.index
 	size := len(b62)
 	num := uint64(0)
-	base := float64(len(alphabet))
+	base := float64(len(Alphabet))
 
 	for i, ch := range b62 {
-		loc := uint64(strings.IndexRune(b62, ch))
-		pow := uint64(math.Pow(base, float64(size-i+1)))
+		idx := i + 1
+		loc := uint64(strings.IndexRune(Alphabet, ch))
+		pow := uint64(math.Pow(base, float64(size-idx)))
 		num += loc * pow
 	}