Skip to content

Commit

Permalink
Fix off by one error in highlight (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
vesdev authored Aug 10, 2024
1 parent f8cfa57 commit 62f53e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions src/handlers/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,10 @@ impl MessageData {
) -> Vec<Span<'s>> {
static EMOTE_FINDER: Lazy<memmem::Finder> =
Lazy::new(|| memmem::Finder::new(ZERO_WIDTH_SPACE_STR));
let line_is_empty = line.is_empty();

// A line contains emotes if `emotes` is not empty and `line` starts with a unicode placeholder or contains ZWS.
if emotes.is_empty()
let spans = if emotes.is_empty()
|| (!line.starts_with(PRIVATE_USE_UNICODE)
&& EMOTE_FINDER.find(line.as_bytes()).is_none())
{
Expand Down Expand Up @@ -436,7 +437,9 @@ impl MessageData {

*start_index = start_index.saturating_sub(ZERO_WIDTH_SPACE_STR.len());
spans
}
};
*start_index += usize::from(!line_is_empty);
spans
}

pub fn to_vec(
Expand Down Expand Up @@ -846,7 +849,7 @@ mod tests {

assert_eq!(emotes, EMOTES_ID_PID);

assert_eq!(start_index, line.len());
assert_eq!(start_index - 1, line.len());

assert_eq!(
spans,
Expand Down Expand Up @@ -899,7 +902,7 @@ mod tests {
);

assert!(emotes.is_empty());
assert_eq!(start_index, line.len());
assert_eq!(start_index - 1, line.len());

assert_eq!(
spans,
Expand Down

0 comments on commit 62f53e4

Please sign in to comment.