ruby-changes:2920
From: ko1@a...
Date: 21 Dec 2007 16:03:12 +0900
Subject: [ruby-changes:2920] matz - Ruby:r14411 (trunk): * io.c (rb_io_s_read): allow specifying encoding explicitly.
matz 2007-12-21 16:02:55 +0900 (Fri, 21 Dec 2007) New Revision: 14411 Modified files: trunk/ChangeLog trunk/encoding.c trunk/io.c trunk/numeric.c Log: * io.c (rb_io_s_read): allow specifying encoding explicitly. * io.c (rb_io_binmode): specifies encoding to ASCII-8BIT (binary). * io.c (rb_io_s_read): IO should be in binary mode when offset is specified. * encoding.c (rb_to_encoding): returns default encoding if no corresponding encoding found. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/numeric.c?r1=14411&r2=14410 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=14411&r2=14410 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/io.c?r1=14411&r2=14410 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/encoding.c?r1=14411&r2=14410 Index: encoding.c =================================================================== --- encoding.c (revision 14410) +++ encoding.c (revision 14411) @@ -94,9 +94,12 @@ { int idx; - if (NIL_P(enc)) return rb_enc_from_index(0); + if (NIL_P(enc)) return 0; idx = enc_check_encoding(enc); if (idx >= 0) return RDATA(enc)->data; + if (NIL_P(enc = rb_check_string_type(enc))) { + return 0; + } if ((idx = rb_enc_find_index(StringValueCStr(enc))) < 0) { rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc)); } Index: ChangeLog =================================================================== --- ChangeLog (revision 14410) +++ ChangeLog (revision 14411) @@ -8,6 +8,18 @@ * parse.y (UTF8_ENC): uses rb_utf8_encoding(). +Fri Dec 21 15:31:59 2007 Yukihiro Matsumoto <matz@r...> + + * io.c (rb_io_s_read): allow specifying encoding explicitly. + + * io.c (rb_io_binmode): specifies encoding to ASCII-8BIT (binary). + + * io.c (rb_io_s_read): IO should be in binary mode when offset is + specified. + + * encoding.c (rb_to_encoding): returns default encoding if no + corresponding encoding found. + Fri Dec 21 15:24:22 2007 Shugo Maeda <shugo@r...> * lib/net/imap.rb (initialize): accept service name. changed Index: io.c =================================================================== --- io.c (revision 14410) +++ io.c (revision 14411) @@ -2289,7 +2289,7 @@ GetOpenFile(io, fptr); rb_io_check_readable(fptr); if (NIL_P(c)) return Qnil; - enc = rb_enc_get(io); + enc = fptr->enc ? fptr->enc : rb_default_external_encoding(); if (FIXNUM_P(c)) { int cc = FIX2INT(c); char buf[16]; @@ -2842,18 +2842,19 @@ VALUE rb_io_binmode(VALUE io) { -#if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__) rb_io_t *fptr; GetOpenFile(io, fptr); if (!(fptr->mode & FMODE_BINMODE) && READ_DATA_BUFFERED(fptr)) { rb_raise(rb_eIOError, "buffer already filled with text-mode content"); } +#if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__) if (0 <= fptr->fd && setmode(fptr->fd, O_BINARY) == -1) rb_sys_fail(fptr->path); +#endif fptr->mode |= FMODE_BINMODE; -#endif + fptr->enc = rb_default_encoding(); return io; } @@ -5629,10 +5630,14 @@ /* * call-seq: * IO.read(name, [length [, offset]] ) => string + * IO.read(name, encoding) => string * * Opens the file, optionally seeks to the given offset, then returns * <i>length</i> bytes (defaulting to the rest of the file). * <code>read</code> ensures the file is closed before returning. + * If the second argument is a string or an encoding object, the encoding + * of the result string is set to that encoding. Otherwise the encoding + * will be default external encoding. * * IO.read("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n" * IO.read("testfile", 20) #=> "This is line one\nThi" @@ -5642,16 +5647,29 @@ static VALUE rb_io_s_read(int argc, VALUE *argv, VALUE io) { - VALUE fname, offset; + rb_encoding *enc = 0; + VALUE fname, arg1, offset; struct foreach_arg arg; - rb_scan_args(argc, argv, "12", &fname, NULL, &offset); + rb_scan_args(argc, argv, "12", &fname, &arg1, &offset); FilePathValue(fname); - arg.argc = argc > 1 ? 1 : 0; - arg.argv = argv + 1; + if (argc == 2 && (enc = rb_to_encoding(arg1))) { + arg.argc = 0; + } + else { + arg.argc = argc > 1 ? 1 : 0; + arg.argv = argv + 1; + } arg.io = rb_io_open(RSTRING_PTR(fname), "r"); if (NIL_P(arg.io)) return Qnil; - if (!NIL_P(offset)) { + if (enc) { + rb_io_t *fptr; + + GetOpenFile(arg.io, fptr); + fptr->enc = enc; + } + else if (!NIL_P(offset)) { + rb_io_binmode(arg.io); rb_io_seek(arg.io, offset, SEEK_SET); } return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io); Index: numeric.c =================================================================== --- numeric.c (revision 14410) +++ numeric.c (revision 14411) @@ -1860,6 +1860,7 @@ break; } enc = rb_to_encoding(argv[0]); + if (!enc) enc = rb_default_encoding(); if (i < 0 || (n = rb_enc_codelen(i, enc)) <= 0) goto out_of_range; str = rb_enc_str_new(0, n, enc); rb_enc_mbcput(i, RSTRING_PTR(str), enc); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml