mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
6 Commits
v2.1.11
...
d59259bbe0
| Author | SHA1 | Date | |
|---|---|---|---|
| d59259bbe0 | |||
| 3189628d54 | |||
| 62c5e251a5 | |||
| 6e6444c8c0 | |||
| dd613e98b2 | |||
| 2d905ab03e |
@@ -19,7 +19,7 @@
|
|||||||
Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js.
|
Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
English | [简体中文](./README_zh-CN.md) | [Website](https://uvdream.github.io/lancet-docs/en/)
|
English | [简体中文](./README_zh-CN.md)
|
||||||
|
|
||||||
## Feature
|
## Feature
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@
|
|||||||
lancet(柳叶刀)是一个全面、高效、可复用的go语言工具函数库。 lancet受到了java apache common包和lodash.js的启发。
|
lancet(柳叶刀)是一个全面、高效、可复用的go语言工具函数库。 lancet受到了java apache common包和lodash.js的启发。
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
简体中文 | [English](./README.md) | [文档](https://uvdream.github.io/lancet-docs)
|
简体中文 | [English](./README.md)
|
||||||
|
|
||||||
## 特性
|
## 特性
|
||||||
|
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ func ToString(value any) string {
|
|||||||
newValue, _ := json.Marshal(value)
|
newValue, _ := json.Marshal(value)
|
||||||
return string(newValue)
|
return string(newValue)
|
||||||
|
|
||||||
// todo: maybe we should't supprt other type convertion
|
// todo: maybe we should't supprt other type conversion
|
||||||
// v := reflect.ValueOf(value)
|
// v := reflect.ValueOf(value)
|
||||||
// log.Panicf("Unsupported data type: %s ", v.String())
|
// log.Panicf("Unsupported data type: %s ", v.String())
|
||||||
// return ""
|
// return ""
|
||||||
|
|||||||
+14
-2
@@ -17,15 +17,19 @@ import (
|
|||||||
// AesEcbEncrypt encrypt data with key use AES ECB algorithm
|
// AesEcbEncrypt encrypt data with key use AES ECB algorithm
|
||||||
// len(key) should be 16, 24 or 32
|
// len(key) should be 16, 24 or 32
|
||||||
func AesEcbEncrypt(data, key []byte) []byte {
|
func AesEcbEncrypt(data, key []byte) []byte {
|
||||||
cipher, _ := aes.NewCipher(generateAesKey(key))
|
|
||||||
length := (len(data) + aes.BlockSize) / aes.BlockSize
|
length := (len(data) + aes.BlockSize) / aes.BlockSize
|
||||||
plain := make([]byte, length*aes.BlockSize)
|
plain := make([]byte, length*aes.BlockSize)
|
||||||
|
|
||||||
copy(plain, data)
|
copy(plain, data)
|
||||||
|
|
||||||
pad := byte(len(plain) - len(data))
|
pad := byte(len(plain) - len(data))
|
||||||
for i := len(data); i < len(plain); i++ {
|
for i := len(data); i < len(plain); i++ {
|
||||||
plain[i] = pad
|
plain[i] = pad
|
||||||
}
|
}
|
||||||
|
|
||||||
encrypted := make([]byte, len(plain))
|
encrypted := make([]byte, len(plain))
|
||||||
|
cipher, _ := aes.NewCipher(generateAesKey(key))
|
||||||
|
|
||||||
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
||||||
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
|
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
|
||||||
}
|
}
|
||||||
@@ -108,27 +112,32 @@ func AesCfbEncrypt(data, key []byte) []byte {
|
|||||||
|
|
||||||
encrypted := make([]byte, aes.BlockSize+len(data))
|
encrypted := make([]byte, aes.BlockSize+len(data))
|
||||||
iv := encrypted[:aes.BlockSize]
|
iv := encrypted[:aes.BlockSize]
|
||||||
|
|
||||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
stream := cipher.NewCFBEncrypter(block, iv)
|
stream := cipher.NewCFBEncrypter(block, iv)
|
||||||
stream.XORKeyStream(encrypted[aes.BlockSize:], data)
|
stream.XORKeyStream(encrypted[aes.BlockSize:], data)
|
||||||
|
|
||||||
return encrypted
|
return encrypted
|
||||||
}
|
}
|
||||||
|
|
||||||
// AesCfbDecrypt decrypt data with key use AES CFB algorithm
|
// AesCfbDecrypt decrypt data with key use AES CFB algorithm
|
||||||
// len(encrypted) should be great than 16, len(key) should be 16, 24 or 32
|
// len(encrypted) should be great than 16, len(key) should be 16, 24 or 32
|
||||||
func AesCfbDecrypt(encrypted, key []byte) []byte {
|
func AesCfbDecrypt(encrypted, key []byte) []byte {
|
||||||
block, _ := aes.NewCipher(key)
|
|
||||||
if len(encrypted) < aes.BlockSize {
|
if len(encrypted) < aes.BlockSize {
|
||||||
panic("encrypted data is too short")
|
panic("encrypted data is too short")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
block, _ := aes.NewCipher(key)
|
||||||
iv := encrypted[:aes.BlockSize]
|
iv := encrypted[:aes.BlockSize]
|
||||||
encrypted = encrypted[aes.BlockSize:]
|
encrypted = encrypted[aes.BlockSize:]
|
||||||
|
|
||||||
stream := cipher.NewCFBDecrypter(block, iv)
|
stream := cipher.NewCFBDecrypter(block, iv)
|
||||||
|
|
||||||
stream.XORKeyStream(encrypted, encrypted)
|
stream.XORKeyStream(encrypted, encrypted)
|
||||||
|
|
||||||
return encrypted
|
return encrypted
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,6 +148,7 @@ func AesOfbEncrypt(data, key []byte) []byte {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data = pkcs7Padding(data, aes.BlockSize)
|
data = pkcs7Padding(data, aes.BlockSize)
|
||||||
encrypted := make([]byte, aes.BlockSize+len(data))
|
encrypted := make([]byte, aes.BlockSize+len(data))
|
||||||
iv := encrypted[:aes.BlockSize]
|
iv := encrypted[:aes.BlockSize]
|
||||||
@@ -148,6 +158,7 @@ func AesOfbEncrypt(data, key []byte) []byte {
|
|||||||
|
|
||||||
stream := cipher.NewOFB(block, iv)
|
stream := cipher.NewOFB(block, iv)
|
||||||
stream.XORKeyStream(encrypted[aes.BlockSize:], data)
|
stream.XORKeyStream(encrypted[aes.BlockSize:], data)
|
||||||
|
|
||||||
return encrypted
|
return encrypted
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,5 +181,6 @@ func AesOfbDecrypt(data, key []byte) []byte {
|
|||||||
mode.XORKeyStream(decrypted, data)
|
mode.XORKeyStream(decrypted, data)
|
||||||
|
|
||||||
decrypted = pkcs7UnPadding(decrypted)
|
decrypted = pkcs7UnPadding(decrypted)
|
||||||
|
|
||||||
return decrypted
|
return decrypted
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -15,7 +15,6 @@ import (
|
|||||||
// DesEcbEncrypt encrypt data with key use DES ECB algorithm
|
// DesEcbEncrypt encrypt data with key use DES ECB algorithm
|
||||||
// len(key) should be 8
|
// len(key) should be 8
|
||||||
func DesEcbEncrypt(data, key []byte) []byte {
|
func DesEcbEncrypt(data, key []byte) []byte {
|
||||||
cipher, _ := des.NewCipher(generateDesKey(key))
|
|
||||||
length := (len(data) + des.BlockSize) / des.BlockSize
|
length := (len(data) + des.BlockSize) / des.BlockSize
|
||||||
plain := make([]byte, length*des.BlockSize)
|
plain := make([]byte, length*des.BlockSize)
|
||||||
copy(plain, data)
|
copy(plain, data)
|
||||||
@@ -26,6 +25,8 @@ func DesEcbEncrypt(data, key []byte) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
encrypted := make([]byte, len(plain))
|
encrypted := make([]byte, len(plain))
|
||||||
|
cipher, _ := des.NewCipher(generateDesKey(key))
|
||||||
|
|
||||||
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
||||||
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
|
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
|
||||||
}
|
}
|
||||||
@@ -59,6 +60,7 @@ func DesCbcEncrypt(data, key []byte) []byte {
|
|||||||
|
|
||||||
encrypted := make([]byte, des.BlockSize+len(data))
|
encrypted := make([]byte, des.BlockSize+len(data))
|
||||||
iv := encrypted[:des.BlockSize]
|
iv := encrypted[:des.BlockSize]
|
||||||
|
|
||||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
// Package iterator provides a way to iterate over values stored in containers.
|
// Package iterator provides a way to iterate over values stored in containers.
|
||||||
// note:
|
// note:
|
||||||
// 1. Full feature iterator is complicated, this pacakge is just a experiment to explore how iterators could work in Go.
|
// 1. Full feature iterator is complicated, this package is just a experiment to explore how iterators could work in Go.
|
||||||
// 2. The functionality of this package is very simple and limited, may not meet the actual dev needs.
|
// 2. The functionality of this package is very simple and limited, may not meet the actual dev needs.
|
||||||
// 3. It is currently under development, unstable, and will not be completed for some time in the future.
|
// 3. It is currently under development, unstable, and will not be completed for some time in the future.
|
||||||
// So, based on above factors, you may not use it in production. but, anyone is welcome to improve it.
|
// So, based on above factors, you may not use it in production. but, anyone is welcome to improve it.
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
// Package iterator provides a way to iterate over values stored in containers.
|
// Package iterator provides a way to iterate over values stored in containers.
|
||||||
// note:
|
// note:
|
||||||
// 1. Full feature iterator is complicated, this pacakge is just a experiment to explore how iterators could work in Go.
|
// 1. Full feature iterator is complicated, this package is just a experiment to explore how iterators could work in Go.
|
||||||
// 2. The functionality of this package is very simple and limited, may not meet the actual dev needs.
|
// 2. The functionality of this package is very simple and limited, may not meet the actual dev needs.
|
||||||
// 3. It is currently under development, unstable, and will not be completed for some time in the future.
|
// 3. It is currently under development, unstable, and will not be completed for some time in the future.
|
||||||
// So, based on above factors, you may not use it in production. but, anyone is welcome to improve it.
|
// So, based on above factors, you may not use it in production. but, anyone is welcome to improve it.
|
||||||
|
|||||||
+42
-5
@@ -14,6 +14,13 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/lancetconstraints"
|
"github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Create a static variable to store the hash table.
|
||||||
|
// This variable has the same lifetime as the entire program and can be shared by functions that are called more than once.
|
||||||
|
var (
|
||||||
|
memoryHashMap = make(map[string]map[any]int)
|
||||||
|
memoryHashCounter = make(map[string]int)
|
||||||
|
)
|
||||||
|
|
||||||
// Contain check if the target value is in the slice or not
|
// Contain check if the target value is in the slice or not
|
||||||
func Contain[T comparable](slice []T, target T) bool {
|
func Contain[T comparable](slice []T, target T) bool {
|
||||||
for _, item := range slice {
|
for _, item := range slice {
|
||||||
@@ -832,14 +839,44 @@ func Without[T comparable](slice []T, items ...T) []T {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// IndexOf returns the index at which the first occurrence of a item is found in a slice or return -1 if the item cannot be found.
|
// IndexOf returns the index at which the first occurrence of an item is found in a slice or return -1 if the item cannot be found.
|
||||||
func IndexOf[T comparable](slice []T, item T) int {
|
func IndexOf[T comparable](arr []T, val T) int {
|
||||||
for i, v := range slice {
|
limit := 10
|
||||||
if v == item {
|
// gets the hash value of the array as the key of the hash table.
|
||||||
return i
|
key := fmt.Sprintf("%p", arr)
|
||||||
|
// determines whether the hash table is empty. If so, the hash table is created.
|
||||||
|
if memoryHashMap[key] == nil {
|
||||||
|
memoryHashMap[key] = make(map[any]int)
|
||||||
|
// iterate through the array, adding the value and index of each element to the hash table.
|
||||||
|
for i := len(arr) - 1; i >= 0; i-- {
|
||||||
|
memoryHashMap[key][arr[i]] = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// update the hash table counter.
|
||||||
|
memoryHashCounter[key]++
|
||||||
|
|
||||||
|
// use the hash table to find the specified value. If found, the index is returned.
|
||||||
|
if index, ok := memoryHashMap[key][val]; ok {
|
||||||
|
// calculate the memory usage of the hash table.
|
||||||
|
size := len(memoryHashMap)
|
||||||
|
// If the memory usage of the hash table exceeds the memory limit, the hash table with the lowest counter is cleared.
|
||||||
|
if size > limit {
|
||||||
|
var minKey string
|
||||||
|
var minVal int
|
||||||
|
for k, v := range memoryHashCounter {
|
||||||
|
if k == key {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if minVal == 0 || v < minVal {
|
||||||
|
minKey = k
|
||||||
|
minVal = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete(memoryHashMap, minKey)
|
||||||
|
delete(memoryHashCounter, minKey)
|
||||||
|
}
|
||||||
|
return index
|
||||||
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package slice
|
package slice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -669,8 +670,39 @@ func TestIndexOf(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestIndexOf")
|
assert := internal.NewAssert(t, "TestIndexOf")
|
||||||
|
|
||||||
arr := []string{"a", "a", "b", "c"}
|
arr := []string{"a", "a", "b", "c"}
|
||||||
|
key := fmt.Sprintf("%p", arr)
|
||||||
assert.Equal(0, IndexOf(arr, "a"))
|
assert.Equal(0, IndexOf(arr, "a"))
|
||||||
assert.Equal(-1, IndexOf(arr, "d"))
|
assert.Equal(-1, IndexOf(arr, "d"))
|
||||||
|
assert.Equal(2, memoryHashCounter[key])
|
||||||
|
|
||||||
|
arr1 := []int{1, 2, 3, 4, 5}
|
||||||
|
key1 := fmt.Sprintf("%p", arr1)
|
||||||
|
assert.Equal(3, IndexOf(arr1, 4))
|
||||||
|
assert.Equal(-1, IndexOf(arr1, 6))
|
||||||
|
assert.Equal(2, memoryHashCounter[key1])
|
||||||
|
|
||||||
|
arr2 := []float64{1.1, 2.2, 3.3, 4.4, 5.5}
|
||||||
|
key2 := fmt.Sprintf("%p", arr2)
|
||||||
|
assert.Equal(2, IndexOf(arr2, 3.3))
|
||||||
|
assert.Equal(3, IndexOf(arr2, 4.4))
|
||||||
|
assert.Equal(-1, IndexOf(arr2, 6.6))
|
||||||
|
assert.Equal(3, memoryHashCounter[key2])
|
||||||
|
|
||||||
|
for i := 0; i < 6; i++ {
|
||||||
|
a := []string{"a", "b", "c"}
|
||||||
|
IndexOf(a, "a")
|
||||||
|
IndexOf(a, "b")
|
||||||
|
}
|
||||||
|
minArr := []string{"c", "b", "a"}
|
||||||
|
minKey := fmt.Sprintf("%p", minArr)
|
||||||
|
assert.Equal(0, IndexOf(minArr, "c"))
|
||||||
|
|
||||||
|
arr3 := []string{"q", "w", "e"}
|
||||||
|
key3 := fmt.Sprintf("%p", arr3)
|
||||||
|
assert.Equal(1, IndexOf(arr3, "w"))
|
||||||
|
assert.Equal(-1, IndexOf(arr3, "r"))
|
||||||
|
assert.Equal(2, memoryHashCounter[key3])
|
||||||
|
assert.Equal(0, memoryHashCounter[minKey])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLastIndexOf(t *testing.T) {
|
func TestLastIndexOf(t *testing.T) {
|
||||||
|
|||||||
+26
-35
@@ -15,11 +15,24 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
var isAlphaRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
var (
|
||||||
|
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
||||||
|
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
||||||
|
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||||
|
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
||||||
|
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
|
||||||
|
emailMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`)
|
||||||
|
chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`)
|
||||||
|
chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
|
||||||
|
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
|
||||||
|
chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`)
|
||||||
|
creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
|
||||||
|
base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
|
||||||
|
)
|
||||||
|
|
||||||
// IsAlpha checks if the string contains only letters (a-zA-Z)
|
// IsAlpha checks if the string contains only letters (a-zA-Z)
|
||||||
func IsAlpha(str string) bool {
|
func IsAlpha(str string) bool {
|
||||||
return isAlphaRegexMatcher.MatchString(str)
|
return alphaMatcher.MatchString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAllUpper check if the string is all upper case letters A-Z
|
// IsAllUpper check if the string is all upper case letters A-Z
|
||||||
@@ -62,11 +75,9 @@ func ContainLower(str string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var containLetterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
|
||||||
|
|
||||||
// ContainLetter check if the string contain at least one letter
|
// ContainLetter check if the string contain at least one letter
|
||||||
func ContainLetter(str string) bool {
|
func ContainLetter(str string) bool {
|
||||||
return containLetterRegexMatcher.MatchString(str)
|
return letterRegexMatcher.MatchString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsJSON checks if the string is valid JSON
|
// IsJSON checks if the string is valid JSON
|
||||||
@@ -86,11 +97,9 @@ func IsFloatStr(str string) bool {
|
|||||||
return e == nil
|
return e == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var isIntStrRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
|
||||||
|
|
||||||
// IsIntStr check if the string can convert to a integer.
|
// IsIntStr check if the string can convert to a integer.
|
||||||
func IsIntStr(str string) bool {
|
func IsIntStr(str string) bool {
|
||||||
return isIntStrRegexMatcher.MatchString(str)
|
return intStrMatcher.MatchString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsIp check if the string is a ip address.
|
// IsIp check if the string is a ip address.
|
||||||
@@ -125,8 +134,6 @@ func IsPort(str string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var isUrlRegexMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
|
||||||
|
|
||||||
// IsUrl check if the string is url.
|
// IsUrl check if the string is url.
|
||||||
func IsUrl(str string) bool {
|
func IsUrl(str string) bool {
|
||||||
if str == "" || len(str) >= 2083 || len(str) <= 3 || strings.HasPrefix(str, ".") {
|
if str == "" || len(str) >= 2083 || len(str) <= 3 || strings.HasPrefix(str, ".") {
|
||||||
@@ -143,64 +150,48 @@ func IsUrl(str string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return isUrlRegexMatcher.MatchString(str)
|
return urlMatcher.MatchString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isDnsRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
|
|
||||||
|
|
||||||
// IsDns check if the string is dns.
|
// IsDns check if the string is dns.
|
||||||
func IsDns(dns string) bool {
|
func IsDns(dns string) bool {
|
||||||
return isDnsRegexMatcher.MatchString(dns)
|
return dnsMatcher.MatchString(dns)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isEmailRegexMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`)
|
|
||||||
|
|
||||||
// IsEmail check if the string is a email address.
|
// IsEmail check if the string is a email address.
|
||||||
func IsEmail(email string) bool {
|
func IsEmail(email string) bool {
|
||||||
return isEmailRegexMatcher.MatchString(email)
|
return emailMatcher.MatchString(email)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isChineseMobileRegexMatcher *regexp.Regexp = regexp.MustCompile("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$")
|
|
||||||
|
|
||||||
// IsChineseMobile check if the string is chinese mobile number.
|
// IsChineseMobile check if the string is chinese mobile number.
|
||||||
func IsChineseMobile(mobileNum string) bool {
|
func IsChineseMobile(mobileNum string) bool {
|
||||||
return isChineseMobileRegexMatcher.MatchString(mobileNum)
|
return chineseMobileMatcher.MatchString(mobileNum)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isChineseIdNumRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
|
|
||||||
|
|
||||||
// IsChineseIdNum check if the string is chinese id number.
|
// IsChineseIdNum check if the string is chinese id number.
|
||||||
func IsChineseIdNum(id string) bool {
|
func IsChineseIdNum(id string) bool {
|
||||||
return isChineseIdNumRegexMatcher.MatchString(id)
|
return chineseIdMatcher.MatchString(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
var containChineseRegexMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
|
|
||||||
|
|
||||||
// ContainChinese check if the string contain mandarin chinese.
|
// ContainChinese check if the string contain mandarin chinese.
|
||||||
func ContainChinese(s string) bool {
|
func ContainChinese(s string) bool {
|
||||||
return containChineseRegexMatcher.MatchString(s)
|
return chineseMatcher.MatchString(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isChinesePhoneRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`)
|
|
||||||
|
|
||||||
// IsChinesePhone check if the string is chinese phone number.
|
// IsChinesePhone check if the string is chinese phone number.
|
||||||
// Valid chinese phone is xxx-xxxxxxxx or xxxx-xxxxxxx
|
// Valid chinese phone is xxx-xxxxxxxx or xxxx-xxxxxxx
|
||||||
func IsChinesePhone(phone string) bool {
|
func IsChinesePhone(phone string) bool {
|
||||||
return isChinesePhoneRegexMatcher.MatchString(phone)
|
return chinesePhoneMatcher.MatchString(phone)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isCreditCardRegexMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
|
|
||||||
|
|
||||||
// IsCreditCard check if the string is credit card.
|
// IsCreditCard check if the string is credit card.
|
||||||
func IsCreditCard(creditCart string) bool {
|
func IsCreditCard(creditCart string) bool {
|
||||||
return isCreditCardRegexMatcher.MatchString(creditCart)
|
return creditCardMatcher.MatchString(creditCart)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isBase64RegexMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
|
|
||||||
|
|
||||||
// IsBase64 check if the string is base64 string.
|
// IsBase64 check if the string is base64 string.
|
||||||
func IsBase64(base64 string) bool {
|
func IsBase64(base64 string) bool {
|
||||||
return isBase64RegexMatcher.MatchString(base64)
|
return base64Matcher.MatchString(base64)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEmptyString check if the string is empty.
|
// IsEmptyString check if the string is empty.
|
||||||
|
|||||||
Reference in New Issue
Block a user