aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/hash.cc
diff options
context:
space:
mode:
authorCarlo Nucera <carlo.nucera@protonmail.com>2020-06-30 14:10:30 -0400
committerCarlo Nucera <carlo.nucera@protonmail.com>2020-06-30 14:10:30 -0400
commitb798efb829415eb47a532e9479523afdff74eca7 (patch)
treee683c5de61bd6db343f8a8222d0653f2899952fc /src/libutil/hash.cc
parenta1f66d1d9e70d4204a3686002b02277465a6b7ab (diff)
WIP initial design
Diffstat (limited to 'src/libutil/hash.cc')
-rw-r--r--src/libutil/hash.cc17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index 26ab5a110..36de293bb 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -132,10 +132,13 @@ std::string Hash::to_string(Base base, bool includeType) const
return s;
}
-Hash::Hash(std::string_view s, HashType type) : Hash(s, std::optional { type }) { }
-Hash::Hash(std::string_view s) : Hash(s, std::optional<HashType>{}) { }
+Hash fromSRI(std::string_view original) {
-Hash::Hash(std::string_view original, std::optional<HashType> optType)
+}
+
+Hash::Hash(std::string_view s) : Hash(s, std::nullopt) { }
+
+static HashType newFunction(std::string_view & rest, std::optional<HashType> optType)
{
auto rest = original;
@@ -161,13 +164,17 @@ Hash::Hash(std::string_view original, std::optional<HashType> optType)
if (!optParsedType && !optType) {
throw BadHash("hash '%s' does not include a type, nor is the type otherwise known from context.", rest);
} else {
- this->type = optParsedType ? *optParsedType : *optType;
if (optParsedType && optType && *optParsedType != *optType)
throw BadHash("hash '%s' should have type '%s'", original, printHashType(*optType));
+ return optParsedType ? *optParsedType : *optType;
}
+}
- init();
+// mutates the string_view
+Hash::Hash(std::string_view original, std::optional<HashType> optType)
+ : Hash(original, newFunction(original, optType))
+Hash::Hash(std::string_view original, HashType type) : Hash(type) {
if (!isSRI && rest.size() == base16Len()) {
auto parseHexDigit = [&](char c) {