//! Fixed-capacity ASCII string, allocated entirely on the stack. //! Shared by the line discipline (`tty`) and the shell. use core::fmt; /// A fixed-capacity string allocated entirely on the stack. pub struct String { buf: [u8; N], len: usize, } impl Default for String { fn default() -> Self { Self::new() } } impl String { pub const fn new() -> Self { String { buf: [0u8; N], len: 0, } } pub fn as_str(&self) -> &str { // SAFETY: we only write valid ASCII bytes. unsafe { core::str::from_utf8_unchecked(&self.buf[..self.len]) } } pub fn push(&mut self, c: char) -> Result<(), ()> { if self.len >= N { return Err(()); } self.buf[self.len] = c as u8; self.len += 1; Ok(()) } pub fn pop(&mut self) -> Option { if self.len == 0 { return None; } self.len -= 1; Some(self.buf[self.len] as char) } pub fn is_empty(&self) -> bool { self.len == 0 } pub fn clear(&mut self) { self.len = 0; } pub fn trim(&self) -> &str { self.as_str().trim() } } impl fmt::Display for String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.as_str()) } } impl fmt::Debug for String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.as_str()) } }