From 87b32bab05ff91981c8847d66cd5502feb44f3b5 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sat, 28 Mar 2020 23:22:10 +0000 Subject: Use `enum struct` and drop prefixes This does a few enums; the rest will be gotten in subsequent commits. --- src/libutil/hash.hh | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'src/libutil/hash.hh') 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 "[:]" or "-" (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); } }; -- cgit v1.2.3 From 450dcf2c1b60a36f5ffeab2411805287d122bcdd Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 2 Jun 2020 15:52:13 +0000 Subject: Remove `HashType::Unknown` Instead, `Hash` uses `std::optional`. In the future, we may also make `Hash` itself require a known hash type, encoraging people to use `std::optional` instead. --- src/libutil/hash.hh | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/libutil/hash.hh') diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index 0fe6e7677..41322be67 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -11,7 +11,6 @@ MakeError(BadHash, Error); enum struct HashType : char { - Unknown, MD5, SHA1, SHA256, @@ -40,7 +39,7 @@ struct Hash unsigned int hashSize = 0; unsigned char hash[maxHashSize] = {}; - HashType type = HashType::Unknown; + std::optional type = {}; /* Create an unset hash object. */ Hash() { }; @@ -51,14 +50,18 @@ struct Hash /* Initialize the hash from a string representation, in the format "[:]" or "-" (a Subresource Integrity hash expression). If the 'type' argument - is HashType::Unknown, then the hash type must be specified in the + is not present, then the hash type must be specified in the string. */ - Hash(const std::string & s, HashType type = HashType::Unknown); + Hash(const std::string & s, std::optional type); + // type must be provided + Hash(const std::string & s, HashType type); + // hash type must be part of string + Hash(const std::string & s); void init(); /* Check whether a hash is set. */ - operator bool () const { return type != HashType::Unknown; } + operator bool () const { return (bool) type; } /* Check whether two hash are equal. */ bool operator == (const Hash & h2) const; @@ -127,6 +130,8 @@ Hash compressHash(const Hash & hash, unsigned int newSize); /* Parse a string representing a hash type. */ HashType parseHashType(const string & s); +/* Will return nothing on parse error */ +std::optional parseHashTypeOpt(const string & s); /* And the reverse. */ string printHashType(HashType ht); -- cgit v1.2.3 From 15abb2aa2ba7de06a86e05511f81633616e17d87 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 18 Jun 2020 22:09:22 +0000 Subject: Revert the `enum struct` change Not a regular git revert as there have been many merges and things. --- src/libutil/hash.hh | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) (limited to 'src/libutil/hash.hh') diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index 8f9364440..0d9916508 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -10,12 +10,7 @@ namespace nix { MakeError(BadHash, Error); -enum struct HashType : char { - MD5, - SHA1, - SHA256, - SHA512, -}; +enum HashType : char { htMD5, htSHA1, htSHA256, htSHA512 }; const int md5HashSize = 16; @@ -25,12 +20,7 @@ const int sha512HashSize = 64; extern const string base32Chars; -enum struct Base { - Base64, - Base32, - Base16, - SRI, -}; +enum Base : int { Base64, Base32, Base16, SRI }; struct Hash @@ -97,14 +87,14 @@ struct Hash std::string gitRev() const { - assert(type == HashType::SHA1); - return to_string(Base::Base16, false); + assert(type == htSHA1); + return to_string(Base16, false); } std::string gitShortRev() const { - assert(type == HashType::SHA1); - return std::string(to_string(Base::Base16, false), 0, 7); + assert(type == htSHA1); + return std::string(to_string(Base16, false), 0, 7); } }; -- cgit v1.2.3