Skip to content

Commit

Permalink
feat:add new rand string function RandLowerAlphanumeric
Browse files Browse the repository at this point in the history
  • Loading branch information
R22627 authored and R22627 committed Dec 29, 2023
1 parent 0575d71 commit 257de1f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
26 changes: 19 additions & 7 deletions math/randstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package math
import "strings"

const (
alphabetic = "abcdefghijklmnopqrstuvwxyz"
alphabeticLen = len(alphabetic)
alphabeticMix = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
alphabeticMixLen = len(alphabeticMix)
alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
alphanumericLen = len(alphanumeric)
alphabetic = "abcdefghijklmnopqrstuvwxyz"
alphabeticLen = len(alphabetic)
alphabeticMix = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
alphabeticMixLen = len(alphabeticMix)
alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
alphanumericLen = len(alphanumeric)
lowerAlphanumeric = "abcdefghijklmnopqrstuvwxyz0123456789"
lowerAlphanumericLen = len(lowerAlphanumeric)
)

// RandLowerStr returns a truly random lowercase string of a specified length.
Expand All @@ -26,7 +28,7 @@ func RandUpperStr(l uint32) string {
}

// RandStr returns a truly random string of the specified length and the contents of
// which are composed of upper and lowercase letters.
// which are composed of uppercase and lowercase letters.
func RandStr(l int) string {
s := make([]byte, l)
for i := range s {
Expand All @@ -44,3 +46,13 @@ func RandAlphanumeric(l int) string {
}
return string(s)
}

// RandLowerAlphanumeric returns a truly random string of the specified length and the contents of
// which are composed of lowercase letters and numbers.
func RandLowerAlphanumeric(l int) string {
s := make([]byte, l)
for i := range s {
s[i] = alphanumeric[RandIntn(lowerAlphanumericLen)]
}
return string(s)
}
6 changes: 6 additions & 0 deletions math/randstr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ func TestRandAlphanumeric(t *testing.T) {
s := RandAlphanumeric(4)
assert.Equal(4, len(s))
}

func TestRandLowerAlphanumeric(t *testing.T) {
assert := internal.NewAssert(t, "TestRandLowerAlphanumeric")
s := RandLowerAlphanumeric(4)
assert.Equal(4, len(s))
}

0 comments on commit 257de1f

Please sign in to comment.