修复崩溃问题:字节索引和字符索引不匹配导致的 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),
|ui| {
let mut is_heading = false;
let mut remaining = indented.as_str();
while !remaining.is_empty() {
if let Some(pos) = remaining.find(['\x01', '\x02']) {
if pos > 0 {
let text = &remaining[..pos];
let mut text_start = 0usize;
let char_indices: Vec<_> = indented.char_indices().collect();
let mut idx = 0;
while idx < char_indices.len() {
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)
.size(style.font_size)
.color(colors.text);
@@ -253,24 +256,27 @@ egui::ComboBox::from_id_salt("bg_type_selector")
}
ui.add(egui::Label::new(rt).wrap());
}
let marker = remaining.chars().nth(pos).unwrap();
if marker == '\x01' {
is_heading = true;
is_heading = ch == '\x01';
idx += 1;
if idx < char_indices.len() {
text_start = char_indices[idx].0;
} else {
is_heading = false;
text_start = indented.len();
}
remaining = &remaining[pos + 1..];
} else {
let mut rt = egui::RichText::new(remaining)
.size(style.font_size)
.color(colors.text);
if is_heading {
rt = rt.strong();
}
ui.add(egui::Label::new(rt).wrap());
break;
idx += 1;
}
}
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());
}
},
);
});