Repository navigation
Expand file tree
/
Copy pathtext_encoding.rs
More file actions
429 lines (380 loc) · 11.6 KB
/
Copy pathtext_encoding.rs
File metadata and controls
429 lines (380 loc) · 11.6 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Text encoding utilities.
//!
//! Provides text encoding detection and conversion for handling
//! various file encodings.
use std::borrow::Cow;
use std::io::{self, Read};
use std::path::Path;
use serde::{Deserialize, Serialize};
/// Text encoding type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(Default)]
pub enum Encoding {
/// UTF-8 encoding.
#[default]
Utf8,
/// UTF-8 with BOM.
Utf8Bom,
/// UTF-16 Little Endian.
Utf16Le,
/// UTF-16 Big Endian.
Utf16Be,
/// ASCII.
Ascii,
/// ISO-8859-1 (Latin-1).
Latin1,
/// Windows-1252.
Windows1252,
/// Unknown encoding.
Unknown,
}
impl Encoding {
/// Detect encoding from bytes.
pub fn detect(data: &[u8]) -> Self {
// Check for BOM
if data.starts_with(&[0xEF, 0xBB, 0xBF]) {
return Self::Utf8Bom;
}
if data.starts_with(&[0xFF, 0xFE]) {
return Self::Utf16Le;
}
if data.starts_with(&[0xFE, 0xFF]) {
return Self::Utf16Be;
}
// Try UTF-8
if std::str::from_utf8(data).is_ok() {
// Check if it's pure ASCII
if data.iter().all(|&b| b < 128) {
return Self::Ascii;
}
return Self::Utf8;
}
// Check for likely Latin-1/Windows-1252
if data.iter().all(|&b| b < 128 || (160..=255).contains(&b)) {
return Self::Latin1;
}
Self::Unknown
}
/// Detect encoding from file.
pub fn detect_file(path: impl AsRef<Path>) -> io::Result<Self> {
let mut file = std::fs::File::open(path)?;
let mut buffer = vec![0u8; 8192];
let n = file.read(&mut buffer)?;
buffer.truncate(n);
Ok(Self::detect(&buffer))
}
/// Get the BOM for this encoding.
pub fn bom(&self) -> &'static [u8] {
match self {
Self::Utf8Bom => &[0xEF, 0xBB, 0xBF],
Self::Utf16Le => &[0xFF, 0xFE],
Self::Utf16Be => &[0xFE, 0xFF],
_ => &[],
}
}
/// Get encoding name.
pub fn name(&self) -> &'static str {
match self {
Self::Utf8 => "UTF-8",
Self::Utf8Bom => "UTF-8-BOM",
Self::Utf16Le => "UTF-16LE",
Self::Utf16Be => "UTF-16BE",
Self::Ascii => "ASCII",
Self::Latin1 => "ISO-8859-1",
Self::Windows1252 => "Windows-1252",
Self::Unknown => "Unknown",
}
}
/// Check if encoding is Unicode-based.
pub fn is_unicode(&self) -> bool {
matches!(
self,
Self::Utf8 | Self::Utf8Bom | Self::Utf16Le | Self::Utf16Be
)
}
}
impl std::fmt::Display for Encoding {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}
/// Decode bytes to string.
pub fn decode(data: &[u8], encoding: Encoding) -> Result<Cow<'_, str>, EncodingError> {
match encoding {
Encoding::Utf8 => std::str::from_utf8(data)
.map(Cow::Borrowed)
.map_err(|e| EncodingError::InvalidUtf8(e.valid_up_to())),
Encoding::Utf8Bom => {
let data = data.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(data);
std::str::from_utf8(data)
.map(Cow::Borrowed)
.map_err(|e| EncodingError::InvalidUtf8(e.valid_up_to()))
}
Encoding::Utf16Le => decode_utf16le(data),
Encoding::Utf16Be => decode_utf16be(data),
Encoding::Ascii => {
if data.iter().all(|&b| b < 128) {
// Safe: all bytes are valid ASCII
Ok(Cow::Owned(data.iter().map(|&b| b as char).collect()))
} else {
Err(EncodingError::InvalidAscii)
}
}
Encoding::Latin1 | Encoding::Windows1252 => {
// Latin-1 maps directly to Unicode code points
Ok(Cow::Owned(data.iter().map(|&b| b as char).collect()))
}
Encoding::Unknown => {
// Try UTF-8, then Latin-1
std::str::from_utf8(data)
.map(Cow::Borrowed)
.or_else(|_| Ok(Cow::Owned(data.iter().map(|&b| b as char).collect())))
}
}
}
/// Decode UTF-16LE bytes.
fn decode_utf16le(data: &[u8]) -> Result<Cow<'static, str>, EncodingError> {
// Skip BOM if present
let data = data.strip_prefix(&[0xFF, 0xFE]).unwrap_or(data);
if !data.len().is_multiple_of(2) {
return Err(EncodingError::InvalidLength);
}
let chars: Result<String, _> = data
.chunks_exact(2)
.map(|chunk| {
let code = u16::from_le_bytes([chunk[0], chunk[1]]);
char::from_u32(code as u32).ok_or(EncodingError::InvalidCodePoint(code as u32))
})
.collect();
chars.map(Cow::Owned)
}
/// Decode UTF-16BE bytes.
fn decode_utf16be(data: &[u8]) -> Result<Cow<'static, str>, EncodingError> {
// Skip BOM if present
let data = data.strip_prefix(&[0xFE, 0xFF]).unwrap_or(data);
if !data.len().is_multiple_of(2) {
return Err(EncodingError::InvalidLength);
}
let chars: Result<String, _> = data
.chunks_exact(2)
.map(|chunk| {
let code = u16::from_be_bytes([chunk[0], chunk[1]]);
char::from_u32(code as u32).ok_or(EncodingError::InvalidCodePoint(code as u32))
})
.collect();
chars.map(Cow::Owned)
}
/// Encode string to bytes.
pub fn encode(text: &str, encoding: Encoding) -> Vec<u8> {
match encoding {
Encoding::Utf8 => text.as_bytes().to_vec(),
Encoding::Utf8Bom => {
let mut result = vec![0xEF, 0xBB, 0xBF];
result.extend_from_slice(text.as_bytes());
result
}
Encoding::Utf16Le => {
let mut result = vec![0xFF, 0xFE];
for c in text.chars() {
let code = c as u32;
if code <= 0xFFFF {
result.extend_from_slice(&(code as u16).to_le_bytes());
}
}
result
}
Encoding::Utf16Be => {
let mut result = vec![0xFE, 0xFF];
for c in text.chars() {
let code = c as u32;
if code <= 0xFFFF {
result.extend_from_slice(&(code as u16).to_be_bytes());
}
}
result
}
Encoding::Ascii => text
.chars()
.filter(|&c| (c as u32) < 128)
.map(|c| c as u8)
.collect(),
Encoding::Latin1 | Encoding::Windows1252 => text
.chars()
.filter(|&c| (c as u32) < 256)
.map(|c| c as u8)
.collect(),
Encoding::Unknown => text.as_bytes().to_vec(),
}
}
/// Encoding error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EncodingError {
/// Invalid UTF-8 at position.
InvalidUtf8(usize),
/// Invalid ASCII byte.
InvalidAscii,
/// Invalid code point.
InvalidCodePoint(u32),
/// Invalid length for encoding.
InvalidLength,
/// IO error.
Io(String),
}
impl std::fmt::Display for EncodingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidUtf8(pos) => write!(f, "Invalid UTF-8 at position {pos}"),
Self::InvalidAscii => write!(f, "Invalid ASCII byte"),
Self::InvalidCodePoint(cp) => write!(f, "Invalid code point: U+{cp:04X}"),
Self::InvalidLength => write!(f, "Invalid length for encoding"),
Self::Io(msg) => write!(f, "IO error: {msg}"),
}
}
}
impl std::error::Error for EncodingError {}
impl From<io::Error> for EncodingError {
fn from(e: io::Error) -> Self {
Self::Io(e.to_string())
}
}
/// Normalize line endings.
pub fn normalize_line_endings(text: &str, style: LineEnding) -> String {
let normalized = text.replace("\r\n", "\n").replace("\r", "\n");
match style {
LineEnding::Lf => normalized,
LineEnding::CrLf => normalized.replace("\n", "\r\n"),
LineEnding::Cr => normalized.replace("\n", "\r"),
}
}
/// Line ending style.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LineEnding {
/// Unix-style (LF).
Lf,
/// Windows-style (CRLF).
CrLf,
/// Old Mac-style (CR).
Cr,
}
impl LineEnding {
/// Detect line ending style.
pub fn detect(text: &str) -> Self {
if text.contains("\r\n") {
Self::CrLf
} else if text.contains("\r") {
Self::Cr
} else {
Self::Lf
}
}
/// Get the line ending string.
pub fn as_str(&self) -> &'static str {
match self {
Self::Lf => "\n",
Self::CrLf => "\r\n",
Self::Cr => "\r",
}
}
/// Get system default.
pub fn system_default() -> Self {
#[cfg(windows)]
{
Self::CrLf
}
#[cfg(not(windows))]
{
Self::Lf
}
}
}
impl Default for LineEnding {
fn default() -> Self {
Self::system_default()
}
}
/// Check if text is valid UTF-8.
pub fn is_valid_utf8(data: &[u8]) -> bool {
std::str::from_utf8(data).is_ok()
}
/// Check if text is binary (contains null bytes or too many non-printable characters).
pub fn is_binary(data: &[u8]) -> bool {
// Check for null bytes
if data.contains(&0) {
return true;
}
// Check ratio of non-printable characters
let non_printable = data
.iter()
.filter(|&&b| b < 32 && b != 9 && b != 10 && b != 13)
.count();
let threshold = (data.len() as f64 * 0.1) as usize;
non_printable > threshold
}
/// Sanitize text by removing or replacing invalid characters.
pub fn sanitize(text: &str) -> String {
text.chars()
.filter(|&c| !c.is_control() || c == '\n' || c == '\r' || c == '\t')
.collect()
}
/// Truncate text to fit within byte limit while respecting UTF-8 boundaries.
pub fn truncate_bytes(text: &str, max_bytes: usize) -> &str {
if text.len() <= max_bytes {
return text;
}
let mut end = max_bytes;
while end > 0 && !text.is_char_boundary(end) {
end -= 1;
}
&text[..end]
}
/// Count lines in text.
pub fn count_lines(text: &str) -> usize {
if text.is_empty() {
0
} else {
text.lines().count()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_detect_encoding() {
assert_eq!(Encoding::detect(b"hello"), Encoding::Ascii);
assert_eq!(Encoding::detect("héllo".as_bytes()), Encoding::Utf8);
assert_eq!(
Encoding::detect(&[0xEF, 0xBB, 0xBF, 0x68, 0x65, 0x6C, 0x6C, 0x6F]),
Encoding::Utf8Bom
);
}
#[test]
fn test_decode_utf8() {
let text = decode(b"hello", Encoding::Utf8).unwrap();
assert_eq!(text, "hello");
}
#[test]
fn test_encode_utf8() {
let bytes = encode("hello", Encoding::Utf8);
assert_eq!(bytes, b"hello");
}
#[test]
fn test_line_endings() {
assert_eq!(LineEnding::detect("hello\nworld"), LineEnding::Lf);
assert_eq!(LineEnding::detect("hello\r\nworld"), LineEnding::CrLf);
let normalized = normalize_line_endings("hello\r\nworld", LineEnding::Lf);
assert_eq!(normalized, "hello\nworld");
}
#[test]
fn test_is_binary() {
assert!(!is_binary(b"hello world"));
assert!(is_binary(&[0x00, 0x01, 0x02]));
}
#[test]
fn test_truncate_bytes() {
let text = "héllo";
let truncated = truncate_bytes(text, 3);
assert_eq!(truncated, "hé");
}
}