update en de short id

This commit is contained in:
aichy126 2023-03-02 19:22:32 +08:00
parent ffee84ced2
commit e0579d8ca6
2 changed files with 68 additions and 7 deletions

View File

@ -1,8 +1,12 @@
package uid
import "strconv"
import (
"fmt"
"strconv"
)
const salt = int64(12345678)
// const salt = int64(1000000)
const salt = int64(0)
var AlphanumericSet = []rune{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
@ -21,28 +25,64 @@ func init() {
// NumToString num to string
func NumToShortID(id int64) string {
sid := strconv.FormatInt(id, 10)
if len(sid) < 17 {
return ""
}
sTypeCode := sid[1:4]
sid = sid[4:int32(len(sid))]
id, err := strconv.ParseInt(sid, 10, 64)
if err != nil {
return ""
}
typeCode, err := strconv.ParseInt(sTypeCode, 10, 64)
if err != nil {
return ""
}
id = id + salt
fmt.Println("[EN1]", typeCode, id)
var code []rune
var tcode []rune
for id > 0 {
idx := id % int64(len(AlphanumericSet))
code = append(code, AlphanumericSet[idx])
id = id / int64(len(AlphanumericSet))
}
return string(code)
for typeCode > 0 {
idx := typeCode % int64(len(AlphanumericSet))
tcode = append(tcode, AlphanumericSet[idx])
typeCode = typeCode / int64(len(AlphanumericSet))
}
fmt.Println("[EN2]", string(tcode), string(code))
return string(tcode) + string(code)
}
// StringToNum string to num
func ShortIDToNum(code string) int64 {
var id int64
if len(code) < 2 {
return 0
}
scodeType := code[0:1]
code = code[1:int32(len(code))]
fmt.Println("[DE1]", scodeType, code)
var id, codeType int64
runes := []rune(code)
codeRunes := []rune(scodeType)
for i := len(runes) - 1; i >= 0; i-- {
ru := runes[i]
idx := AlphanumericIndex[ru]
id = id*int64(len(AlphanumericSet)) + int64(idx)
}
for i := len(codeRunes) - 1; i >= 0; i-- {
ru := codeRunes[i]
idx := AlphanumericIndex[ru]
codeType = codeType*int64(len(AlphanumericSet)) + int64(idx)
}
id = id - salt
return id
fmt.Println("[DE2]", codeType, id)
return 10000000000000000 + codeType*10000000000000 + id
}
func EnShortID(id string) string {

View File

@ -2,11 +2,24 @@ package uid
import (
"fmt"
"strconv"
"testing"
)
func Test_ShortID(t *testing.T) {
nums := []int64{0, 1, 10, 100, 1000, 10000, 100000, 10010000000001316, 10030000000001316, 999999999999999999, 1999999999999999999}
nums := []int64{
10030000000000689, 10010000000000676, 10020000000000658, 10020000000000654,
10030000009000689, 10010000009000676, 10020000009000658, 10020000009999999,
10030000090000689, 10010000090000676, 10020000090000658, 10020000099999999,
10030000900000689, 10010000900000676, 10020000900000658, 10020000999999999,
10030009000000689, 10010009000000676, 10020009000000658, 10020009999999999,
10030090000000689, 10010090000000676, 10020090000000658, 10020099999999999,
10030900000000689, 10010900000000676, 10020900000000658, 10020999999999999,
10039000000000689, 10019000000000676, 10029000000000658, 10029999999999999,
10620000000000689, 10620000000000676, 10620000000000658, 10620000000000654,
19990000000000689, 19990000000000676, 19990000000000658, 19990000000000654,
19999000000000689, 19999000000000676, 19999000000000658, 19999999999999999,
}
for _, num := range nums {
code := NumToShortID(num)
denum := ShortIDToNum(code)
@ -15,10 +28,18 @@ func Test_ShortID(t *testing.T) {
}
func Test_EnDeShortID(t *testing.T) {
nums := []string{"0", "1", "10", "100", "1000", "10000", "100000", "1234567", "10010000000001316", "10030000000001316", "99999999999999999", "999999999999999999", "1999999999999999999"}
nums := []string{"0", "1", "10", "100", "1000", "10000", "100000", "1234567", "10000000000000000", "10010000000001316", "10030000000001316", "99999999999999999", "999999999999999999", "1999999999999999999"}
for _, num := range nums {
code := EnShortID(num)
denum := DeShortID(code)
fmt.Println(num, code, denum)
}
}
func Test_Demo(t *testing.T) {
nums := []int64{0, 1, 10, 100, 1000, 10000, 100000, 1000000001316, 9000000001316, 10000000000000000, 10010000000001316, 10030000000001316, 99999999999999999, 999999999999999999, 1999999999999999999}
for _, num := range nums {
code := strconv.FormatInt(num, 36) //10 yo 16
fmt.Println(num, code)
}
}