summaryrefslogtreecommitdiff
path: root/ext/stringio
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-02-05 16:57:54 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-02-05 16:57:54 +0000
commite5757042f94830dd3f5c019ab5ec2838ea794cd8 (patch)
tree9dc0cc210f1e889e376c33a5b92c4a02cf569bef /ext/stringio
parent63cbe555b7c040efd335392f31dd99d45e8134db (diff)
* ext/stringio/stringio.c (strio_ungetc): pads with \000 when the
current position is after the end. [ruby-dev:40271] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26588 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/stringio')
-rw-r--r--ext/stringio/stringio.c29
1 files changed, 19 insertions, 10 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index f193c6ef6a..faf974e78d 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -725,17 +725,26 @@ strio_ungetc(VALUE self, VALUE c)
c = rb_str_conv_enc(c, enc2, enc);
}
}
- /* get logical position */
- lpos = 0; p = RSTRING_PTR(ptr->string); pend = p + ptr->pos;
- for (;;) {
- clen = rb_enc_mbclen(p, pend, enc);
- if (p+clen >= pend) break;
- p += clen;
- lpos++;
+ if (RSTRING_LEN(ptr->string) < ptr->pos) {
+ long len = RSTRING_LEN(ptr->string);
+ rb_str_resize(ptr->string, ptr->pos - 1);
+ memset(RSTRING_PTR(ptr->string) + len, 0, ptr->pos - len - 1);
+ rb_str_concat(ptr->string, c);
+ ptr->pos--;
+ }
+ else {
+ /* get logical position */
+ lpos = 0; p = RSTRING_PTR(ptr->string); pend = p + ptr->pos;
+ for (;;) {
+ clen = rb_enc_mbclen(p, pend, enc);
+ if (p+clen >= pend) break;
+ p += clen;
+ lpos++;
+ }
+ clen = p - RSTRING_PTR(ptr->string);
+ rb_str_update(ptr->string, lpos, ptr->pos ? 1 : 0, c);
+ ptr->pos = clen;
}
- clen = p - RSTRING_PTR(ptr->string);
- rb_str_update(ptr->string, lpos, ptr->pos ? 1 : 0, c);
- ptr->pos = clen;
return Qnil;
}