Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

the seed parameter support objectify object, string and number.

  • Loading branch information...
commit 1611b7d25a873ba7c73f2d89bdc019ab271c5d06 1 parent 5f90ec7
@fbzhong authored
Showing with 66 additions and 11 deletions.
  1. +25 −11 binding.cc
  2. +41 −0 test.js
View
36 binding.cc
@@ -59,6 +59,16 @@ to_uint64(const Local<Object> &obj) {
return HighLow32ToUint64(low->Value(), high->Value());
}
+inline uint64
+to_uint64(const Local<Value> &obj) {
+ if(obj->IsObject()) {
+ return to_uint64(obj->ToObject());
+ } else {
+ String::AsciiValue strObj(obj->ToString());
+ return to_uint64(*strObj, strObj.length());
+ }
+}
+
void
to_uint128(uint128* v, const char* data, size_t len) {
std::stringstream str;
@@ -90,6 +100,16 @@ to_uint128(uint128 *v, const Local<Object> &obj) {
v->second = to_uint64(obj->Get(String::New("high"))->ToObject());
}
+inline void
+to_uint128(uint128 *v, const Local<Value> &obj) {
+ if(obj->IsObject()) {
+ to_uint128(v, obj->ToObject());
+ } else {
+ String::AsciiValue strObj(obj->ToString());
+ to_uint128(v, *strObj, strObj.length());
+ }
+}
+
Local<String>
stringify_hash(const uint64 &hash) {
// hash to char
@@ -206,16 +226,12 @@ node_CityHash64(const Arguments& args) {
if(args_len == 1) {
hash = CityHash64(str, len);
} else if(args_len == 2) {
- String::AsciiValue seedString(args[1]->ToString());
- uint64 seed = to_uint64(*seedString, seedString.length());
+ uint64 seed = to_uint64(args[1]);
hash = CityHash64WithSeed(str, len, seed);
} else if(args_len == 3) {
- String::AsciiValue seed0String(args[1]->ToString());
- String::AsciiValue seed1String(args[2]->ToString());
- uint64 seed0 = to_uint64(*seed0String, seed0String.length());
- uint64 seed1 = to_uint64(*seed1String, seed1String.length());
-
+ uint64 seed0 = to_uint64(args[1]);
+ uint64 seed1 = to_uint64(args[2]);
hash = CityHash64WithSeeds(str, len, seed0, seed1);
}
@@ -239,8 +255,7 @@ node_CityHash128(const Arguments& args) {
if(args.Length() == 2) {
uint128 seed;
- String::AsciiValue seedString(args[1]->ToString());
- to_uint128(&seed, *seedString, seedString.length());
+ to_uint128(&seed, args[1]);
hash = CityHash128WithSeed(str, len, seed);
} else {
@@ -267,8 +282,7 @@ node_CityHashCrc128(const Arguments& args) {
if(args.Length() == 2) {
uint128 seed;
- String::AsciiValue seedString(args[1]->ToString());
- to_uint128(&seed, *seedString, seedString.length());
+ to_uint128(&seed, args[1]);
hash = CityHashCrc128WithSeed(str, len, seed);
} else {
View
41 test.js
@@ -87,6 +87,13 @@ assertEqual({
}, cityhash.hash64('Hello', 87392039), 'Hash64 for "hello" with seed 87392039');
assertEqual({
+ low: 2569634289,
+ high: 3664379964,
+ uint64: true,
+ value: '15738392108067291633'
+ }, cityhash.hash64('Hello', cityhash.objectify(87392039)), 'Hash64 for "hello" with objectify seed 87392039');
+
+assertEqual({
low: 2389520903,
high: 3787608545,
value: '16267654833214665223',
@@ -146,6 +153,23 @@ assertEqual({
assertEqual({
"low": {
+ "low": 3184066266,
+ "high": 3674042232,
+ "value": "15779891233746910938",
+ "uint64": true
+ },
+ "high": {
+ "low": 4196783977,
+ "high": 3519958761,
+ "value": "15118107765960464233",
+ "uint64": true
+ },
+ "value": "15779891233746910938,15118107765960464233",
+ "uint128": true
+ }, cityhash.hash128('Hello', cityhash.objectify('12343,30293')), 'Hash128 for "Hello" with objectify seed 12343,30293');
+
+assertEqual({
+ "low": {
"low": 68277041,
"high": 2127607426,
"value": "9138004313465017137",
@@ -178,4 +202,21 @@ assertEqual({
"uint128": true
}, cityhash.crc128('Hello', '12343,30293'), 'Crc128 for "Hello" with seed 12343,30293');
+assertEqual({
+ "low": {
+ "low": 3184066266,
+ "high": 3674042232,
+ "value": "15779891233746910938",
+ "uint64": true
+ },
+ "high": {
+ "low": 4196783977,
+ "high": 3519958761,
+ "value": "15118107765960464233",
+ "uint64": true
+ },
+ "value": "15779891233746910938,15118107765960464233",
+ "uint128": true
+ }, cityhash.crc128('Hello', cityhash.objectify('12343,30293')), 'Crc128 for "Hello" with objectify seed 12343,30293');
+
end();
Please sign in to comment.
Something went wrong with that request. Please try again.