Skip to content

utils: rewrite count_newlines using bytecount::count #3096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ rustc-ap-rustc_target = "272.0.0"
rustc-ap-syntax = "272.0.0"
rustc-ap-syntax_pos = "272.0.0"
failure = "0.1.1"
bytecount = { version = "0.3", features = ["simd-accel"] }

[dev-dependencies]
assert_cli = "0.6"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#[macro_use]
extern crate derive_new;
extern crate atty;
extern crate bytecount;
extern crate diff;
extern crate failure;
extern crate itertools;
Expand Down
6 changes: 4 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use std::borrow::Cow;

use bytecount;

use rustc_target::spec::abi;
use syntax::ast::{
self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind, Path,
Expand Down Expand Up @@ -305,8 +307,8 @@ pub fn stmt_expr(stmt: &ast::Stmt) -> Option<&ast::Expr> {

#[inline]
pub fn count_newlines(input: &str) -> usize {
// Using `as_bytes` to omit UTF-8 decoding
input.as_bytes().iter().filter(|&&c| c == b'\n').count()
// Using bytes to omit UTF-8 decoding
bytecount::count(input.as_bytes(), b'\n')
}

// For format_missing and last_pos, need to use the source callsite (if applicable).
Expand Down