| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| //! RFC 8259 JSON string encoder (zero allocation, zero `.rodata` tables). | |
| use crate::types::JsonError; | |
| #[inline(always)] | |
| const fn lower_hex_digit(nibble: u8) -> u8 { | |
| let n = nibble & 0x0F; | |
| if n < 10 { b'0' + n } else { b'a' + (n - 10) } | |
| } | |
| #[curry::verified] | |
| fn push_byte(out: &mut [u8], pos: &mut usize, b: u8) -> Result<(), JsonError> { | |
| let idx = *pos; | |
| let slot = out.get_mut(idx).ok_or(JsonError::BufferTooSmall)?; | |
| *slot = b; | |
| *pos = idx + 1; | |
| Ok(()) | |
| } | |
| #[curry::verified] | |
| fn push_two_bytes(out: &mut [u8], pos: &mut usize, b0: u8, b1: u8) -> Result<(), JsonError> { | |
| let idx = *pos; | |
| let end = idx.checked_add(2).ok_or(JsonError::BufferTooSmall)?; | |
| if end > out.len() { | |
| return Err(JsonError::BufferTooSmall); | |
| } | |
| if let Some(s0) = out.get_mut(idx) { | |
| *s0 = b0; | |
| } | |
| if let Some(s1) = out.get_mut(idx + 1) { | |
| *s1 = b1; | |
| } | |
| *pos = end; | |
| Ok(()) | |
| } | |
| #[curry::verified] | |
| fn push_u00_escape(out: &mut [u8], pos: &mut usize, b: u8) -> Result<(), JsonError> { | |
| let idx = *pos; | |
| let end = idx.checked_add(6).ok_or(JsonError::BufferTooSmall)?; | |
| if end > out.len() { | |
| return Err(JsonError::BufferTooSmall); | |
| } | |
| if let Some(s) = out.get_mut(idx) { | |
| *s = b'\\'; | |
| } | |
| if let Some(s) = out.get_mut(idx + 1) { | |
| *s = b'u'; | |
| } | |
| if let Some(s) = out.get_mut(idx + 2) { | |
| *s = b'0'; | |
| } | |
| if let Some(s) = out.get_mut(idx + 3) { | |
| *s = b'0'; | |
| } | |
| if let Some(s) = out.get_mut(idx + 4) { | |
| *s = lower_hex_digit(b >> 4); | |
| } | |
| if let Some(s) = out.get_mut(idx + 5) { | |
| *s = lower_hex_digit(b & 0x0F); | |
| } | |
| *pos = end; | |
| Ok(()) | |
| } | |
| #[curry::verified] | |
| fn append_escaped_byte(out: &mut [u8], pos: &mut usize, b: u8) -> Result<(), JsonError> { | |
| if b == b'"' || b == b'\\' { | |
| return push_two_bytes(out, pos, b'\\', b); | |
| } | |
| if b == 0x08 { | |
| return push_two_bytes(out, pos, b'\\', b'b'); | |
| } | |
| if b == 0x0C { | |
| return push_two_bytes(out, pos, b'\\', b'f'); | |
| } | |
| if b == b'\n' { | |
| return push_two_bytes(out, pos, b'\\', b'n'); | |
| } | |
| if b == b'\r' { | |
| return push_two_bytes(out, pos, b'\\', b'r'); | |
| } | |
| if b == b'\t' { | |
| return push_two_bytes(out, pos, b'\\', b't'); | |
| } | |
| if b < 0x20 { | |
| return push_u00_escape(out, pos, b); | |
| } | |
| push_byte(out, pos, b) | |
| } | |
| /// Write `s` as a quoted, RFC 8259-escaped JSON string (`"..."`) into | |
| /// `out[*pos..]`, updating `*pos` on success. | |
| /// | |
| /// Escapes `"`, `\`, `\b`, `\f`, `\n`, `\r`, `\t`, and all other ASCII control | |
| /// characters (`0x00..=0x1F` as `\u00xx`). Leaves `*pos` unchanged on | |
| /// [`JsonError::BufferTooSmall`]. | |
| #[curry::verified] | |
| pub fn write_escaped_json_string_into( | |
| s: &str, | |
| out: &mut [u8], | |
| pos: &mut usize, | |
| ) -> Result<(), JsonError> { | |
| let mut cur = *pos; | |
| push_byte(out, &mut cur, b'"')?; | |
| let bytes = s.as_bytes(); | |
| let mut i: usize = 0; | |
| while i < bytes.len() { | |
| let &b = bytes.get(i).ok_or(JsonError::BufferTooSmall)?; | |
| append_escaped_byte(out, &mut cur, b)?; | |
| i += 1; | |
| } | |
| push_byte(out, &mut cur, b'"')?; | |
| *pos = cur; | |
| Ok(()) | |
| } |