1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* io.c (rb_io_mode_flags): preserve append mode flag.

[ruby-dev:24436]

* io.c (rb_io_modenum_mode): do not use external output buffer.

* string.c (rb_str_justify): differ pointer retrieval to prevent
  padding string modification.  [ruby-dev:24434]

* range.c (range_each_func): allow func to terminate loop by
  returning RANGE_EACH_BREAK.

* range.c (member_i): use RANGE_EACH_BREAK. [ruby-talk:114959]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6999 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2004-10-06 07:40:06 +00:00
parent 3d17082a50
commit fbb88b011a
13 changed files with 90 additions and 89 deletions

View file

@ -1,3 +1,18 @@
Wed Oct 6 09:21:00 2004 Yukihiro Matsumoto <matz@ruby-lang.org>
* io.c (rb_io_mode_flags): preserve append mode flag.
[ruby-dev:24436]
* io.c (rb_io_modenum_mode): do not use external output buffer.
* string.c (rb_str_justify): differ pointer retrieval to prevent
padding string modification. [ruby-dev:24434]
* range.c (range_each_func): allow func to terminate loop by
returning RANGE_EACH_BREAK.
* range.c (member_i): use RANGE_EACH_BREAK. [ruby-talk:114959]
Tue Oct 5 09:53:22 2004 Yukihiro Matsumoto <matz@ruby-lang.org>
* io.c (rb_fopen): mode string copy at the lowest level.

6
eval.c
View file

@ -2187,7 +2187,6 @@ is_defined(self, node, buf, noeval)
int state;
static const char *ex = "expression";
again:
if (!node) return ex;
switch (nd_type(node)) {
case NODE_SUPER:
@ -7745,7 +7744,6 @@ static void
frame_dup(frame)
struct FRAME *frame;
{
VALUE *argv;
struct FRAME *tmp;
for (;;) {
@ -7873,7 +7871,6 @@ bind_eval(argc, argv, bind)
{
struct BLOCK *data;
VALUE args[4];
VALUE dummy;
rb_scan_args(argc, argv, "12", &args[0], &args[2], &args[3]);
args[1] = bind;
@ -11343,7 +11340,7 @@ rb_thread_start_0(fn, arg, th)
{
volatile rb_thread_t th_save = th;
volatile VALUE thread = th->thread;
struct BLOCK *volatile saved_block = 0, *block;
struct BLOCK *volatile saved_block = 0;
enum thread_status status;
int state;
@ -12146,7 +12143,6 @@ rb_callcc(self)
volatile rb_thread_t th_save;
struct tag *tag;
struct RVarmap *vars;
struct BLOCK *blk;
THREAD_ALLOC(th);
cont = Data_Wrap_Struct(rb_cCont, thread_mark, thread_free, th);

4
gc.c
View file

@ -614,8 +614,6 @@ mark_locations_array(x, n)
register VALUE *x;
register long n;
{
VALUE tmp;
while (n--) {
if (is_pointer_to_heap((void *)*x)) {
gc_mark(*x, 0);
@ -1033,7 +1031,7 @@ gc_sweep()
{
RVALUE *p, *pend, *final_list;
int freed = 0;
int i, j;
int i;
unsigned long live = 0;
mark_source_filename(ruby_sourcefile);

2
hash.c
View file

@ -862,8 +862,6 @@ static VALUE
rb_hash_clear(hash)
VALUE hash;
{
void *tmp;
rb_hash_modify(hash);
if (RHASH(hash)->tbl->num_entries > 0) {
rb_hash_foreach(hash, clear_i, 0);

View file

@ -253,7 +253,7 @@ VALUE rb_gc_enable _((void));
VALUE rb_gc_disable _((void));
VALUE rb_gc_start _((void));
/* hash.c */
void st_foreach _((struct st_table *, int (*)(), unsigned long));
void st_foreach_safe _((struct st_table *, int (*)(), unsigned long));
void rb_hash_foreach _((VALUE, int (*)(), VALUE));
VALUE rb_hash _((VALUE));
VALUE rb_hash_new _((void));

72
io.c
View file

@ -2288,11 +2288,16 @@ rb_io_flags_mode(flags)
int flags;
{
#ifdef O_BINARY
# define MODE_BINMODE(a,b) ((flags & O_BINARY) ? (a) : (b))
# define MODE_BINMODE(a,b) ((flags & FMODE_BINMODE) ? (a) : (b))
#else
# define MODE_BINMODE(a,b) (a)
#endif
if (flags & FMODE_APPEND) {
if ((flags & FMODE_READWRITE) == FMODE_READWRITE) {
return MODE_BINMODE("a+", "ab+");
}
return MODE_BINMODE("a", "ab");
}
switch (flags & FMODE_READWRITE) {
case FMODE_READABLE:
return MODE_BINMODE("r", "rb");
@ -2320,7 +2325,7 @@ rb_io_mode_flags(mode)
flags |= FMODE_WRITABLE;
break;
case 'a':
flags |= FMODE_WRITABLE;
flags |= FMODE_WRITABLE | FMODE_APPEND;
break;
default:
error:
@ -2357,10 +2362,13 @@ rb_io_modenum_flags(mode)
flags = FMODE_WRITABLE;
break;
case O_RDWR:
flags = FMODE_WRITABLE|FMODE_READABLE;
flags = FMODE_READWRITE;
break;
}
if (mode & O_APPEND) {
flags |= FMODE_APPEND;
}
#ifdef O_BINARY
if (mode & O_BINARY) {
flags |= FMODE_BINMODE;
@ -2413,36 +2421,30 @@ rb_io_mode_modenum(mode)
#define MODENUM_MAX 4
static char*
rb_io_modenum_mode(flags, mode)
rb_io_modenum_mode(flags)
int flags;
char *mode;
{
char *p = mode;
#ifdef O_BINARY
# define MODE_BINARY(a,b) ((mode & O_BINARY) ? (a) : (b))
#else
# define MODE_BINARY(a,b) (a)
#endif
if (flags & O_APPEND) {
if ((flags & O_RDWR) == O_RDWR) {
return MODE_BINARY("a+", "ab+");
}
return MODE_BINARY("a", "ab");
}
switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
case O_RDONLY:
*p++ = 'r';
break;
return MODE_BINARY("r", "rb");
case O_WRONLY:
*p++ = 'w';
break;
return MODE_BINARY("w", "wb");
case O_RDWR:
*p++ = 'r';
*p++ = '+';
break;
return MODE_BINARY("r+", "rb+");
}
*p++ = '\0';
#ifdef O_BINARY
if (flags & O_BINARY) {
if (mode[1] == '+') {
mode[1] = 'b'; mode[2] = '+'; mode[3] = '\0';
}
else {
mode[1] = 'b'; mode[2] = '\0';
}
}
#endif
return mode;
rb_raise(rb_eArgError, "illegal access modenum %o", flags);
return NULL; /* not reached */
}
static int
@ -2567,12 +2569,11 @@ rb_file_sysopen_internal(io, fname, flags, mode)
OpenFile *fptr;
int fd;
char *m;
char mbuf[MODENUM_MAX];
MakeOpenFile(io, fptr);
fd = rb_sysopen(fname, flags, mode);
m = rb_io_modenum_mode(flags, mbuf);
m = rb_io_modenum_mode(flags);
fptr->mode = rb_io_modenum_flags(flags);
fptr->f = rb_fdopen(fd, m);
fptr->path = strdup(fname);
@ -2871,13 +2872,12 @@ rb_io_popen(str, argc, argv, klass)
{
char *mode;
VALUE pname, pmode, port;
char mbuf[MODENUM_MAX];
if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) {
mode = "r";
}
else if (FIXNUM_P(pmode)) {
mode = rb_io_modenum_mode(FIX2INT(pmode), mbuf);
mode = rb_io_modenum_mode(FIX2INT(pmode));
}
else {
mode = StringValuePtr(pmode);
@ -2964,7 +2964,7 @@ rb_io_s_popen(argc, argv, klass)
mode = "r";
}
else if (FIXNUM_P(pmode)) {
mode = rb_io_modenum_mode(FIX2INT(pmode), mbuf);
mode = rb_io_modenum_mode(FIX2INT(pmode));
}
else {
strncpy(mbuf, StringValuePtr(pmode), sizeof(mbuf) - 1);
@ -3346,7 +3346,7 @@ rb_io_reopen(argc, argv, file)
VALUE file;
{
VALUE fname, nmode;
char mode[MODENUM_MAX];
char *mode;
OpenFile *fptr;
rb_secure(4);
@ -3375,8 +3375,9 @@ rb_io_reopen(argc, argv, file)
}
fptr->path = strdup(RSTRING(fname)->ptr);
mode = rb_io_flags_mode(fptr->mode);
if (!fptr->f) {
fptr->f = rb_fopen(fptr->path, rb_io_flags_mode(fptr->mode));
fptr->f = rb_fopen(fptr->path, mode);
if (fptr->f2) {
fclose(fptr->f2);
fptr->f2 = 0;
@ -3936,7 +3937,6 @@ rb_io_initialize(argc, argv, io)
VALUE fnum, mode, orig;
OpenFile *fp, *ofp = NULL;
int fd, flags, fmode;
char mbuf[MODENUM_MAX];
rb_secure(4);
rb_scan_args(argc, argv, "11", &fnum, &mode);
@ -3973,7 +3973,7 @@ rb_io_initialize(argc, argv, io)
if (!ofp) {
MakeOpenFile(io, fp);
fp->mode = fmode;
fp->f = rb_fdopen(fd, rb_io_modenum_mode(flags, mbuf));
fp->f = rb_fdopen(fd, rb_io_modenum_mode(flags));
}
else {
if (argc == 2) {

View file

@ -134,6 +134,7 @@ class PStore
content = dump(@table)
if !md5 || size != content.size || md5 != Digest::MD5.digest(content)
File.open(tmp_file, "w") {|t|
t.binmode
t.write(content)
}
File.rename(tmp_file, new_file)
@ -168,6 +169,7 @@ class PStore
f.rewind
new_file = @filename + ".new"
File.open(new_file) do |nf|
nf.binmode
FileUtils.copy_stream(nf, f)
end
File.unlink(new_file)

View file

@ -4413,7 +4413,7 @@ rb_compile_string(f, s, line)
int line;
{
struct parser_params *parser = parser_new();
volatile VALUE p = parser->value;
volatile VALUE p = parser->value;
lex_gets = lex_get_str;
lex_gets_ptr = 0;

View file

@ -1559,7 +1559,6 @@ rb_spawn(argc, argv)
int status;
VALUE prog;
#if defined HAVE_FORK
int pid;
struct rb_exec_arg earg;
#endif

42
range.c
View file

@ -230,18 +230,8 @@ str_step(args)
return rb_str_upto(args[0], args[1], EXCL(args[2]));
}
static VALUE
step_i(i, iter)
VALUE i;
long *iter;
{
iter[0]--;
if (iter[0] == 0) {
rb_yield(i);
iter[0] = iter[1];
}
return Qnil;
}
#define RANGE_EACH_BREAK Qfalse
#define RANGE_EACH_CONTINUE Qtrue
static void
range_each_func(range, func, v, e, arg)
@ -254,19 +244,32 @@ range_each_func(range, func, v, e, arg)
if (EXCL(range)) {
while (r_lt(v, e)) {
(*func)(v, arg);
if ((*func)(v, arg) == RANGE_EACH_BREAK) break;
v = rb_funcall(v, id_succ, 0, 0);
}
}
else {
while (RTEST(c = r_le(v, e))) {
(*func)(v, arg);
if ((*func)(v, arg) == RANGE_EACH_BREAK) break;
if (c == INT2FIX(0)) break;
v = rb_funcall(v, id_succ, 0, 0);
}
}
}
static VALUE
step_i(i, iter)
VALUE i;
long *iter;
{
iter[0]--;
if (iter[0] == 0) {
rb_yield(i);
iter[0] = iter[1];
}
return RANGE_EACH_CONTINUE;
}
/*
* call-seq:
* rng.step(n=1) {| obj | block } => rng
@ -335,7 +338,7 @@ range_step(argc, argv, range)
if (unit == 0) rb_raise(rb_eArgError, "step can't be 0");
args[0] = b; args[1] = e; args[2] = range;
iter[0] = 1; iter[1] = unit;
rb_iterate((VALUE(*)_((VALUE)))str_step, (VALUE)args, step_i, (VALUE)iter);
rb_iterate(str_step, (VALUE)args, step_i, (VALUE)iter);
}
else if (rb_obj_is_kind_of(b, rb_cNumeric)) {
ID c = rb_intern(EXCL(range) ? "<" : "<=");
@ -368,7 +371,8 @@ each_i(v, arg)
VALUE v;
void *arg;
{
return rb_yield(v);
rb_yield(v);
return RANGE_EACH_CONTINUE;
}
/*
@ -417,7 +421,7 @@ range_each(range)
args[0] = beg; args[1] = end; args[2] = range;
iter[0] = 1; iter[1] = 1;
rb_iterate((VALUE(*)_((VALUE)))str_step, (VALUE)args, step_i, (VALUE)iter);
rb_iterate(str_step, (VALUE)args, step_i, (VALUE)iter);
}
else {
range_each_func(range, each_i, beg, end, NULL);
@ -548,14 +552,16 @@ range_inspect(range)
return str;
}
static void
static VALUE
member_i(v, args)
VALUE v;
VALUE *args;
{
if (rb_equal(v, args[0])) {
args[1] = Qtrue;
return RANGE_EACH_BREAK;
}
return RANGE_EACH_CONTINUE;
}
/*

View file

@ -30,6 +30,7 @@ typedef struct OpenFile {
#define FMODE_READABLE 1
#define FMODE_WRITABLE 2
#define FMODE_READWRITE 3
#define FMODE_APPEND 64
#define FMODE_BINMODE 4
#define FMODE_SYNC 8
#define FMODE_WBUF 16

View file

@ -1243,9 +1243,6 @@ static VALUE
rb_str_match(x, y)
VALUE x, y;
{
VALUE reg;
long start;
switch (TYPE(y)) {
case T_STRING:
rb_raise(rb_eTypeError, "type mismatch: String given");
@ -4462,18 +4459,17 @@ rb_str_justify(argc, argv, str, jflag)
long n;
VALUE pad;
if (rb_scan_args(argc, argv, "11", &w, &pad) == 2) {
if (!NIL_P(pad)) {
StringValue(pad);
if (RSTRING(pad)->len > 0) {
f = RSTRING(pad)->ptr;
flen = RSTRING(pad)->len;
}
}
}
rb_scan_args(argc, argv, "11", &w, &pad);
width = NUM2LONG(w);
if (width < 0 || RSTRING(str)->len >= width) return rb_str_dup(str);
res = rb_str_new5(str, 0, width);
if (argc == 2) {
StringValue(pad);
if (RSTRING(pad)->len > 0) {
f = RSTRING(pad)->ptr;
flen = RSTRING(pad)->len;
}
}
p = RSTRING(res)->ptr;
if (jflag != 'l') {
n = width - RSTRING(str)->len;

View file

@ -1695,15 +1695,6 @@ rb_define_global_const(name, val)
rb_define_const(rb_cObject, name, val);
}
static VALUE
original_module(c)
VALUE c;
{
if (TYPE(c) == T_ICLASS)
return RBASIC(c)->klass;
return c;
}
void
rb_cvar_set(klass, id, val, warn)
VALUE klass;
@ -1720,7 +1711,6 @@ rb_cvar_get(klass, id)
ID id;
{
VALUE value;
VALUE tmp;
if (RCLASS(klass)->iv_tbl && st_lookup(RCLASS(klass)->iv_tbl,id,&value)) {
return value;