forked from lix-project/lix
WIP initial design
This commit is contained in:
parent
a1f66d1d9e
commit
b798efb829
|
@ -132,10 +132,13 @@ std::string Hash::to_string(Base base, bool includeType) const
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
Hash::Hash(std::string_view s, HashType type) : Hash(s, std::optional { type }) { }
|
Hash fromSRI(std::string_view original) {
|
||||||
Hash::Hash(std::string_view s) : Hash(s, std::optional<HashType>{}) { }
|
|
||||||
|
|
||||||
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;
|
auto rest = original;
|
||||||
|
|
||||||
|
@ -161,13 +164,17 @@ Hash::Hash(std::string_view original, std::optional<HashType> optType)
|
||||||
if (!optParsedType && !optType) {
|
if (!optParsedType && !optType) {
|
||||||
throw BadHash("hash '%s' does not include a type, nor is the type otherwise known from context.", rest);
|
throw BadHash("hash '%s' does not include a type, nor is the type otherwise known from context.", rest);
|
||||||
} else {
|
} else {
|
||||||
this->type = optParsedType ? *optParsedType : *optType;
|
|
||||||
if (optParsedType && optType && *optParsedType != *optType)
|
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));
|
||||||
|
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()) {
|
if (!isSRI && rest.size() == base16Len()) {
|
||||||
|
|
||||||
auto parseHexDigit = [&](char c) {
|
auto parseHexDigit = [&](char c) {
|
||||||
|
|
|
@ -40,13 +40,16 @@ struct Hash
|
||||||
is not present, then the hash type must be specified in the
|
is not present, then the hash type must be specified in the
|
||||||
string. */
|
string. */
|
||||||
Hash(std::string_view s, std::optional<HashType> type);
|
Hash(std::string_view s, std::optional<HashType> type);
|
||||||
// type must be provided
|
|
||||||
Hash(std::string_view s, HashType type);
|
|
||||||
// hash type must be part of string
|
// hash type must be part of string
|
||||||
Hash(std::string_view s);
|
Hash(std::string_view s);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// type must be provided, s must not include <type> prefix
|
||||||
|
Hash(std::string_view s, HashType type);
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
|
public:
|
||||||
/* Check whether a hash is set. */
|
/* Check whether a hash is set. */
|
||||||
operator bool () const { return (bool) type; }
|
operator bool () const { return (bool) type; }
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
@ -8,9 +9,7 @@ namespace nix {
|
||||||
// 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.
|
||||||
std::optional<std::string_view> splitPrefix(std::string_view & string, char separator);
|
static inline std::optional<std::string_view> splitPrefix(std::string_view & string, char separator) {
|
||||||
|
|
||||||
std::optional<std::string_view> splitPrefix(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) {
|
||||||
|
|
Loading…
Reference in a new issue