summaryrefslogtreecommitdiff
path: root/ext/stringio
diff options
context:
space:
mode:
Diffstat (limited to 'ext/stringio')
-rw-r--r--ext/stringio/stringio.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 27b8aba8ba..11fe8ef455 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -125,7 +125,6 @@ check_modifiable(ptr)
if (OBJ_FROZEN(ptr->string)) {
rb_raise(rb_eIOError, "not modifiable string");
}
- rb_str_modify(ptr->string);
}
static VALUE strio_s_allocate _((VALUE));
@@ -548,6 +547,7 @@ strio_ungetc(self, ch)
if ((unsigned char)RSTRING(ptr->string)->ptr[--ptr->pos] !=
(unsigned char)cc) {
check_modifiable(ptr);
+ rb_str_modify(ptr->string);
RSTRING(ptr->string)->ptr[ptr->pos] = cc;
}
}
@@ -740,10 +740,18 @@ strio_write(self, str)
if (ptr->flags & STRIO_APPEND) {
ptr->pos = RSTRING(ptr->string)->len;
}
- if (ptr->pos + len > RSTRING(ptr->string)->len) {
- rb_str_resize(ptr->string, ptr->pos + len);
+ if (ptr->pos == RSTRING(ptr->string)->len) {
+ rb_str_cat(ptr->string, RSTRING(str)->ptr, len);
+ }
+ else {
+ if (ptr->pos + len > RSTRING(ptr->string)->len) {
+ rb_str_resize(ptr->string, ptr->pos + len);
+ }
+ else {
+ rb_str_modify(ptr->string);
+ }
+ rb_str_update(ptr->string, ptr->pos, len, str);
}
- rb_str_update(ptr->string, ptr->pos, len, str);
ptr->pos += len;
return LONG2NUM(len);
}
@@ -768,6 +776,9 @@ strio_putc(self, ch)
if (ptr->pos >= RSTRING(ptr->string)->len) {
rb_str_resize(ptr->string, ptr->pos + 1);
}
+ else {
+ rb_str_modify(ptr->string);
+ }
RSTRING(ptr->string)->ptr[ptr->pos++] = c;
return ch;
}