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

Function write_json_string

crates/stdlib/src/json/machinery.rs:75–107  ·  view source on GitHub ↗
(s: &str, ascii_only: bool, w: &mut W)

Source from the content-addressed store, hash-verified

73}
74
75pub fn write_json_string<W: io::Write>(s: &str, ascii_only: bool, w: &mut W) -> io::Result<()> {
76 w.write_all(b"\"")?;
77 let mut write_start_idx = 0;
78 let bytes = s.as_bytes();
79 if ascii_only {
80 for (idx, c) in s.char_indices() {
81 if c.is_ascii() {
82 if let Some(escaped) = json_escaped_char(c as u8) {
83 w.write_all(&bytes[write_start_idx..idx])?;
84 w.write_all(escaped.as_bytes())?;
85 write_start_idx = idx + 1;
86 }
87 } else {
88 w.write_all(&bytes[write_start_idx..idx])?;
89 write_start_idx = idx + c.len_utf8();
90 // codepoints outside the BMP get 2 '\uxxxx' sequences to represent them
91 for point in c.encode_utf16(&mut [0; 2]) {
92 write!(w, "\\u{point:04x}")?;
93 }
94 }
95 }
96 } else {
97 for (idx, c) in s.bytes().enumerate() {
98 if let Some(escaped) = json_escaped_char(c) {
99 w.write_all(&bytes[write_start_idx..idx])?;
100 w.write_all(escaped.as_bytes())?;
101 write_start_idx = idx + 1;
102 }
103 }
104 }
105 w.write_all(&bytes[write_start_idx..])?;
106 w.write_all(b"\"")
107}
108
109#[derive(Debug)]
110pub struct DecodeError {

Callers 1

encode_stringFunction · 0.85

Calls 4

json_escaped_charFunction · 0.85
as_bytesMethod · 0.45
is_asciiMethod · 0.45
bytesMethod · 0.45

Tested by

no test coverage detected