* io.c (rb_io_s_read): encoding argument reverted.

* io.c (mode_enc): independent function to share code.

* io.c (rb_io_internal_encoding): new method.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14531 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-12-23 15:56:41 +00:00
parent 53e0672c4e
commit 2e791ace7f
3 changed files with 82 additions and 54 deletions

View File

@ -1,3 +1,11 @@
Mon Dec 24 00:52:15 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* io.c (rb_io_s_read): encoding argument reverted.
* io.c (mode_enc): independent function to share code.
* io.c (rb_io_internal_encoding): new method.
Mon Dec 24 00:47:05 2007 Yukihiro Matsumoto <matz@ruby-lang.org> Mon Dec 24 00:47:05 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* test/ruby/test_beginendblock.rb (TestBeginEndBlock::test_endblockwarn): * test/ruby/test_beginendblock.rb (TestBeginEndBlock::test_endblockwarn):

120
io.c
View File

@ -348,9 +348,9 @@ rb_io_check_readable(rb_io_t *fptr)
io_fflush(fptr); io_fflush(fptr);
} }
if (!fptr->enc) { if (!fptr->enc) {
fptr->enc = (fptr->fd == 0) fptr->enc = (fptr->mode & FMODE_BINMODE)
? rb_default_external_encoding() ? rb_ascii8bit_encoding()
: rb_ascii8bit_encoding(); : rb_default_external_encoding();
} }
} }
@ -3091,42 +3091,52 @@ rb_io_modenum_mode(int flags)
return NULL; /* not reached */ return NULL; /* not reached */
} }
void static void
rb_io_mode_enc(rb_io_t *fptr, const char *mode) mode_enc(rb_io_t *fptr, const char *estr)
{ {
const char *p0, *p1; const char *p0, *p1;
char *enc2name; char *enc2name;
int idx, idx2; int idx, idx2;
p0 = strrchr(mode, ':'); p0 = strrchr(estr, ':');
if (p0) { if (!p0) p1 = estr;
idx = rb_enc_find_index(p0+1); else p1 = p0 + 1;
if (idx >= 0) { idx = rb_enc_find_index(p1);
fptr->enc = rb_enc_from_index(idx); if (idx >= 0) {
fptr->enc = rb_enc_from_index(idx);
}
else {
rb_warn("Unsupported encoding %s ignored", p1);
}
p1 = strchr(estr, ':');
if (p0 && p1 && p1 < p0) {
enc2name = ALLOCA_N(char, p0-p1);
strncpy(enc2name, p1+1, p0-p1-1);
enc2name[p0-p1-1] = '\0';
idx2=rb_enc_find_index(enc2name);
if (idx2 == idx) {
rb_warn("Ignoring internal encoding %s: it is identical to external encoding %s",
enc2name, p0+1);
}
else if (idx2 >= 0) {
fptr->enc2 = rb_enc_from_index(idx2);
} }
else { else {
rb_warn("Unsupported encoding %s ignored", p0+1); rb_warn("Unsupported encoding %s ignored", enc2name);
}
p1 = strchr(mode, ':');
if (p1 < p0) {
enc2name = ALLOCA_N(char, p0-p1);
strncpy(enc2name, p1+1, p0-p1-1);
enc2name[p0-p1-1] = '\0';
idx2=rb_enc_find_index(enc2name);
if (idx2 == idx) {
rb_warn("Ignoring internal encoding %s: it is identical to external encoding %s",
enc2name, p0+1);
}
else if (idx2 >= 0) {
fptr->enc2 = rb_enc_from_index(idx2);
}
else {
rb_warn("Unsupported encoding %s ignored", enc2name);
}
} }
} }
} }
void
rb_io_mode_enc(rb_io_t *fptr, const char *mode)
{
const char *p = strchr(mode, ':');
if (p) {
mode_enc(fptr, p+1);
}
}
struct sysopen_struct { struct sysopen_struct {
char *fname; char *fname;
int flag; int flag;
@ -5683,14 +5693,10 @@ io_s_read(struct foreach_arg *arg)
/* /*
* call-seq: * call-seq:
* IO.read(name, [length [, offset]] ) => string * IO.read(name, [length [, offset]] ) => string
* IO.read(name, encoding) => string
* *
* Opens the file, optionally seeks to the given offset, then returns * Opens the file, optionally seeks to the given offset, then returns
* <i>length</i> bytes (defaulting to the rest of the file). * <i>length</i> bytes (defaulting to the rest of the file).
* <code>read</code> ensures the file is closed before returning. * <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") #=> "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" * IO.read("testfile", 20) #=> "This is line one\nThi"
@ -5700,28 +5706,16 @@ io_s_read(struct foreach_arg *arg)
static VALUE static VALUE
rb_io_s_read(int argc, VALUE *argv, VALUE io) rb_io_s_read(int argc, VALUE *argv, VALUE io)
{ {
rb_encoding *enc = 0; VALUE fname, offset, tmp = Qnil;
VALUE fname, arg1, offset;
struct foreach_arg arg; struct foreach_arg arg;
rb_scan_args(argc, argv, "12", &fname, &arg1, &offset); rb_scan_args(argc, argv, "12", &fname, NULL, &offset);
FilePathValue(fname); FilePathValue(fname);
if (argc == 2 && (enc = rb_to_encoding(arg1))) { arg.argc = argc > 1 ? 1 : 0;
arg.argc = 0; arg.argv = argv + 1;
}
else {
arg.argc = argc > 1 ? 1 : 0;
arg.argv = argv + 1;
}
arg.io = rb_io_open(RSTRING_PTR(fname), "r"); arg.io = rb_io_open(RSTRING_PTR(fname), "r");
if (NIL_P(arg.io)) return Qnil; if (NIL_P(arg.io)) return Qnil;
if (enc) { if (!NIL_P(offset)) {
rb_io_t *fptr;
GetOpenFile(arg.io, fptr);
fptr->enc = enc;
}
else if (!NIL_P(offset)) {
rb_io_binmode(arg.io); rb_io_binmode(arg.io);
rb_io_seek(arg.io, offset, SEEK_SET); rb_io_seek(arg.io, offset, SEEK_SET);
} }
@ -5748,10 +5742,34 @@ rb_io_external_encoding(VALUE io)
return rb_enc_from_encoding(fptr->enc); return rb_enc_from_encoding(fptr->enc);
} }
/*
* call-seq:
* io.internal_encoding => encoding
*
* Returns the Encoding of the internal string if conversion is
* specified. Otherwise returns nil.
*/
static VALUE
rb_io_internal_encoding(VALUE io)
{
rb_io_t *fptr;
GetOpenFile(io, fptr);
if (!fptr->enc2) return Qnil;
return rb_enc_from_encoding(fptr->enc2);
}
static VALUE static VALUE
argf_external_encoding(void) argf_external_encoding(void)
{ {
return rb_enc_default_external(); return rb_io_external_encoding(current_file);
}
static VALUE
argf_internal_encoding(void)
{
return rb_io_internal_encoding(current_file);
} }
static VALUE static VALUE
@ -6331,6 +6349,7 @@ Init_IO(void)
rb_define_method(rb_cIO, "inspect", rb_io_inspect, 0); rb_define_method(rb_cIO, "inspect", rb_io_inspect, 0);
rb_define_method(rb_cIO, "external_encoding", rb_io_external_encoding, 0); rb_define_method(rb_cIO, "external_encoding", rb_io_external_encoding, 0);
rb_define_method(rb_cIO, "internal_encoding", rb_io_internal_encoding, 0);
rb_define_variable("$stdin", &rb_stdin); rb_define_variable("$stdin", &rb_stdin);
rb_stdin = prep_stdio(stdin, FMODE_READABLE, rb_cIO, "<STDIN>"); rb_stdin = prep_stdio(stdin, FMODE_READABLE, rb_cIO, "<STDIN>");
@ -6391,6 +6410,7 @@ Init_IO(void)
rb_define_singleton_method(argf, "lineno=", argf_set_lineno, 1); rb_define_singleton_method(argf, "lineno=", argf_set_lineno, 1);
rb_define_singleton_method(argf, "external_encoding", argf_external_encoding, 0); rb_define_singleton_method(argf, "external_encoding", argf_external_encoding, 0);
rb_define_singleton_method(argf, "internal_encoding", argf_internal_encoding, 0);
rb_global_variable(&current_file); rb_global_variable(&current_file);
rb_define_readonly_variable("$FILENAME", &filename); rb_define_readonly_variable("$FILENAME", &filename);

View File

@ -1,7 +1,7 @@
#define RUBY_VERSION "1.9.0" #define RUBY_VERSION "1.9.0"
#define RUBY_RELEASE_DATE "2007-12-23" #define RUBY_RELEASE_DATE "2007-12-24"
#define RUBY_VERSION_CODE 190 #define RUBY_VERSION_CODE 190
#define RUBY_RELEASE_CODE 20071223 #define RUBY_RELEASE_CODE 20071224
#define RUBY_PATCHLEVEL 0 #define RUBY_PATCHLEVEL 0
#define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MAJOR 1
@ -9,7 +9,7 @@
#define RUBY_VERSION_TEENY 0 #define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_YEAR 2007 #define RUBY_RELEASE_YEAR 2007
#define RUBY_RELEASE_MONTH 12 #define RUBY_RELEASE_MONTH 12
#define RUBY_RELEASE_DAY 23 #define RUBY_RELEASE_DAY 24
#ifdef RUBY_EXTERN #ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[]; RUBY_EXTERN const char ruby_version[];
@ -32,7 +32,7 @@ RUBY_EXTERN const char ruby_copyright[];
#define RUBY_REVISION 0 #define RUBY_REVISION 0
#endif #endif
#if !RUBY_REVISION #if RUBY_VERSION_TEENY > 0 && RUBY_PATCHLEVEL < 5000 && !RUBY_REVISION
#define RUBY_RELEASE_STR "patchlevel" #define RUBY_RELEASE_STR "patchlevel"
#define RUBY_RELEASE_NUM RUBY_PATCHLEVEL #define RUBY_RELEASE_NUM RUBY_PATCHLEVEL
#else #else