Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mrbgems/mruby-string-ext/test/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ def assert_upto(exp, receiver, *args)
assert_nothing_raised { ("\1" * 100).dump } # regress #1210
end

assert('String#inspect of a binary string escapes every byte') do
# `inspect` passes a whole character through unescaped so it stays readable,
# which a string holding no characters has nothing to gain from. `dump` on
# the same string escaped every byte already, so the two agree there.
if UTF8STRING
assert_equal('"る"', "る".inspect)
assert_equal('"\xe3\x82\x8b"', "る".b.inspect)
assert_equal("る".b.dump, "る".b.inspect)
end
end

assert('String#strip') do
s = " abc "
assert_equal("abc", s.strip)
Expand Down
6 changes: 6 additions & 0 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,12 @@ str_escape(mrb_state *mrb, mrb_value str, mrb_bool inspect)
#endif

p = RSTRING_PTR(str); pend = RSTRING_END(str);
#ifdef MRB_UTF8_STRING
/* `inspect` passes a whole character through unescaped so it stays readable.
A byte-indexed string holds no characters to keep readable, so it escapes
byte by byte, which is what `dump` on the same string already did. */
if (RSTR_BINARY_P(mrb_str_ptr(str))) inspect = FALSE;
#endif
for (;p < pend; p++) {
unsigned char c, cc;
#ifdef MRB_UTF8_STRING
Expand Down
Loading