Skip to content

Commit d3f5753

Browse files
committed
perf: use memchr for split_into_lines
closes #73
1 parent d83af25 commit d3f5753

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/helpers.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -469,26 +469,38 @@ pub fn split_into_potential_tokens(source: &str) -> PotentialTokens {
469469
}
470470
}
471471

472-
// /[^\n]+\n?|\n/g
473-
pub fn split_into_lines(source: &str) -> Vec<&str> {
474-
let mut results = Vec::new();
475-
let mut i = 0;
476-
let bytes = source.as_bytes();
477-
while i < bytes.len() {
478-
let cc = bytes[i];
479-
if cc == 10 {
480-
results.push("\n");
481-
i += 1;
482-
} else {
483-
let mut j = i + 1;
484-
while j < bytes.len() && bytes[j] != 10 {
485-
j += 1;
472+
/// Split the string with a needle, each string will contain the needle.
473+
///
474+
/// Copied and modified from https://github.com/rust-lang/cargo/blob/30efe860c0e4adc1a6d7057ad223dc6e47d34edf/src/cargo/sources/registry/index.rs#L1048-L1072
475+
fn split(haystack: &str, needle: u8) -> impl Iterator<Item = &str> {
476+
struct Split<'a> {
477+
haystack: &'a str,
478+
needle: u8,
479+
}
480+
481+
impl<'a> Iterator for Split<'a> {
482+
type Item = &'a str;
483+
484+
fn next(&mut self) -> Option<&'a str> {
485+
if self.haystack.is_empty() {
486+
return None;
486487
}
487-
results.push(&source[i..(j + 1).min(bytes.len())]);
488-
i = j + 1;
488+
let (ret, remaining) =
489+
match memchr::memchr(self.needle, self.haystack.as_bytes()) {
490+
Some(pos) => (&self.haystack[..=pos], &self.haystack[pos + 1..]),
491+
None => (self.haystack, ""),
492+
};
493+
self.haystack = remaining;
494+
Some(ret)
489495
}
490496
}
491-
results
497+
498+
Split { haystack, needle }
499+
}
500+
501+
// /[^\n]+\n?|\n/g
502+
pub fn split_into_lines(source: &str) -> Vec<&str> {
503+
split(source, b'\n').collect()
492504
}
493505

494506
pub fn get_generated_source_info(source: &str) -> GeneratedInfo {

0 commit comments

Comments
 (0)