1
0
Fork 0
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:
Ossi Herrala 2025-08-20 23:07:57 +03:00
parent 03dfa72b8f
commit b74fc535c6
No known key found for this signature in database

View file

@ -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 {