Skip to content

mruby-bin-mirb: read UTF-8 through the core scanner - #7123

Merged
matz merged 1 commit into
mruby:masterfrom
takumin:mirb-utf8-core-helpers
Aug 12, 2026
Merged

mruby-bin-mirb: read UTF-8 through the core scanner#7123
matz merged 1 commit into
mruby:masterfrom
takumin:mirb-utf8-core-helpers

Conversation

@takumin

@takumin takumin commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

mrbgems/mruby-bin-mirb/tools/mirb/mirb_buffer.c carries its own copy of the
UTF-8 scanner that src/string.c already has. Both are compiled only under
MRB_UTF8_STRING, so the two are asked the same question, and the copy has
drifted from the original.

copy in mirb_buffer.c core in src/string.c
utf8_islead() utf8_islead macro
utf8_len_table[] mrb_utf8len_table[]
utf8_char_len() mrb_utf8len()
utf8_prev_char_start() mrb_utf8_char_head()

This drops the copy and calls mrb_utf8len() and mrb_utf8_char_head()
instead, removing 69 lines against 15 added.

What changes for the user

The copy validates continuation bytes and nothing else, so it reads a sequence
RFC 3629 rejects as a single character. mrb_utf8len() rejects overlong forms,
surrogates, and anything above U+10FFFF, so the cursor now steps over the same
bytes the core counts:

bytes at the cursor before after
\xE0\x80\xAF (overlong U+002F) 3 1
\xC1\x81 (overlong U+0041) 2 1
\xED\xA0\x80 (surrogate U+D800) 3 1
\xF4\x90\x80\x80 (above U+10FFFF) 4 1
\xF5\x80\x80\x80 4 1

Well formed UTF-8 measures the same either way; ASCII, U+3042, U+1F363, and a
truncated lead byte all agree.

Walking back was looser still. utf8_prev_char_start() took the first lead
byte it found within four bytes without asking whether that byte reaches the
cursor, so a continuation byte that no character covers swallowed the character
before it: with "a\x81" in the line, cursor left jumped from byte offset 2 to
0 rather than to 1. mrb_utf8_char_head() treats such a byte as a character of
its own.

Two more things fall out of this:

  • utf8_display_col() passed str + byte_pos + 4 as the end of the string it
    scans, with a /* +4 for safety */ comment, so it could read up to four bytes
    past the position it was asked about. It now passes the end it was given.
  • mirb_buffer.c gains #include <mruby/internal.h>, which mirb.c in the
    same gem already includes.

The display width helpers (utf8_char_width() and utf8_display_col()) stay as
they are. How wide a character prints on a terminal is a question the core
answers nothing about.

Testing

Built with the full-core gembox, where mruby-encoding defines
MRB_UTF8_STRING, and with the default gembox, which does not. Neither build
produces a new warning.

bintest/mirb.rb drives mirb through a pipe, so it never enters raw mode and
never reaches this code. I exercised the buffer through its public API instead
(insert, cursor left and right, backspace, delete, display column) over
hiragana, a 4 byte emoji, ASCII, and each of the ill formed sequences above, and
separately compared the removed functions against the core ones to produce the
table above.

Summary by CodeRabbit

  • Bug Fixes
    • Improved UTF-8 handling in the interactive console.
    • Cursor movement, character deletion, and display positioning now work more reliably with multibyte characters.
    • Updated backward navigation and deletion to respect line boundaries correctly.

mirb_buffer.c carried its own lead byte table and character length
scanner, restating what string.c already answers. Both sit behind
MRB_UTF8_STRING, so the copy was asked the same question as
`mrb_utf8len` and `mrb_utf8_char_head`, and only the original has been
kept correct.

The copy checked continuation bytes and nothing else, so it read a
sequence RFC 3629 rejects as one character. An overlong "\xE0\x80\xAF"
measured 3 bytes to it and 1 to `mrb_utf8len`, a surrogate
"\xED\xA0\x80" 3 and 1, and "\xF5\x80\x80\x80" above U+10FFFF 4 and 1.
Walking back was looser still: it took the first lead byte within four
bytes without asking whether that byte reaches the cursor, so a
continuation byte no character covers swallowed the character before it.
Left, right, and backspace now step over the bytes the core counts.

`utf8_display_col` also passed `str + byte_pos + 4` as the end of what
it scans, reading up to four bytes past the position it was asked about.
It now passes the end it was given.

The display width helpers stay: how wide a character prints is a
question the core answers nothing about.
@takumin
takumin requested a review from matz as a code owner August 12, 2026 14:11
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

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 Plus

Run ID: c96429aa-0f0f-4f54-ad85-99f34163fcc0

📥 Commits

Reviewing files that changed from the base of the PR and between 8d5ae77 and eea1e12.

📒 Files selected for processing (1)
  • mrbgems/mruby-bin-mirb/tools/mirb/mirb_buffer.c

📝 Walkthrough

Walkthrough

mirb_buffer.c now uses mruby’s shared UTF-8 APIs. Display traversal, deletion, and cursor movement use mrb_utf8len and mrb_utf8_char_head instead of local parsing helpers.

Changes

mirb UTF-8 handling

Layer / File(s) Summary
Adopt shared UTF-8 APIs
mrbgems/mruby-bin-mirb/tools/mirb/mirb_buffer.c
The buffer imports mruby/internal.h and replaces local UTF-8 parsing with mrb_utf8_char_head and mrb_utf8len.
Update buffer operations
mrbgems/mruby-bin-mirb/tools/mirb/mirb_buffer.c
Display traversal, deletion, and cursor movement use the shared UTF-8 helpers.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • mruby/mruby#7093: Modifies the RFC 3629 validation behavior used by mrb_utf8len().
  • mruby/mruby#7094: Replaces local UTF-8 helpers with shared mruby APIs in another component.
  • mruby/mruby#7109: Introduces the shared mrb_utf8_char_head and mrb_utf8len APIs used here.

Suggested labels: core

Suggested reviewers: matz

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: using mruby's core UTF-8 scanner in mruby-bin-mirb.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

@matz
matz merged commit 833d316 into mruby:master Aug 12, 2026
20 of 21 checks passed
@takumin
takumin deleted the mirb-utf8-core-helpers branch August 12, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants