Repository: editorconfig-checker/editorconfig-checker (go)

Issue:
Could not decode the IBM420_rtl encoded file
Type `printf 'inc\nsub\n' > abc` to create a file `abc`.
There is no `.ecrc` and type `editorconfig-checker`. It shows error. Why?

<img width="503" alt="CleanShot 2023-02-07 at 15 22 27@2x" src="https://user-images.githubusercontent.com/1998490/217175826-4ae07360-28e9-4dd7-8ea6-c27d8933b9e8.png">


Pull request description:
Ignore unrecognized charsets (Fixes #252)
All tests pass.

Patch:
diff --git a/pkg/encoding/encoding.go b/pkg/encoding/encoding.go
index b7d10af..a1cb6e7 100644
--- a/pkg/encoding/encoding.go
+++ b/pkg/encoding/encoding.go
@@ -126,6 +126,10 @@ func detectText(contentBytes []byte) (string, error) {
 	confidence := -1
 	keys := make([]string, 0, len(results))
 	for _, result := range results {
+		_, ok := getEncoding(result.Charset)
+		if !ok {
+			continue
+		}
 		if result.Confidence < confidence {
 			break
 		}
@@ -137,18 +141,14 @@ func detectText(contentBytes []byte) (string, error) {
 }
 
 func decodeText(contentBytes []byte, charset string) (string, error) {
-	r := strings.NewReplacer("-", "", "_", "")
-	key := strings.ToLower(r.Replace(charset))
-	enc, ok := encodings[key]
+	enc, ok := getEncoding(charset)
 	if !ok {
 		return "", fmt.Errorf("unrecognized charset %s", charset)
 	}
-	if enc != nil {
-		var err error
-		contentBytes, err = enc.NewDecoder().Bytes(contentBytes)
-		if err != nil {
-			return "", err
-		}
+	var err error
+	contentBytes, err = enc.NewDecoder().Bytes(contentBytes)
+	if err != nil {
+		return "", err
 	}
 	if !utf8.Valid(contentBytes) {
 		return "", fmt.Errorf("the file is not a valid UTF-8 encoded file")
@@ -156,6 +156,13 @@ func decodeText(contentBytes []byte, charset string) (string, error) {
 	return string(contentBytes), nil
 }
 
+func getEncoding(charset string) (encoding.Encoding, bool) {
+	r := strings.NewReplacer("-", "", "_", "")
+	key := strings.ToLower(r.Replace(charset))
+	enc, ok := encodings[key]
+	return enc, ok
+}
+
 var binaryChars = [256]bool{}
 
 func init() {


Test patch:
diff --git a/pkg/encoding/encoding_test.go b/pkg/encoding/encoding_test.go
index fc69f0b..c49e846 100644
--- a/pkg/encoding/encoding_test.go
+++ b/pkg/encoding/encoding_test.go
@@ -16,6 +16,7 @@ type decodeTest struct {
 }
 
 var decodeTextTests = []decodeTest{
+	// Encodings
 	{"8859_1_da.html", "ISO-8859-1", false, false},
 	{"8859_1_de.html", "ISO-8859-1", false, false},
 	{"8859_1_en.html", "ISO-8859-1", false, false},
@@ -68,6 +69,8 @@ var decodeTextTests = []decodeTest{
 	{"utf8-sdl.txt", "windows-1252", false, false}, // should be UTF-8
 	{"utf8.txt", "UTF-8", false, false},
 	{"utf8.txt-encoding-test-files.txt", "UTF-8", false, false},
+	// Issues
+	{"issue252.txt", "UTF-8", false, false},
 }
 
 func TestDecodeBytesText(t *testing.T) {
diff --git a/pkg/encoding/testdata/issue252.txt b/pkg/encoding/testdata/issue252.txt
new file mode 100644
index 0000000..8d384c3
--- /dev/null
+++ b/pkg/encoding/testdata/issue252.txt
@@ -0,0 +1,2 @@
+inc
+sub