fix(preview): decode UTF-8 runes in IsBufferPrintable text detection#1507
fix(preview): decode UTF-8 runes in IsBufferPrintable text detection#1507KristianHolme wants to merge 3 commits into
Conversation
IsTextFile() reads the first 1024 bytes and calls IsBufferPrintable(). The previous byte-wise check treated UTF-8 continuation bytes (0x80-0xBF) as C1 control characters, causing Slurm *_err logs with Julia stderr output (e.g. precompile checkmarks) to be rejected as non-text. Decode runes with utf8.DecodeRune and test printability per rune. Allow an incomplete UTF-8 sequence at the end of the buffer (fixed-size reads). Add unit tests for UTF-8 samples and a Slurm err log fixture. Co-authored-by: Kristian Holme <KristianHolme@users.noreply.github.com>
Co-authored-by: Kristian Holme <KristianHolme@users.noreply.github.com>
|
🎉 Thank you for your first contribution to superfile! We’re really excited to have you here 🙌 A maintainer might ask you to make a few changes before we can merge this PR. 👉 Please also take a moment to review our Contribution Guide If you have any questions, feel free to open a Discussion or just ask in the comments! |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe buffer printability check now scans UTF-8 runes instead of raw bytes, and the tests expand coverage for Unicode, invalid UTF-8, truncated sequences, and text-file detection. ChangesPrintability logic and tests
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/internal/common/string_function.go`:
- Around line 205-208: The UTF-8 detection branch in the function that calls
utf8.DecodeRune is treating any utf8.RuneError as invalid, which incorrectly
rejects valid U+FFFD text. Update this check to inspect the returned size from
utf8.DecodeRune and only return false when the result is utf8.RuneError with n
== 1, so the replacement character itself is accepted while malformed UTF-8 is
still rejected.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b2112447-4eaf-4f84-8907-6e4fe752735f
📒 Files selected for processing (2)
src/internal/common/string_function.gosrc/internal/common/string_function_test.go
utf8.RuneError equals U+FFFD, so decoding a valid replacement character returns r == utf8.RuneError with n == 3. Only reject malformed UTF-8 when n == 1. Co-authored-by: Kristian Holme <KristianHolme@users.noreply.github.com>
Had some issues with some text files with special characters not being able to preview. This fixes it. Hope it can be useful :)
🤖 Written with Cursor.
Description
Fix text preview failing on UTF-8 text files without a recognized extension.
IsTextFile()reads the first 1024 bytes and callsIsBufferPrintable(). The previous implementation checked each byte withunicode.IsPrint(rune(b)), which misclassifies UTF-8 continuation bytes (0x80–0xBF) as C1 control characters. Files containing multi-byte Unicode early in the buffer (e.g.✓, box-drawing characters) were incorrectly rejected as non-text and showed "unsupported format" in the preview panel.IsBufferPrintable()now decodes UTF-8 runes withutf8.DecodeRuneand checks printability per rune. Incomplete UTF-8 at the end of the buffer is allowed viautf8.FullRune(relevant for the fixed 1024-byte read inIsTextFile()). Binary content with NUL bytes is still rejected.Related Issues
✅ Pre-Submission Checklist
go fmt ./...to format the codegolangci-lint runand fixed any reported issuesSummary by CodeRabbit