Permalink
Browse files

it works

  • Loading branch information...
0 parents commit 1b647e50f72c2105aacb2dd2319a435daa185cf3 @pkrumins committed Jul 20, 2010
Showing with 186 additions and 0 deletions.
  1. +45 −0 bufferdiff.cc
  2. +40 −0 package.json
  3. +52 −0 readme.txt
  4. +21 −0 tests/buftest.js
  5. +1 −0 tests/databuf-1
  6. BIN tests/databuf-1029
  7. +27 −0 wscript
@@ -0,0 +1,45 @@
+#include <v8.h>
+#include <node.h>
+#include <node_buffer.h>
+#include <cstring>
+
+using namespace v8;
+using namespace node;
+
+static Handle<Value>
+VException(const char *msg) {
+ HandleScope scope;
+ return ThrowException(Exception::Error(String::New(msg)));
+}
+
+Handle<Value>
+EqBuf(const Arguments &args)
+{
+ HandleScope scope;
+
+ if (args.Length() != 2)
+ return VException("Two arguments required - buffer1 and buffer2");
+
+ if (!Buffer::HasInstance(args[0]))
+ return VException("First argument must be Buffer.");
+ if (!Buffer::HasInstance(args[1]))
+ return VException("Second argument must be Buffer.");
+
+ Buffer *buf1 = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
+ Buffer *buf2 = ObjectWrap::Unwrap<Buffer>(args[1]->ToObject());
+
+ if (buf1->length() != buf2->length())
+ return scope.Close(False());
+
+ return scope.Close(
+ Boolean::New(memcmp(buf1->data(), buf2->data(), buf1->length())==0)
+ );
+}
+
+extern "C" void
+init(Handle<Object> target)
+{
+ HandleScope scope;
+ target->Set(String::New("eqBuf"), FunctionTemplate::New(EqBuf)->GetFunction());
+}
+
@@ -0,0 +1,40 @@
+{
+ "name": "bufferdiff",
+ "version": "1.0.9",
+ "main": "bufferdiff",
+ "description": "A C++ module for node-js to test if two buffers are equal, fast (could add diff later).",
+ "keywords": [
+ "buffers",
+ "buffer",
+ "diff"
+ ],
+ "maintainers": [
+ {
+ "name": "Peteris Krumins",
+ "email": "peteris.krumins@gmail.com",
+ "web": "http://www.catonmat.net",
+ "twitter": "pkrumins"
+ }
+ ],
+ "licenses": [
+ {
+ "type": "MIT"
+ }
+ ],
+ "repositories": [
+ {
+ "type": "git",
+ "url": "http://github.com/pkrumins/node-bufferdiff.git"
+ }
+ ],
+ "directories": {
+ "tests": "tests"
+ },
+ "engines": {
+ "node": ">=0.1.93"
+ },
+ "scripts": {
+ "install": "node-waf configure build"
+ }
+}
+
@@ -0,0 +1,52 @@
+This is a node.js module, written in C++, that compares two buffers.
+For now it implements just eqBuf to test if two buffers are equal, fast.
+
+It was written by Peteris Krumins (peter@catonmat.net).
+His blog is at http://www.catonmat.net -- good coders code, great reuse.
+
+------------------------------------------------------------------------------
+
+The module exports one function:
+
+ * eqBuf - given two buffers, compare if they are equal.
+
+Example usage:
+
+ var eqBuf = require('bufferdiff');
+ // ...
+ if (eqBuf(buf1, buf2)) {
+ sys.log('buffers buf1 and buf2 are equal');
+ }
+
+
+How to compile?
+---------------
+
+As usual,
+
+ node-waf configure build
+
+The module will be called `bufferdiff.node`. To use it, make sure it's in NODE_PATH.
+
+
+Extra info
+----------
+
+This library was written for my and SubStack's StackVM startup:
+
+ http://www.stackvm.com
+
+ http://github.com/pkrumins/stackvm
+ http://github.com/substack/stackvm
+
+
+------------------------------------------------------------------------------
+
+Yo dawg, I heard you liked buffers so I put a buffer in yo buffer so you can
+compare while you compare.
+
+
+Sincerely,
+Peteris Krumins
+http://www.catonmat.net
+
@@ -0,0 +1,21 @@
+var sys = require('sys');
+var eqBuf = require('bufferdiff').eqBuf;
+var fs = require('fs');
+var assert = require('assert');
+
+// test 1: equal buffers
+var b1 = fs.readFileSync('./databuf-1029');
+var b2 = fs.readFileSync('./databuf-1029');
+assert.ok(eqBuf(b1, b1), 'b1 != b1');
+assert.ok(eqBuf(b1, b2), 'b1 != b2');
+assert.ok(eqBuf(b2, b2), 'b2 != b2');
+
+
+// test 2: 1 byte buffer
+var b3 = fs.readFileSync('./databuf-1');
+assert.ok(eqBuf(b3, b3), 'b3 != b3');
+assert.ok(!eqBuf(b3, b1), 'b3 = b3');
+assert.ok(!eqBuf(b3, b2), 'b3 = b2');
+assert.ok(!eqBuf(b1, b3), 'b1 = b3');
+
+sys.log('all tests ok');
@@ -0,0 +1 @@
+I
Binary file not shown.
27 wscript
@@ -0,0 +1,27 @@
+import Options
+from os import unlink, symlink, popen
+from os.path import exists
+
+srcdir = "."
+blddir = "build"
+VERSION = "0.0.1"
+
+def set_options(opt):
+ opt.tool_options("compiler_cxx")
+
+def configure(conf):
+ conf.check_tool("compiler_cxx")
+ conf.check_tool("node_addon")
+
+def build(bld):
+ obj = bld.new_task_gen("cxx", "shlib", "node_addon")
+ obj.target = "bufferdiff"
+ obj.source = "bufferdiff.cc"
+
+def shutdown():
+ if Options.commands['clean']:
+ if exists('bufferdiff.node'): unlink('bufferdiff.node')
+ else:
+ if exists('build/default/bufferdiff.node') and not exists('bufferdiff.node'):
+ symlink('build/default/bufferdiff.node', 'bufferdiff.node')
+

0 comments on commit 1b647e5

Please sign in to comment.