初始化项目
This commit is contained in:
119
trunk/goutil/baseUtil/base.go
Normal file
119
trunk/goutil/baseUtil/base.go
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
一个处理不同进制的工具包;用于将十进制和其它进制进行互相转换
|
||||
*/
|
||||
package baseUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// 进制对象定义
|
||||
type Base struct {
|
||||
elementList []string
|
||||
base uint64
|
||||
}
|
||||
|
||||
// 将10进制的uint64类型数据转换为字符串形式
|
||||
// source:10进制的uint64类型数据
|
||||
// 返回值:
|
||||
// 对应进制的字符串形式
|
||||
func (this *Base) Transform(source uint64) (result string) {
|
||||
quotient, remainder := uint64(0), source
|
||||
|
||||
for {
|
||||
quotient, remainder = remainder/this.base, remainder%this.base
|
||||
result = this.elementList[remainder] + result
|
||||
if quotient == 0 {
|
||||
break
|
||||
}
|
||||
remainder = quotient
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 将字符串解析为10进制的uint64类型
|
||||
// source:对应进制的字符串形式
|
||||
// 返回值:10进制的uint64类型数据
|
||||
func (this *Base) Parse(source string) (result uint64) {
|
||||
if source == "" {
|
||||
return
|
||||
}
|
||||
|
||||
sourceList := make([]string, 0, len(source))
|
||||
for _, v := range source {
|
||||
sourceList = append(sourceList, string(v))
|
||||
}
|
||||
|
||||
for idx, exp := len(sourceList)-1, 0; idx >= 0; idx, exp = idx-1, exp+1 {
|
||||
sourceItem := sourceList[idx]
|
||||
|
||||
// Find the source item in the elementList
|
||||
for i, v := range this.elementList {
|
||||
if sourceItem == v {
|
||||
result += uint64(float64(i) * math.Pow(float64(this.base), float64(exp)))
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 以指定的任意非重复的数组,来指定基于的进制数
|
||||
func New(elements string) (baseObj *Base, err error) {
|
||||
if len(elements) == 0 {
|
||||
err = fmt.Errorf("输入的字符数串为空")
|
||||
return
|
||||
}
|
||||
|
||||
elementList := make([]string, 0, len(elements))
|
||||
elementMap := make(map[rune]struct{}, len(elements))
|
||||
for _, v := range elements {
|
||||
if _, exist := elementMap[v]; exist {
|
||||
err = fmt.Errorf("输入的字符串中含有重复的字符:%s", string(v))
|
||||
return
|
||||
} else {
|
||||
elementMap[v] = struct{}{}
|
||||
elementList = append(elementList, string(v))
|
||||
}
|
||||
}
|
||||
|
||||
baseObj = &Base{
|
||||
elementList: elementList,
|
||||
base: uint64(len(elementList)),
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 包含01
|
||||
func NewBase2() (baseObj *Base, err error) {
|
||||
return New("01")
|
||||
}
|
||||
|
||||
// 包含0-7
|
||||
func NewBase8() (baseObj *Base, err error) {
|
||||
return New("01234567")
|
||||
}
|
||||
|
||||
// 包含0-9,a-x
|
||||
func NewBase16() (baseObj *Base, err error) {
|
||||
return New("0123456789abcdef")
|
||||
}
|
||||
|
||||
// 包含a-z
|
||||
func NewBase26() (baseObj *Base, err error) {
|
||||
return New("abcdefghijklmnopqrstuvwxyz")
|
||||
}
|
||||
|
||||
// 包含0-9,a-z
|
||||
func NewBase36() (baseObj *Base, err error) {
|
||||
return New("0123456789abcdefghijklmnopqrstuvwxyz")
|
||||
}
|
||||
|
||||
// 包含0-9,a-z,A-Z
|
||||
func NewBase62() (baseObj *Base, err error) {
|
||||
return New("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
}
|
||||
394
trunk/goutil/baseUtil/base_test.go
Normal file
394
trunk/goutil/baseUtil/base_test.go
Normal file
@@ -0,0 +1,394 @@
|
||||
package baseUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
elements := ""
|
||||
_, err := New(elements)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
elements = "00"
|
||||
_, err = New(elements)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't.")
|
||||
return
|
||||
}
|
||||
|
||||
elements = "01"
|
||||
_, err = New(elements)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is one:%v.", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewBase2(t *testing.T) {
|
||||
_, err := NewBase2()
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is one:%v.", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewBase8(t *testing.T) {
|
||||
_, err := NewBase8()
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is one:%v.", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewBase16(t *testing.T) {
|
||||
_, err := NewBase16()
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is one:%v.", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewBase26(t *testing.T) {
|
||||
_, err := NewBase26()
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is one:%v.", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewBase36(t *testing.T) {
|
||||
_, err := NewBase36()
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is one:%v.", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewBase62(t *testing.T) {
|
||||
_, err := NewBase62()
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is one:%v.", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransform(t *testing.T) {
|
||||
base2, _ := NewBase2()
|
||||
base8, _ := NewBase8()
|
||||
base16, _ := NewBase16()
|
||||
base26, _ := NewBase26()
|
||||
base36, _ := NewBase36()
|
||||
base62, _ := NewBase62()
|
||||
|
||||
var source uint64 = 0
|
||||
expected := "0"
|
||||
got := base2.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "0"
|
||||
got = base8.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "0"
|
||||
got = base16.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "a"
|
||||
got = base26.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "0"
|
||||
got = base36.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "0"
|
||||
got = base62.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
source = 1
|
||||
expected = "1"
|
||||
got = base2.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "1"
|
||||
got = base8.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "1"
|
||||
got = base16.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "b"
|
||||
got = base26.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "1"
|
||||
got = base36.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "1"
|
||||
got = base62.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
source = 2
|
||||
expected = "10"
|
||||
got = base2.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "2"
|
||||
got = base8.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "2"
|
||||
got = base16.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "c"
|
||||
got = base26.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "2"
|
||||
got = base36.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "2"
|
||||
got = base62.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
source = 100
|
||||
expected = "1100100"
|
||||
got = base2.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "144"
|
||||
got = base8.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "64"
|
||||
got = base16.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "dw"
|
||||
got = base26.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "2s"
|
||||
got = base36.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = "1C"
|
||||
got = base62.Transform(source)
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %s, but got %s", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
base2, _ := NewBase2()
|
||||
base8, _ := NewBase8()
|
||||
base16, _ := NewBase16()
|
||||
base26, _ := NewBase26()
|
||||
base36, _ := NewBase36()
|
||||
base62, _ := NewBase62()
|
||||
|
||||
expected := uint64(0)
|
||||
got := base2.Parse("0")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base8.Parse("0")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base16.Parse("0")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base26.Parse("a")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base36.Parse("0")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base62.Parse("0")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = uint64(1)
|
||||
got = base2.Parse("1")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base8.Parse("1")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base16.Parse("1")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base26.Parse("b")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base36.Parse("1")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base62.Parse("1")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = uint64(2)
|
||||
got = base2.Parse("10")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base8.Parse("2")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base16.Parse("2")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base26.Parse("c")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base36.Parse("2")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base62.Parse("2")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
|
||||
expected = uint64(100)
|
||||
got = base2.Parse("1100100")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base8.Parse("144")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base16.Parse("64")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base26.Parse("dw")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base36.Parse("2s")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
got = base62.Parse("1C")
|
||||
if got != expected {
|
||||
t.Errorf("Expected to get %d, but got %d", expected, got)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user