From dd5ddbfc4311b64b0b51578e53a0838cc84b8d6f Mon Sep 17 00:00:00 2001 From: R Aadarsh Date: Sun, 7 Sep 2025 11:07:19 +0530 Subject: [PATCH] - Remove unnecessary calls to `collect` - Simplify UTF-16 BOM detection Co-authored-by: CrazyboyQCD Release Notes: - Add support for opening and saving files in different encodings and a setting to enable or disable the indicator --- crates/fs/src/encodings.rs | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/crates/fs/src/encodings.rs b/crates/fs/src/encodings.rs index 4d11b600f72136bdaa147bbee37ddc6e2e965975..c6c13f64d2e6b2b5db7897eff87fe685880c95a5 100644 --- a/crates/fs/src/encodings.rs +++ b/crates/fs/src/encodings.rs @@ -54,22 +54,16 @@ impl EncodingWrapper { // Check if the input starts with a BOM for UTF-16 encodings only if not forced to // use the encoding specified. if !force { - if input.len() >= 2 { - if (input[0] == 0xFF) & (input[1] == 0xFE) { - self.0 = encoding_rs::UTF_16LE; - - if let Some(v) = buffer_encoding { - if let Ok(mut v) = (*v).lock() { - *v = encoding_rs::UTF_16LE; - } - } - } else if (input.len() >= 2) & (input[0] == 0xFE) & (input[1] == 0xFF) { - self.0 = encoding_rs::UTF_16BE; - - if let Some(v) = buffer_encoding { - if let Ok(mut v) = (*v).lock() { - *v = encoding_rs::UTF_16BE; - } + if let Some(encoding) = match input.get(..2) { + Some([0xFF, 0xFE]) => Some(encoding_rs::UTF_16LE), + Some([0xFE, 0xFF]) => Some(encoding_rs::UTF_16BE), + _ => None, + } { + self.0 = encoding; + + if let Some(v) = buffer_encoding { + if let Ok(mut v) = (*v).lock() { + *v = encoding; } } } @@ -88,8 +82,7 @@ impl EncodingWrapper { let mut data = Vec::::with_capacity(input.len() * 2); // Convert the input string to UTF-16BE bytes - let utf16be_bytes: Vec = - input.encode_utf16().flat_map(|u| u.to_be_bytes()).collect(); + let utf16be_bytes = input.encode_utf16().flat_map(|u| u.to_be_bytes()); data.extend(utf16be_bytes); return Ok(data); @@ -97,8 +90,7 @@ impl EncodingWrapper { let mut data = Vec::::with_capacity(input.len() * 2); // Convert the input string to UTF-16LE bytes - let utf16le_bytes: Vec = - input.encode_utf16().flat_map(|u| u.to_le_bytes()).collect(); + let utf16le_bytes = input.encode_utf16().flat_map(|u| u.to_le_bytes()); data.extend(utf16le_bytes); return Ok(data);