diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2021-08-16 09:28:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-16 09:28:29 +0200 |
commit | 163f2ef57117afd410f03be7e7519fc89bd18173 (patch) | |
tree | 03080b2306927efbb2af64869b418c009abd92f5 /cli/text_encoding.rs | |
parent | 02b23e057561f11daf004b4ed8d455081dbeac65 (diff) |
fix: parse error when transpiling code with BOM (#11688)
Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'cli/text_encoding.rs')
-rw-r--r-- | cli/text_encoding.rs | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/cli/text_encoding.rs b/cli/text_encoding.rs index 8d316909c..f61b877dc 100644 --- a/cli/text_encoding.rs +++ b/cli/text_encoding.rs @@ -6,6 +6,8 @@ use std::{ io::{Error, ErrorKind}, }; +pub const BOM_CHAR: char = '\u{FEFF}'; + /// Attempts to detect the character encoding of the provided bytes. /// /// Supports UTF-8, UTF-16 Little Endian and UTF-16 Big Endian. @@ -43,6 +45,15 @@ pub fn convert_to_utf8<'a>( } } +/// Strips the byte order mark from the provided text if it exists. +pub fn strip_bom(text: &str) -> &str { + if text.starts_with(BOM_CHAR) { + &text[BOM_CHAR.len_utf8()..] + } else { + text + } +} + #[cfg(test)] mod tests { use super::*; |