Skip to content

Commit

Permalink
refactor: change FastRandBytes() to RandBytes(), add more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Apr 19, 2024
1 parent 9307054 commit 38b63ab
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 107 deletions.
82 changes: 66 additions & 16 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import "github.com/fufuok/utils"
- [func FastIntn\(n int\) int](<#FastIntn>)
- [func FastRand\(\) uint32](<#FastRand>)
- [func FastRand64\(\) uint64](<#FastRand64>)
- [func FastRandBytes\(n int\) \[\]byte](<#FastRandBytes>)
- [func FastRandn\(n uint32\) uint32](<#FastRandn>)
- [func FastRandu\(\) uint](<#FastRandu>)
- [func GetBytes\(v interface\{\}, defaultVal ...\[\]byte\) \[\]byte](<#GetBytes>)
Expand Down Expand Up @@ -152,8 +151,14 @@ import "github.com/fufuok/utils"
- [func ParseIPx\(s string\) \(net.IP, bool\)](<#ParseIPx>)
- [func ParseIPxWithNumeric\(s string\) \(net.IP, bool\)](<#ParseIPxWithNumeric>)
- [func ParseInts\(s string\) \(\[\]int, error\)](<#ParseInts>)
- [func RandAlphaBytes\(n int\) \[\]byte](<#RandAlphaBytes>)
- [func RandAlphaString\(n int\) string](<#RandAlphaString>)
- [func RandBytes\(n int\) \[\]byte](<#RandBytes>)
- [func RandHex\(nHalf int\) string](<#RandHex>)
- [func RandBytesLetters\(n int, letters string\) \[\]byte](<#RandBytesLetters>)
- [func RandDecBytes\(n int\) \[\]byte](<#RandDecBytes>)
- [func RandDecString\(n int\) string](<#RandDecString>)
- [func RandHexBytes\(n int\) \[\]byte](<#RandHexBytes>)
- [func RandHexString\(n int\) string](<#RandHexString>)
- [func RandInt\(min, max int\) int](<#RandInt>)
- [func RandString\(n int\) string](<#RandString>)
- [func RandUint32\(min, max uint32\) uint32](<#RandUint32>)
Expand Down Expand Up @@ -860,15 +865,6 @@ func FastRand64() uint64



<a name="FastRandBytes"></a>
## func FastRandBytes

```go
func FastRandBytes(n int) []byte
```

FastRandBytes random bytes, but faster.

<a name="FastRandn"></a>
## func FastRandn

Expand Down Expand Up @@ -1646,23 +1642,77 @@ func ParseInts(s string) ([]int, error)

ParseInts 解析字符串, 得到去重并排序后的数字列表 "5,0\-3, 3" =\> \[0,1,2,3,5\]

<a name="RandAlphaBytes"></a>
## func RandAlphaBytes

```go
func RandAlphaBytes(n int) []byte
```

RandAlphaBytes generates random alpha bytes.

<a name="RandAlphaString"></a>
## func RandAlphaString

```go
func RandAlphaString(n int) string
```

RandAlphaString 指定长度的随机字母字符串

<a name="RandBytes"></a>
## func RandBytes

```go
func RandBytes(n int) []byte
```

RandBytes random bytes
RandBytes random bytes, but faster.

<a name="RandBytesLetters"></a>
## func RandBytesLetters

```go
func RandBytesLetters(n int, letters string) []byte
```

RandBytesLetters 生成指定长度的字符切片

<a name="RandDecBytes"></a>
## func RandDecBytes

```go
func RandDecBytes(n int) []byte
```

RandDecBytes 指定长度的随机数字切片

<a name="RandDecString"></a>
## func RandDecString

```go
func RandDecString(n int) string
```

RandDecString 指定长度的随机数字字符串

<a name="RandHexBytes"></a>
## func RandHexBytes

```go
func RandHexBytes(n int) []byte
```

RandHexBytes generates random hexadecimal bytes.

<a name="RandHex"></a>
## func RandHex
<a name="RandHexString"></a>
## func RandHexString

```go
func RandHex(nHalf int) string
func RandHexString(n int) string
```

RandHex a random string containing only the following characters: 0123456789abcdef
RandHexString 指定长度的随机 hex 字符串

<a name="RandInt"></a>
## func RandInt
Expand Down
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ package utils // import "github.com/fufuok/utils"

const Byte = 1 << (iota * 10) ...
const IByte = 1 ...
const IPv4Min = 0 ...
const PtrSize = 4 << (^uintptr(0) >> 63)
var BigByte ...
var BigSIByte ...
var Rand = NewRand() ...
var ErrInvalidHostPort = errors.New("invalid Host or Port")
var StackTraceBufferSize = 4 << 10
func AddString(s ...string) string
func AddStringBytes(s ...string) []byte
func B2S(b []byte) string
func B64Decode(s string) []byte
func B64Encode(b []byte) string
Expand Down Expand Up @@ -86,7 +85,6 @@ func ExecutableDir(evalSymlinks ...bool) string
func FastIntn(n int) int
func FastRand() uint32
func FastRand64() uint64
func FastRandBytes(n int) []byte
func FastRandn(n uint32) uint32
func FastRandu() uint
func GetBytes(v interface{}, defaultVal ...[]byte) []byte
Expand Down Expand Up @@ -119,18 +117,23 @@ func IPv42Long(ip net.IP) int
func IPv42LongLittle(ip net.IP) int
func IPv4String2Long(ip string) int
func IPv4String2LongLittle(ip string) int
func IPv62Int(ip net.IP) *big.Int
func IPv6String2Int(ip string) *big.Int
func InIPNet(ip net.IP, ipNets map[*net.IPNet]struct{}) bool
func InIPNetString(ip string, ipNets map[*net.IPNet]struct{}) bool
func InInts(slice []int, n int) bool
func InStrings(ss []string, s string) bool
func InitCSTLocation() (name string, loc *time.Location, cst *time.Location, ok bool)
func InitLocation(name string) (*time.Location, bool)
func Int2IPv6(ipInt *big.Int) net.IP
func Int2IPv6String(n *big.Int) string
func IsIP(ip string) bool
func IsIPv4(ip string) bool
func IsIPv6(ip string) bool
func IsIPv4(s string) bool
func IsIPv6(s string) bool
func IsInternalIPv4(ip net.IP) bool
func IsInternalIPv4String(ip string) bool
func IsLeapYear(year int) bool
func IsNumeric(s string) bool
func IsPrivateIP(ip net.IP) bool
func IsPrivateIPString(ip string) bool
func JoinBytes(b ...[]byte) []byte
Expand Down Expand Up @@ -164,9 +167,17 @@ func ParseHumanBytes(s string) (uint64, error)
func ParseIP(s string) (net.IP, bool)
func ParseIPv4(ip string) net.IP
func ParseIPv6(ip string) net.IP
func ParseIPx(s string) (net.IP, bool)
func ParseIPxWithNumeric(s string) (net.IP, bool)
func ParseInts(s string) ([]int, error)
func RandAlphaBytes(n int) []byte
func RandAlphaString(n int) string
func RandBytes(n int) []byte
func RandHex(nHalf int) string
func RandBytesLetters(n int, letters string) []byte
func RandDecBytes(n int) []byte
func RandDecString(n int) string
func RandHexBytes(n int) []byte
func RandHexString(n int) string
func RandInt(min, max int) int
func RandString(n int) string
func RandUint32(min, max uint32) uint32
Expand All @@ -188,10 +199,6 @@ func SearchInt(slice []int, n int) int
func SearchString(ss []string, s string) int
func Sleep(ctx context.Context, interval time.Duration) error
func SplitHostPort(hostPort string) (host, port string)
func Str2Bytes(s string) (b []byte)
func StrToBytes(s string) []byte
func String2Bytes(s string) (bs []byte)
func StringToBytes(s string) (b []byte)
func SumInt(v ...int) int
func ToLower(b string) string
func ToLowerBytes(b []byte) []byte
Expand All @@ -216,6 +223,8 @@ func WaitNextMinuteWithTime(t ...time.Time) (now time.Time)
func WaitNextSecond(t ...time.Time)
func WaitNextSecondWithTime(t ...time.Time) (now time.Time)
func WaitSignal(sig ...os.Signal) os.Signal
func WaitUntilMinute(m int, t ...time.Time)
func WaitUntilSecond(s int, t ...time.Time)
func Zip(data []byte) ([]byte, error)
func ZipLevel(data []byte, level int) (dst []byte, err error)
type Bool struct{ ... }
Expand Down Expand Up @@ -1125,7 +1134,7 @@ func main() {

fmt.Println(utils.Rand.Intn(10), utils.FastIntn(10))

dec, _ := utils.Zip(utils.FastRandBytes(3000))
dec, _ := utils.Zip(utils.RandBytes(3000))
src, _ := utils.Unzip(dec)
fmt.Println(len(dec), len(src)) // 2288 3000

Expand Down
58 changes: 37 additions & 21 deletions random.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package utils

import (
rrand "crypto/rand"
"encoding/hex"
"io"
"math"
)

const (
decBytes = "0123456789"
hexBytes = "0123456789abcdef"
alphaBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
Expand Down Expand Up @@ -58,40 +58,56 @@ func FastIntn(n int) int {
// RandString a random string, which may contain uppercase letters, lowercase letters and numbers.
// Ref: stackoverflow.icza
func RandString(n int) string {
return B2S(FastRandBytes(n))
return B2S(RandBytes(n))
}

// RandHex a random string containing only the following characters: 0123456789abcdef
func RandHex(nHalf int) string {
return hex.EncodeToString(FastRandBytes(nHalf))
// RandHexString 指定长度的随机 hex 字符串
func RandHexString(n int) string {
return B2S(RandHexBytes(n))
}

// RandBytes random bytes
// RandAlphaString 指定长度的随机字母字符串
func RandAlphaString(n int) string {
return B2S(RandAlphaBytes(n))
}

// RandDecString 指定长度的随机数字字符串
func RandDecString(n int) string {
return B2S(RandDecBytes(n))
}

// RandBytes random bytes, but faster.
func RandBytes(n int) []byte {
if n < 1 {
return nil
}
return RandBytesLetters(n, letterBytes)
}

b := make([]byte, n)
if _, err := io.ReadFull(rrand.Reader, b); err != nil {
return nil
}
// RandAlphaBytes generates random alpha bytes.
func RandAlphaBytes(n int) []byte {
return RandBytesLetters(n, alphaBytes)
}

return b
// RandHexBytes generates random hexadecimal bytes.
func RandHexBytes(n int) []byte {
return RandBytesLetters(n, hexBytes)
}

// RandDecBytes 指定长度的随机数字切片
func RandDecBytes(n int) []byte {
return RandBytesLetters(n, decBytes)
}

// FastRandBytes random bytes, but faster.
func FastRandBytes(n int) []byte {
if n < 1 {
// RandBytesLetters 生成指定长度的字符切片
func RandBytesLetters(n int, letters string) []byte {
if n < 1 || len(letters) < 2 {
return nil
}
b := make([]byte, n)
for i, cache, remain := n-1, FastRand(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = FastRand(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
if idx := int(cache & letterIdxMask); idx < len(letters) {
b[i] = letters[idx]
i--
}
cache >>= letterIdxBits
Expand Down
Loading

0 comments on commit 38b63ab

Please sign in to comment.