Project

General

Profile

Feature #12786 ยป string_casecmp_.patch

rringler (Ryan Ringler), 09/24/2016 03:55 AM

View differences:

string.c
3192 3192
    return INT2FIX(-1);
3193 3193
}
3194 3194

  
3195
/*
3196
 *  call-seq:
3197
 *     str.casecmp?(other_str)   -> true or false
3198
 *
3199
 *  Case-insensitive string comparison.
3200
 *
3201
 *     "abcdef".casecmp?("abcde")     #=> false
3202
 *     "aBcDeF".casecmp?("abcdef")    #=> true
3203
 *     "abcdef".casecmp?("abcdefg")   #=> false
3204
 *     "abcdef".casecmp?("ABCDEF")    #=> true
3205
 */
3206

  
3207
static VALUE
3208
rb_str_casecmp_p(VALUE str1, VALUE str2)
3209
{
3210
    return rb_str_casecmp(str1, str2) == INT2FIX(0) ? Qtrue : Qfalse;
3211
}
3212

  
3195 3213
#define rb_str_index(str, sub, offset) rb_strseq_index(str, sub, offset, 0)
3196 3214

  
3197 3215
static long
......
9604 9622
}
9605 9623

  
9606 9624
/*
9625
 *  call-seq:
9626
 *     sym.casecmp?(other)  -> true, false, or nil
9627
 *
9628
 *  Case-insensitive symbol comparison.
9629
 */
9630

  
9631
static VALUE
9632
sym_casecmp_p(VALUE sym, VALUE other)
9633
{
9634
    if (!SYMBOL_P(other)) {
9635
        return Qnil;
9636
    }
9637
    return sym_casecmp(sym, other) == INT2FIX(0) ? Qtrue : Qfalse;
9638
}
9639

  
9640
/*
9607 9641
 * call-seq:
9608 9642
 *   sym =~ obj   -> integer or nil
9609 9643
 *
......
9802 9836
    rb_define_method(rb_cString, "eql?", rb_str_eql, 1);
9803 9837
    rb_define_method(rb_cString, "hash", rb_str_hash_m, 0);
9804 9838
    rb_define_method(rb_cString, "casecmp", rb_str_casecmp, 1);
9839
    rb_define_method(rb_cString, "casecmp?", rb_str_casecmp_p, 1);
9805 9840
    rb_define_method(rb_cString, "+", rb_str_plus, 1);
9806 9841
    rb_define_method(rb_cString, "*", rb_str_times, 1);
9807 9842
    rb_define_method(rb_cString, "%", rb_str_format_m, 1);
......
9951 9986

  
9952 9987
    rb_define_method(rb_cSymbol, "<=>", sym_cmp, 1);
9953 9988
    rb_define_method(rb_cSymbol, "casecmp", sym_casecmp, 1);
9989
    rb_define_method(rb_cSymbol, "casecmp?", sym_casecmp_p, 1);
9954 9990
    rb_define_method(rb_cSymbol, "=~", sym_match, 1);
9955 9991

  
9956 9992
    rb_define_method(rb_cSymbol, "[]", sym_aref, -1);
test/ruby/enc/test_utf16.rb
138 138
    assert_not_equal(0, s1.casecmp(s2), "#{encdump s1}.casecmp(#{encdump s2})")
139 139
  end
140 140

  
141
  def test_casecmp?
142
    s1 = "aa".force_encoding("utf-16be")
143
    s2 = "AA"
144
    assert_not_equal(true, s1.casecmp?(s2), "#{encdump s1}.casecmp?(#{encdump s2})")
145
  end
146

  
141 147
  def test_end_with
142 148
    s1 = "ab".force_encoding("utf-16be")
143 149
    s2 = "b".force_encoding("utf-16be")
......
382 388
    a = ary.sort {|x,y| x.casecmp(y) }
383 389
    assert_equal(e, a)
384 390
  end
391

  
392
  def test_casecmp2?
393
    assert_equal(true, "\0A".force_encoding("UTF-16BE").casecmp?("\0a".force_encoding("UTF-16BE")))
394
    assert_not_equal(true, "\0A".force_encoding("UTF-16LE").casecmp?("\0a".force_encoding("UTF-16LE")))
395
    assert_not_equal(true, "A\0".force_encoding("UTF-16BE").casecmp?("a\0".force_encoding("UTF-16BE")))
396
    assert_equal(true, "A\0".force_encoding("UTF-16LE").casecmp?("a\0".force_encoding("UTF-16LE")))
397
  end
385 398
end
test/ruby/test_m17n.rb
1155 1155
    assert_not_equal(0, s1.casecmp(s2))
1156 1156
  end
1157 1157

  
1158
  def test_casecmp?
1159
    s1 = s("\x81\x41")
1160
    s2 = s("\x81\x61")
1161
    assert_not_equal(true, s1.casecmp?(s2))
1162
  end
1163

  
1158 1164
  def test_reverse
1159 1165
    bug11387 = '[ruby-dev:49189] [Bug #11387]'
1160 1166
    s1 = u("abcdefghij\xf0")
test/ruby/test_m17n_comb.rb
593 593
    }
594 594
  end
595 595

  
596
  def test_str_casecmp?
597
    combination(STRINGS, STRINGS) {|s1, s2|
598
      next unless s1.valid_encoding? && s2.valid_encoding? && Encoding.compatible?(s1, s2)
599
      r = s1.casecmp?(s2)
600
      assert_equal(s1.casecmp(s2) == 0, r)
601
    }
602
  end
603

  
596 604
  def test_str_center
597 605
    combination(STRINGS, [0,1,2,3,10]) {|s1, width|
598 606
      t = s1.center(width)
test/ruby/test_string.rb
2210 2210
    assert_equal(1, "\u3042B".casecmp("\u3042a"))
2211 2211
  end
2212 2212

  
2213
  def test_casecmp?
2214
    assert_equal(false, "\u3042B".casecmp?("\u3042a"))
2215
  end
2216

  
2213 2217
  def test_upcase2
2214 2218
    assert_equal("\u3042AB", "\u3042aB".upcase)
2215 2219
  end
test/ruby/test_symbol.rb
276 276
    assert_nil(:foo.casecmp("foo"))
277 277
  end
278 278

  
279
  def test_casecmp?
280
    assert_equal(true, :FoO.casecmp?(:fOO))
281
    assert_equal(false, :FoO.casecmp?(:BaR))
282
    assert_equal(false, :baR.casecmp?(:FoO))
283
    assert_nil(:foo.casecmp?("foo"))
284
  end
285

  
279 286
  def test_length
280 287
    assert_equal(3, :FoO.length)
281 288
    assert_equal(3, :FoO.size)