add
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package common
|
||||
|
||||
// UnionUintArray returns a new unique slice that contains all elements from both input slices
|
||||
func UnionUintArray(a []uint, b []uint) []uint {
|
||||
m := make(map[uint]bool)
|
||||
for _, v := range a {
|
||||
m[v] = true
|
||||
}
|
||||
for _, v := range b {
|
||||
m[v] = true
|
||||
}
|
||||
var res []uint
|
||||
for k := range m {
|
||||
res = append(res, k)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// Find different elements in two slices
|
||||
// Returns elements in 'a' that are not in 'b' and elements in 'b' that are not in 'a'
|
||||
func DiffUintArray(a []uint, b []uint) []uint {
|
||||
different := []uint{}
|
||||
set := make(map[uint]bool)
|
||||
|
||||
for _, item := range a {
|
||||
set[item] = true
|
||||
}
|
||||
for _, item := range b {
|
||||
if !set[item] {
|
||||
different = append(different, item)
|
||||
}
|
||||
}
|
||||
|
||||
set = make(map[uint]bool)
|
||||
for _, item := range b {
|
||||
set[item] = true
|
||||
}
|
||||
for _, item := range a {
|
||||
if !set[item] {
|
||||
different = append(different, item)
|
||||
}
|
||||
}
|
||||
|
||||
return different
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/alireza0/s-ui/logger"
|
||||
)
|
||||
|
||||
func NewErrorf(format string, a ...interface{}) error {
|
||||
msg := fmt.Sprintf(format, a...)
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
||||
func NewError(a ...interface{}) error {
|
||||
msg := fmt.Sprintln(a...)
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
||||
func Recover(msg string) interface{} {
|
||||
panicErr := recover()
|
||||
if panicErr != nil {
|
||||
if msg != "" {
|
||||
logger.Error(msg, "panic:", panicErr)
|
||||
}
|
||||
}
|
||||
return panicErr
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
crand "crypto/rand"
|
||||
"math/big"
|
||||
mrand "math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
allSeq []rune = []rune{
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
}
|
||||
|
||||
fallbackRand = mrand.New(mrand.NewSource(time.Now().UnixNano()))
|
||||
fallbackMu = sync.Mutex{}
|
||||
)
|
||||
|
||||
func Random(n int) string {
|
||||
if n <= 0 || len(allSeq) == 0 {
|
||||
return ""
|
||||
}
|
||||
result := make([]rune, n)
|
||||
maxBig := big.NewInt(int64(len(allSeq)))
|
||||
for i := 0; i < n; i++ {
|
||||
num, err := crand.Int(crand.Reader, maxBig)
|
||||
if err != nil {
|
||||
// fallback
|
||||
fallbackMu.Lock()
|
||||
result[i] = allSeq[fallbackRand.Intn(len(allSeq))]
|
||||
fallbackMu.Unlock()
|
||||
continue
|
||||
}
|
||||
result[i] = allSeq[int(num.Int64())]
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
func RandomInt(n int) int {
|
||||
if n <= 0 {
|
||||
return 0
|
||||
}
|
||||
max := big.NewInt(int64(n))
|
||||
result, err := crand.Int(crand.Reader, max)
|
||||
if err != nil {
|
||||
// fallback
|
||||
fallbackMu.Lock()
|
||||
defer fallbackMu.Unlock()
|
||||
return fallbackRand.Intn(n)
|
||||
}
|
||||
return int(result.Int64())
|
||||
}
|
||||
Reference in New Issue
Block a user