1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-License-Identifier: Apache-2.0
//! Content analysis utilities for detecting binary vs text data.
//!
//! This module provides functions to analyze byte content and determine
//! whether it represents binary or text data. This is useful for:
//! - Deciding how to display content in user interfaces
//! - Determining appropriate encoding/decoding strategies
//! - Validating input data types
//!
//! # Example
//!
//! ```
//! use hakanai_lib::utils::content_analysis::is_binary;
//!
//! let text_data = b"Hello, world!";
//! let binary_data = b"\x00\x01\x02\xFF";
//!
//! assert!(!is_binary(text_data));
//! assert!(is_binary(binary_data));
//! ```
/// Checks if the given content is binary data.
///
/// This function uses a simple but effective heuristic: the presence of null bytes.
/// Most text encodings (UTF-8, ASCII, etc.) don't contain null bytes, while binary
/// formats (executables, images, compressed files) commonly do.
///
/// # Arguments
///
/// * `content` - A byte slice to analyze
///
/// # Returns
///
/// * `true` if the content appears to be binary data
/// * `false` if the content appears to be text
///
/// # Limitations
///
/// This is a heuristic approach and may have edge cases:
/// - Some text files with special encodings might contain null bytes
/// - Some binary formats might not contain null bytes in their header
///
/// For more robust detection, consider additional checks like UTF-8 validation
/// or magic byte detection for specific file formats.
///
/// # Example
///
/// ```
/// use hakanai_lib::utils::content_analysis::is_binary;
///
/// // Text content
/// assert!(!is_binary(b"Hello, world!"));
/// assert!(!is_binary(b"UTF-8 text: \xE2\x9C\x93")); // ✓
///
/// // Binary content
/// assert!(is_binary(b"\x00\x01\x02"));
/// assert!(is_binary(b"PNG\x00header"));
/// ```