Add full file checker CheckCharsetRule.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package editorconfig
|
package editorconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/saintfish/chardet"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -8,8 +9,7 @@ import (
|
|||||||
var fullFileCheckers = map[string]FullFileChecker{
|
var fullFileCheckers = map[string]FullFileChecker{
|
||||||
"end_of_line": CheckEndOfLineRule,
|
"end_of_line": CheckEndOfLineRule,
|
||||||
"insert_final_newline": CheckInsertFinalNewLineRule,
|
"insert_final_newline": CheckInsertFinalNewLineRule,
|
||||||
// @todo - add checker for charset.
|
"charset": CheckCharsetRule,
|
||||||
// "charset": CheckCharsetRule,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type FullFileChecker func(ruleValue string, fileContent string) *FullFileCheckResult
|
type FullFileChecker func(ruleValue string, fileContent string) *FullFileCheckResult
|
||||||
@@ -95,3 +95,30 @@ func CheckInsertFinalNewLineRule(ruleValue string, fileContent string) *FullFile
|
|||||||
|
|
||||||
return &FullFileCheckResult{isOk: false, messageIfNotOk: "unexpected condition"}
|
return &FullFileCheckResult{isOk: false, messageIfNotOk: "unexpected condition"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CheckCharsetRule(ruleValue string, fileContent string) *FullFileCheckResult {
|
||||||
|
// Valid rules values are "latin1", "utf-8", "utf-8-bom", "utf-16be" or "utf-16le".
|
||||||
|
if ruleValue != "latin1" && ruleValue != "utf-8" && ruleValue != "utf-8-bom" && ruleValue != "utf-16be" && ruleValue != "utf-16le" {
|
||||||
|
return &FullFileCheckResult{isOk: false, messageIfNotOk: "charset value is invalid: " + ruleValue}
|
||||||
|
}
|
||||||
|
|
||||||
|
detector := chardet.NewTextDetector()
|
||||||
|
|
||||||
|
bestGuess, err := detector.DetectBest([]byte(fileContent))
|
||||||
|
if err != nil {
|
||||||
|
ExitBecauseOfInternalError(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := strings.ToLower(bestGuess.Charset)
|
||||||
|
|
||||||
|
if ruleValue == actual {
|
||||||
|
return &FullFileCheckResult{isOk: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ruleValue == "utf-8" && strings.HasPrefix(actual, "iso-8859-") {
|
||||||
|
// iso-8859-* is a subset of utf-8.
|
||||||
|
return &FullFileCheckResult{isOk: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &FullFileCheckResult{isOk: false, messageIfNotOk: "expected " + ruleValue + " but is: " + actual}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user