Skip to content

Commit

Permalink
fix length calculation error when Buffer as first argument
Browse files Browse the repository at this point in the history
  • Loading branch information
yyfrankyy committed Nov 8, 2012
1 parent e782a30 commit 0abc42a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions binding.cc
Expand Up @@ -21,6 +21,7 @@
#include <v8.h>
#include <string.h>
#include <sstream>
#include <node_buffer.h>
#include "city.h"
#ifdef __SSE4_2__
#include "citycrc.h"
Expand All @@ -30,6 +31,7 @@
#define MAX_128_HASH_LEN MAX_64_HASH_LEN*2

using namespace v8;
using node::Buffer;

inline uint32 Uint64High32(const uint64 &x) {
return (uint32)((x >> 32) & 0xFFFFFFFF);
Expand Down Expand Up @@ -235,6 +237,12 @@ node_CityHash64(const Arguments& args) {
const char* str = *data;
size_t len = data.length();

if (Buffer::HasInstance(args[0])) {
Local<Object> obj = args[0]->ToObject();
str = Buffer::Data(obj);
len = Buffer::Length(obj);
}

uint64 hash;

if(args_len == 1) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "cityhash",
"version": "0.0.2",
"version": "0.0.3",
"main": "./build/Release/node-cityhash",
"description": "NodeJS binding for Google CityHash.",
"dependencies": [ ],
Expand Down
9 changes: 9 additions & 0 deletions test.js
Expand Up @@ -88,6 +88,15 @@ assertEqual({
value: '15738392108067291633'
}, cityhash.hash64('Hello', 87392039), 'Hash64 for "hello" with seed 87392039');

var b = new Buffer(4)
b.writeUInt32LE(7771789, 0)
assertEqual({
low: 3883967176,
high: 3780832496,
uint64: true,
value:'16238551925858017992'
}, cityhash.hash64(b, "2310915232984335893"), 'Hash64 for Buffer');

assertEqual({
low: 2569634289,
high: 3664379964,
Expand Down

0 comments on commit 0abc42a

Please sign in to comment.