MCPcopy Index your code
hub / github.com/RustPython/RustPython / find_newline

Method find_newline

crates/vm/src/stdlib/_io.rs:2315–2358  ·  view source on GitHub ↗

returns position where the new line starts if found, otherwise position at which to continue the search after more is read into the buffer

(&self, s: &Wtf8)

Source from the content-addressed store, hash-verified

2313 /// returns position where the new line starts if found, otherwise position at which to
2314 /// continue the search after more is read into the buffer
2315 fn find_newline(&self, s: &Wtf8) -> Result<usize, usize> {
2316 let len = s.len();
2317 match self {
2318 Self::Universal | Self::Lf => s.find("\n".as_ref()).map(|p| p + 1).ok_or(len),
2319 Self::Passthrough => {
2320 let bytes = s.as_bytes();
2321 memchr::memchr2(b'\n', b'\r', bytes)
2322 .map(|p| {
2323 let nl_len =
2324 if bytes[p] == b'\r' && bytes.get(p + 1).copied() == Some(b'\n') {
2325 2
2326 } else {
2327 1
2328 };
2329 p + nl_len
2330 })
2331 .ok_or(len)
2332 }
2333 Self::Cr => s.find("\r".as_ref()).map(|p| p + 1).ok_or(len),
2334 Self::Crlf => {
2335 // s[searched..] == remaining
2336 let mut searched = 0;
2337 let mut remaining = s.as_bytes();
2338 loop {
2339 match memchr::memchr(b'\r', remaining) {
2340 Some(p) => match remaining.get(p + 1) {
2341 Some(&ch_after_cr) => {
2342 let pos_after = p + 2;
2343 if ch_after_cr == b'\n' {
2344 break Ok(searched + pos_after);
2345 } else {
2346 searched += pos_after;
2347 remaining = &remaining[pos_after..];
2348 continue;
2349 }
2350 }
2351 None => break Err(searched + p),
2352 },
2353 None => break Err(len),
2354 }
2355 }
2356 }
2357 }
2358 }
2359 }
2360
2361 impl TryFromObject for Newlines {

Callers 1

readlineMethod · 0.80

Calls 8

SomeClass · 0.50
ErrClass · 0.50
lenMethod · 0.45
mapMethod · 0.45
findMethod · 0.45
as_refMethod · 0.45
as_bytesMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected