diff options
author | Matthew Bauer <mjbauer95@gmail.com> | 2020-06-12 10:09:42 -0500 |
---|---|---|
committer | Matthew Bauer <mjbauer95@gmail.com> | 2020-06-12 10:11:16 -0500 |
commit | b260c9ee0325d7a4c78d91e47bd6e2afbde16e28 (patch) | |
tree | 176d906367d85d75e96089ce067b776fe232eacf /src/libutil | |
parent | 19aa892f2064d437d32a3c12758d6a623b7ae8e5 (diff) |
Add newHashAllowEmpty helper function
This replaces the copy&paste with a helper function in hash.hh.
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/hash.cc | 10 | ||||
-rw-r--r-- | src/libutil/hash.hh | 2 |
2 files changed, 12 insertions, 0 deletions
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 7caee1da7..2c8085e42 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -205,6 +205,16 @@ Hash::Hash(const std::string & s, HashType type) throw BadHash("hash '%s' has wrong length for hash type '%s'", s, printHashType(type)); } +Hash newHashAllowEmpty(std::string hashStr, HashType ht) +{ + if (hashStr.empty()) + { + Hash h(ht); + warn("found empty hash, assuming you wanted '%s'", h.to_string()); + } else + return Hash(hashStr, ht); +} + union Ctx { diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index ea9fca3e7..449cb1b86 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -94,6 +94,8 @@ struct Hash } }; +/* Helper that defaults empty hashes to the 0 hash. */ +Hash newHashAllowEmpty(std::string hashStr, HashType ht); /* Print a hash in base-16 if it's MD5, or base-32 otherwise. */ string printHash16or32(const Hash & hash); |