Fix detecting bom as binary

This commit is contained in:
2025-03-12 21:44:37 +01:00
parent 01c16b7699
commit a246b79240

36
main.go
View File

@@ -34,22 +34,28 @@ type ExtData struct {
textCount int textCount int
} }
var debug bool
func main() { func main() {
raw := flag.Bool("r", false, "More application friendly output") raw := flag.Bool("r", false, "More application friendly output")
debugF := flag.Bool("d", false, "Debug mode")
flag.Parse() flag.Parse()
debug = *debugF
dir := flag.Arg(0) dir := flag.Arg(0)
if dir == "" { if dir == "" {
dir = "." dir = "."
} }
dir = NormalizePath(dir) dir = NormalizePath(dir)
if !*raw { if debug {
log.Printf("Scanning directory: %s", dir) log.Printf("Scanning directory: %s", dir)
} }
//log.Printf("%#v", IsStringBinary("using RimWorld;"))
//return
files := make(chan string, 10000) files := make(chan string, 10000)
status := make(chan error) //status := make(chan error)
go GetSyncFilesRecursively(dir, files, status) //go GetSyncFilesRecursively(dir, files, status)
files <- "CompRegisterAlternateGraphic.cs"
extensionTypeCount := sync.Map{} extensionTypeCount := sync.Map{}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
@@ -59,7 +65,7 @@ func main() {
defer wg.Done() defer wg.Done()
f, err := os.Open(file) f, err := os.Open(file)
if err != nil { if err != nil {
if !*raw { if debug {
log.Printf("Error opening file %s: %v", file, err) log.Printf("Error opening file %s: %v", file, err)
} }
return return
@@ -71,13 +77,17 @@ func main() {
extData, _ := extensionTypeCount.LoadOrStore(ext, &ExtData{ext: ext, binaryCount: 0, textCount: 0}) extData, _ := extensionTypeCount.LoadOrStore(ext, &ExtData{ext: ext, binaryCount: 0, textCount: 0})
if IsStringBinary(scanner.Text()) { if IsStringBinary(scanner.Text()) {
extData.(*ExtData).binaryCount++ extData.(*ExtData).binaryCount++
//log.Printf("Binary file: %s (%s)", file, ext) if debug {
log.Printf("Binary file: %s (%s)", file, ext)
}
} else { } else {
extData.(*ExtData).textCount++ extData.(*ExtData).textCount++
//log.Printf("Text file: %s (%s)", file, ext) if debug {
log.Printf("Text file: %s (%s)", file, ext)
}
} }
} else if err := scanner.Err(); err != nil { } else if err := scanner.Err(); err != nil {
if !*raw { if debug {
log.Printf("Error reading line from file %s: %v", file, err) log.Printf("Error reading line from file %s: %v", file, err)
} }
} }
@@ -104,11 +114,21 @@ func main() {
} }
func IsStringBinary(s string) bool { func IsStringBinary(s string) bool {
if debug {
log.Printf("Checking if string is binary: %q", s)
}
for _, c := range s { for _, c := range s {
if c < ' ' || c > '~' { // 65279 is GOD DAMNED BOM dogshit
if (c < ' ' || c > '~') && c != 65279 {
if debug {
log.Printf("Found non-printable character: '%c' with ASCII value %d", c, c)
}
return true return true
} }
} }
if debug {
log.Println("String is not binary.")
}
return false return false
} }