Check if content is binary. Uses heuristics to determine if content is binary data rather than text. # Arguments `content` - The content to check # Returns `true` if the content appears to be binary. # Algorithm Content is considered binary if: 1. It contains null bytes 2. More than 30% of bytes are non-printable (excluding whitespace) # Example ```rust use atomic_core::record::workflow::
(content: &[u8])
| 277 | /// assert!(is_binary(&[0x00, 0x01, 0x02])); |
| 278 | /// ``` |
| 279 | pub fn is_binary(content: &[u8]) -> bool { |
| 280 | // Quick check: null bytes indicate binary |
| 281 | if content.contains(&0) { |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | // Empty content is not binary |
| 286 | if content.is_empty() { |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | // Check ratio of non-printable characters |
| 291 | let non_printable = content |
| 292 | .iter() |
| 293 | .filter(|&&b| { |
| 294 | // Non-printable: < 0x20 except tab (0x09), newline (0x0A), carriage return (0x0D) |
| 295 | b < 0x20 && b != b'\t' && b != b'\n' && b != b'\r' |
| 296 | }) |
| 297 | .count(); |
| 298 | |
| 299 | // If more than 30% non-printable, consider binary |
| 300 | non_printable * 100 / content.len() > 30 |
| 301 | } |
| 302 | |
| 303 | // ============================================================================ |
| 304 | // CONTENT COMPARISON |