-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Buffer - feature request: Buffer.compare with offsets. #521
Comments
What about just slicing the buffers before comparing? Slicing will
|
Hi @feross, |
P.S |
Whenever I compare buffers, it's also almost always using an offset and common length argument, exactly as @rootslab suggested, for example compare(buffer1, offset1, buffer2, offset2, comparisonLength), so this would really fit my typical use case. Slicing to compare within a binary search (usually where I need to compare) would make no sense. |
I'm cool with the idea. Though having that many overloads would become a pain to check. How about we start with the simple case:
We can work on the syntax for |
Oh, and it's not an iojs addition. The feature is also in v0.11. :-) |
https://github.com/bnoordhuis/node-buffertools is my go-to for this kind of thing but I'm a big +1 on putting the most useful stuff in core because having to compile an addon just to do simple things often makes me resort to this kind of thing which I'm sure would make @trevnorris scream: |
@jorangreef, then, I'm not alone ;)) @trevnorris ops.. I didn't see it in v0.11! ;) @rvagg thanks for suggestion, I know buffertools by @bnoordhuis, however, let me scream together with @trevnorris about toString('hex') ;)) it also converts every byte of raw data to 2 bytes (ASCII) chars, before comparing. Thanks to all! ;) |
@trevnorris @rvagg, I hope it is useful for something :) compare : function ( buf1, buf2, bpos1, bpos2, bytes ) {
if ( ! ( buf1 instanceof Buffer &&
buf2 instanceof Buffer ) )
throw new TypeError( 'Arguments must be Buffers' );
var abs = Math.abs
, min = Math.min
, blen1 = b1.length
, blen2 = b2.length
// normalize arg values
, s1 = bpos1 >>> 0 ? abs( + bpos1 ) : 0
, s2 = bpos2 >>> 0 ? abs( + bpos2 ) : 0
, len = bytes >>> 0 ? abs( + bytes ) : min( blen1 - s1, blen2 - s2 )
;
if ( ! blen1 || ! blen2 )
throw new Error( '..0 length buffer..' );
if ( ( len <= 0 ) ||
( s1 + len > blen1 ) ||
( s2 + len > blen2 ) )
throw new RangeError( 'out of range index' );
/*
* Indexes range:
*
* - buf1 -> slice(s1, s1 + len)
* - buf2 -> slice(s2, s2 + len)
*
* call native compare, with safe indexes:
*/
return internal.compare( buf1, buf2, s1, s2, len );
} |
@trevnorris et al.: Assuming this is still a desirable feature, how would the arguments for |
@Trott If the hope is to skip the
It's a bit ugly, but don't see much way around that. EDIT: Guess |
Fairly -1 on this. Using slice is a bit more verbose but it works. |
I wanted to use I think Trevor's API suggestion is right. |
I can't see a technical reason why this API isn't a valid proposition. We already support similar with |
Ok. That works for me then.
|
Adds additional `targetStart`, `targetEnd`, `sourceStart, and `sourceEnd` arguments to `Buffer.prototype.compare` to allow comparison of sub-ranges of two Buffers without requiring Buffer.prototype.slice() Fixes: nodejs#521
Adds additional `targetStart`, `targetEnd`, `sourceStart, and `sourceEnd` arguments to `Buffer.prototype.compare` to allow comparison of sub-ranges of two Buffers without requiring Buffer.prototype.slice() Fixes: #521 PR-URL: #5880 Reviewed-By: Trevor Norris <[email protected]>
Adds additional `targetStart`, `targetEnd`, `sourceStart, and `sourceEnd` arguments to `Buffer.prototype.compare` to allow comparison of sub-ranges of two Buffers without requiring Buffer.prototype.slice() Fixes: #521 PR-URL: #5880 Reviewed-By: Trevor Norris <[email protected]>
Adds additional `targetStart`, `targetEnd`, `sourceStart, and `sourceEnd` arguments to `Buffer.prototype.compare` to allow comparison of sub-ranges of two Buffers without requiring Buffer.prototype.slice() Fixes: #521 PR-URL: #5880 Reviewed-By: Trevor Norris <[email protected]>
I'm really impressed with the new iojs features, an awesome work!
Buffer.compare is a great iojs addition ( memcmp is very fast!), but, unfortunately, the current method implementation doesn't offer a way to compare 2 portions of Buffers: you are forced to slice one or both Buffers to compare desired portions of data.
Now, Buffer.compare accepts 2 buffers as arguments, instead, it could accept 4 args (+1 optional), or something like that:
@trevnorris It would be great if it was planned to add this functionality, it simplify things a lot!!
The text was updated successfully, but these errors were encountered: