Skip to content

Commit

Permalink
✨ feat: strutil - add new base conv func: BaseConv, Base10Conv, BaseC…
Browse files Browse the repository at this point in the history
…onvByTpl
  • Loading branch information
inhere committed Sep 1, 2023
1 parent 1193638 commit b15c388
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
81 changes: 81 additions & 0 deletions strutil/convbase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package strutil

import (
"strconv"
"strings"

"github.com/gookit/goutil/basefn"
)

//
// -------------------- convert base --------------------
//

const (
Base10Chars = "0123456789"
Base16Chars = "0123456789abcdef"
Base32Chars = "0123456789abcdefghjkmnpqrstvwxyz"
Base36Chars = "0123456789abcdefghijklmnopqrstuvwxyz"
Base62Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Base64Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"
)

// Base10Conv convert base 10 string to new base string.
func Base10Conv(src string, to int) string {
return BaseConv(src, 10, to)
}

// BaseConv convert base string by from and to base.
//
// Note: from and to base must be in [2, 64]
//
// Usage:
//
// BaseConv("123", 10, 16) // Output: "7b"
// BaseConv("7b", 16, 10) // Output: "123"
func BaseConv(src string, from, to int) string {
if from > 64 || from < 2 {
return ""
}
if to > 64 || to < 2 {
return ""
}

return BaseConvByTpl(src, Base64Chars[:from], Base64Chars[:to])
}

// BaseConvByTpl convert base string by template.
//
// Usage:
//
// BaseConvert("123", Base62Chars, Base16Chars) // Output: "1e"
// BaseConvert("1e", Base16Chars, Base62Chars) // Output: "123"
func BaseConvByTpl(src string, fromBase, toBase string) string {
if fromBase == toBase {
return src
}

// convert to base 10
var dec uint64 = 0
if fromBase == Base10Chars {
var err error
dec, err = strconv.ParseUint(src, 10, 0)
if err != nil {
basefn.Panicf("input is not a valid decimal number: %s", src)
}
} else {
fLen := uint64(len(fromBase))
for _, c := range src {
dec = dec*fLen + uint64(strings.IndexRune(fromBase, c))
}
}

// convert to new base
var res string
toLen := uint64(len(toBase))
for dec > 0 {
res = string(toBase[dec%toLen]) + res
dec /= toLen
}
return res
}
79 changes: 79 additions & 0 deletions strutil/convbase_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package strutil_test

import (
"testing"

"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil/assert"
)

func TestBaseConv(t *testing.T) {
tests := []struct {
give string
from int
to int
want string
}{
// 10 -> 16
{"123", 10, 16, "7b"},
{"7b", 16, 10, "123"},
// 10 -> 32
{"123", 10, 32, "3r"},
{"3r", 32, 10, "123"},
// 10 -> 62
{"123", 10, 62, "1Z"},
{"1Z", 62, 10, "123"},
// 10 -> 64
{"123", 10, 64, "1X"},
{"1X", 64, 10, "123"},
}

for _, tt := range tests {
t.Run(tt.give, func(t *testing.T) {
got := strutil.BaseConv(tt.give, tt.from, tt.to)
if got != tt.want {
t.Errorf("BaseConv() got = %v, want %v; give=%s", got, tt.want, tt.give)
}
})
}
}

func TestBase10Conv(t *testing.T) {
// fmt.Println(time.Now().Format("20060102150405"))
date := "20230829194900" // seconds
t.Run("date sec", func(t *testing.T) {
base16 := strutil.Base10Conv(date, 16)
assert.Eq(t, "12665b633e94", base16)

base32 := strutil.Base10Conv(date, 32)
assert.Eq(t, "icpdm6fkk", base32)

base62 := strutil.Base10Conv(date, 62)
assert.Eq(t, "5KaR3zRW", base62)

base64 := strutil.Base10Conv(date, 64)
assert.Eq(t, "4CproPWk", base64)
})

// fmt.Println(time.Now().Format("20060102150405.000"))
msDate := "20230829195105843"
t.Run("date ms", func(t *testing.T) {
base16 := strutil.Base10Conv(msDate, 16)
assert.Eq(t, "47dfd4fbaf9633", base16)

base32 := strutil.Base10Conv(msDate, 32)
assert.Eq(t, "huvqjtqv5hj", base32)

base62 := strutil.Base10Conv(msDate, 62)
assert.Eq(t, "1uEL5LJptx", base62)

base64 := strutil.Base10Conv(msDate, 64)
assert.Eq(t, "17TZjXHVoP", base64)
})

t.Run("panic", func(t *testing.T) {
assert.Panics(t, func() {
strutil.Base10Conv("invalid", 16)
})
})
}

0 comments on commit b15c388

Please sign in to comment.