--- /dev/null
+// 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);
+}
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
// 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
};
+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':
};
+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 = '';
};
+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;
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") \
}
+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());
internal->Set(env->byte_length_string(),
FunctionTemplate::New(
env->isolate(), ByteLength)->GetFunction());
+ internal->Set(env->compare_string(),
+ FunctionTemplate::New(
+ env->isolate(), Compare)->GetFunction());
}
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');
+});