Non-UTF-8 documents open with replacement characters and are destroyed on save
Describe the bug
A document in a legacy encoding (GBK, Big5, Shift-JIS, CP-1252 …) can be opened, is shown full of U+FFFD replacement characters, and saving it writes that back — permanently destroying the original. U+FFFD is not reversible; the original bytes are gone.
Whether it happens at all currently depends on the file's size, because the read paths use two different decoders:
| File | Decoder | Result |
|---|---|---|
| ≤ 50 KB | read_to_string (strict) |
fails to open |
| > 50 KB | from_utf8_lossy (truncated-preview branch) |
opens as mojibake, saveable |
To Reproduce
- Create a
.mdlarger than 50 KB and save it as GBK (or Big5 / Shift-JIS) - Open it in Markpad — the text is replacement characters
- Type anything, or let auto-save fire
- The file on disk is now U+FFFD; the original content cannot be recovered
Expected behavior
What the editors people compare this with do — VS Code, Sublime Text and Typora all detect the encoding on open and preserve it on save, with a status-bar control to reinterpret or convert. None of them decode lossily and then write the loss back.
Doing that properly needs an encoding-detection dependency (encoding_rs is the usual choice in Rust), plus a place in the UI to show and change the detected encoding. That is a feature, and it needs your call on both the dependency and the UI surface — hence this issue rather than a PR.
Interim
A separate PR refuses to save a buffer that was decoded lossily and points the user at Save As, so the file can be read and its content copied out but never silently overwritten. That closes the data-loss path without deciding the encoding question. It is deliberately a guard, not a feature — if you would rather have the full detection story instead, say so and the guard can be dropped.
Device
- Logic is platform-independent; reproduced on macOS.
- App version: current
master(a291cda)