Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[idea] offsetOf for Struct #26

Open
vshymanskyy opened this issue Jun 14, 2017 · 0 comments
Open

[idea] offsetOf for Struct #26

vshymanskyy opened this issue Jun 14, 2017 · 0 comments

Comments

@vshymanskyy
Copy link

vshymanskyy commented Jun 14, 2017

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant