diff options
| author | Shulhan <ms@kilabit.info> | 2021-10-23 14:46:29 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-10-23 14:46:29 +0700 |
| commit | 9114e75d5c50fcc66380d699d4efb53303d2f771 (patch) | |
| tree | d2fa3856685aa47fe0db805ae527bf4f164c5b4b | |
| parent | b4a25cec0af6077061751f2928e4210922928624 (diff) | |
| download | pakakeh.go-9114e75d5c50fcc66380d699d4efb53303d2f771.tar.xz | |
lib/ini: add function IsValidVarName
The IsValidVarName check if "v" is valid variable name, where the
first character must be a letter and the rest should contains only
letter, digit, period, hyphen, or underscore.
If "v" is valid it will return true.
| -rw-r--r-- | lib/ini/common.go | 21 | ||||
| -rw-r--r-- | lib/ini/common_example_test.go | 19 |
2 files changed, 40 insertions, 0 deletions
diff --git a/lib/ini/common.go b/lib/ini/common.go index 82ed27c4..efe6efe0 100644 --- a/lib/ini/common.go +++ b/lib/ini/common.go @@ -6,9 +6,30 @@ package ini import ( "strings" + "unicode" ) // +// IsValidVarName check if "v" is valid variable name, where the +// first character must be a letter and the rest should contains only letter, +// digit, period, hyphen, or underscore. +// If "v" is valid it will return true. +// +func IsValidVarName(v string) bool { + for x, r := range v { + if x == 0 && !unicode.IsLetter(r) { + return false + } + if unicode.IsLetter(r) || unicode.IsDigit(r) || + r == tokHyphen || r == tokDot || r == tokUnderscore { + continue + } + return false + } + return true +} + +// // IsValueBoolTrue will return true if variable contains boolean value for // true. The following conditions is boolean true for value: "" (empty // string), "true", "yes", "ya", "t", "1" (all of string is case insensitive). diff --git a/lib/ini/common_example_test.go b/lib/ini/common_example_test.go new file mode 100644 index 00000000..27149096 --- /dev/null +++ b/lib/ini/common_example_test.go @@ -0,0 +1,19 @@ +package ini + +import "fmt" + +func ExampleIsValidVarName() { + fmt.Println(IsValidVarName("1abcd")) + fmt.Println(IsValidVarName("-abcd")) + fmt.Println(IsValidVarName("_abcd")) + fmt.Println(IsValidVarName(".abcd")) + fmt.Println(IsValidVarName("a@bcd")) + fmt.Println(IsValidVarName("a-b_c.d")) + //Output: + //false + //false + //false + //false + //false + //true +} |
