Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
Add functions that check if a file is text file
Browse files Browse the repository at this point in the history
  • Loading branch information
harryzcy committed Mar 28, 2021
1 parent e11bf65 commit 200527b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions internal/localcheck/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package localcheck

import (
"os"
"unicode/utf8"
)

// IsText reports whether a significant prefix of s looks like correct UTF-8;
// that is, if it is likely that s is human-readable text.
func IsText(s []byte) bool {
const max = 1024 // at least utf8.UTFMax
if len(s) > max {
s = s[0:max]
}
for i, c := range string(s) {
if i+utf8.UTFMax > len(s) {
// last char may be incomplete - ignore
break
}
if c == 0xFFFD || c < ' ' && c != '\n' && c != '\t' && c != '\f' {
// decoding error or control character - not a text file
return false
}
}
return true
}

// IsTextFile reports if a significant chunk of the specified file looks like
// correct UTF-8; that is, if it is likely that the file contains human-
// readable text.
func IsTextFile(filename string) bool {
// read an initial chunk of the file
// and check if it looks like text
f, err := os.Open(filename)
if err != nil {
return false
}
defer f.Close()

var buf [1024]byte
n, err := f.Read(buf[0:])
if err != nil {
return false
}

return IsText(buf[0:n])
}

0 comments on commit 200527b

Please sign in to comment.