Skip to content

fix: preserve UTF-8 encoding in comment parsing#208

Open
2catycm wants to merge 1 commit into
vlang:masterfrom
2catycm:fix-utf8-comment-corruption
Open

fix: preserve UTF-8 encoding in comment parsing#208
2catycm wants to merge 1 commit into
vlang:masterfrom
2catycm:fix-utf8-comment-corruption

Conversation

@2catycm

@2catycm 2catycm commented Jun 5, 2026

Copy link
Copy Markdown

Summary

  • Fix corrupted non-ASCII (e.g. Chinese/Japanese/Korean) comments in translated output

Root Cause

In parse_comment(), for c in str iterates over bytes (u8). The subsequent comment.write_rune(c) treats each byte as a Unicode code point and re-encodes it as UTF-8, causing multi-byte characters to be double-encoded.

For example, (UTF-8: [0xE4, 0xBD, 0xA0]) becomes ä½ because each byte gets individually re-encoded as a 2-byte UTF-8 sequence.

Fix

Replace write_rune(c) with write_u8(c) at all 4 call sites in parse_comment(). Since we're iterating bytes and want to preserve them as-is, write_u8 is the correct method.

Testing

  • Verified with a C file containing Chinese comments — output now preserves them correctly
  • All existing c2v translation tests pass

In parse_comment(), iterating `for c in str` yields u8 bytes.
Calling write_rune(c) re-encodes each byte as a Unicode code point,
corrupting multi-byte UTF-8 characters (e.g. Chinese comments become
garbled). Use write_u8(c) instead to pass bytes through unchanged.

Fixes comment corruption for all non-ASCII source files.
@JalonSolov

Copy link
Copy Markdown
Contributor

Perhaps simpler would be to change to for c in str.runes(), then comment.write_rune(c) would be correct.

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.

2 participants