From b6e137e93c5fe62f3f6dc1a0112a6c067d456f9b Mon Sep 17 00:00:00 2001 From: usa Date: Thu, 1 Dec 2016 13:20:47 +0000 Subject: Remove `offset:` from `Array#pack` * pack.c (pack_pack): can use `@` instead of `offset:`. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56958 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- pack.c | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) (limited to 'pack.c') diff --git a/pack.c b/pack.c index fbbf0b81cd..88adf74f5f 100644 --- a/pack.c +++ b/pack.c @@ -142,7 +142,6 @@ rb_str_associated(VALUE str) * call-seq: * arr.pack( aTemplateString ) -> aBinaryString * arr.pack( aTemplateString, buffer: aBufferString ) -> aBufferString - * arr.pack( aTemplateString, buffer: aBufferString, offset: offsetOfBuffer ) -> aBufferString * * Packs the contents of arr into a binary sequence according to * the directives in aTemplateString (see the table below) @@ -166,10 +165,10 @@ rb_str_associated(VALUE str) * * If aBufferString is specified and its capacity is enough, * +pack+ uses it as the buffer and returns it. - * User can also specify offsetOfBuffer, then the result of +pack+ - * is filled after offsetOfBuffer bytes. + * When the offset is specified by the beginning of aTemplateString, + * the result is filled after the offset. * If original contents of aBufferString exists and it's longer than - * offsetOfBuffer, the rest of offsetOfBuffer are cut. + * the offset, the rest of offsetOfBuffer are overwritten by the result. * If it's shorter, the gap is filled with ``\0''. * * Note that ``buffer:'' option does not guarantee not to allocate memory @@ -290,36 +289,16 @@ pack_pack(int argc, VALUE *argv, VALUE ary) p = RSTRING_PTR(fmt); pend = p + RSTRING_LEN(fmt); if (!NIL_P(opt)) { - static ID keyword_ids[2]; - VALUE kwargs[2]; - VALUE voff; + static ID keyword_ids[1]; long offset; - if (!keyword_ids[0]) { + if (!keyword_ids[0]) CONST_ID(keyword_ids[0], "buffer"); - CONST_ID(keyword_ids[1], "offset"); - } - rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs); - buffer = kwargs[0]; - voff = kwargs[1]; - - if (buffer == Qundef && !NIL_P(voff)) - rb_raise(rb_eArgError, "offset is specified without buffer"); - if (buffer != Qundef) { - long len; - if (!RB_TYPE_P(buffer, T_STRING)) - rb_raise(rb_eTypeError, "buffer must be String, not %s", rb_obj_classname(buffer)); - if (voff != Qundef) { - offset = NUM2LONG(voff); - if (rb_str_capacity(buffer) < offset) - rb_raise(rb_eArgError, "buffer is too small for offset"); - len = RSTRING_LEN(buffer); - rb_str_set_len(buffer, offset); - if (len < offset) - memset(RSTRING_PTR(buffer) + len, '\0', offset - len); - } - } + rb_get_kwargs(opt, keyword_ids, 0, 1, &buffer); + + if (buffer != Qundef && !RB_TYPE_P(buffer, T_STRING)) + rb_raise(rb_eTypeError, "buffer must be String, not %s", rb_obj_classname(buffer)); } if (buffer) res = buffer; -- cgit v1.2.3