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
5 changes: 4 additions & 1 deletion mrbgems/mruby-string-ext/src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,10 @@ str_casecmp(mrb_state *mrb, mrb_value self)
if (p1 == p2) return mrb_fixnum_value(0);

for (mrb_int i=0; i<len; i++) {
int c1 = p1[i], c2 = p2[i];
/* Read as unsigned, as `String#<=>` reads the same bytes: a plain `char`
is signed on most targets, which would order a byte of 0x80 or above
below every ASCII one. */
int c1 = (unsigned char)p1[i], c2 = (unsigned char)p2[i];
if (ISASCII(c1) && ISUPPER(c1)) c1 = TOLOWER(c1);
if (ISASCII(c2) && ISUPPER(c2)) c2 = TOLOWER(c2);
if (c1 > c2) return mrb_fixnum_value(1);
Expand Down
5 changes: 5 additions & 0 deletions mrbgems/mruby-string-ext/test/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ def assert_upto(exp, receiver, *args)
assert_equal 0, "aBcDeF".casecmp("abcdef")
assert_equal(-1, "abcdef".casecmp("abcdefg"))
assert_equal 0, "abcdef".casecmp("ABCDEF")
# A byte of 0x80 or above orders above every ASCII one, which is where
# `String#<=>` puts it as well.
assert_equal 1, "\xC3".casecmp("a")
assert_equal 1, ("\xC3" <=> "a")
assert_equal(-1, "a".casecmp("\xC3"))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
end

assert('String#count') do
Expand Down
Loading