Have splitPrefix and splitPrefixTo parser helpers

This commit is contained in:
John Ericson 2020-07-02 23:16:57 +00:00
parent a7cd7425d9
commit 13796be78d
3 changed files with 15 additions and 9 deletions

View file

@ -46,14 +46,14 @@ ContentAddress parseContentAddress(std::string_view rawCa) {
std::string_view prefix; std::string_view prefix;
{ {
auto optPrefix = splitPrefix(rest, ':'); auto optPrefix = splitPrefixTo(rest, ':');
if (!optPrefix) if (!optPrefix)
throw UsageError("not a content address because it is not in the form \"<prefix>:<rest>\": %s", rawCa); throw UsageError("not a content address because it is not in the form \"<prefix>:<rest>\": %s", rawCa);
prefix = *optPrefix; prefix = *optPrefix;
} }
auto parseHashType_ = [&](){ auto parseHashType_ = [&](){
auto hashTypeRaw = splitPrefix(rest, ':'); auto hashTypeRaw = splitPrefixTo(rest, ':');
if (!hashTypeRaw) if (!hashTypeRaw)
throw UsageError("content address hash must be in form \"<algo>:<hash>\", but found: %s", rawCa); throw UsageError("content address hash must be in form \"<algo>:<hash>\", but found: %s", rawCa);
HashType hashType = parseHashType(*hashTypeRaw); HashType hashType = parseHashType(*hashTypeRaw);
@ -73,10 +73,8 @@ ContentAddress parseContentAddress(std::string_view rawCa) {
} else if (prefix == "fixed") { } else if (prefix == "fixed") {
// Parse method // Parse method
auto method = FileIngestionMethod::Flat; auto method = FileIngestionMethod::Flat;
if (rest.substr(0, 2) == "r:") { if (splitPrefix(rest, "r:"))
method = FileIngestionMethod::Recursive; method = FileIngestionMethod::Recursive;
rest = rest.substr(2);
}
HashType hashType = parseHashType_(); HashType hashType = parseHashType_();
return FixedOutputHash { return FixedOutputHash {
.method = method, .method = method,

View file

@ -136,7 +136,7 @@ Hash Hash::parseSRI(std::string_view original) {
auto rest = original; auto rest = original;
// Parse the has type before the separater, if there was one. // Parse the has type before the separater, if there was one.
auto hashRaw = splitPrefix(rest, '-'); auto hashRaw = splitPrefixTo(rest, '-');
if (!hashRaw) if (!hashRaw)
throw BadHash("hash '%s' is not SRI", original); throw BadHash("hash '%s' is not SRI", original);
HashType parsedType = parseHashType(*hashRaw); HashType parsedType = parseHashType(*hashRaw);
@ -151,10 +151,10 @@ static std::pair<std::optional<HashType>, bool> getParsedTypeAndSRI(std::string_
// Parse the has type before the separater, if there was one. // Parse the has type before the separater, if there was one.
std::optional<HashType> optParsedType; std::optional<HashType> optParsedType;
{ {
auto hashRaw = splitPrefix(rest, ':'); auto hashRaw = splitPrefixTo(rest, ':');
if (!hashRaw) { if (!hashRaw) {
hashRaw = splitPrefix(rest, '-'); hashRaw = splitPrefixTo(rest, '-');
if (hashRaw) if (hashRaw)
isSRI = true; isSRI = true;
} }

View file

@ -3,13 +3,15 @@
#include <optional> #include <optional>
#include <string_view> #include <string_view>
#include "util.hh"
namespace nix { namespace nix {
// If `separator` is found, we return the portion of the string before the // If `separator` is found, we return the portion of the string before the
// separator, and modify the string argument to contain only the part after the // separator, and modify the string argument to contain only the part after the
// separator. Otherwise, wer return `std::nullopt`, and we leave the argument // separator. Otherwise, wer return `std::nullopt`, and we leave the argument
// string alone. // string alone.
static inline std::optional<std::string_view> splitPrefix(std::string_view & string, char separator) { static inline std::optional<std::string_view> splitPrefixTo(std::string_view & string, char separator) {
auto sepInstance = string.find(separator); auto sepInstance = string.find(separator);
if (sepInstance != std::string_view::npos) { if (sepInstance != std::string_view::npos) {
@ -21,5 +23,11 @@ static inline std::optional<std::string_view> splitPrefix(std::string_view & str
return std::nullopt; return std::nullopt;
} }
static inline bool splitPrefix(std::string_view & string, std::string_view prefix) {
bool res = hasPrefix(string, prefix);
if (res)
string.remove_prefix(prefix.length());
return res;
}
} }