Skip to content

Commit

Permalink
COMMON: Use ScopedPtr in our hash functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 29, 2016
1 parent ec462af commit 7d92c69
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions src/common/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define COMMON_HASH_H

#include "src/common/types.h"
#include "src/common/scopedptr.h"
#include "src/common/ustring.h"
#include "src/common/encoding.h"
#include "src/common/memreadstream.h"
Expand Down Expand Up @@ -59,14 +60,13 @@ static inline uint32 hashStringDJB2(const UString &string) {
static inline uint32 hashStringDJB2(const UString &string, Encoding encoding) {
uint32 hash = 5381;

SeekableReadStream *data = convertString(string, encoding, false);
if (data) {
uint32 c;
while ((c = data->readChar()) != ReadStream::kEOF)
hash = hashDJB2(hash, c);
ScopedPtr<SeekableReadStream> data(convertString(string, encoding, false));
if (!data)
return hash;

delete data;
}
uint32 c;
while ((c = data->readChar()) != ReadStream::kEOF)
hash = hashDJB2(hash, c);

return hash;
}
Expand All @@ -89,14 +89,13 @@ static inline uint32 hashStringFNV32(const UString &string) {
static inline uint32 hashStringFNV32(const UString &string, Encoding encoding) {
uint32 hash = 0x811C9DC5;

SeekableReadStream *data = convertString(string, encoding, false);
if (data) {
uint32 c;
while ((c = data->readChar()) != ReadStream::kEOF)
hash = hashFNV32(hash, c);
ScopedPtr<SeekableReadStream> data(convertString(string, encoding, false));
if (!data)
return hash;

delete data;
}
uint32 c;
while ((c = data->readChar()) != ReadStream::kEOF)
hash = hashFNV32(hash, c);

return hash;
}
Expand All @@ -119,14 +118,13 @@ static inline uint64 hashStringFNV64(const UString &string) {
static inline uint64 hashStringFNV64(const UString &string, Encoding encoding) {
uint64 hash = 0xCBF29CE484222325LL;

SeekableReadStream *data = convertString(string, encoding, false);
if (data) {
uint32 c;
while ((c = data->readChar()) != ReadStream::kEOF)
hash = hashFNV64(hash, c);
ScopedPtr<SeekableReadStream> data(convertString(string, encoding, false));
if (!data)
return hash;

delete data;
}
uint32 c;
while ((c = data->readChar()) != ReadStream::kEOF)
hash = hashFNV64(hash, c);

return hash;
}
Expand Down Expand Up @@ -204,14 +202,13 @@ static inline uint32 hashStringCRC32(const UString &string) {
static inline uint32 hashStringCRC32(const UString &string, Encoding encoding) {
uint32 hash = 0xFFFFFFFF;

SeekableReadStream *data = convertString(string, encoding, false);
if (data) {
uint32 c;
while ((c = data->readChar()) != ReadStream::kEOF)
hash = hashCRC32(hash, c);
ScopedPtr<SeekableReadStream> data(convertString(string, encoding, false));
if (!data)
return hash;

delete data;
}
uint32 c;
while ((c = data->readChar()) != ReadStream::kEOF)
hash = hashCRC32(hash, c);

return hash ^ 0xFFFFFFFF;
}
Expand Down

0 comments on commit 7d92c69

Please sign in to comment.