diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-02-03 14:43:07 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-02-03 14:43:07 +0000 |
commit | 97c65c74a1673b550aeb8d894871e77d237a5b54 (patch) | |
tree | ae78e955ba774b71dd830a890d83f9f9d2bf62e9 /ext/stringio | |
parent | 2633b2c4b7a9d3ba4508b01bfec205b69b77eb54 (diff) | |
download | ruby-97c65c74a1673b550aeb8d894871e77d237a5b54.tar.gz ruby-97c65c74a1673b550aeb8d894871e77d237a5b54.tar.xz ruby-97c65c74a1673b550aeb8d894871e77d237a5b54.zip |
* ext/stringio/stringio.c (strio_close, strio_close_read, strio_close_write):
should return nil instead of self as well as IO. [ruby-dev:25623]
* ext/stringio/stringio.c (strio_extend, strio_putc): fill with zero
extended portion. [ruby-dev:25626]
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@7874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/stringio')
-rw-r--r-- | ext/stringio/stringio.c | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index de9612136..4426786c4 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -339,7 +339,7 @@ strio_close(self) } ptr->string = Qnil; ptr->flags &= ~FMODE_READWRITE; - return self; + return Qnil; } static VALUE @@ -353,7 +353,7 @@ strio_close_read(self) if (!((ptr->flags &= ~FMODE_READABLE) & FMODE_READWRITE)) { ptr->string = Qnil; } - return self; + return Qnil; } static VALUE @@ -367,7 +367,7 @@ strio_close_write(self) if (!((ptr->flags &= ~FMODE_WRITABLE) & FMODE_READWRITE)) { ptr->string = Qnil; } - return self; + return Qnil; } static VALUE @@ -559,6 +559,25 @@ strio_getc(self) return CHR2FIX(c); } +static void +strio_extend(ptr, pos, len) + struct StringIO *ptr; + long pos, len; +{ + long olen; + + check_modifiable(ptr); + olen = RSTRING(ptr->string)->len; + if (pos + len > olen) { + rb_str_resize(ptr->string, pos + len); + if (pos > olen) + MEMZERO(RSTRING(ptr->string)->ptr + olen, char, pos - olen); + } + else { + rb_str_modify(ptr->string); + } +} + static VALUE strio_ungetc(self, ch) VALUE self, ch; @@ -568,18 +587,11 @@ strio_ungetc(self, ch) long len, pos = ptr->pos; if (cc != EOF && pos > 0) { - if ((len = RSTRING(ptr->string)->len) < pos || - (unsigned char)RSTRING(ptr->string)->ptr[pos - 1] != + if ((len = RSTRING(ptr->string)->len) < pos-- || + (unsigned char)RSTRING(ptr->string)->ptr[pos] != (unsigned char)cc) { - check_modifiable(ptr); - if (len < pos) { - rb_str_resize(ptr->string, pos); - MEMZERO(RSTRING(ptr->string)->ptr + len, char, pos - len - 1); - } - else { - rb_str_modify(ptr->string); - } - RSTRING(ptr->string)->ptr[pos - 1] = cc; + strio_extend(ptr, pos, 1); + RSTRING(ptr->string)->ptr[pos] = cc; OBJ_INFECT(ptr->string, self); } --ptr->pos; @@ -784,13 +796,7 @@ strio_write(self, str) rb_str_cat(ptr->string, RSTRING(str)->ptr, len); } else { - if (ptr->pos + len > olen) { - rb_str_resize(ptr->string, ptr->pos + len); - MEMZERO(RSTRING(ptr->string)->ptr + olen, char, ptr->pos + len - olen); - } - else { - rb_str_modify(ptr->string); - } + strio_extend(ptr, ptr->pos, len); rb_str_update(ptr->string, ptr->pos, len, str); } OBJ_INFECT(ptr->string, self); @@ -810,17 +816,14 @@ strio_putc(self, ch) { struct StringIO *ptr = writable(StringIO(self)); int c = NUM2CHR(ch); + long olen; check_modifiable(ptr); + olen = RSTRING(ptr->string)->len; if (ptr->flags & FMODE_APPEND) { - ptr->pos = RSTRING(ptr->string)->len; - } - if (ptr->pos >= RSTRING(ptr->string)->len) { - rb_str_resize(ptr->string, ptr->pos + 1); - } - else { - rb_str_modify(ptr->string); + ptr->pos = olen; } + strio_extend(ptr, ptr->pos, 1); RSTRING(ptr->string)->ptr[ptr->pos++] = c; OBJ_INFECT(ptr->string, self); return ch; |