From: "tenderlovemaking (Aaron Patterson) via ruby-core" Date: 2025-12-19T17:22:15+00:00 Subject: [ruby-core:124325] [Ruby Feature#21796] unpack variant that returns the final offset Issue #21796 has been updated by tenderlovemaking (Aaron Patterson). I really like this idea. @jhawthorn suggested `^` instead of `o` though, and I really like it. ```ruby bytes = "\x01\x02\x03" offset = 0 leb128_value1, offset = bytes.unpack("R^", offset: offset) #=> 1 leb128_value2, offset = bytes.unpack("R^", offset: offset) #=> 2 leb128_value3, offset = bytes.unpack("R^", offset: offset) #=> 3 ``` > I think the simplest would be a new keyword parameter Why a new parameter? You might be interested in more than one location. We already have [pack directives for skipping bytes](https://github.com/ruby/ruby/blob/master/doc/language/packed_data.rdoc#additional-directives-for-unpacking) (`@`, `X`, and `x`). It seems natural to add a directive to return the current offset. > Another possibility would be to add an unpack like method to StringScanner, for the case where you want to iteratively deserialize a binary string. I think this would be very useful in general, but I think maybe a separate Redmine ticket? ---------------------------------------- Feature #21796: unpack variant that returns the final offset https://bugs.ruby-lang.org/issues/21796#change-115830 * Author: nobu (Nobuyoshi Nakada) * Status: Open ---------------------------------------- mame (Yusuke Endoh) wrote in [#note-4](https://bugs.ruby-lang.org/issues/21785#note-4): > It's a shame `unpack` doesn't tell you how many bytes it read. You'd probably want a `unpack` variant that returns the final offset too, or a specifier that returns the current offset (like `o`?). > > ```ruby > bytes = "\x01\x02\x03" > offset = 0 > leb128_value1, offset = bytes.unpack("Ro", offset: offset) #=> 1 > leb128_value2, offset = bytes.unpack("Ro", offset: offset) #=> 2 > leb128_value3, offset = bytes.unpack("Ro", offset: offset) #=> 3 > ``` mame (Yusuke Endoh) wrote in [#note-6](https://bugs.ruby-lang.org/issues/21785#note-6): > > You could tell how many bytes you read based on the size of the leb128_value returned. > > That apparoach is unreliable because LEB128 is redundant. For example, both `"\x03"` and `"\x83\x00"` are valid LEB128 encodings of the value 3. > See the note of the section Values - Integers, in the Wasm spec. > https://webassembly.github.io/spec/core/binary/values.html#integers -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/