Project

General

Profile

Feature #11300 ยป string_bin.patch

0x0dea (D.E. Akers), 06/24/2015 01:00 AM

View differences:

string.c
7705 7705
/*
7706 7706
 *  call-seq:
7707
 *     str.bin   -> integer
7708
 *
7709
 *  Treats leading characters from <i>str</i> as a string of binary digits
7710
 *  (with an optional sign and an optional <code>0b</code>) and returns the
7711
 *  corresponding number. Zero is returned on error.
7712
 *
7713
 *     "1010".bin     #=> 10
7714
 *     "-0b111".bin   #=> -7
7715
 *     "2".bin        #=> 0
7716
 *     "12".bin       #=> 1
7717
 */
7718

  
7719
static VALUE
7720
rb_str_bin(VALUE str)
7721
{
7722
    return rb_str_to_inum(str, 2, FALSE);
7723
}
7724

  
7725

  
7726
/*
7727
 *  call-seq:
7707 7728
 *     str.hex   -> integer
7708 7729
 *
7709 7730
 *  Treats leading characters from <i>str</i> as a string of hexadecimal digits
......
9153 9174
    rb_define_method(rb_cString, "capitalize!", rb_str_capitalize_bang, 0);
9154 9175
    rb_define_method(rb_cString, "swapcase!", rb_str_swapcase_bang, 0);
9176
    rb_define_method(rb_cString, "bin", rb_str_bin, 0);
9155 9177
    rb_define_method(rb_cString, "hex", rb_str_hex, 0);
9156 9178
    rb_define_method(rb_cString, "oct", rb_str_oct, 0);
9157 9179
    rb_define_method(rb_cString, "split", rb_str_split_m, -1);
test/ruby/test_string.rb
303 303
    casetest(S("CaT"), S('cAt'), true) # find these in the case.
304 304
  end
305
  def test_bin
306
    assert_equal(15,  S("0b1111").bin)
307
    assert_equal(-15, S("-0b1111").bin)
308
    assert_equal(15,  S("1111").bin)
309
    assert_equal(-15, S("-1111").bin)
310
    assert_equal(0,   S("2").bin)
311
    assert_equal(1,   S("12").bin)
312
  end
313

  
305 314
  def test_capitalize
306 315
    assert_equal(S("Hello"),  S("hello").capitalize)
307 316
    assert_equal(S("Hello"),  S("hELLO").capitalize)