package strutilimport ()// Equal check, alias of strings.EqualFoldvarEqual = strings.EqualFoldvarIsHttpURL = checkfn.IsHttpURL// IsNumChar returns true if the given character is a numeric, otherwise false.func ( byte) bool { return >= '0' && <= '9' }var ( uintReg = regexp.MustCompile(`^\d+$`) intReg = regexp.MustCompile(`^[-+]?\d+$`) floatReg = regexp.MustCompile(`^[-+]?\d*\.?\d+$`))// IsInt check the string is an integer numberfunc ( string) bool {if == "" {returnfalse }returnintReg.MatchString()}// IsUint check the string is an unsigned integer numberfunc ( string) bool {if == "" {returnfalse }returnuintReg.MatchString()}// IsFloat check the string is a float numberfunc ( string) bool {if == "" {returnfalse }returnfloatReg.MatchString()}// IsNumeric returns true if the given string is a numeric(int/float), otherwise false.func ( string) bool { returncheckfn.IsNumeric() }// IsPositiveNum check the string is a positive numberfunc ( string) bool { returncheckfn.IsPositiveNum() }// IsAlphabet charfunc ( uint8) bool {// A 65 -> Z 90if >= 'A' && <= 'Z' {returntrue }// a 97 -> z 122if >= 'a' && <= 'z' {returntrue }returnfalse}// IsAlphaNum reports whether the byte is an ASCII letter, number, or underscorefunc ( uint8) bool {return == '_' || '0' <= && <= '9' || 'a' <= && <= 'z' || 'A' <= && <= 'Z'}// IsUpper returns true if the given string is an uppercase, otherwise false.func ( string) bool {for := 0; < len(); ++ {if [] >= 'A' && [] <= 'Z' {continue }returnfalse }returntrue}// IsLower returns true if the given string is a lowercase, otherwise false.func ( string) bool {for := 0; < len(); ++ {if [] >= 'a' && [] <= 'z' {continue }returnfalse }returntrue}// IsAllASCII 判断字符串是否全为可打印 ASCII(无中文等多字节字符)func ( string) bool {for := 0; < len(); ++ {if [] > 0x7E || [] < 0x20 {returnfalse } }returntrue}// StrPos alias of the strings.Indexfunc (, string) int { returnstrings.Index(, ) }// BytePos alias of the strings.IndexBytefunc ( string, byte) int { returnstrings.IndexByte(, ) }// IEqual ignore case check given two strings are equals.func (, string) bool { returnstrings.EqualFold(, ) }// NoCaseEq check two strings is equals and case-insensitivityfunc (, string) bool { returnstrings.EqualFold(, ) }// IContains ignore case check substr in the given string.func (, string) bool {returnstrings.Contains(strings.ToLower(), strings.ToLower())}// ContainsByte in given string.func ( string, byte) bool { returnstrings.IndexByte(, ) >= 0 }// ContainsByteOne in given string.func ( string, []byte) bool {for , := range {ifstrings.IndexByte(, ) >= 0 {returntrue } }returnfalse}// InArray alias of HasOneSub()varInArray = HasOneSub// ContainsOne substr(s) in the given string. alias of HasOneSub()func ( string, []string) bool { returnHasOneSub(, ) }// HasOneSub substr(s) in the given string.func ( string, []string) bool {for , := range {ifstrings.Contains(, ) {returntrue } }returnfalse}// IContainsOne ignore case check has one substr(s) in the given string.func ( string, []string) bool { = strings.ToLower()for , := range {ifstrings.Contains(, strings.ToLower()) {returntrue } }returnfalse}// ContainsAll given string should contain all substrings. alias of HasAllSubs()func ( string, []string) bool { returnHasAllSubs(, ) }// HasAllSubs given string should contain all substringsfunc ( string, []string) bool {for , := range {if !strings.Contains(, ) {returnfalse } }returntrue}// IContainsAll like ContainsAll(), but ignore casefunc ( string, []string) bool { = strings.ToLower()for , := range {if !strings.Contains(, strings.ToLower()) {returnfalse } }returntrue}// StartsWithAny alias of the HasOnePrefixvarStartsWithAny = HasOneSuffix// IsStartsOf alias of the HasOnePrefixfunc ( string, []string) bool {returnHasOnePrefix(, )}// HasOnePrefix the string starts with one of the subsfunc ( string, []string) bool {for , := range {iflen() >= len() && [0:len()] == {returntrue } }returnfalse}// StartsWith alias func for HasPrefixvarStartsWith = strings.HasPrefix// HasPrefix substr in the given string.func ( string, string) bool { returnstrings.HasPrefix(, ) }// IsStartOf alias of the strings.HasPrefixfunc (, string) bool { returnstrings.HasPrefix(, ) }// HasSuffix substr in the given string.func ( string, string) bool { returnstrings.HasSuffix(, ) }// IsEndOf alias of the strings.HasSuffixfunc (, string) bool { returnstrings.HasSuffix(, ) }// HasOneSuffix the string end with one of the subsfunc ( string, []string) bool {for , := range {ifstrings.HasSuffix(, ) {returntrue } }returnfalse}// IsValidUtf8 valid utf8 string checkfunc ( string) bool { returnutf8.ValidString() }// ----- refer from github.com/yuin/goldmark/util// refer from github.com/yuin/goldmark/utilvar spaceTable = [256]int8{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}// IsSpace returns true if the given character is a space, otherwise false.func ( byte) bool { returnspaceTable[] == 1 }// IsEmpty returns true if the given string is empty.func ( string) bool { returnlen() == 0 }// IsBlank returns true if the given string is all space characters.func ( string) bool { returnIsBlankBytes([]byte()) }// IsNotBlank returns true if the given string is not blank.func ( string) bool { return !IsBlankBytes([]byte()) }// IsBlankBytes returns true if the given []byte is all space characters.func ( []byte) bool {for , := range {if !IsSpace() {returnfalse } }returntrue}// IsSymbol reports whether the rune is a symbolic character.func ( rune) bool { returnunicode.IsSymbol() }// HasEmpty value for input stringsfunc ( ...string) bool {for , := range {if == "" {returntrue } }returnfalse}// IsAllEmpty for input stringsfunc ( ...string) bool {for , := range {if != "" {returnfalse } }returntrue}var (// regex for check version number verRegex = regexp.MustCompile(`^[0-9][\d.]+(-\w+)?$`)// regex for check variable name varRegex = regexp.MustCompile(`^[a-zA-Z][\w-]*$`)// regex for check env var name envRegex = regexp.MustCompile(`^[A-Z][A-Z0-9_]*$`)// IsVariableName alias for IsVarNameIsVariableName = IsVarName// regex for check uuid string. format: 8-4-4-4-12 uuidPattern = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`))// IsVersion number. eg: 1.2.0func ( string) bool { returnverRegex.MatchString() }// IsVarName is valid variable name.func ( string) bool { returnvarRegex.MatchString() }// IsEnvName is valid ENV var name. eg: APP_NAMEfunc ( string) bool { returnenvRegex.MatchString() }// IsUUID check if the string is a valid UUID format.func ( string) bool { returnuuidPattern.MatchString() }// Compare for two strings.func (, , string) bool {switch {case">", "gt":return > case"<", "lt":return < case">=", "gte":return >= case"<=", "lte":return <= case"!=", "ne", "neq":return != default: // eqreturn == }}// VersionCompare for two version strings. eg: 1.2.0 > 1.1.0func (, , string) bool { := parseVersion() := parseVersion() := compareVersions(, )switch {case">", "gt":return > 0case"<", "lt":return < 0case"=", "==", "eq":return == 0case"!=", "ne", "neq":return != 0case">=", "gte":return >= 0case"<=", "lte":return <= 0default:returnfalse }}// parseVersion 将版本号字符串解析为整数数组func parseVersion( string) []int {if [0] == 'v' || [0] == 'V' { = [1:] }// 处理 Git 描述格式: v1.7.1-16-gc43a587 // 格式: <tag>-<commits>-g<hash>,其中 commits 表示距离 tag 的提交数varintvar []intif := strings.Index(, "-"); > 0 { := [+1:] = [:]// 尝试提取提交数(格式: 数字-g<hash> 或 数字)if := strings.Index(, "-"); > 0 {if , := strconv.Atoi([:]); == nil { = } else { = parsePreRelease() } } elseif , := strconv.Atoi(); == nil { = } else { = parsePreRelease() } } := strings.Split(, ".") := make([]int, len())for , := range { , := strconv.Atoi() [] = }// 如果有额外的提交数,追加到版本号末尾 // 这样 v1.7.1-16-gc43a587 会变成 [1, 7, 1, 16] // 而 v1.7.1 是 [1, 7, 1],比较时前者更大if > 0 { = append(, ) } elseiflen() > 0 { = append(, ...) }return}func parsePreRelease( string) []int { = strings.ToLower() := strings.Trim(, ".-") := 0for , := range {if >= '0' && <= '9' {if , := strconv.Atoi(strings.Trim([:], ".-")); == nil { = } = strings.Trim([:], ".-")break } } := 0switch {case"a", "alpha": = 1case"b", "beta": = 2case"rc": = 3 }return []int{-1, , }}// compareVersions 比较两个版本号数组// 返回: -1 表示 v1 < v2, 0 表示 v1 = v2, 1 表示 v1 > v2func compareVersions(, []int) int { := len()iflen() > { = len() }for := 0; < ; ++ { := 0if < len() { = [] } := 0if < len() { = [] }if > {return1 } elseif < {return -1 } }return0}// SimpleMatch all substring in the give text string.//// Difference the ContainsAll://// - start with ^ for exclude contains check.// - end with $ for the check end with keyword.func ( string, []string) bool {for , := range { := len()if == 0 {continue }// excludeif > 1 && [0] == '^' {ifstrings.Contains(, [1:]) {returnfalse }continue }// end withif > 1 && [-1] == '$' {returnstrings.HasSuffix(, [:-1]) }// includeif !strings.Contains(, ) {returnfalse } }returntrue}// QuickMatch check for a string. pattern can be a substring.func (, string) bool {ifstrings.ContainsRune(, '*') {returnGlobMatch(, ) }returnstrings.Contains(, )}// PathMatch check for a string match the pattern. alias of the path.Match()//// TIP: `*` can match any char, not contain `/`.func (, string) bool { , := path.Match(, )if != nil { = false }return}// GlobMatch check for a string match the pattern.//// Difference with PathMatch() is: `*` can match any char, contain `/`.func (, string) bool {// replace `/` to `S` for path.Match = strings.Replace(, "/", "S", -1) = strings.Replace(, "/", "S", -1) , := path.Match(, )if != nil { = false }return}// LikeMatch simple check for a string match the pattern. pattern like the SQL LIKE.func (, string) bool { := len()if < 2 {returnfalse }// eg `%abc` `%abc%`if [0] == '%' {if > 2 && [-1] == '%' {returnstrings.Contains(, [1:-1]) }returnstrings.HasSuffix(, [1:]) }// eg `abc%`if [-1] == '%' {returnstrings.HasPrefix(, [:-1]) }return == }// MatchNodePath check for a string match the pattern.//// Use on a pattern:// - `*` match any to sep// - `**` match any to end. only allow at start or end on pattern.//// Example://// strutil.MatchNodePath()func (, string, string) bool {if == "**" || == {returntrue }if == "" {returnlen() == 0 }if := strings.Index(, "**"); >= 0 {if == 0 { // at startreturnstrings.HasSuffix(, [2:]) }returnstrings.HasPrefix(, [:len()-2]) } = strings.Replace(, , "/", -1) = strings.Replace(, , "/", -1) , := path.Match(, )if != nil { = false }return}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.