Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cityhash 1.1.0 #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ npm install cityhash
**/
function stringify(obj);

/**
* Compute hash for str by CityHash32().
* @param {string | Buffer} str The string to compute hash.
* @return {hash} 32-bit hash value.
**/
function hash32(str);

/**
* Compute hash for str by CityHash64().
* @param {string} str The string to compute hash.
Expand Down
36 changes: 36 additions & 0 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ stringify_hash(Local<Object> obj) {
}
}

Local<Object>
objectify_hash(const uint32 &hash) {
Local<Object> ret = Object::New();
ret->Set(String::New("low"), Integer::NewFromUnsigned(hash));
ret->Set(String::New("high"), Integer::NewFromUnsigned(0));
ret->Set(String::New("value"), stringify_hash(hash));
ret->Set(String::New("uint64"), Boolean::New(false));

return ret;
}

Local<Object>
objectify_hash(const uint64 &hash) {
uint32 low = Uint64Low32(hash);
Expand Down Expand Up @@ -224,6 +235,30 @@ node_Objectify(const Arguments& args) {
return scope.Close(objectify_hash(obj));
}

Handle<Value>
node_CityHash32(const Arguments& args) {
HandleScope scope;

int args_len = args.Length();
if(args_len == 0 || args_len > 1) {
return ThrowException(String::New("Invalid arguments."));
}

String::Utf8Value data(args[0]->ToString());
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);
}

uint32 hash = CityHash32(str, len);

return scope.Close(objectify_hash(hash));
}

Handle<Value>
node_CityHash64(const Arguments& args) {
HandleScope scope;
Expand Down Expand Up @@ -386,6 +421,7 @@ init (Handle<Object> target) {
HandleScope scope;
target->Set(String::New("stringify"), FunctionTemplate::New(node_Stringify)->GetFunction());
target->Set(String::New("objectify"), FunctionTemplate::New(node_Objectify)->GetFunction());
target->Set(String::New("hash32"), FunctionTemplate::New(node_CityHash32)->GetFunction());
target->Set(String::New("hash64"), FunctionTemplate::New(node_CityHash64)->GetFunction());
target->Set(String::New("hash128"), FunctionTemplate::New(node_CityHash128)->GetFunction());
target->Set(String::New("crc128"), FunctionTemplate::New(node_CityHashCrc128)->GetFunction());
Expand Down
Loading