|
| 1 | +/// Fallback implementation using the standard library. |
| 2 | +/// |
| 3 | +/// # Errors |
| 4 | +/// Returns the zero-sized [`basic::Utf8Error`] on failure. |
| 5 | +#[inline] |
| 6 | +pub const fn validate_utf8_basic(input: &[u8]) -> Result<(), crate::basic::Utf8Error> { |
| 7 | + match core::str::from_utf8(input) { |
| 8 | + Ok(_) => Ok(()), |
| 9 | + Err(_) => Err(crate::basic::Utf8Error {}), |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +/// Fallback implementation using the standard library. |
| 14 | +/// |
| 15 | +/// # Errors |
| 16 | +/// Returns [`compat::Utf8Error`] with detailed error information on failure. |
| 17 | +#[inline] |
| 18 | +pub fn validate_utf8_compat(input: &[u8]) -> Result<(), crate::compat::Utf8Error> { |
| 19 | + super::validate_utf8_at_offset(input, 0) |
| 20 | +} |
| 21 | + |
| 22 | +/// Low-level implementation of the [`basic::imp::Utf8Validator`] trait. |
| 23 | +/// |
| 24 | +/// This is implementation requires CPU SIMD features specified by the module it resides in. |
| 25 | +/// It is undefined behavior to call it if the required CPU features are not |
| 26 | +/// available. |
| 27 | +#[cfg(feature = "public_imp")] |
| 28 | +pub struct Utf8ValidatorImp { |
| 29 | + incomplete_data: [u8; 4], |
| 30 | + incomplete_len: u8, |
| 31 | + err: bool, |
| 32 | +} |
| 33 | + |
| 34 | +use core::panic; |
| 35 | + |
| 36 | +#[cfg(feature = "public_imp")] |
| 37 | +pub use Utf8ValidatorImp as ChunkedUtf8ValidatorImp; |
| 38 | + |
| 39 | +#[cfg(feature = "public_imp")] |
| 40 | +impl Utf8ValidatorImp { |
| 41 | + #[inline] |
| 42 | + #[expect(clippy::cast_possible_truncation)] |
| 43 | + fn update(&mut self, mut input: &[u8]) { |
| 44 | + if self.err { |
| 45 | + return; |
| 46 | + } |
| 47 | + if self.incomplete_len > 0 { |
| 48 | + let total_bytes_needed: usize = match self.incomplete_data[0] { |
| 49 | + 0..0b1000_0000 => { |
| 50 | + panic!("ASCII data should never be incomplete"); |
| 51 | + } |
| 52 | + 0b1000_0000..0b1100_0000 => { |
| 53 | + // first byte cannot be a continuation byte |
| 54 | + self.err = true; |
| 55 | + return; |
| 56 | + } |
| 57 | + 0b1100_0000..0b1110_0000 => 2, |
| 58 | + 0b1110_0000..0b1111_0000 => 3, |
| 59 | + 0b1111_0000..0b1111_1000 => 4, |
| 60 | + _ => { |
| 61 | + // invalid byte for starting sequence |
| 62 | + self.err = true; |
| 63 | + return; |
| 64 | + } |
| 65 | + }; |
| 66 | + if self.incomplete_len as usize >= total_bytes_needed { |
| 67 | + // actually errored on previous update |
| 68 | + self.err = true; |
| 69 | + return; |
| 70 | + } |
| 71 | + let bytes_needed = total_bytes_needed - self.incomplete_len as usize; |
| 72 | + let to_copy = core::cmp::min(bytes_needed, input.len()); |
| 73 | + self.incomplete_data |
| 74 | + [self.incomplete_len as usize..self.incomplete_len as usize + to_copy] |
| 75 | + .copy_from_slice(&input[..to_copy]); |
| 76 | + if to_copy < bytes_needed { |
| 77 | + self.incomplete_len += to_copy as u8; |
| 78 | + return; |
| 79 | + } |
| 80 | + if core::str::from_utf8(&self.incomplete_data[..total_bytes_needed]).is_err() { |
| 81 | + self.err = true; |
| 82 | + return; |
| 83 | + } |
| 84 | + self.incomplete_len = 0; |
| 85 | + input = &input[to_copy..]; |
| 86 | + } |
| 87 | + if let Err(e) = core::str::from_utf8(input) { |
| 88 | + if input.len() - e.valid_up_to() > 3 { |
| 89 | + self.err = true; |
| 90 | + return; |
| 91 | + } |
| 92 | + self.incomplete_len = (input.len() - e.valid_up_to()) as u8; |
| 93 | + self.incomplete_data[..self.incomplete_len as usize] |
| 94 | + .copy_from_slice(&input[e.valid_up_to()..]); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + #[inline] |
| 99 | + const fn finalize(self) -> core::result::Result<(), crate::basic::Utf8Error> { |
| 100 | + if self.err || self.incomplete_len > 0 { |
| 101 | + Err(crate::basic::Utf8Error {}) |
| 102 | + } else { |
| 103 | + Ok(()) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#[cfg(feature = "public_imp")] |
| 109 | +impl crate::basic::imp::Utf8Validator for Utf8ValidatorImp { |
| 110 | + #[inline] |
| 111 | + #[must_use] |
| 112 | + fn new() -> Self { |
| 113 | + Self { |
| 114 | + incomplete_data: [0; 4], |
| 115 | + incomplete_len: 0, |
| 116 | + err: false, |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + #[inline] |
| 121 | + fn update(&mut self, input: &[u8]) { |
| 122 | + if input.is_empty() { |
| 123 | + return; |
| 124 | + } |
| 125 | + self.update(input); |
| 126 | + } |
| 127 | + |
| 128 | + #[inline] |
| 129 | + fn finalize(self) -> core::result::Result<(), crate::basic::Utf8Error> { |
| 130 | + self.finalize() |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +#[cfg(feature = "public_imp")] |
| 135 | +impl crate::basic::imp::ChunkedUtf8Validator for Utf8ValidatorImp { |
| 136 | + #[inline] |
| 137 | + #[must_use] |
| 138 | + fn new() -> Self { |
| 139 | + Self { |
| 140 | + incomplete_data: [0; 4], |
| 141 | + incomplete_len: 0, |
| 142 | + err: false, |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + #[inline] |
| 147 | + fn update_from_chunks(&mut self, input: &[u8]) { |
| 148 | + self.update(input); |
| 149 | + } |
| 150 | + |
| 151 | + #[inline] |
| 152 | + fn finalize( |
| 153 | + mut self, |
| 154 | + remaining_input: core::option::Option<&[u8]>, |
| 155 | + ) -> core::result::Result<(), crate::basic::Utf8Error> { |
| 156 | + if let Some(remaining_input) = remaining_input { |
| 157 | + self.update(remaining_input); |
| 158 | + } |
| 159 | + self.finalize() |
| 160 | + } |
| 161 | +} |
0 commit comments