aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/hash.cc
diff options
context:
space:
mode:
authorCarlo Nucera <carlo.nucera@protonmail.com>2020-07-02 11:21:00 -0400
committerCarlo Nucera <carlo.nucera@protonmail.com>2020-07-02 11:21:00 -0400
commit36cbc74689321399aeae26ff506809b8d9b24674 (patch)
tree12bf7e580956d15fe2a739fb519a6bfaf664518d /src/libutil/hash.cc
parent9462d8a50b5443bc2dac616f94ded9ad37020094 (diff)
Inline and simplify in parseAnyPrefixed
Diffstat (limited to 'src/libutil/hash.cc')
-rw-r--r--src/libutil/hash.cc27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index a077d40a0..75f8f319c 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -144,9 +144,32 @@ Hash Hash::parseSRI(std::string_view original) {
return Hash(rest, parsedType, true);
}
-Hash Hash::parseAnyPrefixed(std::string_view s)
+Hash Hash::parseAnyPrefixed(std::string_view original)
{
- return parseAny(s, std::nullopt);
+ auto rest = original;
+
+ bool isSRI = false;
+
+ // Parse the has type before the separater, if there was one.
+ std::optional<HashType> optParsedType;
+ {
+ auto hashRaw = splitPrefix(rest, ':');
+
+ if (!hashRaw) {
+ hashRaw = splitPrefix(rest, '-');
+ if (hashRaw)
+ isSRI = true;
+ }
+ if (hashRaw)
+ optParsedType = parseHashType(*hashRaw);
+ }
+
+ // Either the string or user must provide the type, if they both do they
+ // must agree.
+ if (!optParsedType)
+ throw BadHash("hash '%s' does not include a type.", rest);
+
+ return Hash(rest, *optParsedType, isSRI);
}
Hash Hash::parseAny(std::string_view original, std::optional<HashType> optType)