diff --git a/Cargo.lock b/Cargo.lock index ffaa43e..02b8812 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,7 @@ dependencies = [ "difference", "divan", "glob", + "memchr", "serde", "snapbox", "toml", @@ -393,9 +394,9 @@ checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "memchr" -version = "2.7.2" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "normalize-line-endings" diff --git a/Cargo.toml b/Cargo.toml index 2b72bd9..d59fa9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ maintenance = { status = "actively-developed" } [dependencies] anstyle = "1.0.4" +memchr = { version = "2.7.4", optional = true } unicode-width = "0.1.11" [dev-dependencies] @@ -47,6 +48,7 @@ harness = false [features] default = [] +simd = ["memchr"] testing-colors = [] [lints.rust] diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index d94a660..b5fef26 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -893,7 +893,7 @@ fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_> if let Some(before_new_start) = snippet.source[0..ann_start].rfind('\n') { let new_start = before_new_start + 1; - let line_offset = snippet.source[..new_start].lines().count(); + let line_offset = newline_count(&snippet.source[..new_start]); snippet.line_start += line_offset; snippet.source = &snippet.source[new_start..]; @@ -919,6 +919,17 @@ fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_> snippet } +fn newline_count(body: &str) -> usize { + #[cfg(feature = "simd")] + { + memchr::memchr_iter(b'\n', body.as_bytes()).count() + } + #[cfg(not(feature = "simd"))] + { + body.lines().count() + } +} + fn fold_body(body: Vec>) -> Vec> { const INNER_CONTEXT: usize = 1; const INNER_UNFOLD_SIZE: usize = INNER_CONTEXT * 2 + 1;