123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- // Copyright 2019 autocareai.com. All rights reserved.
- // Use of this source code is governed by autocareai.com.
- package utils
- import (
- "math/rand"
- "regexp"
- "time"
- "unsafe"
- )
- // VerifyMobileFormat 手机号验证
- func VerifyMobileFormat(phone string) bool {
- regular := "^((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}$"
- reg := regexp.MustCompile(regular)
- return reg.MatchString(phone)
- }
- // RankVCode 最小到最大值之间生成随机数
- func RankVCode(min int, max int) int {
- rand.Seed(time.Now().UnixNano())
- return rand.Intn(max-min) + min
- }
- // StrToBytes 快读的string转byte
- func StrToBytes(s string) []byte {
- x := (*[2]uintptr)(unsafe.Pointer(&s))
- h := [3]uintptr{x[0], x[1], x[1]}
- return *(*[]byte)(unsafe.Pointer(&h))
- }
- // StrToBytes 快速的byte转string
- func BytesToStr(b []byte) string {
- return *(*string)(unsafe.Pointer(&b))
- }
- //根据length获取随机字符串
- func RandomString(length int) string {
- str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- bytes := []byte(str)
- result := []byte{}
- r := rand.New(rand.NewSource(time.Now().UnixNano()))
- for i := 0; i < length; i++ {
- result = append(result, bytes[r.Intn(len(bytes))])
- }
- return string(result)
- }
|