36 lines
781 B
Go
36 lines
781 B
Go
package utils
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func StringInt(s string) int64 {
|
|
num, _ := strconv.Atoi(s)
|
|
return int64(num)
|
|
}
|
|
|
|
func UnderScoreString(str string) string {
|
|
|
|
// convert every letter to lower case
|
|
newStr := strings.ToLower(str)
|
|
|
|
// convert all spaces/tab to underscore
|
|
regExp := regexp.MustCompile("[[:space:][:blank:]]")
|
|
newStrByte := regExp.ReplaceAll([]byte(newStr), []byte("_"))
|
|
|
|
regExp = regexp.MustCompile("`[^a-z0-9]`i")
|
|
newStrByte = regExp.ReplaceAll(newStrByte, []byte("_"))
|
|
|
|
regExp = regexp.MustCompile("[!/']")
|
|
newStrByte = regExp.ReplaceAll(newStrByte, []byte("_"))
|
|
|
|
// and remove underscore from beginning and ending
|
|
|
|
newStr = strings.TrimPrefix(string(newStrByte), "_")
|
|
newStr = strings.TrimSuffix(newStr, "_")
|
|
|
|
return newStr
|
|
}
|