Skip to content
Draft
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
1 change: 1 addition & 0 deletions src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ fn write_end<W: Write>(
fn write_to_end<W: Write>(in_buf: &[u8], writer: &mut W) -> io::Result<usize> {
// using memchr2 significantly improves performances
if let Some(p) = memchr2(b'\n', b'\r', in_buf) {
unsafe { std::hint::assert_unchecked(p < in_buf.len()) };
writer.write_all(&in_buf[..p])?;
Ok(p)
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/uu/cut/src/cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ fn cut_fields_newline_char_delim<R: Read, W: Write>(
has_processed_data = true;

if let Some(pos) = memchr::memchr(newline_char, buf) {
unsafe { std::hint::assert_unchecked(pos < buf.len()) };
let amt = pos + 1;
line.extend_from_slice(&buf[..amt]);
reader.consume(amt);
Expand All @@ -315,6 +316,7 @@ fn cut_fields_newline_char_delim<R: Read, W: Write>(
has_processed_data = true;

if let Some(pos) = memchr::memchr(newline_char, buf) {
unsafe { std::hint::assert_unchecked(pos < buf.len()) };
let bytes_to_consume = pos + 1;
reader.consume(bytes_to_consume);
break;
Expand Down
2 changes: 2 additions & 0 deletions src/uu/cut/src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl Matcher for ExactMatcher<'_> {
let mut pos = 0usize;
loop {
let match_idx = memchr(self.needle[0], &haystack[pos..])?;
unsafe { std::hint::assert_unchecked(match_idx < haystack.len()) };
let match_idx = match_idx + pos; // account for starting from pos

if self.needle.len() == 1 || haystack[match_idx + 1..].starts_with(&self.needle[1..]) {
Expand All @@ -45,6 +46,7 @@ pub struct WhitespaceMatcher {}
impl Matcher for WhitespaceMatcher {
fn next_match(&self, haystack: &[u8]) -> Option<(usize, usize)> {
let match_idx = memchr2(b' ', b'\t', haystack)?;
unsafe { std::hint::assert_unchecked(match_idx < haystack.len()) };
let mut skip = match_idx + 1;

while skip < haystack.len() {
Expand Down
1 change: 1 addition & 0 deletions src/uu/factor/src/factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let (mut display, mut prev) = (true, 0);
for i in memchr3_iter(DELIM_SPACE, DELIM_TAB, DELIM_NULL, &line).chain(once(le))
{
unsafe { std::hint::assert_unchecked(i <= line.len()) };
let has_null = line.get(i) == Some(&DELIM_NULL);
if display && (prev != i || has_null) {
write_factors_str(&line[prev..i], &mut w, print_exponents)?;
Expand Down
5 changes: 4 additions & 1 deletion src/uu/numfmt/src/numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ fn format_and_write<W: std::io::Write>(
) -> UResult<bool> {
// GNU truncates at the first embedded null byte.
let line = match memchr::memchr(b'\0', input_line) {
Some(i) => &input_line[..i],
Some(i) => {
unsafe { std::hint::assert_unchecked(i < input_line.len()) };
&input_line[..i]
}
None => input_line,
};

Expand Down
1 change: 1 addition & 0 deletions src/uu/pr/src/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ fn get_pages(options: &OutputOptions, file_id: usize, buf: &[u8]) -> Vec<(usize,
// and a page comprises several lines. A form feed character marks
// the end of a page regardless of how many lines have been read.
for i in memchr::memchr2_iter(FF, NL, buf) {
unsafe { std::hint::assert_unchecked(i < buf.len()) };
if buf[i] == FF {
// Treat everything up to (but not including) the form feed
// character as the last line of the page.
Expand Down
1 change: 1 addition & 0 deletions src/uu/sort/src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ fn parse_lines<'a>(
let mut start = 0usize;
let mut index = 0usize;
for sep_idx in memchr_iter(separator, read) {
unsafe { std::hint::assert_unchecked(sep_idx < read.len()) };
let line = &read[start..sep_idx];
lines.push(Line::create(line, index, line_data, token_buffer, settings));
index += 1;
Expand Down
1 change: 1 addition & 0 deletions src/uu/split/src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ impl Write for LineChunkWriter<'_> {
let mut total_bytes_written = 0;
let sep = self.settings.separator;
for i in memchr::memchr_iter(sep, buf) {
unsafe { std::hint::assert_unchecked(i < buf.len()) };
// If we have exceeded the number of lines to write in the
// current chunk, then start a new chunk and its
// corresponding writer.
Expand Down
1 change: 1 addition & 0 deletions src/uu/tac/src/tac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ fn buffer_tac(data: &[u8], before: bool, separator: &OsStr) -> std::io::Result<(
// the end of the line (as in "abc\ndef\n") or at the beginning of
// the line (as in "/abc/def").
for i in memmem::rfind_iter(data, separator.as_encoded_bytes()) {
unsafe { std::hint::assert_unchecked(i < data.len()) };
if before {
out.write_all(&data[i..following_line_start])?;
following_line_start = i;
Expand Down
1 change: 1 addition & 0 deletions src/uucore/src/lib/features/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ impl Write for DigestWriter<'_> {
// the beginning of this "\r\n".
let mut i_prev = 0;
for i in memmem::find_iter(buf, b"\r\n") {
unsafe { std::hint::assert_unchecked(i < buf.len()) };
self.digest.hash_update(&buf[i_prev..i]);
i_prev = i + 1;
}
Expand Down
Loading