pw_kvs: Replace string_view with custom type

Create a new type 'Key' which behaves the same as string_view but
doesn't require C++17.

Test: Passes all tests.
Change-Id: I2ebadbb0405f3fa3520dad46ea500cafe652f230
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/27140
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: Rob Oliver <rgoliver@google.com>
diff --git a/pw_kvs/key_value_store.cc b/pw_kvs/key_value_store.cc
index 534313e..77ed52d 100644
--- a/pw_kvs/key_value_store.cc
+++ b/pw_kvs/key_value_store.cc
@@ -32,9 +32,8 @@
 namespace {
 
 using std::byte;
-using std::string_view;
 
-constexpr bool InvalidKey(std::string_view key) {
+constexpr bool InvalidKey(Key key) {
   return key.empty() || (key.size() > internal::Entry::kMaxKeyLength);
 }
 
@@ -360,7 +359,7 @@
   // Read the key from flash & validate the entry (which reads the value).
   Entry::KeyBuffer key_buffer;
   PW_TRY_ASSIGN(size_t key_length, entry.ReadKey(key_buffer));
-  const string_view key(key_buffer.data(), key_length);
+  const Key key(key_buffer.data(), key_length);
 
   PW_TRY(entry.VerifyChecksumInFlash());
 
@@ -402,7 +401,7 @@
   return Status::NotFound();
 }
 
-StatusWithSize KeyValueStore::Get(string_view key,
+StatusWithSize KeyValueStore::Get(Key key,
                                   std::span<byte> value_buffer,
                                   size_t offset_bytes) const {
   PW_TRY_WITH_SIZE(CheckReadOperation(key));
@@ -413,7 +412,7 @@
   return Get(key, metadata, value_buffer, offset_bytes);
 }
 
-Status KeyValueStore::PutBytes(string_view key, std::span<const byte> value) {
+Status KeyValueStore::PutBytes(Key key, std::span<const byte> value) {
   PW_TRY(CheckWriteOperation(key));
   DBG("Writing key/value; key length=%u, value length=%u",
       unsigned(key.size()),
@@ -445,7 +444,7 @@
   return status;
 }
 
-Status KeyValueStore::Delete(string_view key) {
+Status KeyValueStore::Delete(Key key) {
   PW_TRY(CheckWriteOperation(key));
 
   EntryMetadata metadata;
@@ -486,7 +485,7 @@
   return iterator(*this, cache_iterator);
 }
 
-StatusWithSize KeyValueStore::ValueSize(string_view key) const {
+StatusWithSize KeyValueStore::ValueSize(Key key) const {
   PW_TRY_WITH_SIZE(CheckReadOperation(key));
 
   EntryMetadata metadata;
@@ -514,8 +513,7 @@
   return read_result;
 }
 
-Status KeyValueStore::FindEntry(string_view key,
-                                EntryMetadata* found_entry) const {
+Status KeyValueStore::FindEntry(Key key, EntryMetadata* found_entry) const {
   StatusWithSize find_result =
       entry_cache_.Find(partition_, sectors_, formats_, key, found_entry);
 
@@ -525,8 +523,7 @@
   return find_result.status();
 }
 
-Status KeyValueStore::FindExisting(string_view key,
-                                   EntryMetadata* metadata) const {
+Status KeyValueStore::FindExisting(Key key, EntryMetadata* metadata) const {
   Status status = FindEntry(key, metadata);
 
   // If the key's hash collides with an existing key or if the key is deleted,
@@ -538,7 +535,7 @@
   return status;
 }
 
-StatusWithSize KeyValueStore::Get(string_view key,
+StatusWithSize KeyValueStore::Get(Key key,
                                   const EntryMetadata& metadata,
                                   std::span<std::byte> value_buffer,
                                   size_t offset_bytes) const {
@@ -560,7 +557,7 @@
   return result;
 }
 
-Status KeyValueStore::FixedSizeGet(std::string_view key,
+Status KeyValueStore::FixedSizeGet(Key key,
                                    void* value,
                                    size_t size_bytes) const {
   PW_TRY(CheckWriteOperation(key));
@@ -571,7 +568,7 @@
   return FixedSizeGet(key, metadata, value, size_bytes);
 }
 
-Status KeyValueStore::FixedSizeGet(std::string_view key,
+Status KeyValueStore::FixedSizeGet(Key key,
                                    const EntryMetadata& metadata,
                                    void* value,
                                    size_t size_bytes) const {
@@ -599,7 +596,7 @@
   return StatusWithSize(entry.value_size());
 }
 
-Status KeyValueStore::CheckWriteOperation(string_view key) const {
+Status KeyValueStore::CheckWriteOperation(Key key) const {
   if (InvalidKey(key)) {
     return Status::InvalidArgument();
   }
@@ -611,7 +608,7 @@
   return Status::Ok();
 }
 
-Status KeyValueStore::CheckReadOperation(string_view key) const {
+Status KeyValueStore::CheckReadOperation(Key key) const {
   if (InvalidKey(key)) {
     return Status::InvalidArgument();
   }
@@ -626,7 +623,7 @@
 
 Status KeyValueStore::WriteEntryForExistingKey(EntryMetadata& metadata,
                                                EntryState new_state,
-                                               string_view key,
+                                               Key key,
                                                std::span<const byte> value) {
   // Read the original entry to get the size for sector accounting purposes.
   Entry entry;
@@ -635,7 +632,7 @@
   return WriteEntry(key, value, new_state, &metadata, &entry);
 }
 
-Status KeyValueStore::WriteEntryForNewKey(string_view key,
+Status KeyValueStore::WriteEntryForNewKey(Key key,
                                           std::span<const byte> value) {
   if (entry_cache_.full()) {
     WRN("KVS full: trying to store a new entry, but can't. Have %u entries",
@@ -646,7 +643,7 @@
   return WriteEntry(key, value, EntryState::kValid);
 }
 
-Status KeyValueStore::WriteEntry(string_view key,
+Status KeyValueStore::WriteEntry(Key key,
                                  std::span<const byte> value,
                                  EntryState new_state,
                                  EntryMetadata* prior_metadata,
@@ -694,7 +691,7 @@
 
 KeyValueStore::EntryMetadata KeyValueStore::CreateOrUpdateKeyDescriptor(
     const Entry& entry,
-    string_view key,
+    Key key,
     EntryMetadata* prior_metadata,
     size_t prior_size) {
   // If there is no prior descriptor, create a new one.
@@ -796,7 +793,7 @@
 }
 
 Status KeyValueStore::AppendEntry(const Entry& entry,
-                                  string_view key,
+                                  Key key,
                                   std::span<const byte> value) {
   const StatusWithSize result = entry.Write(key, value);
 
@@ -1218,7 +1215,7 @@
 }
 
 KeyValueStore::Entry KeyValueStore::CreateEntry(Address address,
-                                                string_view key,
+                                                Key key,
                                                 std::span<const byte> value,
                                                 EntryState state) {
   // Always bump the transaction ID when creating a new entry.