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

Function next_code_point

crates/sre_engine/src/string.rs:226–265  ·  view source on GitHub ↗
(ptr: &mut *const u8)

Source from the content-addressed store, hash-verified

224/// `bytes` must produce a valid UTF-8-like (UTF-8 or WTF-8) string
225#[inline]
226const unsafe fn next_code_point(ptr: &mut *const u8) -> u32 {
227 // Decode UTF-8
228 let x = unsafe { **ptr };
229 *ptr = unsafe { ptr.offset(1) };
230
231 if x < 128 {
232 return x as u32;
233 }
234
235 // Multibyte case follows
236 // Decode from a byte combination out of: [[[x y] z] w]
237 // NOTE: Performance is sensitive to the exact formulation here
238 let init = utf8_first_byte(x, 2);
239 // SAFETY: `bytes` produces an UTF-8-like string,
240 // so the iterator must produce a value here.
241 let y = unsafe { **ptr };
242 *ptr = unsafe { ptr.offset(1) };
243 let mut ch = utf8_acc_cont_byte(init, y);
244 if x >= 0xE0 {
245 // [[x y z] w] case
246 // 5th bit in 0xE0 .. 0xEF is always clear, so `init` is still valid
247 // SAFETY: `bytes` produces an UTF-8-like string,
248 // so the iterator must produce a value here.
249 let z = unsafe { **ptr };
250 *ptr = unsafe { ptr.offset(1) };
251 let y_z = utf8_acc_cont_byte((y & CONT_MASK) as u32, z);
252 ch = (init << 12) | y_z;
253 if x >= 0xF0 {
254 // [x y z w] case
255 // use only the lower 3 bits of `init`
256 // SAFETY: `bytes` produces an UTF-8-like string,
257 // so the iterator must produce a value here.
258 let w = unsafe { **ptr };
259 *ptr = unsafe { ptr.offset(1) };
260 ch = ((init & 7) << 18) | utf8_acc_cont_byte(y_z, w);
261 }
262 }
263
264 ch
265}
266
267/// Reads the last code point out of a byte iterator (assuming a
268/// UTF-8-like encoding).

Callers 3

advanceMethod · 0.70
peekMethod · 0.70
skipMethod · 0.70

Calls 3

utf8_first_byteFunction · 0.70
utf8_acc_cont_byteFunction · 0.70
offsetMethod · 0.45

Tested by

no test coverage detected