From 257de1f7d72f14dbdcd7e1aa8eb294e9632b271a Mon Sep 17 00:00:00 2001 From: R22627 Date: Fri, 29 Dec 2023 11:03:54 +0800 Subject: [PATCH] feat:add new rand string function RandLowerAlphanumeric --- math/randstr.go | 26 +++++++++++++++++++------- math/randstr_test.go | 6 ++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/math/randstr.go b/math/randstr.go index d25a0cb..579ca66 100644 --- a/math/randstr.go +++ b/math/randstr.go @@ -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. @@ -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 { @@ -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) +} diff --git a/math/randstr_test.go b/math/randstr_test.go index 7811d32..3ee3893 100644 --- a/math/randstr_test.go +++ b/math/randstr_test.go @@ -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)) +}