mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 01:02:28 +08:00
feat: add RandSymbolChar
This commit is contained in:
@@ -17,6 +17,7 @@ const (
|
|||||||
LowwerLetters = "abcdefghijklmnopqrstuvwxyz"
|
LowwerLetters = "abcdefghijklmnopqrstuvwxyz"
|
||||||
UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
SymbolChars = "!@#$%^&*()_+-=[]{}|;':\",./<>?"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -82,6 +83,12 @@ func RandNumeralOrLetter(length int) string {
|
|||||||
return random(Numeral+Letters, length)
|
return random(Numeral+Letters, length)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RandSymbolChar generate a random symbol char(!@#$%^&*()_+-=[]{}|;':\",./<>?).
|
||||||
|
// Play: todo
|
||||||
|
func RandSymbolChar(length int) string {
|
||||||
|
return random(SymbolChars, length)
|
||||||
|
}
|
||||||
|
|
||||||
// random generate a random string based on given string range.
|
// random generate a random string based on given string range.
|
||||||
func random(s string, length int) string {
|
func random(s string, length int) string {
|
||||||
b := make([]byte, length)
|
b := make([]byte, length)
|
||||||
|
|||||||
@@ -134,3 +134,20 @@ func ExampleRandUniqueIntSlice() {
|
|||||||
// Output:
|
// Output:
|
||||||
// ok
|
// ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleRandSymbolChar() {
|
||||||
|
pattern := `^[\W|_]+$`
|
||||||
|
reg := regexp.MustCompile(pattern)
|
||||||
|
|
||||||
|
s := RandSymbolChar(6)
|
||||||
|
|
||||||
|
result1 := reg.MatchString(s)
|
||||||
|
result2 := len(s)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// 6
|
||||||
|
}
|
||||||
|
|||||||
@@ -154,3 +154,16 @@ func hasDuplicate(arr []int) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRandSymbolChar(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
pattern := `^[\W|_]+$`
|
||||||
|
reg := regexp.MustCompile(pattern)
|
||||||
|
|
||||||
|
symbolChars := RandSymbolChar(10)
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestRandSymbolChar")
|
||||||
|
assert.Equal(10, len(symbolChars))
|
||||||
|
assert.Equal(true, reg.MatchString(symbolChars))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user