From 3e5a9b324487c780a17bfbe3a9e5760d7d7c419f Mon Sep 17 00:00:00 2001 From: tiglog Date: Wed, 18 Oct 2023 11:25:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8A=8A=20GenId=20=E5=92=8C=20Uni?= =?UTF-8?q?q=20=E6=8A=BD=E7=A6=BB=E5=88=B0=20uniq=5Fhelper=20=E9=87=8C?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/str_helper.go | 45 +------------------ helper/str_helper_test.go | 73 ------------------------------- helper/uniq_helper.go | 51 ++++++++++++++++++++++ helper/uniq_helper_test.go | 89 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 116 deletions(-) create mode 100644 helper/uniq_helper.go create mode 100644 helper/uniq_helper_test.go diff --git a/helper/str_helper.go b/helper/str_helper.go index 3527399..ac0ddee 100644 --- a/helper/str_helper.go +++ b/helper/str_helper.go @@ -12,9 +12,6 @@ import ( "strings" "time" "unicode" - - "git.hexq.cn/tiglog/golib/crypto/gmd5" - "github.com/rs/xid" ) // 是否是字符串 @@ -26,10 +23,10 @@ func IsString(v interface{}) bool { // 随机字符串 func RandString(n int) string { letterRunes := []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") - rand.Seed(time.Now().UnixNano()) + rd := rand.New(rand.NewSource(time.Now().UnixNano())) b := make([]rune, n) for i := range b { - b[i] = letterRunes[rand.Intn(len(letterRunes))] + b[i] = letterRunes[rd.Intn(len(letterRunes))] } return string(b) } @@ -125,44 +122,6 @@ func Reverse(str string) string { return string(runes[n:]) } -// 唯一字符串 -// 返回字符串长度为 20 -func GenId() string { - guid := xid.New() - return guid.String() -} - -// 可变长度的唯一字符串 -// 长度太短,可能就不唯一了 -// 长度大于等于 16 为最佳 -// 长度小于20时,为 GenId 的值 md5 后的前缀,因此,理论上前6位也能在大多数情况 -// 下唯一 -func Uniq(l int) string { - if l <= 0 { - panic("wrong length param") - } - ret := GenId() - hl := len(ret) - if l < hl { - t, err := gmd5.EncryptString(ret) - if err != nil { - return ret[hl-l:] - } - return t[:l] - } - mhac_len := 6 - pl := len(ret) - var hash string - for l > pl { - hash = GenId() - hash = hash[mhac_len:] - ret += hash - pl += len(hash) - } - // log.Println("ret=", ret, ", pl=", pl, ", l=", l) - return ret[0:l] -} - func GetCjkRange(code int) string { var result string if code >= 0x4E00 && code <= 0x9FFF { diff --git a/helper/str_helper_test.go b/helper/str_helper_test.go index fadb8ea..1db6665 100644 --- a/helper/str_helper_test.go +++ b/helper/str_helper_test.go @@ -46,79 +46,6 @@ func TestUcFirst(t *testing.T) { gtest.Equal(t, "HelloWorld", r3) } -func TestGenId(t *testing.T) { - s1 := helper.GenId() - s2 := helper.GenId() - s3 := helper.GenId() - s4 := helper.GenId() - fmt.Println("gen id: ", s4) - gtest.NotNil(t, s1) - gtest.NotNil(t, s2) - gtest.NotNil(t, s3) - gtest.NotNil(t, s4) - gtest.NotEqual(t, s1, s2) - gtest.NotEqual(t, s1, s3) - gtest.NotEqual(t, s1, s4) - // fmt.Println(s1) - // fmt.Println(s2) - // fmt.Println(s3) - // fmt.Println(s4) - -} - -func TestUniq(t *testing.T) { - s1 := helper.Uniq(1) - fmt.Println("s1=", s1) - gtest.True(t, 1 == len(s1)) - - s12 := helper.Uniq(6) - s13 := helper.Uniq(6) - s14 := helper.Uniq(6) - s15 := helper.Uniq(6) - fmt.Println("s12..15", s12, s13, s14, s15) - gtest.NotNil(t, s12) - gtest.NotNil(t, s13) - gtest.NotNil(t, s14) - gtest.NotNil(t, s15) - gtest.NotEqual(t, s12, s13) - gtest.NotEqual(t, s12, s14) - gtest.NotEqual(t, s12, s15) - - s2 := helper.Uniq(16) - s3 := helper.Uniq(16) - s4 := helper.Uniq(16) - s5 := helper.Uniq(16) - gtest.NotNil(t, s2) - gtest.NotNil(t, s3) - gtest.NotNil(t, s4) - gtest.NotNil(t, s5) - gtest.NotEqual(t, s2, s3) - gtest.NotEqual(t, s2, s4) - gtest.NotEqual(t, s2, s5) - - s6 := helper.Uniq(32) - fmt.Println("s6=", s6) - s7 := helper.Uniq(32) - s8 := helper.Uniq(32) - s9 := helper.Uniq(32) - gtest.NotNil(t, s6) - gtest.NotNil(t, s7) - gtest.NotNil(t, s8) - gtest.NotNil(t, s9) - // fmt.Println("s6789=", s6, s7, s8, s9) - - s60 := helper.Uniq(64) - fmt.Println("s60=", s60) - s70 := helper.Uniq(64) - s80 := helper.Uniq(64) - s90 := helper.Uniq(64) - gtest.NotNil(t, s60) - gtest.NotNil(t, s70) - gtest.NotNil(t, s80) - gtest.NotNil(t, s90) - // fmt.Println(s60, s70, s80, s90) -} - func TestSnake(t *testing.T) { s1 := "XxYy" r1 := helper.SnakeString(s1) diff --git a/helper/uniq_helper.go b/helper/uniq_helper.go new file mode 100644 index 0000000..e3f8f8a --- /dev/null +++ b/helper/uniq_helper.go @@ -0,0 +1,51 @@ +// +// uniq_helper.go +// Copyright (C) 2023 tiglog +// +// Distributed under terms of the MIT license. +// + +package helper + +import ( + "git.hexq.cn/tiglog/golib/crypto/gmd5" + "github.com/rs/xid" +) + +// 唯一字符串 +// 返回字符串长度为 20 +func GenId() string { + guid := xid.New() + return guid.String() +} + +// 可变长度的唯一字符串 +// 长度太短,可能就不唯一了 +// 长度大于等于 16 为最佳 +// 长度小于20时,为 GenId 的值 md5 后的前缀,因此,理论上前6位也能在大多数情况 +// 下唯一 +func Uniq(l int) string { + if l <= 0 { + panic("wrong length param") + } + ret := GenId() + hl := len(ret) + if l < hl { + t, err := gmd5.EncryptString(ret) + if err != nil { + return ret[hl-l:] + } + return t[:l] + } + mhac_len := 6 + pl := len(ret) + var hash string + for l > pl { + hash = GenId() + hash = hash[mhac_len:] + ret += hash + pl += len(hash) + } + // log.Println("ret=", ret, ", pl=", pl, ", l=", l) + return ret[0:l] +} diff --git a/helper/uniq_helper_test.go b/helper/uniq_helper_test.go new file mode 100644 index 0000000..41179f1 --- /dev/null +++ b/helper/uniq_helper_test.go @@ -0,0 +1,89 @@ +// +// uniq_helper_test.go +// Copyright (C) 2023 tiglog +// +// Distributed under terms of the MIT license. +// + +package helper_test + +import ( + "fmt" + "testing" + + "git.hexq.cn/tiglog/golib/gtest" + "git.hexq.cn/tiglog/golib/helper" +) + +func TestGenId(t *testing.T) { + s1 := helper.GenId() + s2 := helper.GenId() + s3 := helper.GenId() + s4 := helper.GenId() + fmt.Println("gen id: ", s4) + gtest.NotNil(t, s1) + gtest.NotNil(t, s2) + gtest.NotNil(t, s3) + gtest.NotNil(t, s4) + gtest.NotEqual(t, s1, s2) + gtest.NotEqual(t, s1, s3) + gtest.NotEqual(t, s1, s4) + // fmt.Println(s1) + // fmt.Println(s2) + // fmt.Println(s3) + // fmt.Println(s4) + +} + +func TestUniq(t *testing.T) { + s1 := helper.Uniq(1) + fmt.Println("s1=", s1) + gtest.True(t, 1 == len(s1)) + + s12 := helper.Uniq(6) + s13 := helper.Uniq(6) + s14 := helper.Uniq(6) + s15 := helper.Uniq(6) + fmt.Println("s12..15", s12, s13, s14, s15) + gtest.NotNil(t, s12) + gtest.NotNil(t, s13) + gtest.NotNil(t, s14) + gtest.NotNil(t, s15) + gtest.NotEqual(t, s12, s13) + gtest.NotEqual(t, s12, s14) + gtest.NotEqual(t, s12, s15) + + s2 := helper.Uniq(16) + s3 := helper.Uniq(16) + s4 := helper.Uniq(16) + s5 := helper.Uniq(16) + gtest.NotNil(t, s2) + gtest.NotNil(t, s3) + gtest.NotNil(t, s4) + gtest.NotNil(t, s5) + gtest.NotEqual(t, s2, s3) + gtest.NotEqual(t, s2, s4) + gtest.NotEqual(t, s2, s5) + + s6 := helper.Uniq(32) + fmt.Println("s6=", s6) + s7 := helper.Uniq(32) + s8 := helper.Uniq(32) + s9 := helper.Uniq(32) + gtest.NotNil(t, s6) + gtest.NotNil(t, s7) + gtest.NotNil(t, s8) + gtest.NotNil(t, s9) + // fmt.Println("s6789=", s6, s7, s8, s9) + + s60 := helper.Uniq(64) + fmt.Println("s60=", s60) + s70 := helper.Uniq(64) + s80 := helper.Uniq(64) + s90 := helper.Uniq(64) + gtest.NotNil(t, s60) + gtest.NotNil(t, s70) + gtest.NotNil(t, s80) + gtest.NotNil(t, s90) + // fmt.Println(s60, s70, s80, s90) +}