feat: add functions.
This commit is contained in:
24
rand/rand.go
Normal file
24
rand/rand.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package rand
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var RANDOM = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
// RandomInt 根据区间产生随机数
|
||||
func RandomInt(min, max int) int {
|
||||
if min >= max {
|
||||
return max
|
||||
}
|
||||
return RANDOM.Intn(max-min) + min
|
||||
}
|
||||
|
||||
// RandomInt64 根据区间产生随机数
|
||||
func RandomInt64(min, max int64) int64 {
|
||||
if min >= max {
|
||||
return max
|
||||
}
|
||||
return RANDOM.Int63n(max-min) + min
|
||||
}
|
||||
17
rand/rand_test.go
Normal file
17
rand/rand_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package rand
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRandomInt(t *testing.T) {
|
||||
for i := 0; i < 1000; i++ {
|
||||
n := RandomInt(1, 100)
|
||||
fmt.Println(n)
|
||||
assert.True(t, n >= 1)
|
||||
assert.True(t, n < 100)
|
||||
}
|
||||
}
|
||||
126
stringutil/cryptorandomstringutils.go
Normal file
126
stringutil/cryptorandomstringutils.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package stringutil
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func CryptoRandomNonAlphaNumeric(count int) (string, error) {
|
||||
return CryptoRandomAlphaNumericCustom(count, false, false)
|
||||
}
|
||||
|
||||
func CryptoRandomAscii(count int) (string, error) {
|
||||
return CryptoRandom(count, 32, 127, false, false)
|
||||
}
|
||||
|
||||
func CryptoRandomNumeric(count int) (string, error) {
|
||||
return CryptoRandom(count, 0, 0, false, true)
|
||||
}
|
||||
|
||||
func CryptoRandomAlphabetic(count int) (string, error) {
|
||||
return CryptoRandom(count, 0, 0, true, false)
|
||||
}
|
||||
|
||||
func CryptoRandomAlphaNumeric(count int) (string, error) {
|
||||
return CryptoRandom(count, 0, 0, true, true)
|
||||
}
|
||||
|
||||
func CryptoRandomAlphaNumericCustom(count int, letters bool, numbers bool) (string, error) {
|
||||
return CryptoRandom(count, 0, 0, letters, numbers)
|
||||
}
|
||||
|
||||
func CryptoRandom(count int, start int, end int, letters bool, numbers bool, chars ...rune) (string, error) {
|
||||
if count == 0 {
|
||||
return "", nil
|
||||
} else if count < 0 {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Requested random string length %v is less than 0", count) // equiv to err := errors.New("...")
|
||||
return "", err
|
||||
}
|
||||
if chars != nil && len(chars) == 0 {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: The chars array must not be empty")
|
||||
return "", err
|
||||
}
|
||||
|
||||
if start == 0 && end == 0 {
|
||||
if chars != nil {
|
||||
end = len(chars)
|
||||
} else {
|
||||
if !letters && !numbers {
|
||||
end = math.MaxInt32
|
||||
} else {
|
||||
end = 'z' + 1
|
||||
start = ' '
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if end <= start {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) must be greater than start (%v)", end, start)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if chars != nil && end > len(chars) {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) cannot be greater than len(chars) (%v)", end, len(chars))
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
buffer := make([]rune, count)
|
||||
gap := end - start
|
||||
|
||||
// high-surrogates range, (\uD800-\uDBFF) = 55296 - 56319
|
||||
// low-surrogates range, (\uDC00-\uDFFF) = 56320 - 57343
|
||||
|
||||
for count != 0 {
|
||||
count--
|
||||
var ch rune
|
||||
if chars == nil {
|
||||
ch = rune(getCryptoRandomInt(gap) + int64(start))
|
||||
} else {
|
||||
ch = chars[getCryptoRandomInt(gap)+int64(start)]
|
||||
}
|
||||
|
||||
if letters && unicode.IsLetter(ch) || numbers && unicode.IsDigit(ch) || !letters && !numbers {
|
||||
if ch >= 56320 && ch <= 57343 { // low surrogate range
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
// Insert low surrogate
|
||||
buffer[count] = ch
|
||||
count--
|
||||
// Insert high surrogate
|
||||
buffer[count] = rune(55296 + getCryptoRandomInt(128))
|
||||
}
|
||||
} else if ch >= 55296 && ch <= 56191 { // High surrogates range (Partial)
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
// Insert low surrogate
|
||||
buffer[count] = rune(56320 + getCryptoRandomInt(128))
|
||||
count--
|
||||
// Insert high surrogate
|
||||
buffer[count] = ch
|
||||
}
|
||||
} else if ch >= 56192 && ch <= 56319 {
|
||||
// private high surrogate, skip it
|
||||
count++
|
||||
} else {
|
||||
// not one of the surrogates*
|
||||
buffer[count] = ch
|
||||
}
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return string(buffer), nil
|
||||
}
|
||||
|
||||
func getCryptoRandomInt(count int) int64 {
|
||||
nBig, err := rand.Int(rand.Reader, big.NewInt(int64(count)))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return nBig.Int64()
|
||||
}
|
||||
112
stringutil/cryptorandomstringutils_test.go
Normal file
112
stringutil/cryptorandomstringutils_test.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package stringutil
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TestCryptoRandomNonAlphaNumeric(t *testing.T) {
|
||||
// If asked for a string 0 characters long, CryptoRandomNonAlphaNumeric should provide an empty string.
|
||||
if x, _ := CryptoRandomNonAlphaNumeric(0); utf8.RuneCountInString(x) != 0 {
|
||||
t.Errorf("String should be 0 characters; string was %v characters", utf8.RuneCountInString(x))
|
||||
}
|
||||
|
||||
// Test CryptoRandomNonAlphaNumeric's ability to generate strings 1 through 100 characters in length.
|
||||
for i := 1; i < 101; i++ {
|
||||
if x, _ := CryptoRandomNonAlphaNumeric(i); utf8.RuneCountInString(x) != i {
|
||||
t.Errorf("String should be %v characters; string was %v characters", i, utf8.RuneCountInString(x))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCryptoRandomAscii(t *testing.T) {
|
||||
// If asked for a string 0 characters long, CryptoRandomAscii should provide an empty string.
|
||||
if x, _ := CryptoRandomAscii(0); len(x) != 0 {
|
||||
t.Errorf("String should be 0 characters; string was %v characters", len(x))
|
||||
}
|
||||
|
||||
// Test CryptoRandomAscii's ability to generate strings 1 through 100 characters in length.
|
||||
for i := 1; i < 101; i++ {
|
||||
if x, _ := CryptoRandomAscii(i); len(x) != i {
|
||||
t.Errorf("String should be %v characters; string was %v characters", i, len(x))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCryptoRandomNumeric(t *testing.T) {
|
||||
// If asked for a string 0 characters long, CryptoRandomNumeric should provide an empty string.
|
||||
if x, _ := CryptoRandomNumeric(0); len(x) != 0 {
|
||||
t.Errorf("String should be 0 characters; string was %v characters", len(x))
|
||||
}
|
||||
|
||||
// Test CryptoRandomNumeric's ability to generate strings 1 through 100 characters in length.
|
||||
for i := 1; i < 101; i++ {
|
||||
if x, _ := CryptoRandomNumeric(i); len(x) != i {
|
||||
t.Errorf("String should be %v characters; string was %v characters", i, len(x))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCryptoRandomAlphabetic(t *testing.T) {
|
||||
// If asked for a string 0 characters long, CryptoRandomAlphabetic should provide an empty string.
|
||||
if x, _ := CryptoRandomAlphabetic(0); len(x) != 0 {
|
||||
t.Errorf("String should be 0 characters; string was %v characters", len(x))
|
||||
}
|
||||
|
||||
// Test CryptoRandomAlphabetic's ability to generate strings 1 through 100 characters in length.
|
||||
for i := 1; i < 101; i++ {
|
||||
if x, _ := CryptoRandomAlphabetic(i); len(x) != i {
|
||||
t.Errorf("String should be %v characters; string was %v characters", i, len(x))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCryptoRandomAlphaNumeric(t *testing.T) {
|
||||
// If asked for a string 0 characters long, CryptoRandomAlphaNumeric should provide an empty string.
|
||||
if x, _ := CryptoRandomAlphaNumeric(0); len(x) != 0 {
|
||||
t.Errorf("String should be 0 characters; string was %v characters", len(x))
|
||||
}
|
||||
|
||||
// Test CryptoRandomAlphaNumeric's ability to generate strings 1 through 100 characters in length.
|
||||
for i := 1; i < 101; i++ {
|
||||
if x, _ := CryptoRandomAlphaNumeric(i); len(x) != i {
|
||||
t.Errorf("String should be %v characters; string was %v characters", i, len(x))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCryptoRandAlphaNumeric_FuzzOnlyNumeric(t *testing.T) {
|
||||
|
||||
// Testing for a reported regression in which some versions produced
|
||||
// a predictably small set of chars.
|
||||
iters := 1000
|
||||
charlen := 0
|
||||
for i := 0; i < 16; i++ {
|
||||
numOnly := 0
|
||||
charlen++
|
||||
for i := 0; i < iters; i++ {
|
||||
out, err := CryptoRandomAlphaNumeric(charlen)
|
||||
if err != nil {
|
||||
t.Fatal("func failed to produce a random thinger")
|
||||
}
|
||||
if _, err := strconv.Atoi(out); err == nil {
|
||||
numOnly++
|
||||
}
|
||||
|
||||
m, err := regexp.MatchString("^[0-9a-zA-Z]+$", out)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !m {
|
||||
t.Fatal("Character is not alphanum")
|
||||
}
|
||||
}
|
||||
|
||||
if numOnly == iters {
|
||||
t.Fatalf("Got %d numeric-only random sequences", numOnly)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
125
stringutil/randomstringutils.go
Normal file
125
stringutil/randomstringutils.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package stringutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var RANDOM = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
func RandomNonAlphaNumeric(count int) (string, error) {
|
||||
return RandomAlphaNumericCustom(count, false, false)
|
||||
}
|
||||
|
||||
func RandomAscii(count int) (string, error) {
|
||||
return Random(count, 32, 127, false, false)
|
||||
}
|
||||
|
||||
func RandomNumeric(count int) (string, error) {
|
||||
return Random(count, 0, 0, false, true)
|
||||
}
|
||||
|
||||
func RandomAlphabetic(count int) (string, error) {
|
||||
return Random(count, 0, 0, true, false)
|
||||
}
|
||||
|
||||
func RandomAlphaNumeric(count int) (string, error) {
|
||||
return Random(count, 0, 0, true, true)
|
||||
}
|
||||
|
||||
func RandomAlphaNumericCustom(count int, letters bool, numbers bool) (string, error) {
|
||||
return Random(count, 0, 0, letters, numbers)
|
||||
}
|
||||
|
||||
func Random(count int, start int, end int, letters bool, numbers bool, chars ...rune) (string, error) {
|
||||
return RandomSeed(count, start, end, letters, numbers, chars, RANDOM)
|
||||
}
|
||||
|
||||
func RandomSeed(count int, start int, end int, letters bool, numbers bool, chars []rune, random *rand.Rand) (string, error) {
|
||||
|
||||
if count == 0 {
|
||||
return "", nil
|
||||
} else if count < 0 {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Requested random string length %v is less than 0", count) // equiv to err := errors.New("...")
|
||||
return "", err
|
||||
}
|
||||
if chars != nil && len(chars) == 0 {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: The chars array must not be empty")
|
||||
return "", err
|
||||
}
|
||||
|
||||
if start == 0 && end == 0 {
|
||||
if chars != nil {
|
||||
end = len(chars)
|
||||
} else {
|
||||
if !letters && !numbers {
|
||||
end = math.MaxInt32
|
||||
} else {
|
||||
end = 'z' + 1
|
||||
start = ' '
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if end <= start {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) must be greater than start (%v)", end, start)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if chars != nil && end > len(chars) {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) cannot be greater than len(chars) (%v)", end, len(chars))
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
buffer := make([]rune, count)
|
||||
gap := end - start
|
||||
|
||||
// high-surrogates range, (\uD800-\uDBFF) = 55296 - 56319
|
||||
// low-surrogates range, (\uDC00-\uDFFF) = 56320 - 57343
|
||||
|
||||
for count != 0 {
|
||||
count--
|
||||
var ch rune
|
||||
if chars == nil {
|
||||
ch = rune(random.Intn(gap) + start)
|
||||
} else {
|
||||
ch = chars[random.Intn(gap)+start]
|
||||
}
|
||||
|
||||
if letters && unicode.IsLetter(ch) || numbers && unicode.IsDigit(ch) || !letters && !numbers {
|
||||
if ch >= 56320 && ch <= 57343 { // low surrogate range
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
// Insert low surrogate
|
||||
buffer[count] = ch
|
||||
count--
|
||||
// Insert high surrogate
|
||||
buffer[count] = rune(55296 + random.Intn(128))
|
||||
}
|
||||
} else if ch >= 55296 && ch <= 56191 { // High surrogates range (Partial)
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
// Insert low surrogate
|
||||
buffer[count] = rune(56320 + random.Intn(128))
|
||||
count--
|
||||
// Insert high surrogate
|
||||
buffer[count] = ch
|
||||
}
|
||||
} else if ch >= 56192 && ch <= 56319 {
|
||||
// private high surrogate, skip it
|
||||
count++
|
||||
} else {
|
||||
// not one of the surrogates*
|
||||
buffer[count] = ch
|
||||
}
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return string(buffer), nil
|
||||
}
|
||||
121
stringutil/randomstringutils_test.go
Normal file
121
stringutil/randomstringutils_test.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package stringutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// ****************************** TESTS ********************************************
|
||||
|
||||
func TestRandomSeed(t *testing.T) {
|
||||
|
||||
// count, start, end, letters, numbers := 5, 0, 0, true, true
|
||||
random := rand.New(rand.NewSource(10))
|
||||
out := "3ip9v"
|
||||
|
||||
// Test 1: Simulating RandomAlphaNumeric(count int)
|
||||
if x, _ := RandomSeed(5, 0, 0, true, true, nil, random); x != out {
|
||||
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, true, true, nil, random, x, out)
|
||||
}
|
||||
|
||||
// Test 2: Simulating RandomAlphabetic(count int)
|
||||
out = "MBrbj"
|
||||
|
||||
if x, _ := RandomSeed(5, 0, 0, true, false, nil, random); x != out {
|
||||
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, true, false, nil, random, x, out)
|
||||
}
|
||||
|
||||
// Test 3: Simulating RandomNumeric(count int)
|
||||
out = "88935"
|
||||
|
||||
if x, _ := RandomSeed(5, 0, 0, false, true, nil, random); x != out {
|
||||
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, false, true, nil, random, x, out)
|
||||
}
|
||||
|
||||
// Test 4: Simulating RandomAscii(count int)
|
||||
out = "H_I;E"
|
||||
|
||||
if x, _ := RandomSeed(5, 32, 127, false, false, nil, random); x != out {
|
||||
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 32, 127, false, false, nil, random, x, out)
|
||||
}
|
||||
|
||||
// Test 5: Simulating RandomSeed(...) with custom chars
|
||||
chars := []rune{'1', '2', '3', 'a', 'b', 'c'}
|
||||
out = "2b2ca"
|
||||
|
||||
if x, _ := RandomSeed(5, 0, 0, false, false, chars, random); x != out {
|
||||
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, false, false, chars, random, x, out)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ****************************** EXAMPLES ********************************************
|
||||
|
||||
func ExampleRandomSeed() {
|
||||
|
||||
var seed int64 = 10 // If you change this seed #, the random sequence below will change
|
||||
random := rand.New(rand.NewSource(seed))
|
||||
chars := []rune{'1', '2', '3', 'a', 'b', 'c'}
|
||||
|
||||
rand1, _ := RandomSeed(5, 0, 0, true, true, nil, random) // RandomAlphaNumeric (Alphabets and numbers possible)
|
||||
rand2, _ := RandomSeed(5, 0, 0, true, false, nil, random) // RandomAlphabetic (Only alphabets)
|
||||
rand3, _ := RandomSeed(5, 0, 0, false, true, nil, random) // RandomNumeric (Only numbers)
|
||||
rand4, _ := RandomSeed(5, 32, 127, false, false, nil, random) // RandomAscii (Alphabets, numbers, and other ASCII chars)
|
||||
rand5, _ := RandomSeed(5, 0, 0, true, true, chars, random) // RandomSeed with custom characters
|
||||
|
||||
fmt.Println(rand1)
|
||||
fmt.Println(rand2)
|
||||
fmt.Println(rand3)
|
||||
fmt.Println(rand4)
|
||||
fmt.Println(rand5)
|
||||
// Output:
|
||||
// 3ip9v
|
||||
// MBrbj
|
||||
// 88935
|
||||
// H_I;E
|
||||
// 2b2ca
|
||||
}
|
||||
|
||||
func TestRandomAlphaNumeric(t *testing.T) {
|
||||
for i := 0; i < 16; i++ {
|
||||
out, _ := RandomAlphaNumeric(20)
|
||||
fmt.Println(out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandAlphaNumeric_FuzzOnlyNumeric(t *testing.T) {
|
||||
|
||||
// Testing for a reported regression in which some versions produced
|
||||
// a predictably small set of chars.
|
||||
iters := 1000
|
||||
charlen := 0
|
||||
for i := 0; i < 16; i++ {
|
||||
numOnly := 0
|
||||
charlen++
|
||||
for i := 0; i < iters; i++ {
|
||||
out, err := RandomAlphaNumeric(charlen)
|
||||
if err != nil {
|
||||
t.Fatal("func failed to produce a random thinger")
|
||||
}
|
||||
if _, err := strconv.Atoi(out); err == nil {
|
||||
numOnly++
|
||||
}
|
||||
|
||||
m, err := regexp.MatchString("^[0-9a-zA-Z]+$", out)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !m {
|
||||
t.Fatal("Character is not alphanum")
|
||||
}
|
||||
}
|
||||
|
||||
if numOnly == iters {
|
||||
t.Fatalf("Got %d numeric-only random sequences", numOnly)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user