Back to News
Advertisement
Advertisement

⚡ Community Insights

Discussion Sentiment

33% Positive

Analyzed from 330 words in the discussion.

Trending Topics

#utf#bom#file#windows#text#unicode#reflect#json#why#boms

Discussion (7 Comments)Read Original on HackerNews

flohofwoeabout 4 hours ago
Ugh, why are BOMs even still a thing in the 21st century? E.g. when will Windows finally arrive in the late 1990s and switch to UTF-8 for everything?

The UTF-8 BOM is especially bizarre because UTF-8 is completely endian-agnostic (so this "UTF-8 Byte Order Mark" is at most an indicator that this file is UTF-8 encoded, but guess what? Outside the Windows bubble, all text files are UTF-8 anyway).

gdwatsonabout 2 hours ago
Windows bought into Unicode hard before Unicode expanded beyond 16 bits. The consequence is that its native strings are theoretically UTF-16, but often behave like UCS-2, allowing random halves of surrogate pairs. Windows APIs reflect that, Windows filenames reflect that, and translation into UTF-8 has to reflect that -- somehow. It's not great.

Personally I'd like a world where Unicode gave up all the compromises that come with supporting UTF-16. But presumably burning all your early adopters is not a winning strategy.

orangepandaabout 3 hours ago
> A byte order mark is a special use of the zero width no-break space character U+FEFF at the beginning of a text file

Isnt BOM allowed to appear anywhere in the file, because of file concatenation?

brewmarcheabout 2 hours ago
No, a ZWNBSP that’s not at the start is just a regular ZWNBSP. Can be used to break up ligatures for example
nvme0n1p1about 3 hours ago
No, it's only allowed at the start. How would that even work? If an app sees a UTF-32 BOM halfway through a file should it interpret the rest of the file as UTF-32? Are there any apps that handle text files like this?
mr_mitmabout 3 hours ago
I hate the BOM so much. It causes so many subtle issues and I don't even understand why it's needed.
remnaviabout 4 hours ago
The csv.DictReader version of this bug is the one that got me. Open a UTF-8-with-BOM CSV as plain utf-8 and your first column name comes back with an invisible U+FEFF glued to the front of it, so every lookup on that one field fails while the other columns behave perfectly. You end up suspecting the source data, the header row, anything except the encoding.

json.loads has a similar tell: a leading BOM gives you "Expecting value: line 1 column 1 (char 0)", which reads like malformed JSON when the JSON is fine.

Same fix, utf-8-sig at the open, and I think the lesson generalises past subtitles: deal with encoding once at the I/O boundary so nothing downstream ever needs to know a BOM existed. Mid-file BOMs like yours are what it looks like when that leaks.