Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
21ae9c70d2
|
|||
c223e2f0fa
|
74
langext/base62.go
Normal file
74
langext/base62.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package langext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"errors"
|
||||||
|
"math"
|
||||||
|
"math/big"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
base62Base = uint64(62)
|
||||||
|
base62CharacterSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RandBase62(rlen int) string {
|
||||||
|
bi52 := big.NewInt(int64(len(base62CharacterSet)))
|
||||||
|
|
||||||
|
randMax := big.NewInt(math.MaxInt64)
|
||||||
|
|
||||||
|
r := ""
|
||||||
|
|
||||||
|
for i := 0; i < rlen; i++ {
|
||||||
|
v, err := rand.Int(rand.Reader, randMax)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r += string(base62CharacterSet[v.Mod(v, bi52).Int64()])
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func EncodeBase62(num uint64) string {
|
||||||
|
if num == 0 {
|
||||||
|
return "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
b := make([]byte, 0)
|
||||||
|
|
||||||
|
// loop as long the num is bigger than zero
|
||||||
|
for num > 0 {
|
||||||
|
r := num % base62Base
|
||||||
|
|
||||||
|
num -= r
|
||||||
|
num /= base62Base
|
||||||
|
|
||||||
|
b = append([]byte{base62CharacterSet[int(r)]}, b...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DecodeBase62(str string) (uint64, error) {
|
||||||
|
if str == "" {
|
||||||
|
return 0, errors.New("empty string")
|
||||||
|
}
|
||||||
|
|
||||||
|
result := uint64(0)
|
||||||
|
|
||||||
|
for _, v := range str {
|
||||||
|
result *= base62Base
|
||||||
|
|
||||||
|
pos := strings.IndexRune(base62CharacterSet, v)
|
||||||
|
if pos == -1 {
|
||||||
|
return 0, errors.New("invalid character: " + string(v))
|
||||||
|
}
|
||||||
|
|
||||||
|
result += uint64(pos)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
@@ -30,3 +30,34 @@ func CompareIntArr(arr1 []int, arr2 []int) bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CompareArr[T OrderedConstraint](arr1 []T, arr2 []T) bool {
|
||||||
|
|
||||||
|
for i := 0; i < len(arr1) || i < len(arr2); i++ {
|
||||||
|
|
||||||
|
if i < len(arr1) && i < len(arr2) {
|
||||||
|
|
||||||
|
if arr1[i] < arr2[i] {
|
||||||
|
return true
|
||||||
|
} else if arr1[i] > arr2[i] {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if i < len(arr1) {
|
||||||
|
|
||||||
|
return true
|
||||||
|
|
||||||
|
} else { // if i < len(arr2)
|
||||||
|
|
||||||
|
return false
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
11
langext/coords.go
Normal file
11
langext/coords.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package langext
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func DegToRad(deg float64) float64 {
|
||||||
|
return deg * (math.Pi / 180.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RadToDeg(rad float64) float64 {
|
||||||
|
return rad / (math.Pi * 180.0)
|
||||||
|
}
|
@@ -3,8 +3,6 @@ package langext
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
|
||||||
"math/big"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func RandBytes(size int) []byte {
|
func RandBytes(size int) []byte {
|
||||||
@@ -15,24 +13,3 @@ func RandBytes(size int) []byte {
|
|||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func RandBase62(rlen int) string {
|
|
||||||
ecs := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
||||||
|
|
||||||
bi52 := big.NewInt(int64(len(ecs)))
|
|
||||||
|
|
||||||
randMax := big.NewInt(math.MaxInt64)
|
|
||||||
|
|
||||||
r := ""
|
|
||||||
|
|
||||||
for i := 0; i < rlen; i++ {
|
|
||||||
v, err := rand.Int(rand.Reader, randMax)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
r += string(ecs[v.Mod(v, bi52).Int64()])
|
|
||||||
}
|
|
||||||
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user