1use std::mem::ManuallyDrop;
2
3use crate::JsString;
4
5#[cfg(feature = "latin1")]
6use crate::Result;
7
8pub struct JsStringLatin1 {
9 pub(crate) inner: JsString,
10 pub(crate) buf: ManuallyDrop<Vec<u8>>,
11}
12
13impl JsStringLatin1 {
14 pub fn as_slice(&self) -> &[u8] {
15 &self.buf
16 }
17
18 pub fn len(&self) -> usize {
19 self.buf.len()
20 }
21
22 pub fn is_empty(&self) -> bool {
23 self.buf.is_empty()
24 }
25
26 pub fn take(self) -> Vec<u8> {
27 self.as_slice().to_vec()
28 }
29
30 pub fn into_value(self) -> JsString {
31 self.inner
32 }
33
34 #[cfg(feature = "latin1")]
35 pub fn into_latin1_string(self) -> Result<String> {
36 let mut dst_str = unsafe { String::from_utf8_unchecked(vec![0; self.len() * 2 + 1]) };
37 encoding_rs::mem::convert_latin1_to_str(self.buf.as_slice(), dst_str.as_mut_str());
38 Ok(dst_str)
39 }
40}
41
42impl From<JsStringLatin1> for Vec<u8> {
43 fn from(value: JsStringLatin1) -> Self {
44 value.take()
45 }
46}
47