feat: reduce allocs in DetectSheBang (#628) · boyter/scc@cb7a310
@@ -9,6 +9,12 @@ import (
99"strings"
1010)
111112+var (
13+errMissingShebang = errors.New("missing shebang")
14+errUnknownShebang = errors.New("unknown shebang")
15+errUnableToDetermineShebangCmd = errors.New("unable to determine shebang command")
16+)
17+1218// DetectLanguage detects a language based on the filename returns the language extension and error
1319func DetectLanguage(name string) ([]string, string) {
1420extension := ""
@@ -51,7 +57,7 @@ func DetectLanguage(name string) ([]string, string) {
5157// DetectSheBang given some content attempt to determine if it has a #! that maps to a known language and return the language
5258func DetectSheBang(content string) (string, error) {
5359if !strings.HasPrefix(content, "#!") {
54-return "", errors.New("Missing #!")
60+return "", errMissingShebang
5561 }
56625763index := strings.Index(content, "\n")
@@ -73,7 +79,7 @@ func DetectSheBang(content string) (string, error) {
7379 }
7480 }
758176-return "", errors.New("Unknown #!")
82+return "", errUnknownShebang
7783}
78847985func scanForSheBang(content []byte) (string, error) {
@@ -134,7 +140,7 @@ loop:
134140return candidate1, nil
135141 }
136142137-return "", errors.New("Unable to determine #! command")
143+return "", errUnableToDetermineShebangCmd
138144}
139145140146type languageGuess struct {