Project

General

Profile

Actions

Feature #21796

open

unpack variant that returns the final offset

Feature #21796: unpack variant that returns the final offset

Added by nobu (Nobuyoshi Nakada) about 9 hours ago. Updated about 4 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:124312]

Description

mame (Yusuke Endoh) wrote in #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?).

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:

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


Related issues 1 (0 open1 closed)

Related to Ruby - Feature #21785: Add signed and unsigned LEB128 support to pack / unpackClosedActions
Actions

Also available in: PDF Atom