From c0313f3026cb8577c4035fd72394772842dec4b4 Mon Sep 17 00:00:00 2001 From: Samyak S Sarnayak Date: Mon, 17 Jan 2022 13:10:44 +0530 Subject: [PATCH] Use chars for highlight instead of graphemes Tokenizer v0.2.7 uses chars instead of graphemes for matching bytes. `unicode-segmentation` dependency isn't needed anymore. Also, oxidised the highlight code :) Co-authored-by: many --- http-ui/Cargo.toml | 1 - http-ui/src/main.rs | 27 ++++++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/http-ui/Cargo.toml b/http-ui/Cargo.toml index 7406a1c1b..f45a85753 100644 --- a/http-ui/Cargo.toml +++ b/http-ui/Cargo.toml @@ -17,7 +17,6 @@ once_cell = "1.5.2" rayon = "1.5.0" structopt = { version = "0.3.21", default-features = false, features = ["wrap_help"] } tempfile = "3.2.0" -unicode-segmentation = "1.6.0" # http server askama = "0.10.5" diff --git a/http-ui/src/main.rs b/http-ui/src/main.rs index 386f10cb4..6502bf83a 100644 --- a/http-ui/src/main.rs +++ b/http-ui/src/main.rs @@ -34,7 +34,6 @@ use structopt::StructOpt; use tokio::fs::File as TFile; use tokio::io::AsyncWriteExt; use tokio::sync::broadcast; -use unicode_segmentation::UnicodeSegmentation; use warp::filters::ws::Message; use warp::http::Response; use warp::Filter; @@ -161,21 +160,19 @@ impl<'a, A: AsRef<[u8]>> Highlighter<'a, A> { let analyzed = self.analyzer.analyze(&old_string); for (word, token) in analyzed.reconstruct() { if token.is_word() { - let chars_to_highlight = matching_words.matching_bytes(&token).unwrap_or(0); - if chars_to_highlight > 0 { - let graphemes = word.graphemes(true); - let chars = graphemes.clone().into_iter(); + match matching_words.matching_bytes(&token) { + Some(chars_to_highlight) => { + let mut chars = word.chars(); - string.push_str(""); - string.push_str( - chars.take(chars_to_highlight).collect::().as_str(), - ); - string.push_str(""); - - let chars = graphemes.into_iter().skip(chars_to_highlight); - string.push_str(chars.collect::().as_str()); - } else { - string.push_str(word); + string.push_str(""); + // push the part to highlight + string.extend(chars.by_ref().take(chars_to_highlight)); + string.push_str(""); + // push the suffix after highlight + string.extend(chars); + } + // no highlight + None => string.push_str(word), } } else { string.push_str(word);