Revision: 3782 http://trac.macosforge.org/projects/ruby/changeset/3782 Author: lsansonetti@apple.com Date: 2010-03-16 18:43:05 -0700 (Tue, 16 Mar 2010) Log Message: ----------- IO and Marshal now returning UTF8 strings Modified Paths: -------------- MacRuby/trunk/encoding.h MacRuby/trunk/io.c MacRuby/trunk/marshal.c MacRuby/trunk/string.c Modified: MacRuby/trunk/encoding.h =================================================================== --- MacRuby/trunk/encoding.h 2010-03-17 00:32:04 UTC (rev 3781) +++ MacRuby/trunk/encoding.h 2010-03-17 01:43:05 UTC (rev 3782) @@ -306,6 +306,7 @@ unsigned long rb_str_hash_uchars(const UChar *chars, long chars_len); long rb_uchar_strtol(UniChar *chars, long chars_len, long pos, long *end_offset); +void rb_str_force_encoding(VALUE str, rb_encoding_t *encoding); #if defined(__cplusplus) } // extern "C" Modified: MacRuby/trunk/io.c =================================================================== --- MacRuby/trunk/io.c 2010-03-17 00:32:04 UTC (rev 3781) +++ MacRuby/trunk/io.c 2010-03-17 01:43:05 UTC (rev 3782) @@ -1118,12 +1118,15 @@ rb_io_t *io_struct = ExtractIOStruct(io); rb_io_assert_readable(io_struct); + bool outbuf_created = false; if (NIL_P(outbuf)) { outbuf = rb_bstr_new(); + outbuf_created = true; } if (NIL_P(len)) { - return rb_io_read_all(io_struct, outbuf); + rb_io_read_all(io_struct, outbuf); + goto bail; } const long size = FIX2LONG(len); @@ -1148,6 +1151,10 @@ } rb_bstr_set_length(outbuf, data_read); +bail: + if (outbuf_created) { + rb_str_force_encoding(outbuf, rb_encodings[ENCODING_UTF8]); + } return outbuf; } @@ -1365,6 +1372,7 @@ OBJ_TAINT(bstr); io_struct->lineno += 1; ARGF.lineno = INT2FIX(io_struct->lineno); + rb_str_force_encoding(bstr, rb_encodings[ENCODING_UTF8]); return bstr; } @@ -3346,7 +3354,7 @@ VALUE outbuf = rb_bstr_new(); rb_io_read_all(ExtractIOStruct(io), outbuf); rb_io_close(io); - + rb_str_force_encoding(outbuf, rb_encodings[ENCODING_UTF8]); return outbuf; } @@ -3852,7 +3860,7 @@ void *ptr; while ((ptr = memchr(&bytes[pos], byte, length - pos)) != NULL) { const long s = (long)ptr - (long)&bytes[pos] + 1; - rb_ary_push(ary, rb_bstr_new_with_data(&bytes[pos], s)); + rb_ary_push(ary, rb_str_new((char *)&bytes[pos], s)); pos += s; } } Modified: MacRuby/trunk/marshal.c =================================================================== --- MacRuby/trunk/marshal.c 2010-03-17 00:32:04 UTC (rev 3781) +++ MacRuby/trunk/marshal.c 2010-03-17 01:43:05 UTC (rev 3782) @@ -1023,7 +1023,9 @@ if (got_io) { rb_io_write(port, 0, arg->str); } - + else { + rb_str_force_encoding(port, rb_encodings[ENCODING_UTF8]); + } return port; } Modified: MacRuby/trunk/string.c =================================================================== --- MacRuby/trunk/string.c 2010-03-17 00:32:04 UTC (rev 3781) +++ MacRuby/trunk/string.c 2010-03-17 01:43:05 UTC (rev 3782) @@ -1533,22 +1533,28 @@ * Changes the encoding to +encoding+ and returns self. */ +void +rb_str_force_encoding(VALUE str, rb_encoding_t *enc) +{ + assert(IS_RSTR(str)); + if (enc != RSTR(str)->encoding) { + str_make_data_binary(RSTR(str)); + if (NATIVE_UTF16_ENC(RSTR(str)->encoding)) { + str_set_stored_in_uchars(RSTR(str), false); + } + RSTR(str)->encoding = enc; + str_unset_facultative_flags(RSTR(str)); + if (NATIVE_UTF16_ENC(RSTR(str)->encoding)) { + str_set_stored_in_uchars(RSTR(str), true); + } + } +} + static VALUE rstr_force_encoding(VALUE self, SEL sel, VALUE encoding) { rstr_modify(self); - rb_encoding_t *enc = rb_to_encoding(encoding); - if (enc != RSTR(self)->encoding) { - str_make_data_binary(RSTR(self)); - if (NATIVE_UTF16_ENC(RSTR(self)->encoding)) { - str_set_stored_in_uchars(RSTR(self), false); - } - RSTR(self)->encoding = enc; - str_unset_facultative_flags(RSTR(self)); - if (NATIVE_UTF16_ENC(RSTR(self)->encoding)) { - str_set_stored_in_uchars(RSTR(self), true); - } - } + rb_str_force_encoding(self, rb_to_encoding(encoding)); return self; }
participants (1)
-
source_changes@macosforge.org