string_cmp.diff

Joshua Ballanco, 11/21/2012 07:54 PM

Download (1.9 KB)

View differences:

string.c
2382 2382
static VALUE
2383 2383
rb_str_cmp_m(VALUE str1, VALUE str2)
2384 2384
{
2385
    long result;
2385
    int result;
2386
    VALUE tmp;
2386 2387

  
2387
    if (!RB_TYPE_P(str2, T_STRING)) {
2388
	if (!rb_respond_to(str2, rb_intern("to_str"))) {
2389
	    return Qnil;
2390
	}
2391
	else if (!rb_respond_to(str2, rb_intern("<=>"))) {
2392
	    return Qnil;
2393
	}
2388
    if (RB_TYPE_P(str2, T_STRING)) {
2389
	result = rb_str_cmp(str1, str2);
2390
    }
2391
    else {
2392
        tmp = rb_check_funcall(str2, rb_intern("to_str"), 0, 0);
2393
	if (tmp != Qundef && RB_TYPE_P(tmp, T_STRING)) {
2394
            result = rb_str_cmp(str1, tmp);
2395
        }
2394 2396
	else {
2395
	    VALUE tmp = rb_funcall(str2, rb_intern("<=>"), 1, str1);
2396

  
2397
            tmp = rb_funcall(str2, rb_intern("<=>"), 1, str1);
2397 2398
	    if (NIL_P(tmp)) return Qnil;
2398
	    if (!FIXNUM_P(tmp)) {
2399
		return rb_funcall(LONG2FIX(0), '-', 1, tmp);
2400
	    }
2401
	    result = -FIX2LONG(tmp);
2399

  
2400
            result = -rb_cmpint(tmp, str1, str2);
2402 2401
	}
2403 2402
    }
2404
    else {
2405
	result = rb_str_cmp(str1, str2);
2406
    }
2407
    return LONG2NUM(result);
2403
    if (result == 0) return INT2FIX(0);
2404
    if (result > 0) return INT2FIX(1);
2405
    return INT2FIX(-1);
2408 2406
}
2409 2407

  
2410 2408
/*
test/ruby/test_string.rb
170 170

  
171 171
    o = Object.new
172 172
    def o.to_str; "bar"; end
173
    assert_nil("foo" <=> o)
173
    assert_equal(1, "foo" <=> o)
174 174

  
175
    class << o;remove_method :to_str;end
175 176
    def o.<=>(x); nil; end
176 177
    assert_nil("foo" <=> o)
177 178

  
......
181 182

  
182 183
    class << o;remove_method :<=>;end
183 184
    def o.<=>(x); 2**100; end
184
    assert_equal(-(2**100), "foo" <=> o)
185
    assert_equal(-1, "foo" <=> o)
185 186
  end
186 187

  
187 188
  def test_EQUAL # '=='