Skip to content

Fix the first line of UTF-16BE input swallowing the second line - #4076

Merged
keith-hall merged 2 commits into
sharkdp:masterfrom
cindykrafft:fix/utf16be-first-line
Oct 17, 2026
Merged

keith-hall merged 2 commits into
sharkdp:masterfrom
cindykrafft:fix/utf16be-first-line

Conversation

@cindykrafft

Copy link
Copy Markdown

UTF-16BE files (with a BOM) of two or more lines show their first two lines as one: line numbers are off from line 2 on, --line-range picks the wrong lines and the first two lines are highlighted together.

$ printf '\xfe\xff' > be.txt; printf 'line1\nline2\nline3\n' | iconv -t utf-16be >> be.txt
$ bat --decorations=always --style=numbers --color=never be.txt
   1 line1
line2
   2 line3

InputReader::try_new (src/input.rs) first reads up to a \n byte. In UTF-16BE a newline is 00 0A, so that read usually already ends exactly at the end of line 1, but the BE branch added in #3369 then always calls read_utf16_line, which reads on to the next newline. The extra read is only needed when the 0x0A byte belonged to another character (e.g. U+4E0A, as in test_UTF-16BE-complicated.txt), which is why the existing tests don't catch it: utf16be uses that fixture, and utf16 runs test_UTF-16BE.txt with --plain, where a merged line prints the same text.

The fix skips the extra read when the first read already ended on a complete 00 0A (even length). UTF-16LE is unaffected.

Tests: new utf16be_first_line_not_merged_with_second in tests/integration_tests.rs fails on master and passes with the change; cargo test, cargo fmt --check and cargo clippy --all-targets are clean. Open PR #3763 also edits try_new (first-line reading for #2262) but leaves the UTF-16 branches alone, so the two may need a trivial rebase.

Disclosure: this was found, reproduced and patched by an AI agent (Claude Code), which ran the reproduction and tests quoted above. It has not been separately reviewed by a human.

claude and others added 2 commits October 16, 2026 19:50
In UTF-16BE a newline is 00 0A, so the initial read_until(b'\n') usually
already ends exactly at the end of line 1. The BE branch then always called
read_utf16_line, which read on to the next newline, merging lines 1 and 2.
Only keep reading when the first read did not end on a complete 00 0A.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo

@keith-hall keith-hall left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice, thanks

@keith-hall
keith-hall enabled auto-merge October 17, 2026 11:29
@keith-hall
keith-hall merged commit dbe6ec2 into sharkdp:master Oct 17, 2026
23 of 24 checks passed
cindykrafft pushed a commit to cindykrafft/bat that referenced this pull request Oct 20, 2026
Resolve the CHANGELOG conflict by keeping the new sharkdp#4091 entry along
with the filled-in sharkdp#4076 and sharkdp#4084 entries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
cindykrafft pushed a commit to cindykrafft/bat that referenced this pull request Nov 2, 2026
Resolve the CHANGELOG conflict with sharkdp#4102 and sharkdp#4103: keep their new
entries, keep this PR's sharkdp#4084 entry, and point the UTF-16BE entry back
at sharkdp#4076 (the merge of sharkdp#4102 had filled its placeholder with sharkdp#4102).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
cindykrafft pushed a commit to cindykrafft/bat that referenced this pull request Nov 2, 2026
Brings in sharkdp#4038, sharkdp#4048 and sharkdp#4070. The merge of sharkdp#4048 re-added the old
UTF-16BE CHANGELOG entry with its "#XXXX" placeholder at the top of
Bugfixes, so drop that duplicate; the entry this PR corrects to sharkdp#4076
stays.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
cindykrafft pushed a commit to cindykrafft/bat that referenced this pull request Nov 4, 2026
Resolve the CHANGELOG conflict: keep the sharkdp#4111 and sharkdp#4110 entries and
drop the stale UTF-16BE '#XXXX' duplicate (the entry is listed as sharkdp#4076).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants