reedline/utils/
text_manipulation.rs1use unicode_segmentation::UnicodeSegmentation;
2
3pub fn remove_last_grapheme(string: &str) -> &str {
4 let mut it = UnicodeSegmentation::graphemes(string, true);
5
6 if it.next_back().is_some() {
7 it.as_str()
8 } else {
9 ""
10 }
11}
12
13#[cfg(test)]
14mod test {
15 use super::*;
16 use pretty_assertions::assert_eq;
17
18 #[test]
19 fn remove_last_char_works_with_empty_string() {
20 let string = "";
21
22 assert_eq!(remove_last_grapheme(string), "");
23 }
24
25 #[test]
26 fn remove_last_char_works_with_normal_string() {
27 let string = "this is a string";
28
29 assert_eq!(remove_last_grapheme(string), "this is a strin");
30 }
31
32 #[test]
33 fn remove_last_char_works_with_string_containing_emojis() {
34 let string = "this is a 😞😄";
35
36 assert_eq!(remove_last_grapheme(string), "this is a 😞");
37 }
38}