Skip to content

Commit e7e5b73

Browse files
committed
* string.c (rb_str_casecmp): make the ordering consistent with
String#<=>. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19490 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 55783c6 commit e7e5b73

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Tue Sep 23 21:26:32 2008 Tanaka Akira <akr@fsij.org>
2+
3+
* string.c (rb_str_casecmp): make the ordering consistent with
4+
String#<=>.
5+
16
Tue Sep 23 20:52:25 2008 Tanaka Akira <akr@fsij.org>
27

38
* io.c (io_binwrite): add nosync argument.

string.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,29 +2105,39 @@ rb_str_casecmp(VALUE str1, VALUE str2)
21052105
if (single_byte_optimizable(str1) && single_byte_optimizable(str2)) {
21062106
while (p1 < p1end && p2 < p2end) {
21072107
if (*p1 != *p2) {
2108-
unsigned int c1 = rb_enc_toupper(*p1 & 0xff, enc);
2109-
unsigned int c2 = rb_enc_toupper(*p2 & 0xff, enc);
2110-
if (c1 > c2) return INT2FIX(1);
2111-
if (c1 < c2) return INT2FIX(-1);
2108+
unsigned int c1 = TOUPPER(*p1 & 0xff);
2109+
unsigned int c2 = TOUPPER(*p2 & 0xff);
2110+
if (c1 != c2)
2111+
return INT2FIX(c1 < c2 ? -1 : 1);
21122112
}
21132113
p1++;
21142114
p2++;
21152115
}
21162116
}
21172117
else {
21182118
while (p1 < p1end && p2 < p2end) {
2119-
unsigned int c1 = rb_enc_codepoint(p1, p1end, enc);
2120-
unsigned int c2 = rb_enc_codepoint(p2, p2end, enc);
2121-
2122-
if (c1 != c2) {
2123-
c1 = rb_enc_toupper(c1, enc);
2124-
c2 = rb_enc_toupper(c2, enc);
2125-
if (c1 > c2) return INT2FIX(1);
2126-
if (c1 < c2) return INT2FIX(-1);
2127-
}
2128-
len = rb_enc_codelen(c1, enc);
2129-
p1 += len;
2130-
p2 += len;
2119+
int l1, c1 = rb_enc_ascget(p1, p1end, &l1, enc);
2120+
int l2, c2 = rb_enc_ascget(p2, p2end, &l2, enc);
2121+
2122+
if (0 <= c1 && 0 <= c2) {
2123+
c1 = TOUPPER(c1);
2124+
c2 = TOUPPER(c2);
2125+
if (c1 != c2)
2126+
return INT2FIX(c1 < c2 ? -1 : 1);
2127+
}
2128+
else {
2129+
int r;
2130+
l1 = rb_enc_mbclen(p1, p1end, enc);
2131+
l2 = rb_enc_mbclen(p2, p2end, enc);
2132+
len = l1 < l2 ? l1 : l2;
2133+
r = memcmp(p1, p2, len);
2134+
if (r != 0)
2135+
return INT2FIX(r < 0 ? -1 : 1);
2136+
if (l1 != l2)
2137+
return INT2FIX(l1 < l2 ? -1 : 1);
2138+
}
2139+
p1 += l1;
2140+
p2 += l2;
21312141
}
21322142
}
21332143
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)) return INT2FIX(0);

test/ruby/enc/test_utf16.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,17 @@ def test_regexp_escape
368368
r = Regexp.new(Regexp.escape(s))
369369
assert(r =~ s, "#{encdump(r)} =~ #{encdump(s)}")
370370
end
371+
372+
def test_casecmp
373+
assert_equal(0, "\0A".force_encoding("UTF-16BE").casecmp("\0a".force_encoding("UTF-16BE")))
374+
assert_not_equal(0, "\0A".force_encoding("UTF-16LE").casecmp("\0a".force_encoding("UTF-16LE")))
375+
assert_not_equal(0, "A\0".force_encoding("UTF-16BE").casecmp("a\0".force_encoding("UTF-16BE")))
376+
assert_equal(0, "A\0".force_encoding("UTF-16LE").casecmp("a\0".force_encoding("UTF-16LE")))
377+
378+
ary = ["01".force_encoding("UTF-16LE"),
379+
"10".force_encoding("UTF-16LE")]
380+
e = ary.sort {|x,y| x <=> y }
381+
a = ary.sort {|x,y| x.casecmp(y) }
382+
assert_equal(e, a)
383+
end
371384
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy