diff options
author | John Ericson <John.Ericson@Obsidian.Systems> | 2020-03-28 23:22:10 +0000 |
---|---|---|
committer | John Ericson <git@JohnEricson.me> | 2020-03-29 11:23:15 -0400 |
commit | 87b32bab05ff91981c8847d66cd5502feb44f3b5 (patch) | |
tree | ff77f6703185a8b9544f354de5853254ef88a4d8 /src/libutil/hash.hh | |
parent | eb1911e277bfcc1b161cb996205ae1696f496099 (diff) |
Use `enum struct` and drop prefixes
This does a few enums; the rest will be gotten in subsequent commits.
Diffstat (limited to 'src/libutil/hash.hh')
-rw-r--r-- | src/libutil/hash.hh | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index ea9fca3e7..0fe6e7677 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -10,7 +10,13 @@ namespace nix { MakeError(BadHash, Error); -enum HashType : char { htUnknown, htMD5, htSHA1, htSHA256, htSHA512 }; +enum struct HashType : char { + Unknown, + MD5, + SHA1, + SHA256, + SHA512, +}; const int md5HashSize = 16; @@ -20,7 +26,12 @@ const int sha512HashSize = 64; extern const string base32Chars; -enum Base : int { Base64, Base32, Base16, SRI }; +enum struct Base : int { + Base64, + Base32, + Base16, + SRI, +}; struct Hash @@ -29,7 +40,7 @@ struct Hash unsigned int hashSize = 0; unsigned char hash[maxHashSize] = {}; - HashType type = htUnknown; + HashType type = HashType::Unknown; /* Create an unset hash object. */ Hash() { }; @@ -40,14 +51,14 @@ struct Hash /* Initialize the hash from a string representation, in the format "[<type>:]<base16|base32|base64>" or "<type>-<base64>" (a Subresource Integrity hash expression). If the 'type' argument - is htUnknown, then the hash type must be specified in the + is HashType::Unknown, then the hash type must be specified in the string. */ - Hash(const std::string & s, HashType type = htUnknown); + Hash(const std::string & s, HashType type = HashType::Unknown); void init(); /* Check whether a hash is set. */ - operator bool () const { return type != htUnknown; } + operator bool () const { return type != HashType::Unknown; } /* Check whether two hash are equal. */ bool operator == (const Hash & h2) const; @@ -79,18 +90,18 @@ struct Hash /* Return a string representation of the hash, in base-16, base-32 or base-64. By default, this is prefixed by the hash type (e.g. "sha256:"). */ - std::string to_string(Base base = Base32, bool includeType = true) const; + std::string to_string(Base base = Base::Base32, bool includeType = true) const; std::string gitRev() const { - assert(type == htSHA1); - return to_string(Base16, false); + assert(type == HashType::SHA1); + return to_string(Base::Base16, false); } std::string gitShortRev() const { - assert(type == htSHA1); - return std::string(to_string(Base16, false), 0, 7); + assert(type == HashType::SHA1); + return std::string(to_string(Base::Base16, false), 0, 7); } }; |