Skip to content

[idea] offsetOf for Struct #26

@vshymanskyy

Description

@vshymanskyy

It's sometimes useful to be able to know a relative offset of a specific field of a Struct.
Currently this works for me, but I would expect to see such API available out-of-the box ;)
Thanks!

r.Struct.prototype.offsetOf = function(field) {
  let keys = Object.keys(this.fields);
  let offset = 0;
  for (let i=0; i<keys.length; i++) {
    let key = keys[i];
    if (key == field) {
      return offset;
    }

    offset += this.fields[key].size();
  }
  return -1;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions