Skip to content

Commit 7b1a230

Browse files
committed
feat: add assert_unchecked for memchr
1 parent 17f1777 commit 7b1a230

10 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/uu/cat/src/cat.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ fn write_end<W: Write>(
650650
fn write_to_end<W: Write>(in_buf: &[u8], writer: &mut W) -> io::Result<usize> {
651651
// using memchr2 significantly improves performances
652652
if let Some(p) = memchr2(b'\n', b'\r', in_buf) {
653+
unsafe { std::hint::assert_unchecked(p < in_buf.len()) };
653654
writer.write_all(&in_buf[..p])?;
654655
Ok(p)
655656
} else {

src/uu/cut/src/cut.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ fn cut_fields_newline_char_delim<R: Read, W: Write>(
294294
has_processed_data = true;
295295

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

317318
if let Some(pos) = memchr::memchr(newline_char, buf) {
319+
unsafe { std::hint::assert_unchecked(pos < buf.len()) };
318320
let bytes_to_consume = pos + 1;
319321
reader.consume(bytes_to_consume);
320322
break;

src/uu/cut/src/matcher.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl Matcher for ExactMatcher<'_> {
2828
let mut pos = 0usize;
2929
loop {
3030
let match_idx = memchr(self.needle[0], &haystack[pos..])?;
31+
unsafe { std::hint::assert_unchecked(match_idx < haystack.len()) };
3132
let match_idx = match_idx + pos; // account for starting from pos
3233

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

5052
while skip < haystack.len() {

src/uu/factor/src/factor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
194194
let (mut display, mut prev) = (true, 0);
195195
for i in memchr3_iter(DELIM_SPACE, DELIM_TAB, DELIM_NULL, &line).chain(once(le))
196196
{
197+
unsafe { std::hint::assert_unchecked(i < line.len()) };
197198
let has_null = line.get(i) == Some(&DELIM_NULL);
198199
if display && (prev != i || has_null) {
199200
write_factors_str(&line[prev..i], &mut w, print_exponents)?;

src/uu/numfmt/src/numfmt.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ fn format_and_write<W: std::io::Write>(
5858
) -> UResult<bool> {
5959
// GNU truncates at the first embedded null byte.
6060
let line = match memchr::memchr(b'\0', input_line) {
61-
Some(i) => &input_line[..i],
61+
Some(i) => {
62+
unsafe { std::hint::assert_unchecked(i < input_line.len()) };
63+
&input_line[..i]
64+
}
6265
None => input_line,
6366
};
6467

src/uu/pr/src/pr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ fn get_pages(options: &OutputOptions, file_id: usize, buf: &[u8]) -> Vec<(usize,
934934
// and a page comprises several lines. A form feed character marks
935935
// the end of a page regardless of how many lines have been read.
936936
for i in memchr::memchr2_iter(FF, NL, buf) {
937+
unsafe { std::hint::assert_unchecked(i < buf.len()) };
937938
if buf[i] == FF {
938939
// Treat everything up to (but not including) the form feed
939940
// character as the last line of the page.

src/uu/sort/src/chunks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ fn parse_lines<'a>(
316316
let mut start = 0usize;
317317
let mut index = 0usize;
318318
for sep_idx in memchr_iter(separator, read) {
319+
unsafe { std::hint::assert_unchecked(sep_idx < read.len()) };
319320
let line = &read[start..sep_idx];
320321
lines.push(Line::create(line, index, line_data, token_buffer, settings));
321322
index += 1;

src/uu/split/src/split.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ impl Write for LineChunkWriter<'_> {
879879
let mut total_bytes_written = 0;
880880
let sep = self.settings.separator;
881881
for i in memchr::memchr_iter(sep, buf) {
882+
unsafe { std::hint::assert_unchecked(i < buf.len()) };
882883
// If we have exceeded the number of lines to write in the
883884
// current chunk, then start a new chunk and its
884885
// corresponding writer.

src/uu/tac/src/tac.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ fn buffer_tac(data: &[u8], before: bool, separator: &OsStr) -> std::io::Result<(
209209
// the end of the line (as in "abc\ndef\n") or at the beginning of
210210
// the line (as in "/abc/def").
211211
for i in memmem::rfind_iter(data, separator.as_encoded_bytes()) {
212+
unsafe { std::hint::assert_unchecked(i < data.len()) };
212213
if before {
213214
out.write_all(&data[i..following_line_start])?;
214215
following_line_start = i;

src/uucore/src/lib/features/sum.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ impl Write for DigestWriter<'_> {
559559
// the beginning of this "\r\n".
560560
let mut i_prev = 0;
561561
for i in memmem::find_iter(buf, b"\r\n") {
562+
unsafe { std::hint::assert_unchecked(i < buf.len()) };
562563
self.digest.hash_update(&buf[i_prev..i]);
563564
i_prev = i + 1;
564565
}

0 commit comments

Comments
 (0)