Skip to content

mruby-regexp: drop the private UTF-8 encoder in favor of mrb_utf8_to_buf - #7094

Merged
matz merged 1 commit into
mruby:masterfrom
takumin:regexp-use-mrb-utf8-to-buf
Aug 12, 2026
Merged

mruby-regexp: drop the private UTF-8 encoder in favor of mrb_utf8_to_buf#7094
matz merged 1 commit into
mruby:masterfrom
takumin:regexp-use-mrb-utf8-to-buf

Conversation

@takumin

@takumin takumin commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

The regexp gem carried a private UTF-8 encoder, mrb_re_utf8_encode in
re_utf8.c, whose implementation is byte for byte the same as
mrb_utf8_to_buf in src/string.c. This PR deletes the private copy and
switches its only caller, emit_codepoint in re_compile.c, to the
shared function.

Using the shared encoder from a gem follows existing practice:
mruby-sprintf, mruby-pack, and mruby-string-ext all call
mrb_utf8_to_buf through <mruby/internal.h>, and regexp.c in this
gem already includes that header.

The one difference between the two functions is that mrb_utf8_to_buf
returns 0 for a codepoint above U+10FFFF, where the private copy
assumed the caller had validated its input. The compiler satisfies that
assumption: every codepoint reaching emit_codepoint comes through
check_unicode_cp, which rejects surrogates and anything past the last
plane as "invalid Unicode range" before encoding, matching CRuby:

/\u{110000}/   # regexp.rb:1: invalid Unicode range (SyntaxError)

So the zero-length case is unreachable and no caller changes behavior.

Net effect: 3 insertions, 31 deletions, no functional change.
rake test passes (2050 tests, 0 failures; bintest 105 OK).

Summary by CodeRabbit

  • Refactor
    • Streamlined internal Unicode handling in regular expression processing.
    • Updated Unicode encoding references to use the runtime’s standard implementation.
    • Removed redundant internal encoding logic without changing user-facing behavior.

The regexp gem carried its own UTF-8 encoder, byte for byte the same
as `mrb_utf8_to_buf` in src/string.c. Core gems already call the
shared encoder through <mruby/internal.h>, as mruby-sprintf,
mruby-pack, and mruby-string-ext do, and regexp.c in this gem already
includes that header, so the private copy only duplicated code.

The shared function returns 0 for a codepoint above U+10FFFF where
the private copy assumed a validated input. The regexp compiler
satisfies that assumption: every codepoint reaching `emit_codepoint`
comes through `check_unicode_cp`, which rejects surrogates and
anything past the last plane as "invalid Unicode range", so the
zero-length case is unreachable and no caller changes behavior.
@takumin
takumin requested a review from matz as a code owner August 12, 2026 02:18
@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: e6554e23-d9be-40f4-8da6-b2f07581c1fc

📥 Commits

Reviewing files that changed from the base of the PR and between 77eca77 and b0c979e.

📒 Files selected for processing (3)
  • mrbgems/mruby-regexp/include/re_internal.h
  • mrbgems/mruby-regexp/src/re_compile.c
  • mrbgems/mruby-regexp/src/re_utf8.c
💤 Files with no reviewable changes (2)
  • mrbgems/mruby-regexp/include/re_internal.h
  • mrbgems/mruby-regexp/src/re_utf8.c

📝 Walkthrough

Walkthrough

The regexp compiler now uses mrb_utf8_to_buf for Unicode encoding. The regexp-specific encoder declaration, implementation, and related references were removed.

Changes

Regexp UTF-8 encoding

Layer / File(s) Summary
Replace the regexp UTF-8 encoder
mrbgems/mruby-regexp/include/re_internal.h, mrbgems/mruby-regexp/src/re_utf8.c, mrbgems/mruby-regexp/src/re_compile.c
The local mrb_re_utf8_encode declaration and implementation were removed. emit_codepoint now calls mrb_utf8_to_buf, and re_compile.c includes mruby/internal.h.

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

Possibly related PRs

  • mruby/mruby#7056: Both changes modify UTF-8 literal handling in re_compile.c.
  • mruby/mruby#7066: Both changes modify UTF-8 emission handling in re_compile.c.
  • mruby/mruby#7074: This change removes the helper introduced by that PR and uses mrb_utf8_to_buf.

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 removal of the private UTF-8 encoder and its replacement with the shared function.
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 1972cd9 into mruby:master Aug 12, 2026
21 checks passed
@takumin
takumin deleted the regexp-use-mrb-utf8-to-buf branch August 12, 2026 03:43
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