Commit c447a5d6 authored by schyrsivochter's avatar schyrsivochter
Browse files

Simplify fgets() with NLL

parent 79f26574
Pipeline #7086 failed with stages
in 27 minutes and 53 seconds
......@@ -318,37 +318,28 @@ pub unsafe extern "C" fn fgets(
}
}
loop {
if left == 0 {
while left != 0 {
let buf = match stream.fill_buf() {
Ok(buf) => buf,
Err(_) => return ptr::null_mut(),
};
if buf.is_empty() {
break;
}
wrote = true;
let len = buf.len().min(left);
// TODO: When NLL is a thing, this block can be flattened out
let (read, exit) = {
let buf = match stream.fill_buf() {
Ok(buf) => buf,
Err(_) => return ptr::null_mut(),
};
if buf.is_empty() {
break;
}
wrote = true;
let len = buf.len().min(left);
let newline = buf[..len].iter().position(|&c| c == b'\n');
let len = newline.map(|i| i + 1).unwrap_or(len);
ptr::copy_nonoverlapping(buf.as_ptr(), out as *mut u8, len);
let newline = buf[..len].iter().position(|&c| c == b'\n');
let len = newline.map(|i| i + 1).unwrap_or(len);
(len, newline.is_some())
};
ptr::copy_nonoverlapping(buf.as_ptr(), out as *mut u8, len);
stream.consume(read);
stream.consume(len);
out = out.add(read);
left -= read;
out = out.add(len);
left -= len;
if exit {
if newline.is_some() {
break;
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment