forked from lix-project/lix
Abstract common parsing functionality
This commit is contained in:
parent
36cbc74689
commit
ea48e3a5b5
|
@ -147,22 +147,7 @@ Hash Hash::parseSRI(std::string_view original) {
|
||||||
Hash Hash::parseAnyPrefixed(std::string_view original)
|
Hash Hash::parseAnyPrefixed(std::string_view original)
|
||||||
{
|
{
|
||||||
auto rest = original;
|
auto rest = original;
|
||||||
|
auto [optParsedType, isSRI] = getParsedTypeAndSRI(rest);
|
||||||
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
|
// Either the string or user must provide the type, if they both do they
|
||||||
// must agree.
|
// must agree.
|
||||||
|
@ -175,23 +160,7 @@ Hash Hash::parseAnyPrefixed(std::string_view original)
|
||||||
Hash Hash::parseAny(std::string_view original, std::optional<HashType> optType)
|
Hash Hash::parseAny(std::string_view original, std::optional<HashType> optType)
|
||||||
{
|
{
|
||||||
auto rest = original;
|
auto rest = original;
|
||||||
|
auto [optParsedType, isSRI] = getParsedTypeAndSRI(rest);
|
||||||
bool isSRI = false;
|
|
||||||
HashType hashType;
|
|
||||||
|
|
||||||
// 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
|
// Either the string or user must provide the type, if they both do they
|
||||||
// must agree.
|
// must agree.
|
||||||
|
@ -200,7 +169,7 @@ Hash Hash::parseAny(std::string_view original, std::optional<HashType> optType)
|
||||||
else if (optParsedType && optType && *optParsedType != *optType)
|
else if (optParsedType && optType && *optParsedType != *optType)
|
||||||
throw BadHash("hash '%s' should have type '%s'", original, printHashType(*optType));
|
throw BadHash("hash '%s' should have type '%s'", original, printHashType(*optType));
|
||||||
|
|
||||||
hashType = optParsedType ? *optParsedType : *optType;
|
HashType hashType = optParsedType ? *optParsedType : *optType;
|
||||||
return Hash(rest, hashType, isSRI);
|
return Hash(rest, hashType, isSRI);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,4 +21,26 @@ static inline std::optional<std::string_view> splitPrefix(std::string_view & str
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mutates the string to eliminate the prefixes when found
|
||||||
|
std::pair<std::optional<HashType>, bool> getParsedTypeAndSRI(std::string_view & rest) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::make_pair(optParsedType, isSRI);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue