From bb2bdc887c717d4770a113b19f1b685f5837d960 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 28 Sep 2007 19:27:10 +0000 Subject: * encoding.c (rb_enc_alias): allow encodings multiple aliases. * encoding.c (rb_enc_find_index): search the encoding which has the given name and return its index if found, or -1. * st.c (type_strcasehash): case-insensitive string hash type. * string.c (rb_str_force_encoding): force encoding of self. this name comes from [ruby-dev:31894] by Martin Duerst. [ruby-dev:31744] * include/ruby/encoding.h (rb_enc_find_index, rb_enc_associate_index): prototyped. * include/ruby/encoding.h (rb_enc_isctype): direct interface to ctype. * include/ruby/st.h (st_init_strcasetable): prototyped. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@13556 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'string.c') diff --git a/string.c b/string.c index ab48a66bb..690ce8685 100644 --- a/string.c +++ b/string.c @@ -228,7 +228,7 @@ rb_tainted_str_new2(const char *ptr) } static VALUE -str_new3(VALUE klass, VALUE str) +str_new_shared(VALUE klass, VALUE str) { VALUE str2 = str_alloc(klass); @@ -244,11 +244,19 @@ str_new3(VALUE klass, VALUE str) RSTRING(str2)->as.heap.aux.shared = str; FL_SET(str2, ELTS_SHARED); } - rb_enc_copy((VALUE)str2, str); return str2; } +static VALUE +str_new3(VALUE klass, VALUE str) +{ + VALUE str2 = str_new_shared(klass, str); + + rb_enc_copy(str2, str); + return str2; +} + VALUE rb_str_new3(VALUE str) { @@ -5108,6 +5116,21 @@ str_encoding(VALUE str) } +/* + * call-seq: + * str.force_encoding(encoding) => str + * + * Changes the encoding to +encoding+ and returns self. + */ + +static VALUE +rb_str_force_encoding(VALUE str, VALUE encname) +{ + str_modifiable(str); + rb_enc_associate(str, rb_enc_find(StringValueCStr(encname))); + return str; +} + /********************************************************************** * Document-class: Symbol * @@ -5519,6 +5542,7 @@ Init_String(void) rb_define_method(rb_cString, "rpartition", rb_str_rpartition, 1); rb_define_method(rb_cString, "encoding", str_encoding, 0); + rb_define_method(rb_cString, "force_encoding", rb_str_force_encoding, 1); id_to_s = rb_intern("to_s"); -- cgit