Skip to content

string.c: a binary string is inspected byte by byte - #7083

Merged
matz merged 1 commit into
mruby:masterfrom
takumin:string-binary-inspect
Aug 10, 2026
Merged

string.c: a binary string is inspected byte by byte#7083
matz merged 1 commit into
mruby:masterfrom
takumin:string-binary-inspect

Conversation

@takumin

@takumin takumin commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

str_escape() serves both inspect and dump, and for inspect it passes a
whole UTF-8 character through unescaped so the result stays readable. A string
marked by String#b is read as bytes everywhere else and holds no characters
to keep readable, but the walk here never asked about the flag.

"る".inspect      # "る"
"る".b.inspect    # "る"            <- CRuby: "\xE3\x82\x8B"
"る".b.dump       # "\xe3\x82\x8b"     escaped byte by byte all along

The same bytes were shown two ways by two methods whose only intended
difference is whether a printable character is escaped.

The change

str_escape() reads RSTR_BINARY_P() and takes the dump path for a binary
string. One condition, and the answer it gives is the one dump already gave
for the same string.

Related, not required

String#b marks the string, and several other places in src/string.c read
such a string as UTF-8 for reasons of their own: #7081 for the length,
#7080 for a copy losing the flag, and String#chop! walking characters. Each
is its own fix; none of them depends on this one and this one does not depend
on them.

Testing

rake test is green on full-core with gcc on x86_64-linux: 2215 asserts,
no failures. The new test fails without the change:

Fail: String#inspect of a binary string escapes every byte
  KO: 1   ->   KO: 0

Checked against CRuby 4.0.6. The escapes differ in letter case (\xe3 here,
\xE3 there), which is what dump in this tree has always produced and is not
touched by this change.

Summary by CodeRabbit

  • Bug Fixes
    • Improved string inspection for binary data by escaping each byte consistently.
    • Preserved readable UTF-8 characters when inspecting valid UTF-8 strings.
    • Ensured binary string inspection matches the corresponding dump output.

`str_escape()` serves both `inspect` and `dump`, and for `inspect` it passes a
whole UTF-8 character through unescaped so the result stays readable. A string
marked by `String#b` is read as bytes everywhere else, and holds no characters
to keep readable, but the walk here never asked.

```ruby
"る".inspect      # "る"
"る".b.inspect    # was "る", CRuby: "\xE3\x82\x8B"
"る".b.dump       # "\xe3\x82\x8b", escaped byte by byte all along
```

So the same bytes were shown two ways by two methods whose only difference is
meant to be whether a printable character is escaped.

Read the flag and escape byte by byte, which is the answer `dump` already gave
for that string.
@takumin
takumin requested a review from matz as a code owner August 10, 2026 16:27
@coderabbitai

coderabbitai Bot commented Aug 10, 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: f80f07b8-cce2-4eda-9e9c-00fea191d5eb

📥 Commits

Reviewing files that changed from the base of the PR and between 7184392 and 47050f0.

📒 Files selected for processing (2)
  • mrbgems/mruby-string-ext/test/string.rb
  • src/string.c

📝 Walkthrough

Walkthrough

Under UTF-8 support, String#inspect now escapes binary strings byte by byte. A regression test confirms valid UTF-8 remains readable and binary inspect matches dump.

Changes

Binary string inspection

Layer / File(s) Summary
Binary inspection behavior
src/string.c, mrbgems/mruby-string-ext/test/string.rb
str_escape disables UTF-8 character preservation for binary strings. Tests cover readable valid UTF-8, byte-level escaping for binary data, and matching dump output.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

  • mruby/mruby#7081: Both changes update str_escape() so binary strings escape byte by byte.
  • mruby/mruby#7080: Both changes modify binary-string handling in src/string.c.

Suggested reviewers: matz

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 and concisely describes the main change: binary strings are inspected byte by byte in string.c.
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 9155c5a into mruby:master Aug 10, 2026
21 checks passed
@takumin
takumin deleted the string-binary-inspect branch August 10, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants