Write the text representation to a buffer, returning the number of bytes written
(&self, buffer: &mut [u8])
| 89 | |
| 90 | /// Write the text representation to a buffer, returning the number of bytes written |
| 91 | pub fn write_text_to_buffer(&self, buffer: &mut [u8]) -> usize { |
| 92 | match self { |
| 93 | SmallValue::BoolTrue => { |
| 94 | buffer[0] = b't'; |
| 95 | 1 |
| 96 | } |
| 97 | SmallValue::BoolFalse => { |
| 98 | buffer[0] = b'f'; |
| 99 | 1 |
| 100 | } |
| 101 | SmallValue::Zero => { |
| 102 | buffer[0] = b'0'; |
| 103 | 1 |
| 104 | } |
| 105 | SmallValue::One => { |
| 106 | buffer[0] = b'1'; |
| 107 | 1 |
| 108 | } |
| 109 | SmallValue::MinusOne => { |
| 110 | buffer[0] = b'-'; |
| 111 | buffer[1] = b'1'; |
| 112 | 2 |
| 113 | } |
| 114 | SmallValue::Empty => 0, |
| 115 | SmallValue::SmallInt { value, .. } => { |
| 116 | // Use itoa for fast integer formatting |
| 117 | let mut itoa_buf = itoa::Buffer::new(); |
| 118 | let formatted = itoa_buf.format(*value); |
| 119 | let bytes = formatted.as_bytes(); |
| 120 | buffer[..bytes.len()].copy_from_slice(bytes); |
| 121 | bytes.len() |
| 122 | } |
| 123 | SmallValue::SmallFloat { value, .. } => { |
| 124 | // Use standard library formatting for now |
| 125 | // TODO: Consider using ryu crate for faster float formatting |
| 126 | use std::io::Write; |
| 127 | let mut cursor = std::io::Cursor::new(&mut buffer[..]); |
| 128 | write!(&mut cursor, "{value}").unwrap(); |
| 129 | cursor.position() as usize |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /// Get the maximum possible text length for this value |
| 135 | pub fn max_text_length(&self) -> usize { |