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

Change buffer[] to buffer.set()/get() #19

Merged
merged 1 commit into from Dec 4, 2012
Merged

Change buffer[] to buffer.set()/get() #19

merged 1 commit into from Dec 4, 2012

Conversation

ghost
Copy link

@ghost ghost commented Dec 4, 2012

for it to work both in node.js and browserify

for it to work both in node.js and browserify
@andris9
Copy link
Owner

andris9 commented Dec 4, 2012

Is it safe to use get/set with Buffers as these methods doesn't seem to be documented anywhere?

@ghost
Copy link
Author

ghost commented Dec 4, 2012

It seems like just a wrapper to the [] of the native object in node.js (and pure js object in browserify). Both node and browserify have it exactly the same:

https://github.com/joyent/node/blob/master/lib/buffer.js#L333
https://github.com/toots/buffer-browserify/blob/master/index.js#L442

Buffer.prototype.get = function get(i) {
  if (i < 0 || i >= this.length) throw new Error('oob');
  return this.parent[this.offset + i];
};


Buffer.prototype.set = function set(i, v) {
  if (i < 0 || i >= this.length) throw new Error('oob');
  return this.parent[this.offset + i] = v;
};

Both [] and get work the same in node.js:

> f=require('fs').readFileSync('movie.avi')
<Buffer 52 49 46 46 ce d4 db 15 41 56 49 20 4c 49 53 54 7e 22 00 00 68 64 72 6c 61 76 69 68 38 00 00 00 ec a2 00 00 00 00 00 00 00 00 00 00 10 01 00 00 38 ee 00 ...>
> f.length
366729216
> f[0]
82
> f.get(0)
82
> f.get(12312)
210
> f.get(12312)
210

But yea, it's undocumented, so I don't know anything more about it. They should probably add it to the docs. We can open an issue there as well.

@andris9
Copy link
Owner

andris9 commented Dec 4, 2012

I checked it and it seems that the get/set methods have been added to the Buffers code already back in 2010 and are present in the latest code so I guess it is safe to use these.

andris9 added a commit that referenced this pull request Dec 4, 2012
Change buffer[] to buffer.set()/get()
@andris9 andris9 merged commit c494c59 into andris9:master Dec 4, 2012
@ghost
Copy link
Author

ghost commented Dec 4, 2012

Thanks

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

Successfully merging this pull request may close these issues.

None yet

1 participant