use simplified hash function to ensure same result on any platform

This commit is contained in:
john30
2017-04-17 14:28:33 +02:00
parent f2dd0ffd25
commit aaea8f72ab
2 changed files with 10 additions and 5 deletions
+8 -3
View File
@@ -118,7 +118,13 @@ void FileReader::tolower(string& str) {
transform(str.begin(), str.end(), str.begin(), ::tolower); transform(str.begin(), str.end(), str.begin(), ::tolower);
} }
static std::hash<string> hashFunction; static size_t hashFunction(const string str) {
size_t hash = 0;
for (char c : str) {
hash = (31 * hash) ^ c;
}
return hash;
}
bool FileReader::splitFields(istream& ifs, vector<string>& row, unsigned int& lineNo, bool FileReader::splitFields(istream& ifs, vector<string>& row, unsigned int& lineNo,
size_t* hash, size_t* size) { size_t* hash, size_t* size) {
@@ -137,8 +143,7 @@ bool FileReader::splitFields(istream& ifs, vector<string>& row, unsigned int& li
*size += length + 1; // normalized with trailing endl *size += length + 1; // normalized with trailing endl
} }
if (hash) { if (hash) {
// TODO ensure 32 bit machine produces same result *hash ^= (hashFunction(line) ^ (length << (7 * (lineNo % 5)))) & 0xffffffff;
*hash ^= (hashFunction(line) << 1) ^ (length << (7 * (lineNo % 5)));
} }
if (!quotedText && (length == 0 || line[0] == '#' || (line.length() > 1 && line[0] == '/' && line[1] == '/'))) { if (!quotedText && (length == 0 || line[0] == '#' || (line.length() > 1 && line[0] == '/' && line[1] == '/'))) {
if (lineNo == 1) { if (lineNo == 1) {
+2 -2
View File
@@ -219,7 +219,7 @@ int main(int argc, char** argv) {
"line 8 col 1 en,line 8 col 1 de,\"line 8 col 2 part 1;\n" "line 8 col 1 en,line 8 col 1 de,\"line 8 col 2 part 1;\n"
"line 8 col 2 part 2\",line 8 col 3;default of col 3\n" "line 8 col 2 part 2\",line 8 col 3;default of col 3\n"
); );
size_t hash = 0, size = 0, expectHash = 0x5e5e086475ab3bd9, expectSize = 389; size_t hash = 0, size = 0, expectHash = 0xb958f1cb, expectSize = 389;
TestReader reader{3, 1}; TestReader reader{3, 1};
unsigned int lineNo = 0; unsigned int lineNo = 0;
vector<string> row; vector<string> row;
@@ -257,7 +257,7 @@ int main(int argc, char** argv) {
"line 8 col 1 de,\"line 8 col 2 part 1;\n" "line 8 col 1 de,\"line 8 col 2 part 1;\n"
"line 8 col 2 part 2\",line 8 col 3,line 8 subcol 1,line 8 subcol 2,line 8 subcol 2,line 8 subcol 3\n" "line 8 col 2 part 2\",line 8 col 3,line 8 subcol 1,line 8 subcol 2,line 8 subcol 2,line 8 subcol 3\n"
); );
hash = 0, size = 0, expectHash = 0x9a675169d5837bb5, expectSize = 539; hash = 0, size = 0, expectHash = 0x2584e0f2, expectSize = 539;
TestReader reader2{7, 0}; TestReader reader2{7, 0};
lineNo = 0; lineNo = 0;
map<string, string> defaults; map<string, string> defaults;