[ruby-core:116152] [Ruby master Feature#20176] Array#pack: support offset kwarg

Issue #20176 has been reported by chucke (Tiago Cardoso). ---------------------------------------- Feature #20176: Array#pack: support offset kwarg https://bugs.ruby-lang.org/issues/20176 * Author: chucke (Tiago Cardoso) * Status: Open * Priority: Normal ---------------------------------------- I was changing some code to use ruby 3.3's new `buffer` kwarg (great addition btw!) when using `Array#pack`. There are a few cases however, where I could perform the change, as not all my usages rely on appending; in some, I'm actually prepending it. To solve this, I'd like to propose the `offset` kwarg, which declares where to add the resulting string. picking up on example from the docs: [65, 66].pack('C*', buffer: 'foo') # => "fooAB" [65, 66].pack('C*', buffer: 'foo', offset: 0) # => "ABfoo" [65, 66].pack('C*', buffer: 'foo', offset: 1) # => "fABoo" -- https://bugs.ruby-lang.org/

Issue #20176 has been updated by Eregon (Benoit Daloze). I don't think this could be implemented more efficiently than doing `buffer.insert offset, array.pack(format)`. Notably because in general the length of the produced string is unknown before packing. So then I think this is not worth adding. ---------------------------------------- Feature #20176: Array#pack: support offset kwarg https://bugs.ruby-lang.org/issues/20176#change-106158 * Author: chucke (Tiago Cardoso) * Status: Open * Priority: Normal ---------------------------------------- I was changing some code to use ruby 3.3's new `buffer` kwarg (great addition btw!) when using `Array#pack`. There are a few cases however, where I could perform the change, as not all my usages rely on appending; in some, I'm actually prepending it. To solve this, I'd like to propose the `offset` kwarg, which declares where to add the resulting string. picking up on example from the docs: [65, 66].pack('C*', buffer: 'foo') # => "fooAB" [65, 66].pack('C*', buffer: 'foo', offset: 0) # => "ABfoo" [65, 66].pack('C*', buffer: 'foo', offset: 1) # => "fABoo" -- https://bugs.ruby-lang.org/

Issue #20176 has been updated by chucke (Tiago Cardoso). There's also `String#prepend`. Not sure if more efficient than `str.insert(0, ` (and if not, what was the argument for adding it back then?), but that's certainly another argument, in that, by reusing the `offset` kwarg from `String.unpack`, it'd reduce the special cases one would need to handle. Right now, because of the lack of such a kwarg, i have to maintain my aux method to handle the several cases: def pack(array_to_pack, template, buffer:, offset: -1) case offset when -1 array_to_pack.pack(template, buffer: buffer) # ideal scenario when 0 buffer.prepend(array_to_pack.pack(template)) else buffer.insert(offset, array_to_pack.pack(template)) end end ---------------------------------------- Feature #20176: Array#pack: support offset kwarg https://bugs.ruby-lang.org/issues/20176#change-106159 * Author: chucke (Tiago Cardoso) * Status: Open * Priority: Normal ---------------------------------------- I was changing some code to use ruby 3.3's new `buffer` kwarg (great addition btw!) when using `Array#pack`. There are a few cases however, where I could perform the change, as not all my usages rely on appending; in some, I'm actually prepending it. To solve this, I'd like to propose the `offset` kwarg, which declares where to add the resulting string. picking up on example from the docs: [65, 66].pack('C*', buffer: 'foo') # => "fooAB" [65, 66].pack('C*', buffer: 'foo', offset: 0) # => "ABfoo" [65, 66].pack('C*', buffer: 'foo', offset: 1) # => "fABoo" -- https://bugs.ruby-lang.org/
participants (2)
-
chucke (Tiago Cardoso)
-
Eregon (Benoit Daloze)