@setoelkahfi / svara / commits / 69b0333

feat: autodetect utf16 endianess

mellbacon committed Aug 13, 2023 at 16:40 UTC 69b03334e270ec4c14faa9771189d7d9f7c1a8a4
1 file changed +22 -12
src-tauri/src/encoding.rs
+22 -12
@@ -8,22 +8,32 @@ pub enum BOM {
8 pub fn convert_to_u16(str: &str, bom: BOM) -> Vec<u8> {
9 let mut bytes = vec![];
10
11 - if bom == BOM::BigEndian {
12 - bytes = core::iter::once(0xFEFF)
13 - .chain(str.encode_utf16())
11 + if bom == BOM::BigEndian {
12 + bytes = core::iter::once(0xFEFF)
13 + .chain(str.encode_utf16())
14 + .flat_map(|word| word.to_be_bytes())
15 + .collect::<Vec<_>>();
16 + }
17 + else if bom == BOM::LittleEndian {
18 + bytes = core::iter::once(0xFFFE)
19 + .chain(str.encode_utf16())
20 + .flat_map(|word| word.to_le_bytes())
21 + .collect::<Vec<_>>();
22 + }
23 + else if bom == BOM::None {
24 + let utf16 = str.encode_utf16();
25 +
26 + // autodetect endianess
27 + if cfg!(target_endian = "big") {
28 + bytes = utf16
29 .flat_map(|word| word.to_be_bytes())
30 .collect::<Vec<_>>();
31 }
17 - else if bom == BOM::LittleEndian {
18 - bytes = core::iter::once(0xFFFE)
19 - .chain(str.encode_utf16())
32 + else {
33 + bytes = utf16
34 .flat_map(|word| word.to_le_bytes())
35 .collect::<Vec<_>>();
36 }
23 - else if bom == BOM::None {
24 - bytes = str.encode_utf16()
25 - .flat_map(|word| word.to_be_bytes())
26 - .collect::<Vec<_>>();
27 - }
37 + }
38 bytes
29 -}
\ No newline at end of file
39 +}