This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions that check if a file is text file
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |