Skip to content

Commit

Permalink
Fix: drop dep + fix false pos with binary detection
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Oct 30, 2023
1 parent f0cc071 commit 7b90737
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 1 addition & 2 deletions putxt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"git.tcp.direct/kayos/common/squish"
"golang.org/x/tools/godoc/util"
)

type Handler interface {
Expand Down Expand Up @@ -79,7 +78,7 @@ readLoop:
return
}
}
if !util.IsText(buf.Bytes()) {
if !IsText(buf.Bytes()) {
client.writeString(MessageBinaryData)
return
}
Expand Down
20 changes: 20 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7b90737

Please sign in to comment.