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
17 changes: 15 additions & 2 deletions mrbgems/mruby-encoding/test/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,20 @@

assert('String#reverse! leaves what the bytes read as standing') do
# Reversing puts the same bytes back with every character whole, so a string
# that read as UTF-8 still does, and one that did not still does not. The
# reversal is where that is decided; asking afterwards is how it is seen.
# that read as UTF-8 still does. A string that did not is the one case the
# reversal can settle either way, since bytes that spell nothing where they
# stood can spell a character once they are turned around. The reversal is
# where all of that is decided; asking afterwards is how it is seen.
if UTF8STRING
a = "あいうz"
a.reverse!
assert_equal "zういあ", a
assert_equal 4, a.length
assert_true a.valid_encoding?
a.reverse!
assert_equal "あいうz", a
assert_equal 4, a.length
assert_true a.valid_encoding?

b = "abc"
b.reverse!
Expand All @@ -235,6 +241,13 @@
c.reverse!
assert_equal "\x81\xE3a".b, c.b
assert_false c.valid_encoding?

d = "\x80\xC2" # a trailing byte and then a lead byte, spelling nothing
assert_false d.valid_encoding? # asked here, so the answer is on the string
d.reverse! # and the same bytes now spell U+0080
assert_equal "\xC2\x80".b, d.b
assert_equal 1, d.length
assert_true d.valid_encoding?
Comment thread
coderabbitai[bot] marked this conversation as resolved.
end
end

Expand Down
7 changes: 5 additions & 2 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -2954,8 +2954,11 @@ mrb_str_reverse_bang(mrb_state *mrb, mrb_value str)
char *p, *e;

/* Reversing writes the string's own bytes back in another order, and both
paths below leave every character whole, so what the bytes read as is
still what they read as: both write through str_modify_keep_cr(). */
paths below leave every character whole, so a string that read as UTF-8
still does: both write through str_modify_keep_cr(). A string already
read as broken is the one this cannot answer for, since bytes that spell
nothing where they stand can spell a character turned around, and that is
the string the helper asks again on its own. */

#ifdef MRB_UTF8_STRING
/* mrb_str_char_len() walks the string and records what it finds. The
Expand Down
Loading