1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* string.c (rb_str_cmp): encoding aware comparison.

* string.c (rb_str_casecmp): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14227 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-12-14 08:37:39 +00:00
parent 8b6cb6994c
commit 269bd16b28
2 changed files with 41 additions and 13 deletions

View file

@ -1,3 +1,9 @@
Fri Dec 14 16:06:18 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_cmp): encoding aware comparison.
* string.c (rb_str_casecmp): ditto.
Fri Dec 14 13:47:54 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* common.mk (ruby.imp): fix for circular dependency. a patch from

View file

@ -118,7 +118,7 @@ rb_enc_str_coderange(VALUE str)
while (p < e) {
int c = (unsigned char)*p;
if (!isascii(c)) {
if (!rb_enc_isascii(c, enc)) {
cr = ENC_CODERANGE_8BIT;
break;
}
@ -1276,12 +1276,18 @@ rb_str_cmp(VALUE str1, VALUE str2)
{
long len;
int retval;
rb_encoding *enc;
rb_enc_check(str1, str2); /* xxxx error-less encoding check? */
enc = rb_enc_compatible(str1, str2);
len = lesser(RSTRING_LEN(str1), RSTRING_LEN(str2));
retval = memcmp(RSTRING_PTR(str1), RSTRING_PTR(str2), len);
if (retval == 0) {
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)) return 0;
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)) {
if (!enc) {
return rb_enc_get_index(str1) - rb_enc_get_index(str2);
}
return 0;
}
if (RSTRING_LEN(str1) > RSTRING_LEN(str2)) return 1;
return -1;
}
@ -1404,19 +1410,35 @@ rb_str_cmp_m(VALUE str1, VALUE str2)
static VALUE
rb_str_casecmp(VALUE str1, VALUE str2)
{
long len;
long i, len;
int retval;
rb_encoding *enc;
char *p1, *p1end, *p2, *p2end;
StringValue(str2);
len = lesser(RSTRING_LEN(str1), RSTRING_LEN(str2));
retval = rb_memcicmp(RSTRING_PTR(str1), RSTRING_PTR(str2), len);
if (retval == 0) {
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)) return INT2FIX(0);
if (RSTRING_LEN(str1) > RSTRING_LEN(str2)) return INT2FIX(1);
return INT2FIX(-1);
enc = rb_enc_compatible(str1, str2);
if (!enc) {
return rb_str_cmp(str1, str2);
}
if (retval == 0) return INT2FIX(0);
if (retval > 0) return INT2FIX(1);
p1 = RSTRING_PTR(str1); p1end = RSTRING_END(p1);
p2 = RSTRING_PTR(str2); p2end = RSTRING_END(str2);
while (p1 < p1end && p2 < p2end) {
int c1 = rb_enc_codepoint(p1, p1end, enc);
int c2 = rb_enc_codepoint(p2, p2end, enc);
if (c1 != c2) {
c1 = rb_enc_toupper(c1, enc);
c2 = rb_enc_toupper(c2, enc);
if (c1 > c2) return INT2FIX(1);
if (c1 < c2) return INT2FIX(-1);
}
len = rb_enc_codelen(c1, enc);
p1 += len;
p2 += len;
}
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)) return INT2FIX(0);
if (RSTRING_LEN(str1) > RSTRING_LEN(str2)) return INT2FIX(1);
return INT2FIX(-1);
}
@ -1834,7 +1856,7 @@ rb_str_succ(VALUE orig)
while ((s = rb_enc_prev_char(sbeg, s, enc)) != 0) {
cc = rb_enc_codepoint(s, e, enc);
if (rb_enc_isalnum(cc, enc)) {
if (isascii(cc)) {
if (rb_enc_isascii(cc, enc)) {
if ((c = succ_char(s)) == 0) break;
}
else {