Skip to content

Commit

Permalink
Rename base58.NewMustEncoder to base58.MustNewEncoder (#25)
Browse files Browse the repository at this point in the history
* Add a package document

* Rename base58.NewMustEncoder to base58.MustNewEncoder
  • Loading branch information
osamingo authored Oct 15, 2016
1 parent 8393547 commit 0c05b7f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 4 additions & 0 deletions _example/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Package main is a example of osamingo/indigo.
*/
package main
14 changes: 6 additions & 8 deletions base58/base58.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ type Encoder struct {
decodeMap [256]int
}

const encodeStd = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
// StdEncoding is Base58 Encoder.
var StdEncoding = MustNewEncoder("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")

// StdEncoding is Base58(Sortable) Encoder.
var StdEncoding = NewMustEncoder(encodeStd)

// NewMustEncoder returns new base58.Encoder.
func NewMustEncoder(source string) *Encoder {
// MustNewEncoder returns new base58.Encoder.
func MustNewEncoder(source string) *Encoder {
enc, err := NewEncoder(source)
if err != nil {
panic(err)
Expand Down Expand Up @@ -70,14 +68,14 @@ func (enc *Encoder) Encode(id uint64) string {
func (enc *Encoder) Decode(id string) (uint64, error) {

if id == "" {
return 0, errors.New("base58: ID should not be empty")
return 0, errors.New("base58: id should not be empty")
}

var n uint64
for i := range id {
u := enc.decodeMap[id[i]]
if u < 0 {
return 0, errors.New("base58: invalid character = " + string(id[i]))
return 0, errors.New("base58: invalid character - " + string(id[i]))
}
n = n*58 + uint64(u)
}
Expand Down
8 changes: 4 additions & 4 deletions base58/base58_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ var bc = map[uint64]string{
math.MaxUint64: "jpXCZedGfVQ",
}

func TestNewMustEncoder(t *testing.T) {
func TestMustNewEncoder(t *testing.T) {

var enc *Encoder
require.NotPanics(t, func() {
enc = NewMustEncoder("rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz")
enc = MustNewEncoder("rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz")
})
require.NotNil(t, enc)

require.Panics(t, func() {
NewMustEncoder("")
MustNewEncoder("")
})

require.Panics(t, func() {
NewMustEncoder("test")
MustNewEncoder("test")
})
}

Expand Down

0 comments on commit 0c05b7f

Please sign in to comment.