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

Function find_last_utf8_boundary

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

Find the last valid UTF-8 boundary in a byte slice.

(buf: &[u8], len: usize)

Source from the content-addressed store, hash-verified

6113
6114 /// Find the last valid UTF-8 boundary in a byte slice.
6115 fn find_last_utf8_boundary(buf: &[u8], len: usize) -> usize {
6116 let len = len.min(buf.len());
6117 for count in 1..=4.min(len) {
6118 let c = buf[len - count];
6119 if c < 0x80 {
6120 return len;
6121 }
6122 if c >= 0xc0 {
6123 let expected = if c < 0xe0 {
6124 2
6125 } else if c < 0xf0 {
6126 3
6127 } else {
6128 4
6129 };
6130 if count < expected {
6131 // Incomplete multibyte sequence
6132 return len - count;
6133 }
6134 return len;
6135 }
6136 }
6137 len
6138 }
6139
6140 #[pyattr]
6141 #[pyclass(module = "_io", name = "_WindowsConsoleIO", base = _RawIOBase)]

Callers 2

writeMethod · 0.85
wchar_to_utf8_countFunction · 0.85

Calls 2

minMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected