Skip to content

Added std::string::String support #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,41 @@ pub fn from_utf8(input: &[u8]) -> Result<&str, Utf8Error> {
Ok(from_utf8_unchecked(input))
}
}
/// for validating owned bytes sequences of UTF-8
pub mod string {
pub use super::*;


/// Simple UTF-8 error containing the invalid utf8 bytes.
///
/// No information is provided where the error occurred or how long the invalid byte
/// byte sequence is.
#[allow(missing_docs)]
#[derive(Debug, PartialEq, Eq)]
pub struct FromUtf8Error {
pub bytes: Vec<u8>,
error: Utf8Error,
}

/// Analogue to [`String::from_utf8()`].
///
/// Checks if the passed byte sequence is valid UTF-8 and returns a
/// [`String`] with taking ownership of the the passed byte slice wrapped in `Ok()` if it is.
///
/// # Errors
/// Will return an Err([`FromUtf8Error`])
/// containing the original bytes passed in along with the zero-sized [`Utf8Error`]
/// if the input contains invalid UTF-8.
#[inline]
pub fn from_utf8(input: Vec<u8>) -> Result<String, FromUtf8Error> {
unsafe {
match validate_utf8_basic(&input) {
Ok(()) => Ok(String::from_utf8_unchecked(input)),
Err(Utf8Error) => Err(FromUtf8Error {bytes:input, error: Utf8Error})
}
}
}
}

/// Analogue to [`std::str::from_utf8_mut()`].
///
Expand Down
37 changes: 37 additions & 0 deletions src/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,43 @@ pub fn from_utf8(input: &[u8]) -> Result<&str, Utf8Error> {
}
}

/// for validating owned bytes sequences of UTF-8
pub mod string {
pub use super::*;


/// Simple UTF-8 error containing the invalid utf8 bytes.
///
/// Contains information on the location of the encountered validation error and the length of the
/// invalid UTF-8 sequence.
#[allow(missing_docs)]
#[derive(Debug, PartialEq, Eq)]
pub struct FromUtf8Error {
pub bytes: Vec<u8>,
error: Utf8Error,
}

/// Analogue to [`String::from_utf8()`].
///
/// Checks if the passed byte sequence is valid UTF-8 and returns a
/// [`String`] with taking ownership of the the passed byte slice wrapped in `Ok()` if it is.
///
/// # Errors
/// Will return an Err([`FromUtf8Error`])
/// containing the original bytes passed in along with a [`Utf8Error`]
/// on if the input contains invalid UTF-8 with detailed error information.
/// if the input contains invalid UTF-8.
#[inline]
pub fn from_utf8(input: Vec<u8>) -> Result<String, FromUtf8Error> {
unsafe {
match validate_utf8_compat(&input) {
Ok(()) => Ok(String::from_utf8_unchecked(input)),
Err(err) => Err(FromUtf8Error {bytes:input, error: err})
}
}
}
}

/// Analogue to [`std::str::from_utf8_mut()`].
///
/// Checks if the passed mutable byte sequence is valid UTF-8 and returns a mutable
Expand Down