summaryrefslogtreecommitdiff
path: root/ext/stringio/stringio.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-02-03 14:43:07 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-02-03 14:43:07 +0000
commit1839bc333d514f567c66e715fd7999a157fde41d (patch)
tree16c5c023b32184a1795b5e31bbcc321bba4c2ced /ext/stringio/stringio.c
parent38df179abc59565d7e89ecacfb4274680127ca21 (diff)
* 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: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@7874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/stringio/stringio.c')
-rw-r--r--ext/stringio/stringio.c59
1 files changed, 31 insertions, 28 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 23ba3dce4d..6c5f7e0d83 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -341,7 +341,7 @@ strio_close(self)
}
ptr->string = Qnil;
ptr->flags &= ~FMODE_READWRITE;
- return self;
+ return Qnil;
}
static VALUE
@@ -355,7 +355,7 @@ strio_close_read(self)
if (!((ptr->flags &= ~FMODE_READABLE) & FMODE_READWRITE)) {
ptr->string = Qnil;
}
- return self;
+ return Qnil;
}
static VALUE
@@ -369,7 +369,7 @@ strio_close_write(self)
if (!((ptr->flags &= ~FMODE_WRITABLE) & FMODE_READWRITE)) {
ptr->string = Qnil;
}
- return self;
+ return Qnil;
}
static VALUE
@@ -565,6 +565,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;
@@ -574,18 +593,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;
@@ -793,13 +805,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);
@@ -819,17 +825,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;