1
0
Fork 0
mirror of https://github.com/azalea-rs/simdnbt.git synced 2025-08-02 07:26:04 +00:00

don't panic on invalid mutf8

This commit is contained in:
mat 2024-03-16 22:23:09 -05:00
parent e16aeb03e2
commit 860d6e1b3a

View file

@ -104,9 +104,10 @@ impl Mutf8Str {
// SAFETY: &[u8] and &str are the same layout.
unsafe { Cow::Borrowed(std::str::from_utf8_unchecked(&self.slice)) }
} else {
match mutf8::decode(&self.slice).expect("Mutf8Str must alwaus be valid MUTF-8") {
Cow::Borrowed(b) => Cow::Borrowed(b),
Cow::Owned(o) => Cow::Owned(o),
match mutf8::decode(&self.slice) {
Ok(Cow::Borrowed(b)) => Cow::Borrowed(b),
Ok(Cow::Owned(o)) => Cow::Owned(o),
Err(_) => Cow::Borrowed(""),
}
}
}
@ -173,9 +174,10 @@ impl Mutf8String {
// SAFETY: &[u8] and &str are the same layout.
unsafe { String::from_utf8_unchecked(self.vec) }
} else {
match mutf8::decode(&self.vec).expect("Mutf8Str must alwaus be valid MUTF-8") {
Cow::Borrowed(b) => b.to_owned(),
Cow::Owned(o) => o,
match mutf8::decode(&self.vec) {
Ok(Cow::Borrowed(b)) => b.to_owned(),
Ok(Cow::Owned(o)) => o,
Err(_) => String::new(),
}
}
}