From eecab02ead751c0479c19871acb00afe16356036 Mon Sep 17 00:00:00 2001 From: xiaji Date: Fri, 15 May 2026 13:44:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B4=A9=E6=BA=83=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9A=E5=AD=97=E8=8A=82=E7=B4=A2=E5=BC=95=E5=92=8C?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E7=B4=A2=E5=BC=95=E4=B8=8D=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=9A=84=20Option::unwrap()=20panic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/reader.rs | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 76836ac..fe8d2bc 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -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()); + } }, ); });