Skip to content

fix(preview): decode UTF-8 runes in IsBufferPrintable text detection#1507

Open
KristianHolme wants to merge 3 commits into
yorukot:mainfrom
KristianHolme:cursor/fix-utf8-text-preview-be6e
Open

fix(preview): decode UTF-8 runes in IsBufferPrintable text detection#1507
KristianHolme wants to merge 3 commits into
yorukot:mainfrom
KristianHolme:cursor/fix-utf8-text-preview-be6e

Conversation

@KristianHolme

@KristianHolme KristianHolme commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

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 calls IsBufferPrintable(). The previous implementation checked each byte with unicode.IsPrint(rune(b)), which misclassifies UTF-8 continuation bytes (0x800xBF) 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 with utf8.DecodeRune and checks printability per rune. Incomplete UTF-8 at the end of the buffer is allowed via utf8.FullRune (relevant for the fixed 1024-byte read in IsTextFile()). Binary content with NUL bytes is still rejected.

Related Issues


✅ Pre-Submission Checklist

  • I have run go fmt ./... to format the code
  • I have run golangci-lint run and fixed any reported issues
  • I have tested my changes and verified they work as expected
  • I have reviewed the diff to make sure I’m not committing any debug logs or TODOs
  • I have checked that the PR title follows the Conventional Commits format

Summary by CodeRabbit

  • Bug Fixes
    • Improved text detection so valid UTF-8 text with international characters, symbols, and whitespace is handled correctly.
    • Reduced false negatives for strings that end with an incomplete UTF-8 sequence in fixed-size buffers.
    • Better handling of invalid control bytes and malformed UTF-8, while continuing to reject clearly non-text content such as binary data with NUL bytes.

cursoragent and others added 2 commits July 1, 2026 12:22
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>
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown

🎉 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.
That’s totally normal and part of the process. Don’t worry, we’ll help guide you through it.

👉 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!

@github-actions github-actions Bot added awaiting pr review test related PR / Issue related to testcases or testing in general. labels Jul 1, 2026
@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dabc7d01-8ee1-4791-88ba-7b7f5865c9dd

📥 Commits

Reviewing files that changed from the base of the PR and between f97cf99 and 3d71c10.

📒 Files selected for processing (2)
  • src/internal/common/string_function.go
  • src/internal/common/string_function_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/internal/common/string_function.go
  • src/internal/common/string_function_test.go

📝 Walkthrough

Walkthrough

The 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.

Changes

Printability logic and tests

Layer / File(s) Summary
Rune-aware printability implementation
src/internal/common/string_function.go
IsBufferPrintable now decodes UTF-8 runes, rejects invalid single-byte decode errors, stops on incomplete trailing sequences, and checks unicode.IsPrint/unicode.IsSpace on decoded runes.
Updated printability test cases and new IsTextFile tests
src/internal/common/string_function_test.go
Adds require import, updates TestIsBufferPrintable with explicit byte/Unicode/truncated-UTF-8 cases, and adds TestIsTextFile covering UTF-8 text and NUL-containing binary files.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: bug fixes

Suggested reviewers: yorukot, lazysegtree

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: UTF-8 rune decoding in IsBufferPrintable for text detection.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0a4336a and f97cf99.

📒 Files selected for processing (2)
  • src/internal/common/string_function.go
  • src/internal/common/string_function_test.go

Comment thread src/internal/common/string_function.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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting pr review test related PR / Issue related to testcases or testing in general.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants