pw_kvs: Make Key an alias for string_view

Key is no longer needed as Pigweed now requires C++17.

Change-Id: I7bc5f0fa4ecf04cf7222409785b7a8a08a7ac49b
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/204590
Pigweed-Auto-Submit: Taylor Cramer <cramertj@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
This commit is contained in:
Taylor Cramer 2024-04-18 22:16:35 +00:00 committed by CQ Bot Account
parent 463dca050a
commit e3662e8e52
2 changed files with 4 additions and 65 deletions

View File

@ -35,8 +35,7 @@ TEST(Key, ConstructorEmpty) {
EXPECT_EQ(key.size(), 0u);
EXPECT_TRUE(key.empty());
EXPECT_EQ(key.data(), nullptr);
EXPECT_EQ(key.begin(), nullptr);
EXPECT_EQ(key.end(), nullptr);
EXPECT_EQ(key.begin(), key.end());
}
TEST(Key, ConstructorString) {

View File

@ -24,69 +24,9 @@
namespace pw {
namespace kvs {
// Key is a simplified string_view used for KVS. This helps KVS work on
// platforms without C++17.
class Key {
public:
using value_type = const char;
// Constructors
constexpr Key() : str_{nullptr}, length_{0} {}
constexpr Key(const Key&) = default;
constexpr Key(const char* str) : str_{str}, length_{CStringLength(str)} {}
constexpr Key(const char* str, size_t len) : str_{str}, length_{len} {}
Key(const std::string& str) : str_{str.data()}, length_{str.length()} {}
#if __cplusplus >= 201703L
constexpr Key(const std::string_view& str)
: str_{str.data()}, length_{str.length()} {}
operator std::string_view() { return std::string_view{str_, length_}; }
#endif // __cplusplus >= 201703L
// Assignment
constexpr Key& operator=(const Key&) = default;
// Traits
constexpr size_t size() const { return length_; }
constexpr size_t length() const { return length_; }
constexpr bool empty() const { return length_ == 0; }
// Access
constexpr const char& operator[](size_t pos) const { return str_[pos]; }
constexpr const char& at(size_t pos) const { return str_[pos]; }
constexpr const char& front() const { return str_[0]; }
constexpr const char& back() const { return str_[length_ - 1]; }
constexpr const char* data() const { return str_; }
// Iterator
constexpr const char* begin() const { return str_; }
constexpr const char* end() const { return str_ + length_; }
// Equal
constexpr bool operator==(Key other_key) const {
return length() == other_key.length() &&
std::memcmp(str_, other_key.data(), length()) == 0;
}
// Not Equal
constexpr bool operator!=(Key other_key) const {
return length() != other_key.length() ||
std::memcmp(str_, other_key.data(), length()) != 0;
}
private:
// constexpr version of strlen
static constexpr size_t CStringLength(const char* str) {
size_t length = 0;
while (str[length] != '\0') {
length += 1;
}
return length;
}
const char* str_;
size_t length_;
};
// Key was a simplified string_view used for KVS pre-C++17.
// It is now a simple alias since Pigweed requires C++17.
using Key = std::string_view;
} // namespace kvs
} // namespace pw