From 05be4870859bca0e961984770925ab6f961df6ff Mon Sep 17 00:00:00 2001
From: dtapps
Date: Wed, 20 Dec 2023 15:05:50 +0800
Subject: [PATCH] update
---
custom.go | 173 ++++-------------------------------------------
custom_common.go | 144 +++++++++++++++++++++++++++++++++++++++
custom_length.go | 56 +++++++++++++++
go.mod | 19 +++++-
go.sum | 63 +++++++++++++++--
random.string.go | 74 ++++++++++++++++++++
string.go | 37 ++++++++++
url.go | 45 ++++++++++++
8 files changed, 446 insertions(+), 165 deletions(-)
create mode 100644 custom_common.go
create mode 100644 custom_length.go
create mode 100644 random.string.go
create mode 100644 url.go
diff --git a/custom.go b/custom.go
index 103ada9..8b217aa 100644
--- a/custom.go
+++ b/custom.go
@@ -1,203 +1,58 @@
package gostring
import (
- "errors"
- "fmt"
- "go.dtapp.net/gorandom"
"go.dtapp.net/gotime"
)
-// GenerateId 生成18一编号
+// GenerateId 生成18位编号
func GenerateId(customId string) string {
currentTime := gotime.Current().Format()
- one, err := generateIdOne(customId, currentTime)
+ one, err := generateIdOne(customId, currentTime, 18)
if err == nil {
return one
}
- two, err := generateIdTwo(customId, currentTime)
+ two, err := generateIdTwo(customId, currentTime, 18)
if err == nil {
return two
}
- three, err := generateIdThree(customId, currentTime)
+ three, err := generateIdThree(customId, currentTime, 18)
if err == nil {
return three
}
- four, err := generateIdFour(customId, currentTime)
+ four, err := generateIdFour(customId, currentTime, 18)
if err == nil {
return four
}
- five, err := generateIdFive(customId, currentTime)
+ five, err := generateIdFive(customId, currentTime, 18)
if err == nil {
return five
}
- six, err := generateIdSix(customId)
+ six, err := generateIdSix(customId, 18)
return six
}
-// GenerateIdAndTime 生成18一编号
+// GenerateIdAndTime 生成18位编号
func GenerateIdAndTime(customId, customTime string) string {
- one, err := generateIdOne(customId, customTime)
+ one, err := generateIdOne(customId, customTime, 18)
if err == nil {
return one
}
- two, err := generateIdTwo(customId, customTime)
+ two, err := generateIdTwo(customId, customTime, 18)
if err == nil {
return two
}
- three, err := generateIdThree(customId, customTime)
+ three, err := generateIdThree(customId, customTime, 18)
if err == nil {
return three
}
- four, err := generateIdFour(customId, customTime)
+ four, err := generateIdFour(customId, customTime, 18)
if err == nil {
return four
}
- five, err := generateIdFive(customId, customTime)
+ five, err := generateIdFive(customId, customTime, 18)
if err == nil {
return five
}
- six, err := generateIdSix(customId)
+ six, err := generateIdSix(customId, 18)
return six
}
-
-// 生成18位时间[年月日时分]唯一编号
-func generateIdOne(customId, setTime string) (string, error) {
-
- var (
- newRandomLength = 0 // 随机数据长度
- customIdLength = len(customId) // 自定义长度
- )
-
- const (
- randomLength = 5 // 随机数据长度
- dataLength = 18 // 默认数据长度
- dateFormat = "200601021504" // 时间格式
- dateFormatLength = len(dateFormat) // 时间格式长度
- )
-
- // 重新计算随机数据长度
- newRandomLength = dataLength - (dateFormatLength + customIdLength)
-
- if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) {
- return "", errors.New("没有满足条件")
- }
- return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil
-}
-
-// 生成18位时间[年月日时]唯一编号
-func generateIdTwo(customId, setTime string) (string, error) {
-
- var (
- newRandomLength = 0 // 随机数据长度
- customIdLength = len(customId) // 自定义长度
- )
-
- const (
- randomLength = 5 // 随机数据长度
- dataLength = 18 // 默认数据长度
- dateFormat = "2006010215" // 时间格式
- dateFormatLength = len(dateFormat) // 时间格式长度
- )
-
- // 重新计算随机数据长度
- newRandomLength = dataLength - (dateFormatLength + customIdLength)
-
- if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) {
- return "", errors.New("没有满足条件")
- }
- return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil
-}
-
-// 生成18位时间[年月日]唯一编号
-func generateIdThree(customId, setTime string) (string, error) {
-
- var (
- newRandomLength = 0 // 随机数据长度
- customIdLength = len(customId) // 自定义长度
- )
-
- const (
- randomLength = 5 // 随机数据长度
- dataLength = 18 // 默认数据长度
- dateFormat = "20060102" // 时间格式
- dateFormatLength = len(dateFormat) // 时间格式长度
- )
-
- // 重新计算随机数据长度
- newRandomLength = dataLength - (dateFormatLength + customIdLength)
-
- if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) {
- return "", errors.New("没有满足条件")
- }
- return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil
-}
-
-// 生成18位时间[年月]唯一编号
-func generateIdFour(customId, setTime string) (string, error) {
-
- var (
- newRandomLength = 0 // 随机数据长度
- customIdLength = len(customId) // 自定义长度
- )
-
- const (
- randomLength = 5 // 随机数据长度
- dataLength = 18 // 默认数据长度
- dateFormat = "200601" // 时间格式
- dateFormatLength = len(dateFormat) // 时间格式长度
- )
-
- // 重新计算随机数据长度
- newRandomLength = dataLength - (dateFormatLength + customIdLength)
-
- if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) {
- return "", errors.New("没有满足条件")
- }
- return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil
-}
-
-// 生成18位时间[年]唯一编号
-func generateIdFive(customId, setTime string) (string, error) {
-
- var (
- newRandomLength = 0 // 随机数据长度
- customIdLength = len(customId) // 自定义长度
- )
-
- const (
- randomLength = 5 // 随机数据长度`
- dataLength = 18 // 默认数据长度
- dateFormat = "2006" // 时间格式
- dateFormatLength = len(dateFormat) // 时间格式长度
- )
-
- // 重新计算随机数据长度
- newRandomLength = dataLength - (dateFormatLength + customIdLength)
-
- if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) {
- return "", errors.New("没有满足条件")
- }
- return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil
-}
-
-// 生成18位随机唯一编号
-func generateIdSix(customId string) (string, error) {
-
- var (
- newRandomLength = 0 // 随机数据长度
- customIdLength = len(customId) // 自定义长度
- )
-
- const (
- randomLength = 5 // 随机数据长度
- dataLength = 18 // 默认数据长度
- )
-
- // 重新计算随机数据长度
- newRandomLength = dataLength - customIdLength
-
- if (customIdLength > dataLength) || (customIdLength == dataLength) || (newRandomLength < randomLength) {
- return "", errors.New("没有满足条件")
- }
- return fmt.Sprintf("%v%s", customId, gorandom.Numeric(newRandomLength)), nil
-}
diff --git a/custom_common.go b/custom_common.go
new file mode 100644
index 0000000..613b597
--- /dev/null
+++ b/custom_common.go
@@ -0,0 +1,144 @@
+package gostring
+
+import (
+ "errors"
+ "fmt"
+ "go.dtapp.net/gorandom"
+ "go.dtapp.net/gotime"
+)
+
+// 生成18位时间[年月日时分]唯一编号
+func generateIdOne(customId, setTime string, dataLength int) (string, error) {
+
+ var (
+ newRandomLength = 0 // 随机数据长度
+ customIdLength = len(customId) // 自定义长度
+ )
+
+ const (
+ randomLength = 5 // 随机数据长度
+ dateFormat = "200601021504" // 时间格式
+ dateFormatLength = len(dateFormat) // 时间格式长度
+ )
+
+ // 重新计算随机数据长度
+ newRandomLength = dataLength - (dateFormatLength + customIdLength)
+
+ if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) {
+ return "", errors.New("没有满足条件")
+ }
+ return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil
+}
+
+// 生成18位时间[年月日时]唯一编号
+func generateIdTwo(customId, setTime string, dataLength int) (string, error) {
+
+ var (
+ newRandomLength = 0 // 随机数据长度
+ customIdLength = len(customId) // 自定义长度
+ )
+
+ const (
+ randomLength = 5 // 随机数据长度
+ dateFormat = "2006010215" // 时间格式
+ dateFormatLength = len(dateFormat) // 时间格式长度
+ )
+
+ // 重新计算随机数据长度
+ newRandomLength = dataLength - (dateFormatLength + customIdLength)
+
+ if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) {
+ return "", errors.New("没有满足条件")
+ }
+ return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil
+}
+
+// 生成18位时间[年月日]唯一编号
+func generateIdThree(customId, setTime string, dataLength int) (string, error) {
+
+ var (
+ newRandomLength = 0 // 随机数据长度
+ customIdLength = len(customId) // 自定义长度
+ )
+
+ const (
+ randomLength = 5 // 随机数据长度
+ dateFormat = "20060102" // 时间格式
+ dateFormatLength = len(dateFormat) // 时间格式长度
+ )
+
+ // 重新计算随机数据长度
+ newRandomLength = dataLength - (dateFormatLength + customIdLength)
+
+ if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) {
+ return "", errors.New("没有满足条件")
+ }
+ return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil
+}
+
+// 生成18位时间[年月]唯一编号
+func generateIdFour(customId, setTime string, dataLength int) (string, error) {
+
+ var (
+ newRandomLength = 0 // 随机数据长度
+ customIdLength = len(customId) // 自定义长度
+ )
+
+ const (
+ randomLength = 5 // 随机数据长度
+ dateFormat = "200601" // 时间格式
+ dateFormatLength = len(dateFormat) // 时间格式长度
+ )
+
+ // 重新计算随机数据长度
+ newRandomLength = dataLength - (dateFormatLength + customIdLength)
+
+ if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) {
+ return "", errors.New("没有满足条件")
+ }
+ return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil
+}
+
+// 生成18位时间[年]唯一编号
+func generateIdFive(customId, setTime string, dataLength int) (string, error) {
+
+ var (
+ newRandomLength = 0 // 随机数据长度
+ customIdLength = len(customId) // 自定义长度
+ )
+
+ const (
+ randomLength = 5 // 随机数据长度`
+ dateFormat = "2006" // 时间格式
+ dateFormatLength = len(dateFormat) // 时间格式长度
+ )
+
+ // 重新计算随机数据长度
+ newRandomLength = dataLength - (dateFormatLength + customIdLength)
+
+ if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) {
+ return "", errors.New("没有满足条件")
+ }
+ return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil
+}
+
+// 生成18位随机唯一编号
+func generateIdSix(customId string, dataLength int) (string, error) {
+
+ var (
+ newRandomLength = 0 // 随机数据长度
+ customIdLength = len(customId) // 自定义长度
+ )
+
+ const (
+ randomLength = 5 // 随机数据长度
+ )
+
+ // 重新计算随机数据长度
+ newRandomLength = dataLength - customIdLength
+
+ if (customIdLength > dataLength) || (customIdLength == dataLength) || (newRandomLength < randomLength) {
+ return "", errors.New("没有满足条件")
+ }
+ return fmt.Sprintf("%v%s", customId, gorandom.Numeric(newRandomLength)), nil
+}
diff --git a/custom_length.go b/custom_length.go
new file mode 100644
index 0000000..a17d760
--- /dev/null
+++ b/custom_length.go
@@ -0,0 +1,56 @@
+package gostring
+
+import "go.dtapp.net/gotime"
+
+// GenerateIdLength 生成自定义长度编号
+func GenerateIdLength(customId string, dataLength int) string {
+ currentTime := gotime.Current().Format()
+ one, err := generateIdOne(customId, currentTime, dataLength)
+ if err == nil {
+ return one
+ }
+ two, err := generateIdTwo(customId, currentTime, dataLength)
+ if err == nil {
+ return two
+ }
+ three, err := generateIdThree(customId, currentTime, dataLength)
+ if err == nil {
+ return three
+ }
+ four, err := generateIdFour(customId, currentTime, dataLength)
+ if err == nil {
+ return four
+ }
+ five, err := generateIdFive(customId, currentTime, dataLength)
+ if err == nil {
+ return five
+ }
+ six, err := generateIdSix(customId, dataLength)
+ return six
+}
+
+// GenerateIdAndTimeLength 生成自定义长度编号
+func GenerateIdAndTimeLength(customId, customTime string, dataLength int) string {
+ one, err := generateIdOne(customId, customTime, dataLength)
+ if err == nil {
+ return one
+ }
+ two, err := generateIdTwo(customId, customTime, dataLength)
+ if err == nil {
+ return two
+ }
+ three, err := generateIdThree(customId, customTime, dataLength)
+ if err == nil {
+ return three
+ }
+ four, err := generateIdFour(customId, customTime, dataLength)
+ if err == nil {
+ return four
+ }
+ five, err := generateIdFive(customId, customTime, dataLength)
+ if err == nil {
+ return five
+ }
+ six, err := generateIdSix(customId, dataLength)
+ return six
+}
diff --git a/go.mod b/go.mod
index c9e80c7..6c940c3 100644
--- a/go.mod
+++ b/go.mod
@@ -3,6 +3,21 @@ module go.dtapp.net/gostring
go 1.18
require (
- go.dtapp.net/gorandom v1.0.1
- go.dtapp.net/gotime v1.0.5
+ go.dtapp.net/gojson v1.0.2
+ go.dtapp.net/gorandom v1.0.2
+ go.dtapp.net/gotime v1.0.6
+)
+
+require (
+ github.com/bytedance/sonic v1.10.2 // indirect
+ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
+ github.com/chenzhuoyu/iasm v0.9.1 // indirect
+ github.com/goccy/go-json v0.10.2 // indirect
+ github.com/json-iterator/go v1.1.12 // indirect
+ github.com/klauspost/cpuid/v2 v2.2.6 // indirect
+ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
+ github.com/modern-go/reflect2 v1.0.2 // indirect
+ github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
+ golang.org/x/arch v0.6.0 // indirect
+ golang.org/x/sys v0.15.0 // indirect
)
diff --git a/go.sum b/go.sum
index 6ce97d6..efdb861 100644
--- a/go.sum
+++ b/go.sum
@@ -1,4 +1,59 @@
-go.dtapp.net/gorandom v1.0.1 h1:IWfMClh1ECPvyUjlqD7MwLq4mZdUusD1qAwAdsvEJBs=
-go.dtapp.net/gorandom v1.0.1/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8=
-go.dtapp.net/gotime v1.0.5 h1:12aNgB2ULpP6QgQHEUkLilZ4ASvhpFxMFQkBwn0par8=
-go.dtapp.net/gotime v1.0.5/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY=
+github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
+github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
+github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
+github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
+github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
+github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
+github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
+github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
+github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
+github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0=
+github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
+github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
+github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
+github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
+github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
+github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
+github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
+github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
+github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
+github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
+github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
+github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
+github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
+github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
+github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
+github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
+github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
+github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
+go.dtapp.net/gojson v1.0.2 h1:NjslBOhAK3XvJepkML7LXcJRPtSfp3rDXGK/29VlDBw=
+go.dtapp.net/gojson v1.0.2/go.mod h1:U0Vd2iSLKqdyg6oungBJVHxuYswTbeQVjruQC3/JqQ4=
+go.dtapp.net/gorandom v1.0.2 h1:08BdcBP/fQiRw2Ii0OXATSTtObwNydlDlqc/j/u5O8Q=
+go.dtapp.net/gorandom v1.0.2/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8=
+go.dtapp.net/gotime v1.0.6 h1:f8YowUxpZtJbYawe5s5PmvGxRj61ydlzxAPYr5Fcetg=
+go.dtapp.net/gotime v1.0.6/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY=
+golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
+golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc=
+golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
+golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
+golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
+rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
diff --git a/random.string.go b/random.string.go
new file mode 100644
index 0000000..3cd4033
--- /dev/null
+++ b/random.string.go
@@ -0,0 +1,74 @@
+package gostring
+
+import (
+ "math/rand"
+ "time"
+)
+
+// GenerateRandomString 生成count个长度length不重复的随机数
+func GenerateRandomString(length, count int) []string {
+ return GenerateRandomStringFunc(length, count, func(num string) bool {
+ return false
+ })
+}
+
+// GenerateRandomStringFunc 生成count个长度length不重复的随机数,支持外部查询
+func GenerateRandomStringFunc(length, count int, dFun func(num string) bool) []string {
+
+ var fI int = 0
+ startStr := "1"
+ endStr := "9"
+ alphabet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+ rand.Seed(time.Now().UnixNano())
+
+ for {
+ if fI+2 > length {
+ break
+ }
+ startStr += "0"
+ endStr += "9"
+
+ fI = fI + 1
+ }
+
+ return GenerateRandomStringNumber(ToInt(startStr), ToInt(endStr), count, alphabet, length, dFun)
+}
+
+// GenerateRandomStringNumber 生成count个[start,end)结束的不重复的随机数
+func GenerateRandomStringNumber(start, end, count int, alphabet string, length int, dFun func(num string) bool) []string {
+
+ // 范围检查
+ if end < start || len(alphabet) == 0 || (end-start) < count {
+ return nil
+ }
+
+ // 存放结果的slice
+ results := make([]string, 0)
+
+ for len(results) < count {
+
+ // 生成随机字符串
+ str := make([]byte, length)
+ for i := range str {
+ str[i] = alphabet[rand.Intn(len(alphabet))]
+ }
+
+ // 查重
+ exist := false
+ for _, v := range results {
+ if v == string(str) {
+ exist = true
+ break
+ }
+ }
+
+ if !exist {
+ isExist := dFun(string(str))
+ if !isExist {
+ results = append(results, string(str))
+ }
+ }
+ }
+
+ return results
+}
diff --git a/string.go b/string.go
index 0929b2f..c2c2a3b 100644
--- a/string.go
+++ b/string.go
@@ -5,9 +5,11 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
+ "go.dtapp.net/gojson"
"strconv"
"strings"
"unicode/utf8"
+ "unsafe"
)
// ToString 转换成string
@@ -150,3 +152,38 @@ func NumericalToString(value interface{}) (string, bool) {
}
return val, true
}
+
+func StringToBytes(s string) []byte {
+ return unsafe.Slice(unsafe.StringData(s), len(s))
+}
+
+func BytesToString(b []byte) string {
+ return unsafe.String(&b[0], len(b))
+}
+
+// ToUpper 转大写
+func ToUpper(s string) string {
+ return strings.ToUpper(s)
+}
+
+// ToLower 转小写
+func ToLower(s string) string {
+ return strings.ToLower(s)
+}
+
+func GetString(i interface{}) string {
+ switch v := i.(type) {
+ case string:
+ return v
+ case []byte:
+ return string(v)
+ case int:
+ return strconv.Itoa(v)
+ case bool:
+ return strconv.FormatBool(v)
+ default:
+
+ bytes, _ := gojson.Marshal(v)
+ return string(bytes)
+ }
+}
diff --git a/url.go b/url.go
new file mode 100644
index 0000000..0f8f297
--- /dev/null
+++ b/url.go
@@ -0,0 +1,45 @@
+package gostring
+
+import (
+ "strings"
+)
+
+// CompleteUrlHttp 补全 URL
+func CompleteUrlHttp(url string) string {
+ if url == "" {
+ return url
+ }
+ if strings.HasPrefix(url, "http://") {
+ return url
+ }
+ if strings.HasPrefix(url, "//") {
+ url = "http:" + url
+ } else if strings.HasPrefix(url, "://") {
+ url = "http" + url
+ } else if strings.HasPrefix(url, "https://") {
+ url = Replace(url, "https://", "http://")
+ } else {
+ url = "http://" + url
+ }
+ return url
+}
+
+// CompleteUrlHttps 补全 URL
+func CompleteUrlHttps(url string) string {
+ if url == "" {
+ return url
+ }
+ if strings.HasPrefix(url, "https://") {
+ return url
+ }
+ if strings.HasPrefix(url, "//") {
+ url = "https:" + url
+ } else if strings.HasPrefix(url, "://") {
+ url = "https" + url
+ } else if strings.HasPrefix(url, "http://") {
+ url = Replace(url, "http://", "https://")
+ } else {
+ url = "https://" + url
+ }
+ return url
+}