musli/
str.rs

1//! Functions for working with strings. The exported implementations change
2//! depending on if the `simdutf8` feature is enabled.
3
4#![cfg(any(
5    feature = "storage",
6    feature = "wire",
7    feature = "descriptive",
8    feature = "json",
9    feature = "value"
10))]
11
12#[cfg(feature = "alloc")]
13use rust_alloc::string::String;
14#[cfg(feature = "alloc")]
15use rust_alloc::vec::Vec;
16
17use core::fmt;
18
19#[cfg(not(feature = "simdutf8"))]
20#[doc(inline)]
21pub use core::str::from_utf8;
22
23/// Error raised in case the UTF-8 sequence could not be decoded.
24#[derive(Debug)]
25#[non_exhaustive]
26pub struct Utf8Error;
27
28impl core::error::Error for Utf8Error {}
29
30impl fmt::Display for Utf8Error {
31    #[inline]
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        write!(f, "invalid or incomplete utf-8 sequence")
34    }
35}
36
37/// The same as [`String::from_utf8`], but the implementation can different
38/// depending on if the `simdutf8` feature is enabled.
39///
40/// [`String::from_utf8`]: rust_alloc::string::String::from_utf8
41#[inline(always)]
42#[cfg(all(feature = "alloc", not(feature = "simdutf8")))]
43pub fn from_utf8_owned(bytes: Vec<u8>) -> Result<String, Utf8Error> {
44    match String::from_utf8(bytes) {
45        Ok(string) => Ok(string),
46        Err(..) => Err(Utf8Error),
47    }
48}
49
50/// The same as [`String::from_utf8`], but the implementation can different
51/// depending on if the `simdutf8` feature is enabled.
52///
53/// [`String::from_utf8`]: rust_alloc::string::String::from_utf8
54#[inline(always)]
55#[cfg(all(feature = "alloc", feature = "simdutf8"))]
56pub fn from_utf8_owned(bytes: Vec<u8>) -> Result<String, Utf8Error> {
57    if simdutf8::basic::from_utf8(&bytes).is_err() {
58        return Err(Utf8Error);
59    }
60
61    // SAFETY: String was checked above.
62    Ok(unsafe { String::from_utf8_unchecked(bytes) })
63}
64
65/// Analogue to [`core::str::from_utf8()`].
66///
67/// Checks if the passed byte sequence is valid UTF-8 and returns an
68/// [`std::str`] reference to the passed byte slice wrapped in `Ok()` if it is.
69///
70/// # Errors
71///
72/// Will return the zero-sized Err([`Utf8Error`]) on if the input contains
73/// invalid UTF-8.
74#[inline]
75#[cfg(feature = "simdutf8")]
76pub fn from_utf8(input: &[u8]) -> Result<&str, Utf8Error> {
77    simdutf8::basic::from_utf8(input).map_err(|_| Utf8Error)
78}