IsBinary performs a heuristic check to determine if data is binary. Rules: - Any NUL byte => binary - Consider only a sample (up to 8 KiB). If >30% of bytes are control characters outside the common whitespace/newline range, treat as binary.
(b []byte)
| 50 | // - Consider only a sample (up to 8 KiB). If >30% of bytes are control characters |
| 51 | // outside the common whitespace/newline range, treat as binary. |
| 52 | func IsBinary(b []byte) bool { |
| 53 | n := len(b) |
| 54 | if n == 0 { |
| 55 | return false |
| 56 | } |
| 57 | if n > 8192 { |
| 58 | n = 8192 |
| 59 | } |
| 60 | sample := b[:n] |
| 61 | bad := 0 |
| 62 | for _, c := range sample { |
| 63 | if c == 0x00 { |
| 64 | return true |
| 65 | } |
| 66 | // Allow common whitespace and control: tab(9), LF(10), CR(13) |
| 67 | if c == 9 || c == 10 || c == 13 { |
| 68 | continue |
| 69 | } |
| 70 | // Count other control chars and DEL as non-text |
| 71 | if c < 32 || c == 127 { |
| 72 | bad++ |
| 73 | } |
| 74 | } |
| 75 | // If more than 30% of sampled bytes are non-text, consider binary |
| 76 | return bad*100 > n*30 |
| 77 | } |
| 78 | |
| 79 | func RefToFileName(ref string) string { |
| 80 | var result strings.Builder |