编辑调整、对比模块
This commit is contained in:
1 parent
c9e61f38c5
commit
6cfa74c901
76 files changed
+9514
-626
No files matched your search
@@ -26,6 +26,7 @@
|
||||
//! 还是 CRLF,否则一次「随手保存」就会在整个 diff 里掀起风暴。
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::fs::Metadata;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
use std::time::UNIX_EPOCH;
|
||||
@@ -293,8 +294,45 @@ fn read_file_inner(
|
||||
if !meta.is_file() {
|
||||
return Err("目标不是普通文件".to_string());
|
||||
}
|
||||
|
||||
let bytes = std::fs::read(p).map_err(|e| format!("读取文件失败: {e}"))?;
|
||||
build_doc(p, &meta, bytes, max_mb, forced_encoding)
|
||||
}
|
||||
|
||||
/// 读取文件,复用调用方**已经读过**的头部(不重开文件、也不重读开头)。
|
||||
///
|
||||
/// 用途是对比模块:它必须「先读头部判断二进制、是文本才整读」(二进制要尽早放弃,
|
||||
/// 不能整读),若先自己嗅探再调 `read_file`,同一个文件会被打开两次、开头读两遍。
|
||||
/// 这里让调用方把**同一个句柄**(读位置已停在 `head` 之后)连同那段头部一起递进来,
|
||||
/// 于是全程只打开一次;也顺带消掉「嗅探时与整读时文件已被换掉」的那个窗口。
|
||||
///
|
||||
/// `meta` 同样由调用方传入(它刚读过一次 `metadata`,两边都省一次 stat)。
|
||||
pub(crate) fn read_file_after_head(
|
||||
mut reader: impl std::io::Read,
|
||||
path: &Path,
|
||||
meta: &Metadata,
|
||||
head: &[u8],
|
||||
max_mb: u32,
|
||||
) -> Result<FileDoc, String> {
|
||||
let mut bytes = Vec::with_capacity(head.len());
|
||||
bytes.extend_from_slice(head);
|
||||
reader
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|e| format!("读取文件失败: {e}"))?;
|
||||
build_doc(path, meta, bytes, max_mb, None)
|
||||
}
|
||||
|
||||
/// 读取链路的公共尾部:由「磁盘字节 + 元信息」组装 [`FileDoc`]。
|
||||
///
|
||||
/// 两个调用方共用 —— [`read_file_inner`](自己整读)与 [`read_file_after_head`]
|
||||
/// (头部由调用方提供)。把这段独立出来是为了让「字节从哪来」与「字节怎么解释」解耦,
|
||||
/// 而不是让解码 / 换行 / 元信息这段逻辑出现两份。
|
||||
fn build_doc(
|
||||
p: &Path,
|
||||
meta: &Metadata,
|
||||
bytes: Vec<u8>,
|
||||
max_mb: u32,
|
||||
forced_encoding: Option<&str>,
|
||||
) -> Result<FileDoc, String> {
|
||||
let encoding = match forced_encoding {
|
||||
Some(enc) if !enc.trim().is_empty() => {
|
||||
let requested = enc.trim();
|
||||
|
||||
Reference in new issue
Block a user