1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::ReplaceErr;

pub struct OutputBuf {
    start: *mut u8,
    cur: *mut u8,
    end: *mut u8,
}

#[derive(Debug, PartialEq)]
pub enum OutputError {
    OutputFull,
    FormattingErr,
}

impl From<core::fmt::Error> for OutputError {
    fn from(_oe: core::fmt::Error) -> Self {
        OutputError::FormattingErr
    }
}

impl OutputBuf {
    pub fn new(bottom: *mut u8, size: usize) -> Self {
        let end = bottom.wrapping_add(size);
        debug_assert!(end >= bottom);
        Self {
            end,
            start: bottom,
            cur: bottom,
        }
    }

    #[inline]
    pub fn capacity(&self) -> usize {
        (self.end as usize) - (self.start as usize)
    }

    pub fn push_bstr(&mut self, bstr: &[u8]) -> Result<(), OutputError> {
        let new_end = self.cur.wrapping_add(bstr.len());
        if new_end > self.end {
            Err(OutputError::OutputFull)
        } else {
            unsafe {
                core::ptr::copy_nonoverlapping(bstr.as_ptr(), self.cur, bstr.len());
                self.cur = new_end;
            }
            Ok(())
        }
    }

    pub fn push_str(&mut self, stir: &str) -> Result<(), OutputError> {
        let bstr = stir.as_bytes();
        self.push_bstr(bstr)
    }

    pub fn clear(&mut self) {
        self.cur = self.start;
    }

    pub fn as_str(&self) -> &str {
        let len = (self.cur as usize) - (self.start as usize);
        if len == 0 {
            ""
        } else {
            unsafe {
                let u8_sli = core::slice::from_raw_parts(self.start, len);
                core::str::from_utf8_unchecked(u8_sli)
            }
        }
    }
}

impl core::fmt::Write for OutputBuf {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        self.push_str(s).replace_err(core::fmt::Error)
    }
}