-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
399 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package is | ||
|
||
import "unicode" | ||
|
||
// The varReservedWords holds a map of reserved words in Python, Go and C++. | ||
// These words cannot be used as variable names. | ||
var varReservedWords = map[string]struct{}{ | ||
"False": {}, "None": {}, "True": {}, "and": {}, "as": {}, "assert": {}, | ||
"async": {}, "await": {}, "break": {}, "class": {}, "continue": {}, | ||
"def": {}, "del": {}, "elif": {}, "else": {}, "except": {}, "finally": {}, | ||
"for": {}, "from": {}, "global": {}, "if": {}, "import": {}, "in": {}, | ||
"is": {}, "lambda": {}, "nonlocal": {}, "not": {}, "or": {}, "pass": {}, | ||
"raise": {}, "return": {}, "try": {}, "while": {}, "with": {}, "yield": {}, | ||
"default": {}, "func": {}, "interface": {}, "select": {}, "case": {}, | ||
"defer": {}, "go": {}, "map": {}, "struct": {}, "chan": {}, "goto": {}, | ||
"package": {}, "switch": {}, "const": {}, "fallthrough": {}, "range": {}, | ||
"type": {}, "var": {}, "asm": {}, "auto": {}, "bool": {}, "catch": {}, | ||
"char": {}, "const_cast": {}, "delete": {}, "do": {}, "double": {}, | ||
"dynamic_cast": {}, "enum": {}, "explicit": {}, "export": {}, "extern": {}, | ||
"float": {}, "friend": {}, "inline": {}, "int": {}, "long": {}, | ||
"mutable": {}, "namespace": {}, "new": {}, "operator": {}, "private": {}, | ||
"protected": {}, "public": {}, "register": {}, "reinterpret_cast": {}, | ||
"short": {}, "signed": {}, "sizeof": {}, "static": {}, "static_cast": {}, | ||
"template": {}, "this": {}, "throw": {}, "typedef": {}, "typeid": {}, | ||
"typename": {}, "union": {}, "unsigned": {}, "using": {}, "virtual": {}, | ||
"void": {}, "volatile": {}, "wchar_t": {}, | ||
} | ||
|
||
// Var validates if the given string can be used as a variable | ||
// name in most programming languages. The function checks if the | ||
// name starts with a letter or underscore, if it doesn't contain | ||
// any special characters or spaces, and if it's not a reserved | ||
// word. Returns true if the given name is valid, false otherwise. | ||
// | ||
// Example usage: | ||
// | ||
// // Test a valid variable name | ||
// fmt.Println(Var("myVar")) // Output: true | ||
// | ||
// // Test a name starting with a digit | ||
// fmt.Println(Var("9myVar")) // Output: false | ||
// | ||
// // Test a name containing a space | ||
// fmt.Println(Var("my Var")) // Output: false | ||
// | ||
// // Test a reserved word | ||
// fmt.Println(Var("return")) // Output: false | ||
func Var(v string) bool { | ||
// Empty string is not a valid variable name. | ||
if v == "" { | ||
return false | ||
} | ||
|
||
// Split the string into runes. | ||
r := []rune(v) | ||
|
||
// Check if the name starts with a letter or underscore. | ||
if !unicode.IsLetter(r[0]) && r[0] != '_' { | ||
return false | ||
} | ||
|
||
// Check if the name doesn't contain any special characters or spaces. | ||
for _, c := range r { | ||
if !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '_' { | ||
return false | ||
} | ||
} | ||
|
||
// Check if the name is not a reserved word. | ||
if _, ok := varReservedWords[v]; ok { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package is | ||
|
||
import "testing" | ||
|
||
// TestVar tests the Var function. | ||
func TestVar(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in string | ||
want bool | ||
}{ | ||
{ | ||
name: "Standard variable name", | ||
in: "myVar", | ||
want: true, | ||
}, | ||
{ | ||
name: "Variable name starting with underscore", | ||
in: "_myVar", | ||
want: true, | ||
}, | ||
{ | ||
name: "Variable name with underscore", | ||
in: "my_var", | ||
want: true, | ||
}, | ||
{ | ||
name: "Variable name with underscore and number", | ||
in: "my_var_2", | ||
want: true, | ||
}, | ||
{ | ||
name: "Starts with a number", | ||
in: "2myVar", | ||
want: false, | ||
}, | ||
{ | ||
name: "Contains space", | ||
in: "my var", | ||
want: false, | ||
}, | ||
{ | ||
name: "Contains hyphen", | ||
in: "my-var", | ||
want: false, | ||
}, | ||
{ | ||
name: "Contains special character", | ||
in: "my@var", | ||
want: false, | ||
}, | ||
{ | ||
name: "Empty string", | ||
in: "", | ||
want: false, | ||
}, | ||
{ | ||
name: "String with spaces", | ||
in: " ", | ||
want: false, | ||
}, | ||
{ | ||
name: "Starts with a number, has underscore", | ||
in: "_2", | ||
want: true, | ||
}, | ||
{ | ||
name: "Contains period", | ||
in: "my.var", | ||
want: false, | ||
}, | ||
{ | ||
name: "Reserved keyword in Go", | ||
in: "return", | ||
want: false, | ||
}, | ||
{ | ||
name: "Reserved keyword in Python", | ||
in: "while", | ||
want: false, | ||
}, | ||
{ | ||
name: "Capitalized variable name", | ||
in: "MyVar", | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Var(tt.in); got != tt.want { | ||
t.Errorf("IsVarName() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.