mirror of
https://gitlab.com/famedly/conduit.git
synced 2025-09-05 18:41:00 +00:00
Validate UTF-8 string before heap allocation
This avoids unnecessary heap allocation with invalid strings.
This commit is contained in:
parent
03dfa72b8f
commit
b74fc535c6
1 changed files with 5 additions and 2 deletions
|
@ -67,8 +67,11 @@ pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64, std::array::TryFromSliceError
|
|||
}
|
||||
|
||||
/// Parses the bytes into a string.
|
||||
pub fn string_from_bytes(bytes: &[u8]) -> Result<String, std::string::FromUtf8Error> {
|
||||
String::from_utf8(bytes.to_vec())
|
||||
///
|
||||
/// If `&str` is enough please use [str::from_utf8] to avoid unnecessary
|
||||
/// allocation.
|
||||
pub fn string_from_bytes(bytes: &[u8]) -> Result<String, std::str::Utf8Error> {
|
||||
str::from_utf8(bytes).map(ToOwned::to_owned)
|
||||
}
|
||||
|
||||
pub fn random_string(length: usize) -> String {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue