From 0b3092922d0ff12923852e64e9146f99d6191287 Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 18 Jan 2001 08:43:14 +0000 Subject: * io.c (rb_io_s_read): new method to call IO#read from pathname. In addition, it accepts third optional argument to specify starting point. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1137 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 ++++++++- ToDo | 1 + eval.c | 1 - ext/socket/socket.c | 2 +- io.c | 48 +++++++++++++++++++++++++++++++++++------------- lib/date.rb | 38 ++++++++++++++++++-------------------- lib/irb/ruby-lex.rb | 2 +- object.c | 6 +++--- pack.c | 30 ++++++++++++++++++++++++++---- 9 files changed, 93 insertions(+), 44 deletions(-) diff --git a/ChangeLog b/ChangeLog index b41cb40961..ce966b16f2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Thu Jan 18 04:28:14 2001 Yukihiro Matsumoto + + * io.c (rb_io_s_read): new method to call IO#read from + pathname. In addition, it accepts third optional argument to + specify starting point. + Wed Jan 17 13:28:26 2001 WATANABE Hirofumi * configure.in: remove DEFS definition. @@ -15,7 +21,8 @@ Tue Jan 16 17:00:50 2001 Minero Aoki Mon Jan 15 16:00:07 2001 Yukihiro Matsumoto * pack.c (pack_unpack): should check associated pointer packed by - pack("P"). restriction added. + pack("P"). Thus pointers can be retrieved only from pointer + packed strings. restriction added. Sun Jan 14 21:49:28 2001 Koji Arai diff --git a/ToDo b/ToDo index b42979f7b3..52901914af 100644 --- a/ToDo +++ b/ToDo @@ -43,6 +43,7 @@ Hacking Interpreter * remove stdio dependency from IOs. * warn for inconsistent local variable usage (lv m and method m at the same time). * MicroRuby +* Built-in Interactive Ruby. Standard Libraries diff --git a/eval.c b/eval.c index ec678eaf34..8609f89b86 100644 --- a/eval.c +++ b/eval.c @@ -6300,7 +6300,6 @@ block_pass(self, node) } POP_TAG(); POP_ITER(); - printf("state: %d(%d)\n", state, _block.tag->dst); if (_block.tag->dst == state) { if (orphan) { state &= TAG_MASK; diff --git a/ext/socket/socket.c b/ext/socket/socket.c index e77e3d609f..d688869c65 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -420,7 +420,7 @@ enum sock_recv_type { RECV_RECV, /* BasicSocket#recv(no from) */ RECV_IP, /* IPSocket#recvfrom */ RECV_UNIX, /* UNIXSocket#recvfrom */ - RECV_SOCKET, /* Socket#recvfrom */ + RECV_SOCKET /* Socket#recvfrom */ }; static VALUE diff --git a/io.c b/io.c index a9dd8a85f8..03a21974a8 100644 --- a/io.c +++ b/io.c @@ -3022,7 +3022,7 @@ struct foreach_arg { }; static VALUE -rb_io_foreach_line(arg) +io_s_foreach(arg) struct foreach_arg *arg; { VALUE str; @@ -3048,21 +3048,14 @@ rb_io_s_foreach(argc, argv, io) arg.argc = argc - 1; arg.io = rb_io_open(RSTRING(fname)->ptr, "r"); if (NIL_P(arg.io)) return Qnil; - return rb_ensure(rb_io_foreach_line, (VALUE)&arg, rb_io_close, arg.io); + return rb_ensure(io_s_foreach, (VALUE)&arg, rb_io_close, arg.io); } static VALUE -rb_io_readline_line(arg) +io_s_readlines(arg) struct foreach_arg *arg; { - VALUE line, ary; - - ary = rb_ary_new(); - while (!NIL_P(line = rb_io_gets_internal(arg->argc, &arg->sep, arg->io))) { - rb_ary_push(ary, line); - } - - return ary; + return rb_io_readlines(arg->argc, &arg->sep, arg->io); } static VALUE @@ -3080,7 +3073,35 @@ rb_io_s_readlines(argc, argv, io) arg.argc = argc - 1; arg.io = rb_io_open(RSTRING(fname)->ptr, "r"); if (NIL_P(arg.io)) return Qnil; - return rb_ensure(rb_io_readline_line, (VALUE)&arg, rb_io_close, arg.io); + return rb_ensure(io_s_readlines, (VALUE)&arg, rb_io_close, arg.io); +} + +static VALUE +io_s_read(arg) + struct foreach_arg *arg; +{ + return io_read(arg->argc, &arg->sep, arg->io); +} + +static VALUE +rb_io_s_read(argc, argv, io) + int argc; + VALUE *argv; + VALUE io; +{ + VALUE fname, offset; + struct foreach_arg arg; + + rb_scan_args(argc, argv, "12", &fname, &arg.sep, &offset); + Check_SafeStr(fname); + + arg.argc = argc ? 1 : 0; + arg.io = rb_io_open(RSTRING(fname)->ptr, "r"); + if (NIL_P(arg.io)) return Qnil; + if (!NIL_P(offset)) { + rb_io_seek(1, &offset, arg.io); + } + return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io); } static VALUE @@ -3115,7 +3136,7 @@ argf_set_pos(self, offset) VALUE self, offset; { if (!next_argv()) { - rb_raise(rb_eArgError, "no stream to pos"); + rb_raise(rb_eArgError, "no stream to set position"); } if (TYPE(current_file) != T_FILE) { @@ -3365,6 +3386,7 @@ Init_IO() rb_define_singleton_method(rb_cIO, "popen", rb_io_s_popen, -1); rb_define_singleton_method(rb_cIO, "foreach", rb_io_s_foreach, -1); rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1); + rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1); rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1); rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, 0); diff --git a/lib/date.rb b/lib/date.rb index 58179a7153..3422121298 100644 --- a/lib/date.rb +++ b/lib/date.rb @@ -1,5 +1,5 @@ -# date.rb: Written by Tadayoshi Funaba 1998-2000 -# $Id: date.rb,v 1.22 2000-07-16 10:23:40+09 tadf Exp $ +# date2.rb: Written by Tadayoshi Funaba 1998-2001 +# $Id: date2.rb,v 1.23 2001-01-18 12:09:47+09 tadf Exp $ class Date @@ -128,16 +128,15 @@ class Date end if d < 0 ny, nm = clfloor(y * 12 + m, 12) - nm, = clfloor(m + 1, 1) - la = nil - 31.downto 1 do |z| - break if la = exist3?(y, m, z, sg) - end - ns = ns?(la, sg) - d = jd_to_civil(civil_to_jd(ny, nm, 1, ns) + d, ns)[-1] + nm, = clfloor(nm + 1, 1) + jd = civil_to_jd(ny, nm, d + 1, sg) + ns = ns?(jd, sg) + return unless [y, m] == jd_to_civil(jd, sg)[0..1] + return unless [ny, nm, 1] == jd_to_civil(jd - d, ns) + else + jd = civil_to_jd(y, m, d, sg) + return unless [y, m, d] == jd_to_civil(jd, sg) end - jd = civil_to_jd(y, m, d, sg) - return unless [y, m, d] == jd_to_civil(jd, sg) jd end @@ -154,16 +153,15 @@ class Date def exist2? (y, d, sg=ITALY) if d < 0 - ny = y + 1 - la = nil - 366.downto 1 do |z| - break if la = exist2?(y, z, sg) - end - ns = ns?(la, sg) - d = jd_to_ordinal(ordinal_to_jd(ny, 1, ns) + d, ns)[-1] + ny, = clfloor(y + 1, 1) + jd = ordinal_to_jd(ny, d + 1, sg) + ns = ns?(jd, sg) + return unless [y] == jd_to_ordinal(jd, sg)[0..0] + return unless [ny, 1] == jd_to_ordinal(jd - d, ns) + else + jd = ordinal_to_jd(y, d, sg) + return unless [y, d] == jd_to_ordinal(jd, sg) end - jd = ordinal_to_jd(y, d, sg) - return unless [y, d] == jd_to_ordinal(jd, sg) jd end diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 4c7a3b1002..534870e329 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -608,7 +608,7 @@ class RubyLex identify_quotation elsif peek(0) == '=' getc - Token(OP_ASGIN, "%") + Token(TkOPASGN, :%) elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/ identify_quotation else diff --git a/object.c b/object.c index 6ded7b089d..159198f922 100644 --- a/object.c +++ b/object.c @@ -288,7 +288,7 @@ rb_obj_taint(obj) VALUE obj; { rb_secure(4); - if (OBJ_TAINTED(obj)) { + if (!OBJ_TAINTED(obj)) { if (OBJ_FROZEN(obj)) { rb_error_frozen("object"); } @@ -302,7 +302,7 @@ rb_obj_untaint(obj) VALUE obj; { rb_secure(3); - if (!OBJ_TAINTED(obj)) { + if (OBJ_TAINTED(obj)) { if (OBJ_FROZEN(obj)) { rb_error_frozen("object"); } @@ -315,7 +315,7 @@ VALUE rb_obj_freeze(obj) VALUE obj; { - if (OBJ_FROZEN(obj)) { + if (!OBJ_FROZEN(obj)) { if (rb_safe_level() >= 4 && !OBJ_TAINTED(obj)) { rb_raise(rb_eSecurityError, "Insecure: can't freeze object"); } diff --git a/pack.c b/pack.c index 87d64e3bef..7aeded5cbd 100644 --- a/pack.c +++ b/pack.c @@ -1625,8 +1625,12 @@ pack_unpack(str, fmt) p = RARRAY(a)->ptr; pend = p + RARRAY(a)->len; while (p < pend) { - if (TYPE(*p) == T_STRING && RSTRING(*p)->ptr == t) + if (TYPE(*p) == T_STRING && RSTRING(*p)->ptr == t) { + if (len > RSTRING(*p)->len) { + len = RSTRING(*p)->len; + } break; + } p++; } if (p == pend) { @@ -1649,13 +1653,31 @@ pack_unpack(str, fmt) break; else { char *t; - VALUE str = rb_str_new(0, 0); + VALUE a, tmp; + VALUE *p, *pend; + + + if (!(a = rb_str_associated(str))) { + rb_raise(rb_eArgError, "no associated pointer"); + } memcpy(&t, s, sizeof(char *)); s += sizeof(char *); + if (t) { - rb_str_cat2(str, t); + p = RARRAY(a)->ptr; + pend = p + RARRAY(a)->len; + while (p < pend) { + if (TYPE(*p) == T_STRING && RSTRING(*p)->ptr == t) { + break; + } + p++; + } + if (p == pend) { + rb_raise(rb_eArgError, "non associated pointer"); + } + tmp = rb_str_new2(t); } - rb_ary_push(ary, str); + rb_ary_push(ary, tmp); } } break; -- cgit v1.2.3