* string.c (rb_str_b): Add String#b, returning a copied string
whose encoding is ASCII-8BIT. [ruby-dev:45992] [Feature #6767] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37486 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
58ef0f06c6
commit
a6ec258452
@ -1,3 +1,8 @@
|
|||||||
|
Tue Nov 6 09:42:26 2012 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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@ruby-lang.org>
|
Tue Nov 6 09:37:57 2012 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* ruby.c (load_file_internal): set default source encoding as
|
* ruby.c (load_file_internal): set default source encoding as
|
||||||
|
4
NEWS
4
NEWS
@ -93,6 +93,10 @@ with all sufficient information, see the ChangeLog file.
|
|||||||
* Signal.trap raises ArgumentError when :SEGV, :BUS, :ILL, :FPE, :VTALRM
|
* Signal.trap raises ArgumentError when :SEGV, :BUS, :ILL, :FPE, :VTALRM
|
||||||
are specified.
|
are specified.
|
||||||
|
|
||||||
|
*String
|
||||||
|
* added method:
|
||||||
|
* added String#b returning a copied string whose encoding is ASCII-8BIT.
|
||||||
|
|
||||||
* Struct
|
* Struct
|
||||||
* added method:
|
* added method:
|
||||||
* added Struct#to_h returning values with keys corresponding to the
|
* added Struct#to_h returning values with keys corresponding to the
|
||||||
|
28
string.c
28
string.c
@ -602,7 +602,7 @@ rb_str_export_to_enc(VALUE str, rb_encoding *enc)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
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) {
|
if (RSTRING_LEN(str) <= RSTRING_EMBED_LEN_MAX) {
|
||||||
STR_SET_EMBED(str2);
|
STR_SET_EMBED(str2);
|
||||||
@ -617,8 +617,14 @@ str_replace_shared(VALUE str2, VALUE str)
|
|||||||
RSTRING(str2)->as.heap.aux.shared = str;
|
RSTRING(str2)->as.heap.aux.shared = str;
|
||||||
FL_SET(str2, ELTS_SHARED);
|
FL_SET(str2, ELTS_SHARED);
|
||||||
}
|
}
|
||||||
rb_enc_cr_str_exact_copy(str2, str);
|
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;
|
return str2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -7360,6 +7366,23 @@ rb_str_force_encoding(VALUE str, VALUE enc)
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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:
|
* call-seq:
|
||||||
* str.valid_encoding? -> true or false
|
* str.valid_encoding? -> true or false
|
||||||
@ -8001,6 +8024,7 @@ Init_String(void)
|
|||||||
|
|
||||||
rb_define_method(rb_cString, "encoding", rb_obj_encoding, 0); /* in encoding.c */
|
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, "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, "valid_encoding?", rb_str_valid_encoding_p, 0);
|
||||||
rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 0);
|
rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 0);
|
||||||
|
|
||||||
|
@ -1471,4 +1471,14 @@ class TestM17N < Test::Unit::TestCase
|
|||||||
yield(*strs)
|
yield(*strs)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user