Skip to content

Function tokenizer::preprocess 3x faster #66

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 5 commits into from
Dec 18, 2014
Merged
Changes from 1 commit
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
Prev Previous commit
Simplify match block removing code for multi-byte chars
  • Loading branch information
ayosec committed Dec 18, 2014
commit 2f08b15c621e77e6446b14b2a3134c07b012a408
35 changes: 7 additions & 28 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,15 @@ fn preprocess(input: &str) -> String {
let bytes = input.as_bytes();
let mut result: Vec<u8> = Vec::with_capacity(bytes.len());
let mut last: u8 = 0;
let mut offset: uint = 0;
while offset < bytes.len() {
let byte = bytes[offset];
match byte {
b'\n' if last == b'\r' => (),
b'\r' | b'\n' | b'\x0C' => result.push(b'\n'),
b'\0' => result.push_all("\u{FFFD}".as_bytes()),
_ if byte < 128 => result.push(byte),
_ => {
// Multi-byte character
result.push(byte);
let remaining = bytes.len() - offset;
if remaining >= 3 && byte >= 0xF0 {
result.push(bytes[offset + 1]);
result.push(bytes[offset + 2]);
result.push(bytes[offset + 3]);
offset += 3;
} else if remaining >= 2 && byte >= 0xE0 {
result.push(bytes[offset + 1]);
result.push(bytes[offset + 2]);
offset += 2;
} else if remaining >= 1 && byte >= 0xC0 {
result.push(bytes[offset + 1]);
offset += 1;
}
}
for byte in bytes.iter() {
match *byte {
b'\n' if last == b'\r' => (),
b'\r' | b'\x0C' => result.push(b'\n'),
b'\0' => result.push_all("\u{FFFD}".as_bytes()),
_ => result.push(*byte),
}

last = byte;
offset += 1;
last = *byte;
}

unsafe { String::from_utf8_unchecked(result) }
Expand Down