ruby-changes:41112
From: nobu <ko1@a...>
Date: Fri, 18 Dec 2015 15:40:19 +0900 (JST)
Subject: [ruby-changes:41112] nobu:r53185 (trunk): stringio.c: strio_unget_bytes
nobu 2015-12-18 15:40:00 +0900 (Fri, 18 Dec 2015) New Revision: 53185 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53185 Log: stringio.c: strio_unget_bytes * ext/stringio/stringio.c (strio_unget_bytes): extract from strio_ungetbyte to share with strio_ungetc. Modified files: trunk/ChangeLog trunk/ext/stringio/stringio.c Index: ChangeLog =================================================================== --- ChangeLog (revision 53184) +++ ChangeLog (revision 53185) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Dec 18 15:40:06 2015 Nobuyoshi Nakada <nobu@r...> + + * ext/stringio/stringio.c (strio_unget_bytes): extract from + strio_ungetbyte to share with strio_ungetc. + Fri Dec 18 12:39:42 2015 SHIBATA Hiroshi <hsbt@r...> * doc/syntax/*.rdoc: separated modifier at sentence. Index: ext/stringio/stringio.c =================================================================== --- ext/stringio/stringio.c (revision 53184) +++ ext/stringio/stringio.c (revision 53185) @@ -29,6 +29,7 @@ struct StringIO { https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L29 }; static void strio_init(int, VALUE *, struct StringIO *, VALUE); +static VALUE strio_unget_bytes(struct StringIO *, const char *, long); #define IS_STRIO(obj) (rb_typeddata_is_kind_of((obj), &strio_data_type)) #define error_inval(msg) (errno = EINVAL, rb_sys_fail(msg)) @@ -723,19 +724,17 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L724 strio_ungetc(VALUE self, VALUE c) { struct StringIO *ptr = readable(self); - long lpos, clen; - char *p, *pend; rb_encoding *enc, *enc2; - if (NIL_P(c)) return Qnil; check_modifiable(ptr); + if (NIL_P(c)) return Qnil; if (FIXNUM_P(c)) { int cc = FIX2INT(c); char buf[16]; enc = rb_enc_get(ptr->string); rb_enc_mbcput(cc, buf, enc); - c = rb_enc_str_new(buf, rb_enc_codelen(cc, enc), enc); + return strio_unget_bytes(ptr, buf, rb_enc_codelen(cc, enc)); } else { SafeStringValue(c); @@ -744,29 +743,10 @@ strio_ungetc(VALUE self, VALUE c) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L743 if (enc != enc2 && enc != rb_ascii8bit_encoding()) { c = rb_str_conv_enc(c, enc2, enc); } + strio_unget_bytes(ptr, RSTRING_PTR(c), RSTRING_LEN(c)); + RB_GC_GUARD(c); + return Qnil; } - 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; - } - - return Qnil; } /* @@ -780,21 +760,32 @@ strio_ungetbyte(VALUE self, VALUE c) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L760 { struct StringIO *ptr = readable(self); char buf[1], *cp = buf; - long pos = ptr->pos, cl = 1, len, rest; - VALUE str = ptr->string; - char *s; + long cl = 1; + check_modifiable(ptr); if (NIL_P(c)) return Qnil; if (FIXNUM_P(c)) { buf[0] = (char)FIX2INT(c); + return strio_unget_bytes(ptr, buf, 1); } else { SafeStringValue(c); cp = RSTRING_PTR(c); cl = RSTRING_LEN(c); if (cl == 0) return Qnil; + strio_unget_bytes(ptr, cp, cl); + RB_GC_GUARD(c); + return Qnil; } - check_modifiable(ptr); +} + +static VALUE +strio_unget_bytes(struct StringIO *ptr, const char *cp, long cl) +{ + long pos = ptr->pos, len, rest; + VALUE str = ptr->string; + char *s; + len = RSTRING_LEN(str); rest = pos - len; if (cl > pos) { @@ -816,7 +807,6 @@ strio_ungetbyte(VALUE self, VALUE c) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L807 } memcpy(s + pos, cp, cl); ptr->pos = pos; - RB_GC_GUARD(c); return Qnil; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/