diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/os_str_bytes/src/wasm | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/os_str_bytes/src/wasm')
-rw-r--r-- | third_party/rust/os_str_bytes/src/wasm/mod.rs | 58 | ||||
-rw-r--r-- | third_party/rust/os_str_bytes/src/wasm/raw.rs | 34 |
2 files changed, 92 insertions, 0 deletions
diff --git a/third_party/rust/os_str_bytes/src/wasm/mod.rs b/third_party/rust/os_str_bytes/src/wasm/mod.rs new file mode 100644 index 0000000000..a8a2996018 --- /dev/null +++ b/third_party/rust/os_str_bytes/src/wasm/mod.rs @@ -0,0 +1,58 @@ +use std::borrow::Cow; +use std::error::Error; +use std::ffi::OsStr; +use std::ffi::OsString; +use std::fmt; +use std::fmt::Display; +use std::fmt::Formatter; +use std::result; +use std::str; +use std::str::Utf8Error; + +if_raw_str! { + pub(super) mod raw; +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(super) struct EncodingError(Utf8Error); + +impl Display for EncodingError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "os_str_bytes: {}", self.0) + } +} + +impl Error for EncodingError {} + +type Result<T> = result::Result<T, EncodingError>; + +macro_rules! expect_utf8 { + ( $result:expr ) => { + $result.expect( + "platform string contains invalid UTF-8, which should not be \ + possible", + ) + }; +} + +fn from_bytes(string: &[u8]) -> Result<&str> { + str::from_utf8(string).map_err(EncodingError) +} + +pub(super) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> { + from_bytes(string).map(|x| Cow::Borrowed(OsStr::new(x))) +} + +pub(super) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> { + Cow::Borrowed(expect_utf8!(os_string.to_str()).as_bytes()) +} + +pub(super) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> { + String::from_utf8(string) + .map(Into::into) + .map_err(|x| EncodingError(x.utf8_error())) +} + +pub(super) fn os_string_into_vec(os_string: OsString) -> Vec<u8> { + expect_utf8!(os_string.into_string()).into_bytes() +} diff --git a/third_party/rust/os_str_bytes/src/wasm/raw.rs b/third_party/rust/os_str_bytes/src/wasm/raw.rs new file mode 100644 index 0000000000..fb291a65fa --- /dev/null +++ b/third_party/rust/os_str_bytes/src/wasm/raw.rs @@ -0,0 +1,34 @@ +use std::fmt; +use std::fmt::Formatter; +use std::str; + +pub(crate) use crate::util::is_continuation; + +use super::Result; + +#[allow(dead_code)] +#[path = "../common/raw.rs"] +mod common_raw; +pub(crate) use common_raw::ends_with; +pub(crate) use common_raw::starts_with; +#[cfg(feature = "uniquote")] +pub(crate) use common_raw::uniquote; + +pub(crate) fn validate_bytes(string: &[u8]) -> Result<()> { + super::from_bytes(string).map(drop) +} + +pub(crate) fn decode_code_point(string: &[u8]) -> u32 { + let string = expect_encoded!(str::from_utf8(string)); + let mut chars = string.chars(); + let ch = chars + .next() + .expect("cannot parse code point from empty string"); + assert_eq!(None, chars.next(), "multiple code points found"); + ch.into() +} + +pub(crate) fn debug(string: &[u8], _: &mut Formatter<'_>) -> fmt::Result { + assert!(string.is_empty()); + Ok(()) +} |