diff --git a/lexers/internal/api.go b/lexers/internal/api.go index 002997a..08ec6ff 100644 --- a/lexers/internal/api.go +++ b/lexers/internal/api.go @@ -37,19 +37,20 @@ func Names(withAliases bool) []string { // Get a Lexer by name, alias or file extension. func Get(name string) chroma.Lexer { - candidates := chroma.PrioritisedLexers{} if lexer := Registry.byName[name]; lexer != nil { - candidates = append(candidates, lexer) + return lexer } if lexer := Registry.byAlias[name]; lexer != nil { - candidates = append(candidates, lexer) + return lexer } if lexer := Registry.byName[strings.ToLower(name)]; lexer != nil { - candidates = append(candidates, lexer) + return lexer } if lexer := Registry.byAlias[strings.ToLower(name)]; lexer != nil { - candidates = append(candidates, lexer) + return lexer } + + candidates := chroma.PrioritisedLexers{} // Try file extension. if lexer := Match("filename." + name); lexer != nil { candidates = append(candidates, lexer) diff --git a/lexers/lexers_test.go b/lexers/lexers_test.go index 5cdbdb2..34496dd 100644 --- a/lexers/lexers_test.go +++ b/lexers/lexers_test.go @@ -38,6 +38,12 @@ func TestGet(t *testing.T) { }) } +func BenchmarkGet(b *testing.B) { + for i := 0; i < b.N; i++ { + lexers.Get("go") + } +} + // Test source files are in the form . and validation data is in the form ..expected. func TestLexers(t *testing.T) { files, err := ioutil.ReadDir("testdata")