ruby-changes:14392
From: knu <ko1@a...>
Date: Fri, 1 Jan 2010 00:41:00 +0900 (JST)
Subject: [ruby-changes:14392] Ruby:r26222 (ruby_1_8): * string.c: Add instance methods to Symbol: succ, next, <=>,
knu 2010-01-01 00:40:46 +0900 (Fri, 01 Jan 2010) New Revision: 26222 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26222 Log: * string.c: Add instance methods to Symbol: succ, next, <=>, casecmp, =~, [], slice, length, size, empty?, match, upcase, downcase, capitalize and swapcase. [backport from 1.9] Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/NEWS branches/ruby_1_8/string.c branches/ruby_1_8/test/ruby/test_symbol.rb Index: ruby_1_8/NEWS =================================================================== --- ruby_1_8/NEWS (revision 26221) +++ ruby_1_8/NEWS (revision 26222) @@ -110,6 +110,24 @@ behavior of String#[] and String#[]= have changed. String#ord is $KCODE aware. + * Symbol#succ + * Symbol#next + * Symbol#<=> + * Symbol#casecmp + * Symbol#=~ + * Symbol#[] + * Symbol#slice + * Symbol#length + * Symbol#size + * Symbol#empty? + * Symbol#match + * Symbol#upcase + * Symbol#downcase + * Symbol#capitalize + * Symbol#swapcase + + New methods. + * dbm DBM#key Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 26221) +++ ruby_1_8/ChangeLog (revision 26222) @@ -1,3 +1,9 @@ +Fri Jan 1 00:36:55 2010 Akinori MUSHA <knu@i...> + + * string.c: Add instance methods to Symbol: succ, next, <=>, + casecmp, =~, [], slice, length, size, empty?, match, upcase, + downcase, capitalize and swapcase. [backport from 1.9] + Fri Jan 1 00:33:13 2010 Akinori MUSHA <knu@i...> * parse.y (dsym): Allow empty symbols. [merge from 1.9] Index: ruby_1_8/string.c =================================================================== --- ruby_1_8/string.c (revision 26221) +++ ruby_1_8/string.c (revision 26222) @@ -347,6 +347,8 @@ return str; } +static VALUE rb_str_length _((VALUE)); + /* * call-seq: * str.length => integer @@ -361,6 +363,8 @@ return LONG2NUM(RSTRING(str)->len); } +static VALUE rb_str_empty _((VALUE)); + /* * call-seq: * str.empty? => true or false @@ -1029,6 +1033,8 @@ return Qfalse; } +static VALUE rb_str_cmp_m _((VALUE, VALUE)); + /* * call-seq: * str <=> other_str => -1, 0, +1 or nil @@ -1083,6 +1089,8 @@ return LONG2NUM(result); } +static VALUE rb_str_casecmp _((VALUE, VALUE)); + /* * call-seq: * str.casecmp(other_str) => -1, 0, +1 or nil @@ -1337,6 +1345,8 @@ return Qnil; } +static VALUE rb_str_match _((VALUE, VALUE)); + /* * call-seq: * str =~ obj => fixnum or nil @@ -1485,6 +1495,8 @@ } +static VALUE rb_str_succ_bang _((VALUE)); + /* * call-seq: * str.succ! => str @@ -1629,6 +1641,8 @@ } +static VALUE rb_str_aref_m _((int, VALUE *, VALUE)); + /* * call-seq: * str[fixnum] => fixnum or nil @@ -2883,6 +2897,8 @@ } +static VALUE rb_str_upcase_bang _((VALUE)); + /* * call-seq: * str.upcase! => str or nil @@ -2937,6 +2953,8 @@ } +static VALUE rb_str_downcase_bang _((VALUE)); + /* * call-seq: * str.downcase! => str or nil @@ -2991,6 +3009,8 @@ } +static VALUE rb_str_capitalize_bang _((VALUE)); + /* * call-seq: * str.capitalize! => str or nil @@ -3054,6 +3074,8 @@ } +static VALUE rb_str_swapcase_bang _((VALUE)); + /* * call-seq: * str.swapcase! => str or nil @@ -5006,6 +5028,183 @@ /* + * call-seq: + * + * sym.succ + * + * Same as <code>sym.to_s.succ.intern</code>. + */ + +static VALUE +sym_succ(sym) + VALUE sym; +{ + VALUE str = rb_sym_to_s(sym); + rb_str_succ_bang(str); + return rb_str_intern(str); +} + +/* + * call-seq: + * + * str <=> other => -1, 0, +1 or nil + * + * Compares _sym_ with _other_ in string form. + */ + +static VALUE +sym_cmp(sym, other) + VALUE sym, other; +{ + if (!SYMBOL_P(other)) { + return Qnil; + } + return rb_str_cmp_m(rb_sym_to_s(sym), rb_sym_to_s(other)); +} + +/* + * call-seq: + * + * sym.casecmp(other) => -1, 0, +1 or nil + * + * Case-insensitive version of <code>Symbol#<=></code>. + */ + +static VALUE +sym_casecmp(sym, other) + VALUE sym, other; +{ + if (!SYMBOL_P(other)) { + return Qnil; + } + return rb_str_casecmp(rb_sym_to_s(sym), rb_sym_to_s(other)); +} + +/* + * call-seq: + * sym =~ obj => fixnum or nil + * + * Returns <code>sym.to_s =~ obj</code>. + */ + +static VALUE +sym_match(sym, other) + VALUE sym, other; +{ + return rb_str_match(rb_sym_to_s(sym), other); +} + +/* + * call-seq: + * sym[idx] => char + * sym[b, n] => char + * + * Returns <code>sym.to_s[]</code>. + */ + +static VALUE +sym_aref(argc, argv, sym) + int argc; + VALUE *argv; + VALUE sym; +{ + return rb_str_aref_m(argc, argv, rb_sym_to_s(sym)); +} + +/* + * call-seq: + * sym.length => integer + * + * Same as <code>sym.to_s.length</code>. + */ + +static VALUE +sym_length(sym) + VALUE sym; +{ + return rb_str_length(rb_sym_to_s(sym)); +} + +/* + * call-seq: + * sym.empty? => true or false + * + * Returns that _sym_ is :"" or not. + */ + +static VALUE +sym_empty(sym) + VALUE sym; +{ + return rb_str_empty(rb_sym_to_s(sym)); +} + +/* + * call-seq: + * sym.upcase => symbol + * + * Same as <code>sym.to_s.upcase.intern</code>. + */ + +static VALUE +sym_upcase(sym) + VALUE sym; +{ + VALUE str = rb_sym_to_s(sym); + rb_str_upcase_bang(str); + return rb_str_intern(str); +} + +/* + * call-seq: + * sym.downcase => symbol + * + * Same as <code>sym.to_s.downcase.intern</code>. + */ + +static VALUE +sym_downcase(sym) + VALUE sym; +{ + VALUE str = rb_sym_to_s(sym); + rb_str_downcase_bang(str); + return rb_str_intern(str); +} + +/* + * call-seq: + * sym.capitalize => symbol + * + * Same as <code>sym.to_s.capitalize.intern</code>. + */ + +static VALUE +sym_capitalize(sym) + VALUE sym; +{ + VALUE str = rb_sym_to_s(sym); + rb_str_capitalize_bang(str); + return rb_str_intern(str); +} + +/* + * call-seq: + * sym.swapcase => symbol + * + * Same as <code>sym.to_s.swapcase.intern</code>. + */ + +static VALUE +sym_swapcase(sym) + VALUE sym; +{ + VALUE str = rb_sym_to_s(sym); + rb_str_swapcase_bang(str); + return rb_str_intern(str); +} + + +/* * A <code>String</code> object holds and manipulates an arbitrary sequence of * bytes, typically representing characters. String objects may be created * using <code>String::new</code> or as literals. @@ -5158,4 +5357,23 @@ rb_fs = Qnil; rb_define_variable("$;", &rb_fs); rb_define_variable("$-F", &rb_fs); + + rb_define_method(rb_cSymbol, "succ", sym_succ, 0); + rb_define_method(rb_cSymbol, "next", sym_succ, 0); + + rb_define_method(rb_cSymbol, "<=>", sym_cmp, 1); + rb_define_method(rb_cSymbol, "casecmp", sym_casecmp, 1); + rb_define_method(rb_cSymbol, "=~", sym_match, 1); + + rb_define_method(rb_cSymbol, "[]", sym_aref, -1); + rb_define_method(rb_cSymbol, "slice", sym_aref, -1); + rb_define_method(rb_cSymbol, "length", sym_length, 0); + rb_define_method(rb_cSymbol, "size", sym_length, 0); + rb_define_method(rb_cSymbol, "empty?", sym_empty, 0); + rb_define_method(rb_cSymbol, "match", sym_match, 1); + + rb_define_method(rb_cSymbol, "upcase", sym_upcase, 0); + rb_define_method(rb_cSymbol, "downcase", sym_downcase, 0); + rb_define_method(rb_cSymbol, "capitalize", sym_capitalize, 0); + rb_define_method(rb_cSymbol, "swapcase", sym_swapcase, 0); } Index: ruby_1_8/test/ruby/test_symbol.rb =================================================================== --- ruby_1_8/test/ruby/test_symbol.rb (revision 26221) +++ ruby_1_8/test/ruby/test_symbol.rb (revision 26222) @@ -89,4 +89,47 @@ assert_equal ary_ids, ary.collect(&:object_id) end end + + def test_call + o = Object.new + def o.foo(x, y); x + y; end + + assert_equal(3, :foo.to_proc.call(o, 1, 2)) + assert_raise(ArgumentError) { :foo.to_proc.call } + end + + def test_succ + assert_equal(:fop, :foo.succ) + end + + def test_cmp + assert_equal(0, :FoO <=> :FoO) + assert_equal(-1, :FoO <=> :fOO) + assert_equal(1, :fOO <=> :FoO) + assert_nil(:foo <=> "foo") + end + + def test_casecmp + assert_equal(0, :FoO.casecmp(:fOO)) + assert_equal(1, :FoO.casecmp(:BaR)) + assert_equal(-1, :baR.casecmp(:FoO)) + assert_nil(:foo.casecmp("foo")) + end + + def test_length + assert_equal(3, :FoO.length) + assert_equal(3, :FoO.size) + end + + def test_empty + assert_equal(false, :FoO.empty?) + assert_equal(true, :"".empty?) + end + + def test_case + assert_equal(:FOO, :FoO.upcase) + assert_equal(:foo, :FoO.downcase) + assert_equal(:Foo, :foo.capitalize) + assert_equal(:fOo, :FoO.swapcase) + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/