git.templeos.me/xultist/go-enry/v2@v2.0.0-20230215093429-6ef3e87f47c0/data/heuristics.go (about) 1 package data 2 3 import "github.com/go-enry/go-enry/v2/data/rule" 4 5 // Heuristics implements a rule-based content matching engine. 6 7 // Heuristics is a number of sequentially applied rule.Heuristic where a 8 // matching one disambiguates language(s) for a single file extension. 9 type Heuristics []rule.Heuristic 10 11 // Match returns languages identified by the matching rule of the heuristic. 12 func (hs Heuristics) Match(data []byte) []string { 13 var matchedLangs []string 14 for _, heuristic := range hs { 15 if heuristic.Match(data) { 16 for _, langOrAlias := range heuristic.Languages() { 17 lang, ok := LanguageByAlias(langOrAlias) 18 if !ok { // should never happen 19 // reaching here means language name/alias in heuristics.yml 20 // is not consistent with languages.yml 21 // but we do not surface any such error at the API 22 continue 23 } 24 matchedLangs = append(matchedLangs, lang) 25 } 26 break 27 } 28 } 29 return matchedLangs 30 } 31 32 // matchString is a convenience used only in tests. 33 func (hs *Heuristics) matchString(data string) []string { 34 return hs.Match([]byte(data)) 35 }