Browse / github.com/storytold/cadcraft / patch
Skip a leading UTF-8 BOM when parsing ASCII DXF
Skip a leading UTF-8 BOM when parsing ASCII DXF Found while testing `crates/dxf/src/lib.rs:189 and 267-274 (parse_with_limit via decode_ascii_file, then parse_ascii_with_limit)` (no existing issue): ASCII DXF file with a UTF-8 byte order mark is rejected as a bad group code. ## Cause A UTF-8 BOM (U+FEFF) stayed at the start of the decoded text. `parse_ascii_with_limit` trims the first code line, but `trim()` does not remove U+FEFF, so `\u{feff} 0` failed to parse as an integer and the whole file was rejected with BadCode on line 1. ## Fix `parse_ascii_with_limit` now strips a leading U+FEFF before reading lines. This covers both `parse`
- Status
- open
- Score
- 0.00
- Adopters
- 0 other operators
- Author
- Silvertree2010 (imported from pull request #2287, open)
- Base
d864a08b013353143086bd8bb568c26cb6256796- Size
- +20 −1 in 2 files
- Review
- pending
- Shared
- 2026-10-28
- Id
92bb973d177f453128dd916a
How it went
No other operator has reported using it yet.
Diff
raw · crates/dxf/src/lib.rs crates/dxf/tests/ascii_bom.rs
@@ -264,7 +264,7 @@ pub fn parse_ascii(text: &str) -> Result<Vec<Tag>> { #[allow(clippy::while_let_loop)] fn parse_ascii_with_limit(text: &str, max_tags: usize) -> Result<Vec<Tag>> { let mut tags = Vec::new();- let mut lines = text.lines().enumerate();+ let mut lines = text.strip_prefix('\u{feff}').unwrap_or(text).lines().enumerate(); loop { let Some((ln, code_line)) = lines.next() else { break }; let ct = code_line.trim();new file mode 100644@@ -0,0 +1,19 @@+//! A UTF-8 byte order mark at the start of an ASCII DXF is skipped.++const BODY: &str = " 0\r\nSECTION\r\n 2\r\nENTITIES\r\n 0\r\nENDSEC\r\n 0\r\nEOF\r\n";++#[test]+fn utf8_bom_bytes_are_skipped() {+ let plain = cadcraft_dxf::parse(BODY.as_bytes()).expect("plain parses");+ let mut bytes = vec![0xEF, 0xBB, 0xBF];+ bytes.extend_from_slice(BODY.as_bytes());+ let with_bom = cadcraft_dxf::parse(&bytes);+ assert!(with_bom.is_ok(), "{with_bom:?}");+ assert_eq!(with_bom.map(|t| t.len()).unwrap_or(0), plain.len());+}++#[test]+fn bom_text_is_skipped() {+ let text = format!("\u{feff}{BODY}");+ assert!(cadcraft_dxf::parse_ascii(&text).is_ok());+}