6 Commits

Author SHA1 Message Date
yun ab8524e5ef 优化64进制转换 2024-10-28 21:28:58 +08:00
yun 7a1b81ebf2 优化int64的转换 2024-10-28 21:07:49 +08:00
yun 1be7f79ad3 提交 2024-10-28 20:33:26 +08:00
yun 1e41151c1d 优化类型的定义 2024-10-28 20:02:37 +08:00
Yun 594aae76c6 转int64和转string优化 2024-10-27 22:19:20 +08:00
yun 0baef759a0 优化ToString 2024-08-15 02:43:22 +00:00
10 changed files with 316 additions and 133 deletions
+16 -11
View File
@@ -1,6 +1,8 @@
package convx
import (
"errors"
"fmt"
"strings"
)
@@ -8,13 +10,17 @@ import (
// 10进制的数字转换为64进制的字符串,用于生成短链接
// 64进制的字符集,可以自定义
const base64 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
const base64 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#*"
// DecToBase64 将10进制的数字转换为64进制的字符串
func Encode10To64(n int) string {
if n == 0 {
return "0"
func Encode10To64(n int64) (string, error) {
if n < 0 {
return "", fmt.Errorf("n must be positive")
}
if n == 0 {
return "0", nil
}
s := ""
for n > 0 {
// 取余数,作为64进制的一位
@@ -26,22 +32,21 @@ func Encode10To64(n int) string {
// 除以64,继续下一轮循环
n = n / 64
}
return s
return s, nil
}
// Base64ToDec 将64进制的字符串转换为10进制的数字
func Decode64To10(s string) int {
n := 0
func Decode64To10(s string) (int64, error) {
n := int64(0)
// 遍历字符串的每个字符
for _, c := range s {
// 在字符集中找到字符的索引,作为64进制的一位
i := strings.Index(base64, string(c))
if i == -1 {
// 如果字符不在字符集中,返回-1表示错误
return -1
return 0, errors.New("invalid character: " + string(c))
}
// 累加到结果数字中,每次乘以64
n = n*64 + i
n = n*64 + int64(i)
}
return n
return n, nil
}
+31
View File
@@ -0,0 +1,31 @@
package convx_test
import (
"fmt"
"math"
"testing"
"code.yun.ink/pkg/convx"
)
func Test10to64(t *testing.T) {
var tests = []struct {
in int64
}{
{math.MaxInt64},
{-987654321},
// {-1234567890},
// {-987654321},
{0},
}
for _, test := range tests {
s, err := convx.Encode10To64(test.in)
fmt.Println(test.in, s, err)
n, err := convx.Decode64To10(s)
fmt.Println(n, err)
if n != test.in {
t.Errorf("10to64: %d != %d", n, test.in)
}
}
}
+3 -3
View File
@@ -8,8 +8,8 @@ import (
func Test64(t *testing.T) {
// 测试10进制转64进制
var v_int int = 123456789
str := convx.Encode10To64(v_int)
var v_int int64 = 123456789
str,_ := convx.Encode10To64(v_int)
if str != "7MyqL" {
t.Fail()
t.Log(str)
@@ -17,7 +17,7 @@ func Test64(t *testing.T) {
// 测试64进制转10进制
var v_string string = "7MyqL"
i := convx.Decode64To10(v_string)
i,_ := convx.Decode64To10(v_string)
if i != 123456789 {
t.Fail()
t.Log(i)
+28
View File
@@ -0,0 +1,28 @@
package convx
import (
"fmt"
"strconv"
)
// ToBool converts v to bool.
func ToBool(v interface{}) (bool, error) {
switch v := v.(type) {
case bool:
return v, nil
case string:
if v, err := strconv.ParseBool(v); err == nil {
return v, nil
} else {
return false, err
}
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
i, err := ToInt(v)
if err != nil {
return false, err
}
return i > 0, nil
default:
return false, fmt.Errorf("unsupported type %T", v)
}
}
+24 -19
View File
@@ -8,40 +8,45 @@ import (
// interface 转 float64
func ToFloat64(val interface{}) (f float64, err error) {
if v, ok := val.(string); ok {
f, err = strconv.ParseFloat(v, 64)
} else if v, ok := val.(float64); ok {
switch v := val.(type) {
case nil:
return 0, errors.New("val is nil")
case float32:
return float64(v), nil
case float64:
return v, nil
} else if v, ok := val.(float32); ok {
case int:
return float64(v), nil
} else if v, ok := val.(int); ok {
case int8:
return float64(v), nil
} else if v, ok := val.(int8); ok {
case int16:
return float64(v), nil
} else if v, ok := val.(int16); ok {
case int32:
return float64(v), nil
} else if v, ok := val.(int32); ok {
case int64:
return float64(v), nil
} else if v, ok := val.(int64); ok {
case uint:
return float64(v), nil
} else if v, ok := val.(uint); ok {
case uint8:
return float64(v), nil
} else if v, ok := val.(uint8); ok {
case uint16:
return float64(v), nil
} else if v, ok := val.(uint16); ok {
case uint32:
return float64(v), nil
} else if v, ok := val.(uint32); ok {
case uint64:
return float64(v), nil
} else if v, ok := val.(uint64); ok {
return float64(v), nil
} else if v, ok := val.(bool); ok {
case string:
return strconv.ParseFloat(v, 64)
case bool:
if v {
return 1, nil
} else {
return 0, nil
}
} else {
return 0, errors.New("类型转换失败")
case []byte:
return strconv.ParseFloat(string(v), 64)
}
return
return 0, errors.New("convx.ToFloat64: unknown type")
}
+32 -25
View File
@@ -8,33 +8,40 @@ import (
// interface 转 int
func ToInt(val interface{}) (i int, err error) {
if v, ok := val.(string); ok {
// 不支持小数转换
i, err = strconv.Atoi(v)
} else if v, ok := val.(float32); ok {
i = int(v)
} else if v, ok := val.(float64); ok {
i = int(v)
} else if v, ok := val.(int); ok {
i = v
} else if v, ok := val.(int32); ok {
i = int(v)
} else if v, ok := val.(int64); ok {
i = int(v)
} else if v, ok := val.(uint); ok {
i = int(v)
} else if v, ok := val.(uint32); ok {
i = int(v)
} else if v, ok := val.(uint64); ok {
i = int(v)
} else if v, ok := val.(bool); ok {
switch v := val.(type) {
case nil:
return 0, nil
case string:
return strconv.Atoi(v)
case float32:
return int(v), nil
case float64:
return int(v), nil
case int:
return v, nil
case int8:
return int(v), nil
case int16:
return int(v), nil
case int32:
return int(v), nil
case int64:
return int(v), nil
case uint:
return int(v), nil
case uint8:
return int(v), nil
case uint32:
return int(v), nil
case uint64:
return int(v), nil
case bool:
if v {
i = 1
return 1, nil
} else {
i = 0
return 0, nil
}
} else {
return 0, errors.New("类型转换失败")
}
return
return 0, errors.New("can not convert to int")
}
+47 -31
View File
@@ -2,40 +2,56 @@ package convx
import (
"errors"
"strconv"
"math/big"
)
// interface 转 int64
func ToInt64(val interface{}) (i int64, err error) {
if v, ok := val.(int64); ok {
i = v
} else if v, ok := val.(int32); ok {
i = int64(v)
} else if v, ok := val.(string); ok {
// string 转 int64
// 第二个参数为基数(2~36),第三个参数位大小表示期望转换的结果类型,其值可以为0, 8, 16, 32和64,分别对应 int, int8, int16, int32和int64
i, err = strconv.ParseInt(v, 10, 64)
} else if v, ok := val.(float64); ok {
i = int64(v)
} else if v, ok := val.(float32); ok {
i = int64(v)
} else if v, ok := val.(int); ok {
i = int64(v)
} else if v, ok := val.(uint); ok {
i = int64(v)
} else if v, ok := val.(uint32); ok {
i = int64(v)
} else if v, ok := val.(uint64); ok {
i = int64(v)
} else if v, ok := val.(bool); ok {
if v {
i = 1
} else {
i = 0
}
} else {
err = errors.New("不支持的参数类型")
if val == nil {
return 0, errors.New("value is nil")
}
return
switch v := val.(type) {
case int:
return int64(v), nil
case int8:
return int64(v), nil
case int16:
return int64(v), nil
case int32:
return int64(v), nil
case int64:
return v, nil
case uint:
return int64(v), nil
case uint8:
return int64(v), nil
case uint16:
return int64(v), nil
case uint32:
return int64(v), nil
case uint64:
return int64(v), nil
case float32:
return int64(v), nil
case float64:
return int64(v), nil
case string:
num := new(big.Float)
_, ok := num.SetString(v)
if !ok {
return 0, errors.New("convx.ToInt64: out of range")
}
i, _ := num.Int64()
// fmt.Println(val, i, b)
return i, nil
case bool:
if v {
return 1, nil
} else {
return 0, nil
}
}
return 0, errors.New("convx.ToInt64: unknown type")
}
+48
View File
@@ -0,0 +1,48 @@
package convx_test
import (
"testing"
"code.yun.ink/pkg/convx"
)
func TestToInt64(t *testing.T) {
var tests = []struct {
input interface{}
output int64
isError bool
}{
{"123", 123, false}, // string
{123.456, 123, false}, // float64
{true, 1, false}, // bool
{"abc", 0, true}, // string
{nil, 0, true}, // nil
{123, 123, false}, // int
{int32(123), 123, false}, // int32
{uint64(123), 123, false}, // uint64
{int64(123), 123, false}, // int64
{float32(123.456), 123, false}, // float32
{float64(123.456), 123, false}, // float64
{"-123", -123, false}, // string
{"123.456", 123, false}, // string
{"170141183460469231731687303715884105727", 9223372036854775807, false}, // max int64
{"-170141183460469231731687303715884105728", -9223372036854775808, false}, // min int64
{"9223372036854775807", 9223372036854775807, false}, // max int64
{"-9223372036854775808", -9223372036854775808, false}, // min int64
{"12345678901234567890", 9223372036854775807, false}, // too large
}
for idx, test := range tests {
output, err := convx.ToInt64(test.input)
if test.isError && err == nil {
t.Error(idx, ": ToInt64(", test.input, ") should return error")
continue
} else if !test.isError && err != nil {
t.Error(idx, ": ToInt64(", test.input, ") should not return error", err.Error())
continue
}
if output != test.output {
t.Error("ToInt64(", test.input, ") should be", test.output, "but was", output)
}
}
}
+46 -44
View File
@@ -1,44 +1,46 @@
package convx
import (
"errors"
"strconv"
)
// interface 转 string
func ToString(val interface{}) (str string, err error) {
var s string
if vv, ok := val.(float32); ok {
s = strconv.FormatFloat(float64(vv), 'f', -1, 32)
} else if vv, ok := val.(float64); ok {
s = strconv.FormatFloat(vv, 'f', -1, 64)
} else if vv, ok := val.(int); ok {
s = strconv.Itoa(vv)
} else if vv, ok := val.(int32); ok {
s = strconv.Itoa(int(vv))
} else if vv, ok := val.(int64); ok {
s = strconv.FormatInt(vv, 10)
} else if vv, ok := val.(string); ok {
s = vv
} else if vv, ok := val.(bool); ok {
s = strconv.FormatBool(vv)
} else if vv, ok := val.(uint); ok {
s = strconv.FormatUint(uint64(vv), 10)
} else if vv, ok := val.(uint32); ok {
s = strconv.FormatUint(uint64(vv), 10)
} else if vv, ok := val.(uint64); ok {
s = strconv.FormatUint(vv, 10)
} else if vv, ok := val.(uint8); ok {
s = strconv.FormatUint(uint64(vv), 10)
} else if vv, ok := val.(uint16); ok {
s = strconv.FormatUint(uint64(vv), 10)
} else if vv, ok := val.(int8); ok {
s = strconv.FormatInt(int64(vv), 10)
} else if vv, ok := val.(int16); ok {
s = strconv.FormatInt(int64(vv), 10)
} else {
return s, errors.New("不支持的参数类型")
}
return s, nil
}
package convx
import (
"errors"
"fmt"
"strconv"
)
// interface 转 string
func ToString(val interface{}) (str string, err error) {
switch v := val.(type) {
case nil:
return "", fmt.Errorf("can not convert to string")
case string:
return v, nil
case int:
return strconv.Itoa(v), nil
case int8:
return strconv.FormatInt(int64(v), 10), nil
case int16:
return strconv.FormatInt(int64(v), 10), nil
case int32:
return strconv.FormatInt(int64(v), 10), nil
case int64:
return strconv.FormatInt(v, 10), nil
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32), nil
case float64:
return strconv.FormatFloat(v, 'f', -1, 64), nil
case bool:
return strconv.FormatBool(v), nil
case uint:
return strconv.FormatUint(uint64(v), 10), nil
case uint8:
return strconv.FormatUint(uint64(v), 10), nil
case uint16:
return strconv.FormatUint(uint64(v), 10), nil
case uint32:
return strconv.FormatUint(uint64(v), 10), nil
case uint64:
return strconv.FormatUint(v, 10), nil
}
return "", errors.New("can not convert to string")
}
+41
View File
@@ -0,0 +1,41 @@
package convx_test
import (
"testing"
"code.yun.ink/pkg/convx"
)
func TestToString(t *testing.T) {
var tests = []struct {
input interface{}
expect string
}{
{"1", "1"}, //string
{1, "1"}, //int
{int8(1), "1"}, //int8
{int16(1), "1"}, //int16
{-1, "-1"}, //int32
{int64(1), "1"}, //int64
{float32(1.1), "1.1"}, //float32
{float64(1.1), "1.1"}, //float64
{true, "true"}, //bool
{uint(1), "1"}, //uint
{uint8(1), "1"}, //uint8
{uint16(1), "1"}, //uint16
{uint32(1), "1"}, //uint32
{uint64(1), "1"}, //uint64
}
for _, test := range tests {
str, err := convx.ToString(test.input)
if err != nil {
t.Errorf("convx.ToString(%v) failed with %v", test.input, err)
}
if str != test.expect {
t.Errorf("convx.ToString(%v) = %v, want %v", test.input, str, test.expect)
}
}
}