修复崩溃问题:字节索引和字符索引不匹配导致的 Option::unwrap() panic

This commit is contained in:
2026-05-15 13:44:46 +08:00
parent 19336d5d34
commit eecab02ead

View File

@@ -240,11 +240,14 @@ egui::ComboBox::from_id_salt("bg_type_selector")
egui::Layout::top_down(align).with_main_justify(false), egui::Layout::top_down(align).with_main_justify(false),
|ui| { |ui| {
let mut is_heading = false; let mut is_heading = false;
let mut remaining = indented.as_str(); let mut text_start = 0usize;
while !remaining.is_empty() { let char_indices: Vec<_> = indented.char_indices().collect();
if let Some(pos) = remaining.find(['\x01', '\x02']) { let mut idx = 0;
if pos > 0 { while idx < char_indices.len() {
let text = &remaining[..pos]; let (byte_pos, ch) = char_indices[idx];
if ch == '\x01' || ch == '\x02' {
if byte_pos > text_start {
let text = &indented[text_start..byte_pos];
let mut rt = egui::RichText::new(text) let mut rt = egui::RichText::new(text)
.size(style.font_size) .size(style.font_size)
.color(colors.text); .color(colors.text);
@@ -253,24 +256,27 @@ egui::ComboBox::from_id_salt("bg_type_selector")
} }
ui.add(egui::Label::new(rt).wrap()); ui.add(egui::Label::new(rt).wrap());
} }
let marker = remaining.chars().nth(pos).unwrap(); is_heading = ch == '\x01';
if marker == '\x01' { idx += 1;
is_heading = true; if idx < char_indices.len() {
text_start = char_indices[idx].0;
} else { } else {
is_heading = false; text_start = indented.len();
} }
remaining = &remaining[pos + 1..];
} else { } else {
let mut rt = egui::RichText::new(remaining) idx += 1;
.size(style.font_size)
.color(colors.text);
if is_heading {
rt = rt.strong();
}
ui.add(egui::Label::new(rt).wrap());
break;
} }
} }
if text_start < indented.len() {
let text = &indented[text_start..];
let mut rt = egui::RichText::new(text)
.size(style.font_size)
.color(colors.text);
if is_heading {
rt = rt.strong();
}
ui.add(egui::Label::new(rt).wrap());
}
}, },
); );
}); });