-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
other: v2 toolkit 新增 convert 和 constrains 包
- Loading branch information
1 parent
e118662
commit a44501e
Showing
5 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package constraints | ||
|
||
type Int interface { | ||
int | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | ||
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ||
} | ||
|
||
type Float interface { | ||
float32 | float64 | ||
~float32 | ~float64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package convert | ||
|
||
import ( | ||
"github.com/kercylan98/minotaur/toolkit/constraints" | ||
"strconv" | ||
) | ||
|
||
// BooleanToInt 将 bool 转换为 int 类型 | ||
func BooleanToInt[I constraints.Int](b bool) I { | ||
if b { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
// BooleanToString 将 bool 转换为 string 类型 | ||
func BooleanToString(b bool) string { | ||
return strconv.FormatBool(b) | ||
} | ||
|
||
// BooleanToByte 将 bool 转换为 byte 类型 | ||
func BooleanToByte(b bool) byte { | ||
return BooleanToInt[byte](b) | ||
} | ||
|
||
// BooleanToRune 将 bool 转换为 rune 类型 | ||
func BooleanToRune(b bool) rune { | ||
return BooleanToInt[rune](b) | ||
} | ||
|
||
// BooleanToFloat32 将 bool 转换为 float32 类型 | ||
func BooleanToFloat32(b bool) float32 { | ||
if b { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
// BooleanToFloat64 将 bool 转换为 float64 类型 | ||
func BooleanToFloat64(b bool) float64 { | ||
if b { | ||
return 1 | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package convert | ||
|
||
import ( | ||
"github.com/kercylan98/minotaur/toolkit/constraints" | ||
"strconv" | ||
) | ||
|
||
// FloatToBoolean 将 float32/float64 转换为 bool 类型 | ||
func FloatToBoolean[F constraints.Float](f F) bool { | ||
return f != 0 | ||
} | ||
|
||
// FloatToInt 将 float32/float64 转换为 int 类型 | ||
func FloatToInt[F constraints.Float, I constraints.Int](f F) I { | ||
return I(f) | ||
} | ||
|
||
// Float32ToString 将 float32 转换为 string 类型 | ||
func Float32ToString(value float32) string { | ||
return strconv.FormatFloat(float64(value), 'f', -1, 32) | ||
} | ||
|
||
// Float64ToString 将 float64 转换为 string 类型 | ||
func Float64ToString(value float64) string { | ||
return strconv.FormatFloat(value, 'f', -1, 64) | ||
} | ||
|
||
// FloatToIntRound 将 float32/float64 转换为 int 类型,四舍五入 | ||
func FloatToIntRound[F constraints.Float, I constraints.Int](f F) I { | ||
return I(f + F(0.5)) | ||
} | ||
|
||
// FloatToIntCeil 将 float32/float64 转换为 int 类型,向上取整 | ||
func FloatToIntCeil[F constraints.Float, I constraints.Int](f F) I { | ||
return I(f + F(1)) | ||
} | ||
|
||
// FloatToIntFloor 将 float32/float64 转换为 int 类型,向下取整 | ||
func FloatToIntFloor[F constraints.Float, I constraints.Int](f F) I { | ||
return FloatToInt[F, I](f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package convert | ||
|
||
import ( | ||
"github.com/kercylan98/minotaur/toolkit/constraints" | ||
"strconv" | ||
) | ||
|
||
// IntToString 将 int 转换为 string 类型 | ||
func IntToString(i int) string { | ||
return strconv.Itoa(i) | ||
} | ||
|
||
// Int8ToString 将 int8 转换为 string 类型 | ||
func Int8ToString(i int8) string { | ||
return strconv.Itoa(int(i)) | ||
} | ||
|
||
// Int16ToString 将 int16 转换为 string 类型 | ||
func Int16ToString(i int16) string { | ||
return strconv.Itoa(int(i)) | ||
} | ||
|
||
// Int32ToString 将 int32 转换为 string 类型 | ||
func Int32ToString(i int32) string { | ||
return strconv.Itoa(int(i)) | ||
} | ||
|
||
// Int64ToString 将 int64 转换为 string 类型 | ||
func Int64ToString(i int64) string { | ||
return strconv.FormatInt(i, 10) | ||
} | ||
|
||
// UintToString 将 uint 转换为 string 类型 | ||
func UintToString(i uint) string { | ||
return strconv.FormatUint(uint64(i), 10) | ||
} | ||
|
||
// Uint8ToString 将 uint8 转换为 string 类型 | ||
func Uint8ToString(i uint8) string { | ||
return strconv.FormatUint(uint64(i), 10) | ||
} | ||
|
||
// Uint16ToString 将 uint16 转换为 string 类型 | ||
func Uint16ToString(i uint16) string { | ||
return strconv.FormatUint(uint64(i), 10) | ||
} | ||
|
||
// Uint32ToString 将 uint32 转换为 string 类型 | ||
func Uint32ToString(i uint32) string { | ||
return strconv.FormatUint(uint64(i), 10) | ||
} | ||
|
||
// Uint64ToString 将 uint64 转换为 string 类型 | ||
func Uint64ToString(i uint64) string { | ||
return strconv.FormatUint(i, 10) | ||
} | ||
|
||
// IntToBoolean 将 int 转换为 bool 类型 | ||
func IntToBoolean[I constraints.Int](i I) bool { | ||
return i != 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package convert | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"unsafe" | ||
) | ||
|
||
// StringToBytesByZeroCopy 转换 string 为 []byte 类型,使用零拷贝 | ||
func StringToBytesByZeroCopy(str string) []byte { | ||
return unsafe.Slice(unsafe.StringData(str), len(str)) | ||
} | ||
|
||
// StringToBytes 转换 string 为 []byte 类型 | ||
func StringToBytes(str string) []byte { | ||
return []byte(str) | ||
} | ||
|
||
// StringToRune 转换 string 为 []rune 类型 | ||
func StringToRune(str string) []rune { | ||
return []rune(str) | ||
} | ||
|
||
// StringToBoolean 转换 string 为 bool 类型 | ||
func StringToBoolean(str string) bool { | ||
result, _ := strconv.ParseBool(strings.ToLower(str)) | ||
return result | ||
} | ||
|
||
// StringToFloat32 转换 string 为 float32 类型 | ||
func StringToFloat32(str string) float32 { | ||
result, _ := strconv.ParseFloat(str, 32) | ||
return float32(result) | ||
} | ||
|
||
// StringToFloat64 转换 string 为 float64 类型 | ||
func StringToFloat64(str string) float64 { | ||
result, _ := strconv.ParseFloat(str, 64) | ||
return result | ||
} | ||
|
||
// StringToInt 转换 string 为 int 类型 | ||
func StringToInt(str string) int { | ||
result, _ := strconv.Atoi(str) | ||
return result | ||
} | ||
|
||
// StringToInt8 转换 string 为 int8 类型 | ||
func StringToInt8(str string) int8 { | ||
result, _ := strconv.ParseInt(str, 10, 8) | ||
return int8(result) | ||
} | ||
|
||
// StringToInt16 转换 string 为 int16 类型 | ||
func StringToInt16(str string) int16 { | ||
result, _ := strconv.ParseInt(str, 10, 16) | ||
return int16(result) | ||
} | ||
|
||
// StringToInt32 转换 string 为 int32 类型 | ||
func StringToInt32(str string) int32 { | ||
result, _ := strconv.ParseInt(str, 10, 32) | ||
return int32(result) | ||
} | ||
|
||
// StringToInt64 转换 string 为 int64 类型 | ||
func StringToInt64(str string) int64 { | ||
result, _ := strconv.ParseInt(str, 10, 64) | ||
return result | ||
} | ||
|
||
// StringToUint 转换 string 为 uint 类型 | ||
func StringToUint(str string) uint { | ||
result, _ := strconv.ParseUint(str, 10, 0) | ||
return uint(result) | ||
} | ||
|
||
// StringToUint8 转换 string 为 uint8 类型 | ||
func StringToUint8(str string) uint8 { | ||
result, _ := strconv.ParseUint(str, 10, 8) | ||
return uint8(result) | ||
} | ||
|
||
// StringToUint16 转换 string 为 uint16 类型 | ||
func StringToUint16(str string) uint16 { | ||
result, _ := strconv.ParseUint(str, 10, 16) | ||
return uint16(result) | ||
} | ||
|
||
// StringToUint32 转换 string 为 uint32 类型 | ||
func StringToUint32(str string) uint32 { | ||
result, _ := strconv.ParseUint(str, 10, 32) | ||
return uint32(result) | ||
} | ||
|
||
// StringToUint64 转换 string 为 uint64 类型 | ||
func StringToUint64(str string) uint64 { | ||
result, _ := strconv.ParseUint(str, 10, 64) | ||
return result | ||
} |