wip: pr file diffing

This commit is contained in:
2026-05-18 22:30:46 +08:00
parent aa99ba2596
commit 553af0290f
32 changed files with 1523 additions and 164 deletions

48
src/util/file.rs Normal file
View File

@@ -0,0 +1,48 @@
use memchr::memchr;
pub(crate) enum ContentType {
Text,
Binary,
}
pub(crate) struct ContentDiff {
old_content: bytes::Bytes,
new_content: bytes::Bytes,
}
pub(crate) struct LineDiff {
old_line: Option<usize>,
old_content_range: std::ops::Range<usize>,
new_line: Option<usize>,
new_content_range: std::ops::Range<usize>,
}
pub(crate) fn classify_content(content: &[u8]) -> ContentType {
if content.is_empty() {
ContentType::Text
} else if content.starts_with(&[0xEF, 0xBB, 0xBF]) // UTF-8
|| content.starts_with(&[0x00, 0x00, 0xFE, 0xFF]) // UTF-32 BE
|| content.starts_with(&[0xFF, 0xFE, 0x00, 0x00]) // UTF-32 LE
|| content.starts_with(&[0xFE, 0xFF]) // UTF-16 BE
|| content.starts_with(&[0xFF, 0xFE])
{
ContentType::Text
} else {
match memchr(0, &content[0..8192]) {
| None => ContentType::Text,
| Some(_) => ContentType::Binary,
}
}
}
pub(crate) fn diff_content(old: &[u8], new: &[u8]) -> ContentDiff {
similar::TextDiff::from_lines::<[u8]>(old, new)
.iter_all_changes()
.map(|change| LineDiff {
old_line: change.old_index(),
old_content_range: change.old_range,
new_line: change.new_index(),
new_content_range: change.new_range,
})
.collect()
}

View File

@@ -1 +1,2 @@
pub(crate) mod file;
pub(crate) mod timeout;