ruby-changes:25428
From: naruse <ko1@a...>
Date: Tue, 6 Nov 2012 09:50:26 +0900 (JST)
Subject: [ruby-changes:25428] naruse:r37486 (trunk): * string.c (rb_str_b): Add String#b, returning a copied string
naruse 2012-11-06 09:50:02 +0900 (Tue, 06 Nov 2012) New Revision: 37486 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37486 Log: * string.c (rb_str_b): Add String#b, returning a copied string whose encoding is ASCII-8BIT. [ruby-dev:45992] [Feature #6767] Modified files: trunk/ChangeLog trunk/NEWS trunk/string.c trunk/test/ruby/test_m17n.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 37485) +++ ChangeLog (revision 37486) @@ -1,3 +1,8 @@ +Tue Nov 6 09:42:26 2012 NARUSE, Yui <naruse@r...> + + * string.c (rb_str_b): Add String#b, returning a copied string + whose encoding is ASCII-8BIT. [ruby-dev:45992] [Feature #6767] + Tue Nov 6 09:37:57 2012 NARUSE, Yui <naruse@r...> * ruby.c (load_file_internal): set default source encoding as Index: string.c =================================================================== --- string.c (revision 37485) +++ string.c (revision 37486) @@ -602,7 +602,7 @@ } static VALUE -str_replace_shared(VALUE str2, VALUE str) +str_replace_shared_without_enc(VALUE str2, VALUE str) { if (RSTRING_LEN(str) <= RSTRING_EMBED_LEN_MAX) { STR_SET_EMBED(str2); @@ -617,8 +617,14 @@ RSTRING(str2)->as.heap.aux.shared = str; FL_SET(str2, ELTS_SHARED); } + return str2; +} + +static VALUE +str_replace_shared(VALUE str2, VALUE str) +{ + str_replace_shared_without_enc(str2, str); rb_enc_cr_str_exact_copy(str2, str); - return str2; } @@ -7362,6 +7368,23 @@ /* * call-seq: + * str.b -> str + * + * Returns a copied string whose encoding is ASCII-8BIT. + */ + +static VALUE +rb_str_b(VALUE str) +{ + VALUE str2 = str_alloc(rb_cString); + str_replace_shared_without_enc(str2, str); + OBJ_INFECT(str2, str); + ENC_CODERANGE_SET(str2, ENC_CODERANGE_VALID); + return str2; +} + +/* + * call-seq: * str.valid_encoding? -> true or false * * Returns true for a string which encoded correctly. @@ -8001,6 +8024,7 @@ rb_define_method(rb_cString, "encoding", rb_obj_encoding, 0); /* in encoding.c */ rb_define_method(rb_cString, "force_encoding", rb_str_force_encoding, 1); + rb_define_method(rb_cString, "b", rb_str_b, 0); rb_define_method(rb_cString, "valid_encoding?", rb_str_valid_encoding_p, 0); rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 0); Index: NEWS =================================================================== --- NEWS (revision 37485) +++ NEWS (revision 37486) @@ -93,6 +93,10 @@ * Signal.trap raises ArgumentError when :SEGV, :BUS, :ILL, :FPE, :VTALRM are specified. + *String + * added method: + * added String#b returning a copied string whose encoding is ASCII-8BIT. + * Struct * added method: * added Struct#to_h returning values with keys corresponding to the Index: test/ruby/test_m17n.rb =================================================================== --- test/ruby/test_m17n.rb (revision 37485) +++ test/ruby/test_m17n.rb (revision 37486) @@ -1471,4 +1471,14 @@ yield(*strs) end end + + def test_str_b + s = "\u3042" + assert_equal(a("\xE3\x81\x82"), s.b) + assert_equal(Encoding::ASCII_8BIT, s.b.encoding) + s.taint + assert_equal(true, s.b.tainted?) + s.untrust + assert_equal(true, s.b.untrusted?) + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/