Skip to content

Commit

Permalink
Merge pull request #45 from acexy/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
acexy authored Oct 30, 2024
2 parents 0786f99 + 30fa559 commit 268627f
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 44 deletions.
4 changes: 2 additions & 2 deletions math/conversion/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (b *Binary) To2Hex() (string, error) {
return appendLeftZero(b.ToHexValue(), 2), nil
}

// NewFormBytes 通过bytes转换
func NewFormBytes(bytes []byte) *Binaries {
// NewFromBytes 通过bytes转换
func NewFromBytes(bytes []byte) *Binaries {
bs := make([]*Binary, len(bytes))
for i, v := range bytes {
bs[i] = NewFromByte(v)
Expand Down
2 changes: 1 addition & 1 deletion math/conversion/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestNewFromRawHex(t *testing.T) {
}

func TestNewFormBytes(t *testing.T) {
bs := NewFormBytes([]byte{255, 0, 3, 45, 251})
bs := NewFromBytes([]byte{255, 0, 3, 45, 251})
fmt.Println(bs.To8Bits(","))
fmt.Println(bs.To2Hex(","))
}
201 changes: 171 additions & 30 deletions math/conversion/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,101 +5,242 @@ import (
"strconv"
)

func parseInt(value string, bit int) int64 {
v, err := strconv.ParseInt(value, 10, bit)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return v
func parseInt(value string, bit int) (int64, error) {
return strconv.ParseInt(value, 10, bit)
}

func parseUint(value string, bit int) (uint64, error) {
return strconv.ParseUint(value, 10, bit)
}

func parseUint(value string, bit int) uint64 {
v, err := strconv.ParseUint(value, 10, bit)
func parseFloat(value string, bit int) (float64, error) {
return strconv.ParseFloat(value, bit)
}

// ParseInt 将字符串转换为int
func ParseInt(value string) int {
v, err := strconv.Atoi(value)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return v
}

func parseFloat(value string, bit int) float64 {
v, err := strconv.ParseFloat(value, bit)
// ParseIntPanic 将字符串转换为int 异常将触发panic
func ParseIntPanic(value string) int {
v, err := strconv.Atoi(value)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
panic(err)
}
return v
}

// ParseInt 将字符串转换为int
func ParseInt(value string) int {
v, err := strconv.Atoi(value)
// ParseUint 将字符串转换为uint
func ParseUint(value string) uint {
v, err := parseUint(value, 0)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return v
return uint(v)
}

// ParseUint 将字符串转换为uint
func ParseUint(value string) uint {
v := parseUint(value, 0)
// ParseUintPanic 将字符串转换为uint 异常将触发panic
func ParseUintPanic(value string) uint {
v, err := parseUint(value, 0)
if err != nil {
panic(err)
}
return uint(v)
}

// ParseInt8 将字符串转换为int8
func ParseInt8(value string) int8 {
v := parseInt(value, 8)
v, err := parseInt(value, 8)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return int8(v)
}

// ParseInt8Panic 将字符串转换为int8 异常将触发panic
func ParseInt8Panic(value string) int8 {
v, err := parseInt(value, 8)
if err != nil {
panic(err)
}
return int8(v)
}

// ParseUint8 将字符串转换为uint8
func ParseUint8(value string) uint8 {
v := parseUint(value, 8)
v, err := parseUint(value, 8)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return uint8(v)
}

// ParseUint8Panic 将字符串转换为uint8 异常将触发panic
func ParseUint8Panic(value string) uint8 {
v, err := parseUint(value, 8)
if err != nil {
panic(err)
}
return uint8(v)
}

// ParseInt16 将字符串转换为int16
func ParseInt16(value string) int16 {
v := parseInt(value, 16)
v, err := parseInt(value, 16)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return int16(v)
}

// ParseInt16Panic 将字符串转换为int16 异常将触发panic
func ParseInt16Panic(value string) int16 {
v, err := parseInt(value, 16)
if err != nil {
panic(err)
}
return int16(v)
}

// ParseUint16 将字符串转换为uint16
func ParseUint16(value string) uint16 {
v := parseUint(value, 16)
v, err := parseUint(value, 16)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return uint16(v)
}

// ParseUint16Panic 将字符串转换为uint16 异常将触发panic
func ParseUint16Panic(value string) uint16 {
v, err := parseUint(value, 16)
if err != nil {
panic(err)
}
return uint16(v)
}

// ParseInt32 将字符串转换为int32
func ParseInt32(value string) int32 {
v := parseInt(value, 32)
v, err := parseInt(value, 32)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return int32(v)
}

// ParseInt32Panic 将字符串转换为int32 异常将触发panic
func ParseInt32Panic(value string) int32 {
v, err := parseInt(value, 32)
if err != nil {
panic(err)
}
return int32(v)
}

// ParseUint32 将字符串转换为uint32
func ParseUint32(value string) uint32 {
v := parseUint(value, 32)
v, err := parseUint(value, 32)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return uint32(v)
}

// ParseUint32Panic 将字符串转换为uint32 异常将触发panic
func ParseUint32Panic(value string) uint32 {
v, err := parseUint(value, 32)
if err != nil {
panic(err)
}
return uint32(v)
}

// ParseInt64 将字符串转换为int64
func ParseInt64(value string) int64 {
return parseInt(value, 64)
v, err := parseInt(value, 64)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return v
}

// ParseInt64Panic 将字符串转换为int64 异常将触发panic
func ParseInt64Panic(value string) int64 {
v, err := parseInt(value, 64)
if err != nil {
panic(err)
}
return v
}

// ParseUint64 将字符串转换为uint64
func ParseUint64(value string) uint64 {
return parseUint(value, 64)
v, err := parseUint(value, 64)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return v
}

// ParseUint64Panic 将字符串转换为uint64 异常将触发panic
func ParseUint64Panic(value string) uint64 {
v, err := parseUint(value, 64)
if err != nil {
panic(err)
}
return v
}

// ParseFloat32 将字符串转换为float32
func ParseFloat32(value string) float32 {
return float32(parseFloat(value, 32))
v, err := parseFloat(value, 32)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return float32(v)
}

// ParseFloat32Panic 将字符串转换为float32 异常将触发panic
func ParseFloat32Panic(value string) float32 {
v, err := parseFloat(value, 32)
if err != nil {
panic(err)
}
return float32(v)
}

// ParseFloat64 将字符串转换为float64
func ParseFloat64(value string) float64 {
return parseFloat(value, 64)
v, err := parseFloat(value, 64)
if err != nil {
logger.Logrus().Warningln("parse string to numeric error. string:", value)
return 0
}
return v
}

// ParseFloat64Panic 将字符串转换为float64 异常将触发panic
func ParseFloat64Panic(value string) float64 {
v, err := parseFloat(value, 64)
if err != nil {
panic(err)
}
return v
}
7 changes: 3 additions & 4 deletions util/coll/sliceutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package coll

// SliceContains 检查指定的元素是否存在切片中
func SliceContains[T comparable](slice []T, target T, compare ...func(T, T) bool) bool {
// compare 比较函数,如果为空 则直接使用值比较 func(*T, *T) 第一个参数为切片中元素, 第二个参数为目标元素
for i := range slice {
if len(compare) > 0 && compare[0] != nil {
if compare[0](slice[i], target) {
Expand Down Expand Up @@ -130,8 +129,8 @@ func SliceComplement[T comparable](slicePart1, slicePart2 []T, compare ...func(p
return result
}

// SliceToMap 将切片按照指定的过滤处理形成map
func SliceToMap[T any, K comparable, V any](slice []T, filter func(T) (K, V, bool)) map[K]V {
// SliceFilterToMap 将切片按照指定的过滤处理形成map
func SliceFilterToMap[T any, K comparable, V any](slice []T, filter func(T) (K, V, bool)) map[K]V {
if len(slice) == 0 {
return nil
}
Expand All @@ -145,7 +144,7 @@ func SliceToMap[T any, K comparable, V any](slice []T, filter func(T) (K, V, boo
return result
}

// SliceCollect 将切片按照指定的映射处理为一个新的切片
// SliceCollect 将切片按照指定的采集映射方法处理为一个新的切片
func SliceCollect[T, R any](input []T, mapFn func(T) R) []R {
if len(input) == 0 {
return nil
Expand Down
14 changes: 7 additions & 7 deletions util/coll/sliceutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func TestSliceContains(t *testing.T) {
fmt.Println(SliceContains(stringSlice, "banana")) // 输出: true
fmt.Println(SliceContains(stringSlice, "grape")) // 输出: false

peoples := []people{
peoples := []*people{
{name: "张三", age: 28},
{name: "李四", age: 20},
{name: "王五", age: 22},
{name: "赵六", age: 20},
}
fmt.Println(SliceContains(peoples, people{name: "张三", age: 28}, func(a, b people) bool {
fmt.Println(SliceContains(peoples, &people{name: "张三", age: 28}, func(a, b *people) bool {
return a.name == b.name && a.age == b.age
}))
list := []string{"US", ""}
Expand Down Expand Up @@ -133,10 +133,10 @@ type Person struct {
Age int
}

func TestSliceToMap(t *testing.T) {
func TestSliceFilterToMap(t *testing.T) {
// 示例1: 整数切片
ints := []int{1, 2, 3, 4, 5, 6}
intMap := SliceToMap(ints, func(t int) (string, int, bool) {
intMap := SliceFilterToMap(ints, func(t int) (string, int, bool) {
if t > 3 {
fromInt := conversion.FromInt(t)
return fromInt, t, true
Expand All @@ -147,7 +147,7 @@ func TestSliceToMap(t *testing.T) {

// 示例2: 字符串切片
strings := []string{"apple", "banana", "cherry", "date"}
stringMap := SliceToMap(strings, func(t string) (string, string, bool) {
stringMap := SliceFilterToMap(strings, func(t string) (string, string, bool) {
if str.CharLength(t) > 4 {
return t, t, true
}
Expand All @@ -156,15 +156,15 @@ func TestSliceToMap(t *testing.T) {
fmt.Println(stringMap)

// 定义一个结构体切片
people := []Person{
people := []*Person{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
{Name: "Dave", Age: 40},
}

// 调用通用方法
personMap := SliceToMap(people, func(t Person) (string, int, bool) {
personMap := SliceFilterToMap(people, func(t *Person) (string, int, bool) {
if t.Age > 30 {
return t.Name, t.Age, true
}
Expand Down
Loading

0 comments on commit 268627f

Please sign in to comment.