MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / is_binary

Function is_binary

atomic-core/src/record/workflow/compare.rs:279–301  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

277/// assert!(is_binary(&[0x00, 0x01, 0x02]));
278/// ```
279pub 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

Callers 1

compare_contentFunction · 0.85

Calls 5

containsMethod · 0.45
is_emptyMethod · 0.45
countMethod · 0.45
iterMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected