Fix unused variable warning and UTF-8 panic in string slicing
- Remove unused total_lines variable in file_ops.rs - Fix UTF-8 boundary panic in utils.rs when generating diff error preview The code was slicing at byte index 200 which could land inside a multi-byte character (e.g., box-drawing chars like ─). Now uses character-based slicing with chars().take() instead.
This commit is contained in:
@@ -255,9 +255,12 @@ pub fn apply_unified_diff_to_string(
|
||||
region_content.replace_range(pos..endpos, new_block);
|
||||
} else {
|
||||
// Not found; provide helpful diagnostics with a short preview
|
||||
let preview_len = old_block.len().min(200);
|
||||
let mut old_preview = old_block[..preview_len].to_string();
|
||||
if old_block.len() > preview_len {
|
||||
// Use character-based slicing to avoid splitting multi-byte UTF-8 characters
|
||||
let max_chars = 200;
|
||||
let preview_len = old_block.chars().count().min(max_chars);
|
||||
let mut old_preview: String = old_block.chars().take(preview_len).collect();
|
||||
let was_truncated = old_block.chars().count() > max_chars;
|
||||
if was_truncated {
|
||||
old_preview.push_str("...");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user