Skip to content

Commit 4006279

Browse files
authored
Merge pull request #286 from Muscraft/fix-simd-newline-count
fix: Make SIMD newline count match non-SIMD
2 parents 5b0e69e + b43f04f commit 4006279

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

src/renderer/mod.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3009,9 +3009,7 @@ fn max_line_number(groups: &[Group<'_>]) -> usize {
30093009
fn newline_count(body: &str) -> usize {
30103010
#[cfg(feature = "simd")]
30113011
{
3012-
memchr::memchr_iter(b'\n', body.as_bytes())
3013-
.count()
3014-
.saturating_sub(1)
3012+
memchr::memchr_iter(b'\n', body.as_bytes()).count()
30153013
}
30163014
#[cfg(not(feature = "simd"))]
30173015
{
@@ -3021,7 +3019,7 @@ fn newline_count(body: &str) -> usize {
30213019

30223020
#[cfg(test)]
30233021
mod test {
3024-
use super::OUTPUT_REPLACEMENTS;
3022+
use super::{newline_count, OUTPUT_REPLACEMENTS};
30253023
use snapbox::IntoData;
30263024

30273025
fn format_replacements(replacements: Vec<(char, &str)>) -> String {
@@ -3043,4 +3041,23 @@ mod test {
30433041
let actual = format_replacements(OUTPUT_REPLACEMENTS.to_owned());
30443042
snapbox::assert_data_eq!(actual, expected.into_data().raw());
30453043
}
3044+
3045+
#[test]
3046+
fn ensure_newline_count_correct() {
3047+
let source = r#"
3048+
cargo-features = ["path-bases"]
3049+
3050+
[package]
3051+
name = "foo"
3052+
version = "0.5.0"
3053+
authors = ["[email protected]"]
3054+
3055+
[dependencies]
3056+
bar = { base = '^^not-valid^^', path = 'bar' }
3057+
"#;
3058+
let actual_count = newline_count(source);
3059+
let expected_count = 10;
3060+
3061+
assert_eq!(expected_count, actual_count);
3062+
}
30463063
}

tests/formatter.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3227,3 +3227,51 @@ LL │ t.field;
32273227
let renderer = renderer.decor_style(DecorStyle::Unicode);
32283228
assert_data_eq!(renderer.render(input), expected_unicode);
32293229
}
3230+
3231+
#[test]
3232+
fn multiple_line_num_widths() {
3233+
let source = r#"
3234+
cargo-features = ["path-bases"]
3235+
3236+
[package]
3237+
name = "foo"
3238+
version = "0.5.0"
3239+
authors = ["[email protected]"]
3240+
3241+
[dependencies]
3242+
bar = { base = '^^not-valid^^', path = 'bar' }
3243+
"#;
3244+
3245+
let title = "invalid character `^` in path base name: `^^not-valid^^`, the first character must be a Unicode XID start character (most letters or `_`)";
3246+
3247+
let input = &[
3248+
Group::with_title(Level::ERROR.primary_title(title)).element(
3249+
Snippet::source(source)
3250+
.path("Cargo.toml")
3251+
.annotation(AnnotationKind::Primary.span(243..282))
3252+
.annotation(AnnotationKind::Visible.span(206..219)),
3253+
),
3254+
];
3255+
3256+
let expected_ascii = str![[r#"
3257+
error: invalid character `^` in path base name: `^^not-valid^^`, the first character must be a Unicode XID start character (most letters or `_`)
3258+
--> Cargo.toml:10:24
3259+
|
3260+
9 | [dependencies]
3261+
10 | bar = { base = '^^not-valid^^', path = 'bar' }
3262+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3263+
"#]];
3264+
let renderer = Renderer::plain();
3265+
assert_data_eq!(renderer.render(input), expected_ascii);
3266+
3267+
let expected_unicode = str![[r#"
3268+
error: invalid character `^` in path base name: `^^not-valid^^`, the first character must be a Unicode XID start character (most letters or `_`)
3269+
╭▸ Cargo.toml:10:24
3270+
3271+
9 │ [dependencies]
3272+
10 │ bar = { base = '^^not-valid^^', path = 'bar' }
3273+
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
3274+
"#]];
3275+
let renderer = renderer.decor_style(DecorStyle::Unicode);
3276+
assert_data_eq!(renderer.render(input), expected_unicode);
3277+
}

0 commit comments

Comments
 (0)