From 74c7d40d25f7ee294546c419a016916fa6fa5f10 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Wed, 1 Jan 2025 16:40:02 -0600 Subject: make prev_char more concise --- src/launcher/strsub.rs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'src/launcher/strsub.rs') diff --git a/src/launcher/strsub.rs b/src/launcher/strsub.rs index 447021a..a50555d 100644 --- a/src/launcher/strsub.rs +++ b/src/launcher/strsub.rs @@ -6,20 +6,8 @@ const VAR_BEGIN: &str = "${"; const VAR_END: &str = "}"; const VAR_DEFAULT: &str = ":-"; -fn prev_char(slice: &str, mut idx: usize) -> Option<(usize, char)> { - if idx == 0 || idx >= slice.len() { - return None; - } - - loop { - // will never panic because the condition always succeeds for idx == 0 - // (the precondition will handle cases where the slice is empty) - idx -= 1; - - if slice.is_char_boundary(idx) { - return Some((idx, slice[idx..].chars().next().unwrap())) - } - } +fn prev_char(slice: &str, idx: usize) -> Option<(usize, char)> { + slice[..idx].char_indices().rev().next() } // basically the same thing as replace_string, but it creates the String itself and returns it. -- cgit v1.2.3-70-g09d2