Project

General

Profile

Feature #11300 ยป string_bin.patch

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

View differences:

string.c
/*
* call-seq:
* str.bin -> integer
*
* Treats leading characters from <i>str</i> as a string of binary digits
* (with an optional sign and an optional <code>0b</code>) and returns the
* corresponding number. Zero is returned on error.
*
* "1010".bin #=> 10
* "-0b111".bin #=> -7
* "2".bin #=> 0
* "12".bin #=> 1
*/
static VALUE
rb_str_bin(VALUE str)
{
return rb_str_to_inum(str, 2, FALSE);
}
/*
* call-seq:
* str.hex -> integer
*
* Treats leading characters from <i>str</i> as a string of hexadecimal digits
......
rb_define_method(rb_cString, "capitalize!", rb_str_capitalize_bang, 0);
rb_define_method(rb_cString, "swapcase!", rb_str_swapcase_bang, 0);
rb_define_method(rb_cString, "bin", rb_str_bin, 0);
rb_define_method(rb_cString, "hex", rb_str_hex, 0);
rb_define_method(rb_cString, "oct", rb_str_oct, 0);
rb_define_method(rb_cString, "split", rb_str_split_m, -1);
test/ruby/test_string.rb
casetest(S("CaT"), S('cAt'), true) # find these in the case.
end
def test_bin
assert_equal(15, S("0b1111").bin)
assert_equal(-15, S("-0b1111").bin)
assert_equal(15, S("1111").bin)
assert_equal(-15, S("-1111").bin)
assert_equal(0, S("2").bin)
assert_equal(1, S("12").bin)
end
def test_capitalize
assert_equal(S("Hello"), S("hello").capitalize)
assert_equal(S("Hello"), S("hELLO").capitalize)
    (1-1/1)