mirror of
https://github.com/warmbly/warmbly.git
synced 2026-11-17 08:18:51 +00:00
24 lines
558 B
Go
24 lines
558 B
Go
// Package textcontent classifies supported UTF-8 text without changing its bytes.
|
|
package textcontent
|
|
|
|
import (
|
|
"errors"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
var ErrNotText = errors.New("file must contain UTF-8 text without binary control characters")
|
|
|
|
// Validate permits a UTF-8 BOM, Unicode, and ordinary text whitespace.
|
|
func Validate(data []byte) error {
|
|
if !utf8.Valid(data) {
|
|
return ErrNotText
|
|
}
|
|
for _, r := range string(data) {
|
|
if unicode.IsControl(r) && r != '\t' && r != '\n' && r != '\r' && r != '\f' {
|
|
return ErrNotText
|
|
}
|
|
}
|
|
return nil
|
|
}
|