Skip to content

Commit

Permalink
fix: loop given empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed Apr 27, 2024
1 parent 5527844 commit 053ca2b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion promkit/src/grapheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ impl StyledGraphemes {

pub fn highlight<S: AsRef<str>>(mut self, query: S, style: ContentStyle) -> Option<Self> {
let query_str = query.as_ref();
if query_str.is_empty() {
return None;
}

let mut result = VecDeque::new();

let mut current_index = 0;
Expand All @@ -186,7 +190,7 @@ impl StyledGraphemes {
// Apply style to all matching graphemes
while current_index < match_end {
let mut grapheme = self.0[current_index].clone();
grapheme.apply_style(style.clone());
grapheme.apply_style(style);
result.push_back(grapheme);
current_index += 1;
}
Expand Down Expand Up @@ -387,6 +391,18 @@ mod test {

use super::*;

#[test]
fn test_apply_style_to_empty_query() {
let graphemes = StyledGraphemes::from("Hello, world!");
let new_style = StyleBuilder::new().fgc(Color::Red).build();
let result = graphemes.highlight("", new_style);

assert!(
result.is_none(),
"Should return None for an empty query string"
);
}

#[test]
fn test_apply_style_to_repeated_substring() {
let graphemes = StyledGraphemes::from("Hello, world! Hello, universe!");
Expand Down

0 comments on commit 053ca2b

Please sign in to comment.