refactor: 把 GenId 和 Uniq 抽离到 uniq_helper 里面
This commit is contained in:
parent
9d7fea4d82
commit
3e5a9b3244
@ -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 {
|
||||
|
@ -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)
|
||||
|
51
helper/uniq_helper.go
Normal file
51
helper/uniq_helper.go
Normal file
@ -0,0 +1,51 @@
|
||||
//
|
||||
// uniq_helper.go
|
||||
// Copyright (C) 2023 tiglog <me@tiglog.com>
|
||||
//
|
||||
// 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]
|
||||
}
|
89
helper/uniq_helper_test.go
Normal file
89
helper/uniq_helper_test.go
Normal file
@ -0,0 +1,89 @@
|
||||
//
|
||||
// uniq_helper_test.go
|
||||
// Copyright (C) 2023 tiglog <me@tiglog.com>
|
||||
//
|
||||
// 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user