Fix: drop dep + fix false pos with binary detection

This commit is contained in:
kayos@tcp.direct 2023-10-29 18:39:16 -07:00
parent f0cc0718ac
commit 7b90737eb7
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
2 changed files with 21 additions and 2 deletions

View File

@ -10,7 +10,6 @@ import (
"time"
"git.tcp.direct/kayos/common/squish"
"golang.org/x/tools/godoc/util"
)
type Handler interface {
@ -79,7 +78,7 @@ readLoop:
return
}
}
if !util.IsText(buf.Bytes()) {
if !IsText(buf.Bytes()) {
client.writeString(MessageBinaryData)
return
}

20
util.go Normal file
View File

@ -0,0 +1,20 @@
package putxt
import "unicode/utf8"
// slightly modified from golang.org/x/tools/godoc/util to include carriage return for dirty windows users
func IsText(s []byte) bool {
const max = 1024
if len(s) > max {
s = s[0:max]
}
for i, c := range string(s) {
if i+utf8.UTFMax > len(s) {
break
}
if c == 0xFFFD || c < ' ' && c != '\n' && c != '\t' && c != '\f' && c != '\r' {
return false
}
}
return true
}