Skip to content

Commit

Permalink
encode(0) returns "0" (martinlindhe#5)
Browse files Browse the repository at this point in the history
* encode(0) returns "0"

* fixed change request
  • Loading branch information
buzzfrog authored and martinlindhe committed Jul 15, 2019
1 parent 7c6542d commit 8e4d8e1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions base36.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ var (
func Encode(value uint64) string {
var res [16]byte
var i int
for i = len(res) - 1; value != 0; i-- {
for i = len(res) - 1; ; i-- {
res[i] = base36[value%36]
value /= 36
if value == 0 {
break
}
}
return string(res[i+1:])

return string(res[i:])
}

// Decode decodes a base36-encoded string.
Expand Down
2 changes: 1 addition & 1 deletion base36_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var raw = []uint64{0, 50, 100, 999, 1000, 1111, 5959, 99999,
123456789, 5481594952936519619, math.MaxInt64 / 2048, math.MaxInt64 / 512,
math.MaxInt64, math.MaxUint64}

var encoded = []string{"", "1E", "2S", "RR", "RS", "UV", "4LJ", "255R",
var encoded = []string{"0", "1E", "2S", "RR", "RS", "UV", "4LJ", "255R",
"21I3V9", "15N9Z8L3AU4EB", "18CE53UN18F", "4XDKKFEK4XR",
"1Y2P0IJ32E8E7", "3W5E11264SGSF"}

Expand Down
1 change: 1 addition & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/martinlindhe/base36"

)

func ExampleEncode() {
Expand Down

0 comments on commit 8e4d8e1

Please sign in to comment.