musli_common/str.rs
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
//! Functions for working with strings. The exported implementations change
//! depending on if the `simdutf8` feature is enabled.
#[cfg(feature = "alloc")]
use alloc::string::String;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::fmt;
#[cfg(not(feature = "simdutf8"))]
#[doc(inline)]
pub use core::str::from_utf8;
#[cfg(feature = "simdutf8")]
#[doc(inline)]
pub use simdutf8::basic::from_utf8;
/// Error raised in case the UTF-8 sequence could not be decoded.
#[non_exhaustive]
#[derive(Debug)]
pub struct Utf8Error;
#[cfg(feature = "std")]
impl std::error::Error for Utf8Error {}
impl fmt::Display for Utf8Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid or incomplete utf-8 sequence")
}
}
/// The same as [`String::from_utf8`], but the implementation can different
/// depending on if the `simdutf8` feature is enabled.
///
/// [`String::from_utf8`]: alloc::string::String::from_utf8
#[inline(always)]
#[cfg(all(feature = "alloc", not(feature = "simdutf8")))]
pub fn from_utf8_owned(bytes: Vec<u8>) -> Result<String, Utf8Error> {
match String::from_utf8(bytes) {
Ok(string) => Ok(string),
Err(..) => Err(Utf8Error),
}
}
/// The same as [`String::from_utf8`], but the implementation can different
/// depending on if the `simdutf8` feature is enabled.
///
/// [`String::from_utf8`]: alloc::string::String::from_utf8
#[inline(always)]
#[cfg(all(feature = "alloc", feature = "simdutf8"))]
pub fn from_utf8_owned(bytes: Vec<u8>) -> Result<String, Utf8Error> {
if from_utf8(&bytes).is_err() {
return Err(Utf8Error);
}
// SAFETY: String was checked above.
Ok(unsafe { String::from_utf8_unchecked(bytes) })
}