diff options
author | Sean McArthur <sean.monstar@gmail.com> | 2014-03-06 10:21:04 -0800 |
---|---|---|
committer | Trevor Norris <trev.norris@gmail.com> | 2014-04-28 22:09:48 -0700 |
commit | 226f98a356fb62a706bc860c36c9ebc6124d776e (patch) | |
tree | bee8ac775e3cdd12188ced6c2634d7fefd56833b /lib/buffer.js | |
parent | 681fe599d7a45fb537f948c08e64cdce06bc585d (diff) | |
download | node-226f98a356fb62a706bc860c36c9ebc6124d776e.tar.gz |
buffer: add compare and equals methods
compare() works like String.localeCompare such that:
Buffer.compare(a, b) === a.compare(b);
equals() does a native check to see if two buffers are equal.
Signed-off-by: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'lib/buffer.js')
-rw-r--r-- | lib/buffer.js | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/buffer.js b/lib/buffer.js index 6891b776f..65f488635 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -130,6 +130,15 @@ Buffer.isBuffer = function isBuffer(b) { }; +Buffer.compare = function compare(a, b) { + if (!(a instanceof Buffer) || + !(b instanceof Buffer)) + throw new TypeError('Arguments must be Buffers'); + + return internal.compare(b, a); +}; + + Buffer.isEncoding = function(encoding) { switch ((encoding + '').toLowerCase()) { case 'hex': @@ -256,6 +265,14 @@ Buffer.prototype.toString = function(encoding, start, end) { }; +Buffer.prototype.equals = function equals(b) { + if (!(b instanceof Buffer)) + throw new TypeError('Argument must be a Buffer'); + + return internal.compare(this, b) === 0; +}; + + // Inspect Buffer.prototype.inspect = function inspect() { var str = ''; @@ -269,6 +286,14 @@ Buffer.prototype.inspect = function inspect() { }; +Buffer.prototype.compare = function compare(b) { + if (!(b instanceof Buffer)) + throw new TypeError('Argument must be a Buffer'); + + return internal.compare(this, b); +}; + + // XXX remove in v0.13 Buffer.prototype.get = util.deprecate(function get(offset) { offset = ~~offset; |