aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean McArthur <sean.monstar@gmail.com>2014-03-06 10:21:04 -0800
committerTrevor Norris <trev.norris@gmail.com>2014-04-28 22:09:48 -0700
commit226f98a356fb62a706bc860c36c9ebc6124d776e (patch)
treebee8ac775e3cdd12188ced6c2634d7fefd56833b
parent681fe599d7a45fb537f948c08e64cdce06bc585d (diff)
downloadandroid-node-v8-226f98a356fb62a706bc860c36c9ebc6124d776e.tar.gz
android-node-v8-226f98a356fb62a706bc860c36c9ebc6124d776e.tar.bz2
android-node-v8-226f98a356fb62a706bc860c36c9ebc6124d776e.zip
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>
-rw-r--r--benchmark/buffers/buffer-compare.js42
-rw-r--r--doc/api/buffer.markdown27
-rw-r--r--lib/buffer.js25
-rw-r--r--src/env.h1
-rw-r--r--src/node_buffer.cc24
-rw-r--r--test/simple/test-buffer.js45
6 files changed, 164 insertions, 0 deletions
diff --git a/benchmark/buffers/buffer-compare.js b/benchmark/buffers/buffer-compare.js
new file mode 100644
index 0000000000..cb3ceb7e9f
--- /dev/null
+++ b/benchmark/buffers/buffer-compare.js
@@ -0,0 +1,42 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common.js');
+
+var bench = common.createBenchmark(main, {
+ size: [16, 512, 1024, 4096, 16386],
+ millions: [1]
+});
+
+function main(conf) {
+ var iter = (conf.millions >>> 0) * 1e6;
+ var size = (conf.size >>> 0);
+ var b0 = new Buffer(size).fill('a');
+ var b1 = new Buffer(size).fill('a');
+
+ b1[size - 1] = 'b'.charCodeAt(0);
+
+ bench.start();
+ for (var i = 0; i < iter; i++) {
+ Buffer.compare(b0, b1);
+ }
+ bench.end(iter / 1e6);
+}
diff --git a/doc/api/buffer.markdown b/doc/api/buffer.markdown
index 4951567589..bfdfb91589 100644
--- a/doc/api/buffer.markdown
+++ b/doc/api/buffer.markdown
@@ -115,6 +115,18 @@ If totalLength is not provided, it is read from the buffers in the list.
However, this adds an additional loop to the function, so it is faster
to provide the length explicitly.
+### Class Method: Buffer.compare(buf1, buf2)
+
+* `buf1` {Buffer}
+* `buf2` {Buffer}
+
+The same as [`buf1.compare(buf2)`](#buffer_buf_compare_otherbuffer). Useful
+for sorting an Array of Buffers:
+
+ var arr = [Buffer('1234'), Buffer('0123')];
+ arr.sort(Buffer.compare);
+
+
### buf.length
* Number
@@ -207,6 +219,21 @@ Example: copy an ASCII string into a buffer, one byte at a time:
// node.js
+### buf.equals(otherBuffer)
+
+* `otherBuffer` {Buffer}
+
+Returns a boolean of whether `this` and `otherBuffer` have the same
+bytes.
+
+### buf.compare(otherBuffer)
+
+* `otherBuffer` {Buffer}
+
+Returns a number indicating whether `this` comes before or after or is
+the same as the `otherBuffer` in sort order.
+
+
### buf.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd])
* `targetBuffer` Buffer object - Buffer to copy into
diff --git a/lib/buffer.js b/lib/buffer.js
index 6891b776fd..65f488635d 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;
diff --git a/src/env.h b/src/env.h
index 224d1d8a0b..31ac8acf15 100644
--- a/src/env.h
+++ b/src/env.h
@@ -69,6 +69,7 @@ namespace node {
V(change_string, "change") \
V(close_string, "close") \
V(code_string, "code") \
+ V(compare_string, "compare") \
V(ctime_string, "ctime") \
V(cwd_string, "cwd") \
V(debug_port_string, "debugPort") \
diff --git a/src/node_buffer.cc b/src/node_buffer.cc
index b1d63ad045..05d3566ae2 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -607,6 +607,27 @@ void ByteLength(const FunctionCallbackInfo<Value> &args) {
}
+void Compare(const FunctionCallbackInfo<Value> &args) {
+ Local<Object> obj_a = args[0].As<Object>();
+ char* obj_a_data =
+ static_cast<char*>(obj_a->GetIndexedPropertiesExternalArrayData());
+ size_t obj_a_len = obj_a->GetIndexedPropertiesExternalArrayDataLength();
+
+ Local<Object> obj_b = args[1].As<Object>();
+ char* obj_b_data =
+ static_cast<char*>(obj_b->GetIndexedPropertiesExternalArrayData());
+ size_t obj_b_len = obj_b->GetIndexedPropertiesExternalArrayDataLength();
+
+ size_t cmp_length = MIN(obj_a_len, obj_b_len);
+
+ int32_t val = memcmp(obj_a_data, obj_b_data, cmp_length);
+ if (!val)
+ val = obj_a_len - obj_b_len;
+
+ args.GetReturnValue().Set(val);
+}
+
+
// pass Buffer object to load prototype methods
void SetupBufferJS(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
@@ -663,6 +684,9 @@ void SetupBufferJS(const FunctionCallbackInfo<Value>& args) {
internal->Set(env->byte_length_string(),
FunctionTemplate::New(
env->isolate(), ByteLength)->GetFunction());
+ internal->Set(env->compare_string(),
+ FunctionTemplate::New(
+ env->isolate(), Compare)->GetFunction());
}
diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js
index c3d210fb4d..60c7b7f442 100644
--- a/test/simple/test-buffer.js
+++ b/test/simple/test-buffer.js
@@ -1037,3 +1037,48 @@ assert.equal(
crypto.createHash('sha1').update(b1).digest('hex'),
crypto.createHash('sha1').update(b2).digest('hex')
);
+
+// Test Compare
+var b = new Buffer(1).fill('a');
+var c = new Buffer(1).fill('c');
+var d = new Buffer(2).fill('aa');
+
+assert.equal(b.compare(c), -2);
+assert.equal(c.compare(d), 2);
+assert.equal(d.compare(b), 1);
+assert.equal(b.compare(d), -1);
+
+assert.equal(Buffer.compare(b, c), 2);
+assert.equal(Buffer.compare(c, d), -2);
+assert.equal(Buffer.compare(d, b), -1);
+assert.equal(Buffer.compare(b, d), 1);
+
+assert.throws(function() {
+ var b = new Buffer(1);
+ Buffer.compare(b, 'abc');
+});
+
+assert.throws(function() {
+ var b = new Buffer(1);
+ Buffer.compare('abc', b);
+});
+
+assert.throws(function() {
+ var b = new Buffer(1);
+ b.compare('abc');
+});
+
+// Test Equals
+var b = new Buffer(5).fill('abcdf');
+var c = new Buffer(5).fill('abcdf');
+var d = new Buffer(5).fill('abcde');
+var e = new Buffer(6).fill('abcdef');
+
+assert.ok(b.equals(c));
+assert.ok(!c.equals(d));
+assert.ok(!d.equals(e));
+
+assert.throws(function() {
+ var b = new Buffer(1);
+ b.equals('abc');
+});