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

Function to_ascii

crates/common/src/str.rs:433–451  ·  view source on GitHub ↗

Convert a string to ascii compatible, escaping unicode-s into escape sequences.

(value: &Wtf8)

Source from the content-addressed store, hash-verified

431/// Convert a string to ascii compatible, escaping unicode-s into escape
432/// sequences.
433pub fn to_ascii(value: &Wtf8) -> AsciiString {
434 let mut ascii = Vec::new();
435 for cp in value.code_points() {
436 if cp.is_ascii() {
437 ascii.push(cp.to_u32() as u8);
438 } else {
439 let c = cp.to_u32();
440 let hex = if c < 0x100 {
441 format!("\\x{c:02x}")
442 } else if c < 0x10000 {
443 format!("\\u{c:04x}")
444 } else {
445 format!("\\U{c:08x}")
446 };
447 ascii.append(&mut hex.into_bytes());
448 }
449 }
450 unsafe { AsciiString::from_ascii_unchecked(ascii) }
451}
452
453#[derive(Clone, Copy)]
454pub struct UnicodeEscapeCodepoint(pub CodePoint);

Callers 1

asciiMethod · 0.85

Calls 7

newFunction · 0.85
code_pointsMethod · 0.80
is_asciiMethod · 0.45
pushMethod · 0.45
to_u32Method · 0.45
appendMethod · 0.45
into_bytesMethod · 0.45

Tested by

no test coverage detected