diff options
| author | Dan Engelbrecht <[email protected]> | 2024-10-03 12:35:44 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2024-10-03 12:35:44 +0200 |
| commit | db531191789f48a141c42fef229bca7a7922443d (patch) | |
| tree | cc1570bf8dedccb975d594bd91284ac5d0bd0bae /src/zencore | |
| parent | simplified CleanDirectory implementation (#182) (diff) | |
| download | zen-db531191789f48a141c42fef229bca7a7922443d.tar.xz zen-db531191789f48a141c42fef229bca7a7922443d.zip | |
cache get command (#183)
* move TryParseObjectId and TryParseIoHash to Oid::TryParse and IoHash::TryParse respectively
* zen cache-get command
Diffstat (limited to 'src/zencore')
| -rw-r--r-- | src/zencore/compactbinaryjson.cpp | 42 | ||||
| -rw-r--r-- | src/zencore/include/zencore/iohash.h | 1 | ||||
| -rw-r--r-- | src/zencore/include/zencore/uid.h | 1 | ||||
| -rw-r--r-- | src/zencore/iohash.cpp | 18 | ||||
| -rw-r--r-- | src/zencore/uid.cpp | 28 |
5 files changed, 45 insertions, 45 deletions
diff --git a/src/zencore/compactbinaryjson.cpp b/src/zencore/compactbinaryjson.cpp index 58a91c731..d8c8a8584 100644 --- a/src/zencore/compactbinaryjson.cpp +++ b/src/zencore/compactbinaryjson.cpp @@ -466,7 +466,7 @@ private: case Json::Type::STRING: { Oid Id; - if (TryParseObjectId(Json.string_value(), Id)) + if (Oid::TryParse(Json.string_value(), Id)) { if (FieldName.empty()) { @@ -481,7 +481,7 @@ private: } IoHash Hash; - if (TryParseIoHash(Json.string_value(), Hash)) + if (IoHash::TryParse(Json.string_value(), Hash)) { if (FieldName.empty()) { @@ -511,44 +511,6 @@ private: return true; } - - static constexpr AsciiSet HexCharSet = AsciiSet("0123456789abcdefABCDEF"); - - static bool TryParseObjectId(std::string_view Str, Oid& Id) - { - using namespace std::literals; - - if (Str.size() == Oid::StringLength && AsciiSet::HasOnly(Str, HexCharSet)) - { - Id = Oid::FromHexString(Str); - return true; - } - - if (Str.starts_with("0x"sv)) - { - return TryParseObjectId(Str.substr(2), Id); - } - - return false; - } - - static bool TryParseIoHash(std::string_view Str, IoHash& Hash) - { - using namespace std::literals; - - if (Str.size() == IoHash::StringLength && AsciiSet::HasOnly(Str, HexCharSet)) - { - Hash = IoHash::FromHexString(Str); - return true; - } - - if (Str.starts_with("0x"sv)) - { - return TryParseIoHash(Str.substr(2), Hash); - } - - return false; - } }; CbFieldIterator diff --git a/src/zencore/include/zencore/iohash.h b/src/zencore/include/zencore/iohash.h index a8fc9e6c1..9c9f487bb 100644 --- a/src/zencore/include/zencore/iohash.h +++ b/src/zencore/include/zencore/iohash.h @@ -50,6 +50,7 @@ struct IoHash static IoHash HashBuffer(const IoBuffer& Buffer); static IoHash FromHexString(const char* string); static IoHash FromHexString(const std::string_view string); + static bool TryParse(std::string_view Str, IoHash& Hash); const char* ToHexString(char* outString /* 40 characters + NUL terminator */) const; StringBuilderBase& ToHexString(StringBuilderBase& outBuilder) const; std::string ToHexString() const; diff --git a/src/zencore/include/zencore/uid.h b/src/zencore/include/zencore/uid.h index 08a335392..1b6fc3f6a 100644 --- a/src/zencore/include/zencore/uid.h +++ b/src/zencore/include/zencore/uid.h @@ -63,6 +63,7 @@ struct Oid const Oid& Generate(); [[nodiscard]] static Oid FromHexString(const std::string_view String); [[nodiscard]] static Oid TryFromHexString(const std::string_view String, const Oid& Default = Oid::Zero); + static bool TryParse(std::string_view Str, Oid& Id); StringBuilderBase& ToString(StringBuilderBase& OutString) const; void ToString(char OutString[StringLength]) const; std::string ToString() const; diff --git a/src/zencore/iohash.cpp b/src/zencore/iohash.cpp index 8f3f8da26..7200e6e3f 100644 --- a/src/zencore/iohash.cpp +++ b/src/zencore/iohash.cpp @@ -99,6 +99,24 @@ IoHash::FromHexString(std::string_view string) return io; } +bool +IoHash::TryParse(std::string_view Str, IoHash& Hash) +{ + using namespace std::literals; + + if (Str.size() == IoHash::StringLength) + { + return ParseHexBytes(Str.data(), Str.size(), Hash.Hash); + } + + if (Str.starts_with("0x"sv)) + { + return TryParse(Str.substr(2), Hash); + } + + return false; +} + const char* IoHash::ToHexString(char* outString /* 40 characters + NUL terminator */) const { diff --git a/src/zencore/uid.cpp b/src/zencore/uid.cpp index 8ef660c7a..d7636f2ad 100644 --- a/src/zencore/uid.cpp +++ b/src/zencore/uid.cpp @@ -102,12 +102,22 @@ Oid::TryFromHexString(const std::string_view String, const Oid& Default) } } -Oid -Oid::FromMemory(const void* Ptr) +bool +Oid::TryParse(std::string_view Str, Oid& Id) { - Oid Id; - memcpy(Id.OidBits, Ptr, sizeof Id); - return Id; + using namespace std::literals; + + if (Str.size() == Oid::StringLength) + { + return ParseHexBytes(Str.data(), Str.size(), reinterpret_cast<uint8_t*>(Id.OidBits)); + } + + if (Str.starts_with("0x"sv)) + { + return TryParse(Str.substr(2), Id); + } + + return false; } void @@ -136,6 +146,14 @@ Oid::ToString(StringBuilderBase& OutString) const return OutString; } +Oid +Oid::FromMemory(const void* Ptr) +{ + Oid Id; + memcpy(Id.OidBits, Ptr, sizeof Id); + return Id; +} + #if ZEN_WITH_TESTS TEST_CASE("Oid") |