1package main 2 3import ( 4 "mvdan.cc/sh/syntax" 5 "os" 6 "regexp" 7) 8 9var (10 // IsName checks if the given string could be a part of a name11 // in the shell command language as defined in section 3.235 of12 // the POSIX base specification.13 IsNamePart = regexp.MustCompile("^[_A-Za-z0-9]+$").MatchString14)1516// IsSpace reports whether the rune is an ascii space character. This17// differs from unicode.IsSpace which reports whether the rune is a18// space character as defined by Unicode's White Space property.19func IsSpace(r rune) bool {20 return r == ' '21}2223// IsParamExp reports whether the given parameter expression can be24// replaced by a short parameter expression.25func IsParamExp(paramExp *syntax.ParamExp) bool {26 return paramExp.Excl || paramExp.Length || paramExp.Width ||27 paramExp.Index != nil || paramExp.Slice != nil ||28 paramExp.Repl != nil || paramExp.Exp != nil29}3031// IsPrefixVar reports whether the given string is prefixed with a32// single ascii underscore character.33func IsPrefixVar(varname string) bool {34 if len(varname) < 2 {35 return false36 }3738 return varname[0] == '_' && varname[1] != '_'39}4041// IsIncluded reports whether the given string is included in the given42// string slice.43func IsIncluded(slice []string, str string) bool {44 for _, s := range slice {45 if s == str {46 return true47 }48 }4950 return false51}5253// IsMetaVar reports whether the given variable is a meta variable.54func IsMetaVar(varname string) bool {55 _, ok := metadataVariables[varname]56 return ok57}5859// IsDir reports whether the given file name is a directory.60func IsDir(fn string) bool {61 fi, err := os.Stat(fn)62 return err == nil && fi.IsDir()63}6465// Exists checks if a file with the given name exists.66func Exists(fn string) bool {67 _, err := os.Stat(fn)68 return !os.IsNotExist(err)69}