diff --git a/src/lib/ebus/filereader.cpp b/src/lib/ebus/filereader.cpp index 01dc45dc..2d63388b 100644 --- a/src/lib/ebus/filereader.cpp +++ b/src/lib/ebus/filereader.cpp @@ -118,7 +118,13 @@ void FileReader::tolower(string& str) { transform(str.begin(), str.end(), str.begin(), ::tolower); } -static std::hash 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& row, unsigned int& lineNo, size_t* hash, size_t* size) { @@ -137,8 +143,7 @@ bool FileReader::splitFields(istream& ifs, vector& row, unsigned int& li *size += length + 1; // normalized with trailing endl } if (hash) { - // TODO ensure 32 bit machine produces same result - *hash ^= (hashFunction(line) << 1) ^ (length << (7 * (lineNo % 5))); + *hash ^= (hashFunction(line) ^ (length << (7 * (lineNo % 5)))) & 0xffffffff; } if (!quotedText && (length == 0 || line[0] == '#' || (line.length() > 1 && line[0] == '/' && line[1] == '/'))) { if (lineNo == 1) { diff --git a/src/lib/ebus/test/test_filereader.cpp b/src/lib/ebus/test/test_filereader.cpp index 276cf34b..ebcd14e3 100644 --- a/src/lib/ebus/test/test_filereader.cpp +++ b/src/lib/ebus/test/test_filereader.cpp @@ -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 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}; unsigned int lineNo = 0; vector 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 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}; lineNo = 0; map defaults;