| 1 | use core::marker::PhantomData; |
| 2 | use core::ptr; |
| 3 | |
| 4 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 5 | pub struct AsciiStr<'a> { |
| 6 | ptr: *const u8, |
| 7 | end: *const u8, |
| 8 | _marker: PhantomData<&'a [u8]>, |
| 9 | } |
| 10 | |
| 11 | impl<'a> AsciiStr<'a> { |
| 12 | #[inline] |
| 13 | pub fn new(s: &'a [u8]) -> Self { |
| 14 | Self { |
| 15 | ptr: s.as_ptr(), |
| 16 | end: unsafe { s.as_ptr().add(s.len()) }, |
| 17 | _marker: PhantomData, |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | pub fn len(&self) -> isize { |
| 22 | self.end as isize - self.ptr as isize |
| 23 | } |
| 24 | |
| 25 | /// # Safety |
| 26 | /// |
| 27 | /// Safe if `n <= self.len()` |
| 28 | #[inline] |
| 29 | pub unsafe fn step_by(&mut self, n: usize) -> &mut Self { |
| 30 | debug_assert!( |
| 31 | // FIXME: remove when we drop support for < 1.43.0 |
| 32 | n < isize::max_value() as usize && n as isize <= self.len(), |
| 33 | "buffer overflow: stepping by greater than our buffer length." |
| 34 | ); |
| 35 | // SAFETY: Safe if `n <= self.len()` |
| 36 | unsafe { self.ptr = self.ptr.add(n) }; |
| 37 | self |
| 38 | } |
| 39 | |
| 40 | /// # Safety |
| 41 | /// |
| 42 | /// Safe if `!self.is_empty()` |
| 43 | #[inline] |
| 44 | pub unsafe fn step(&mut self) -> &mut Self { |
| 45 | debug_assert!(!self.is_empty(), "buffer overflow: buffer is empty."); |
| 46 | // SAFETY: Safe if the buffer is not empty, that is, `self.len() >= 1` |
| 47 | unsafe { self.step_by(1) } |
| 48 | } |
| 49 | |
| 50 | #[inline] |
| 51 | pub fn step_if(&mut self, c: u8) -> bool { |
| 52 | let stepped = self.first_is(c); |
| 53 | if stepped { |
| 54 | // SAFETY: safe since we have at least 1 character in the buffer |
| 55 | unsafe { self.step() }; |
| 56 | } |
| 57 | stepped |
| 58 | } |
| 59 | |
| 60 | #[inline] |
| 61 | pub fn is_empty(&self) -> bool { |
| 62 | self.ptr == self.end |
| 63 | } |
| 64 | |
| 65 | /// # Safety |
| 66 | /// |
| 67 | /// Safe if `!self.is_empty()` |
| 68 | #[inline] |
| 69 | pub unsafe fn first_unchecked(&self) -> u8 { |
| 70 | debug_assert!(!self.is_empty(), "attempting to get first value of empty buffer."); |
| 71 | unsafe { *self.ptr } |
| 72 | } |
| 73 | |
| 74 | #[inline] |
| 75 | pub fn first(&self) -> Option<u8> { |
| 76 | if self.is_empty() { |
| 77 | None |
| 78 | } else { |
| 79 | // SAFETY: safe since `!self.is_empty()` |
| 80 | Some(unsafe { self.first_unchecked() }) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #[inline] |
| 85 | pub fn first_is(&self, c: u8) -> bool { |
| 86 | self.first() == Some(c) |
| 87 | } |
| 88 | |
| 89 | #[inline] |
| 90 | pub fn first_is2(&self, c1: u8, c2: u8) -> bool { |
| 91 | self.first().map_or(false, |c| c == c1 || c == c2) |
| 92 | } |
| 93 | |
| 94 | #[inline] |
| 95 | pub fn first_is_digit(&self) -> bool { |
| 96 | self.first().map_or(false, |c| c.is_ascii_digit()) |
| 97 | } |
| 98 | |
| 99 | #[inline] |
| 100 | pub fn first_digit(&self) -> Option<u8> { |
| 101 | self.first().and_then(|x| { |
| 102 | if x.is_ascii_digit() { |
| 103 | Some(x - b'0') |
| 104 | } else { |
| 105 | None |
| 106 | } |
| 107 | }) |
| 108 | } |
| 109 | |
| 110 | #[inline] |
| 111 | pub fn try_read_digit(&mut self) -> Option<u8> { |
| 112 | let digit = self.first_digit()?; |
| 113 | // SAFETY: Safe since `first_digit` means the buffer is not empty |
| 114 | unsafe { self.step() }; |
| 115 | Some(digit) |
| 116 | } |
| 117 | |
| 118 | #[inline] |
| 119 | pub fn parse_digits(&mut self, mut func: impl FnMut(u8)) { |
| 120 | while let Some(digit) = self.try_read_digit() { |
| 121 | func(digit); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #[inline] |
| 126 | pub fn try_read_u64(&self) -> Option<u64> { |
| 127 | if self.len() >= 8 { |
| 128 | Some(unsafe { self.read_u64_unchecked() }) |
| 129 | } else { |
| 130 | None |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /// # Safety |
| 135 | /// |
| 136 | /// Safe if `self.len() >= 8` |
| 137 | #[inline] |
| 138 | #[allow(clippy::cast_ptr_alignment)] |
| 139 | pub unsafe fn read_u64_unchecked(&self) -> u64 { |
| 140 | debug_assert!(self.len() >= 8, "overflowing buffer: buffer is not 8 bytes long"); |
| 141 | let src = self.ptr as *const u64; |
| 142 | // SAFETY: Safe if `self.len() >= 8` |
| 143 | u64::from_le(unsafe { ptr::read_unaligned(src) }) |
| 144 | } |
| 145 | |
| 146 | #[inline] |
| 147 | pub fn offset_from(&self, other: &Self) -> isize { |
| 148 | isize::wrapping_sub(self.ptr as isize, other.ptr as isize) // assuming the same end |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Most of these are inherently unsafe; we assume we know what we're calling and |
| 153 | // when. |
| 154 | pub trait ByteSlice: AsRef<[u8]> + AsMut<[u8]> { |
| 155 | #[inline] |
| 156 | fn check_first(&self, c: u8) -> bool { |
| 157 | self.as_ref().first() == Some(&c) |
| 158 | } |
| 159 | |
| 160 | #[inline] |
| 161 | fn check_first2(&self, c1: u8, c2: u8) -> bool { |
| 162 | if let Some(&c) = self.as_ref().first() { |
| 163 | c == c1 || c == c2 |
| 164 | } else { |
| 165 | false |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #[inline] |
| 170 | fn eq_ignore_case(&self, u: &[u8]) -> bool { |
| 171 | let s = self.as_ref(); |
| 172 | if s.len() < u.len() { |
| 173 | return false; |
| 174 | } |
| 175 | let d = (0..u.len()).fold(0, |d, i| d | s[i] ^ u[i]); |
| 176 | d == 0 || d == 32 |
| 177 | } |
| 178 | |
| 179 | #[inline] |
| 180 | fn advance(&self, n: usize) -> &[u8] { |
| 181 | &self.as_ref()[n..] |
| 182 | } |
| 183 | |
| 184 | #[inline] |
| 185 | fn skip_chars(&self, c: u8) -> &[u8] { |
| 186 | let mut s = self.as_ref(); |
| 187 | while s.check_first(c) { |
| 188 | s = s.advance(1); |
| 189 | } |
| 190 | s |
| 191 | } |
| 192 | |
| 193 | /// # Safety |
| 194 | /// |
| 195 | /// Safe if `self.len() >= 8`. |
| 196 | #[inline] |
| 197 | #[allow(clippy::cast_ptr_alignment)] |
| 198 | unsafe fn read_u64(&self) -> u64 { |
| 199 | debug_assert!(self.as_ref().len() >= 8); |
| 200 | let src = self.as_ref().as_ptr() as *const u64; |
| 201 | // SAFETY: safe if `self.len() >= 8`. |
| 202 | u64::from_le(unsafe { ptr::read_unaligned(src) }) |
| 203 | } |
| 204 | |
| 205 | /// # Safety |
| 206 | /// |
| 207 | /// Safe if `self.len() >= 8`. |
| 208 | #[inline] |
| 209 | #[allow(clippy::cast_ptr_alignment)] |
| 210 | unsafe fn write_u64(&mut self, value: u64) { |
| 211 | debug_assert!(self.as_ref().len() >= 8); |
| 212 | let dst = self.as_mut().as_mut_ptr() as *mut u64; |
| 213 | // SAFETY: safe if `self.len() >= 8`. |
| 214 | unsafe { ptr::write_unaligned(dst, u64::to_le(value)) }; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | impl ByteSlice for [u8] { |
| 219 | } |
| 220 | |
| 221 | #[inline] |
| 222 | pub fn is_8digits(v: u64) -> bool { |
| 223 | let a = v.wrapping_add(0x4646_4646_4646_4646); |
| 224 | let b = v.wrapping_sub(0x3030_3030_3030_3030); |
| 225 | (a | b) & 0x8080_8080_8080_8080 == 0 |
| 226 | } |
| 227 | |
| 228 | #[inline] |
| 229 | pub fn parse_digits(s: &mut &[u8], mut f: impl FnMut(u8)) { |
| 230 | while let Some(&ch) = s.first() { |
| 231 | let c = ch.wrapping_sub(b'0'); |
| 232 | if c < 10 { |
| 233 | f(c); |
| 234 | *s = s.advance(1); |
| 235 | } else { |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Default)] |
| 242 | pub struct AdjustedMantissa { |
| 243 | pub mantissa: u64, |
| 244 | pub power2: i32, |
| 245 | } |
| 246 | |
| 247 | impl AdjustedMantissa { |
| 248 | #[inline] |
| 249 | pub const fn zero_pow2(power2: i32) -> Self { |
| 250 | Self { |
| 251 | mantissa: 0, |
| 252 | power2, |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | #[cfg(test)] |
| 258 | mod tests { |
| 259 | use super::*; |
| 260 | |
| 261 | #[test] |
| 262 | fn test_read_write_u64() { |
| 263 | let bytes = b"01234567"; |
| 264 | let string = AsciiStr::new(bytes); |
| 265 | let int = string.try_read_u64(); |
| 266 | assert_eq!(int, Some(0x3736353433323130)); |
| 267 | |
| 268 | let int = unsafe { bytes.read_u64() }; |
| 269 | assert_eq!(int, 0x3736353433323130); |
| 270 | |
| 271 | let mut slc = [0u8; 8]; |
| 272 | unsafe { slc.write_u64(0x3736353433323130) }; |
| 273 | assert_eq!(&slc, bytes); |
| 274 | } |
| 275 | } |