• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include <cstring>
17 #include <limits>
18 #include <string>
19 
20 #if __cplusplus >= 201703L
21 #include <string_view>
22 #endif  // __cplusplus >= 201703L
23 
24 namespace pw {
25 namespace kvs {
26 
27 // Key is a simplified string_view used for KVS. This helps KVS work on
28 // platforms without C++17.
29 class Key {
30  public:
31   using value_type = const char;
32 
33   // Constructors
Key()34   constexpr Key() : str_{nullptr}, length_{0} {}
35   constexpr Key(const Key&) = default;
Key(const char * str)36   constexpr Key(const char* str) : str_{str}, length_{CStringLength(str)} {}
Key(const char * str,size_t len)37   constexpr Key(const char* str, size_t len) : str_{str}, length_{len} {}
Key(const std::string & str)38   Key(const std::string& str) : str_{str.data()}, length_{str.length()} {}
39 
40 #if __cplusplus >= 201703L
Key(const std::string_view & str)41   constexpr Key(const std::string_view& str)
42       : str_{str.data()}, length_{str.length()} {}
string_view()43   operator std::string_view() { return std::string_view{str_, length_}; }
44 #endif  // __cplusplus >= 201703L
45 
46   // Assignment
47   constexpr Key& operator=(const Key&) = default;
48 
49   // Traits
size()50   constexpr size_t size() const { return length_; }
length()51   constexpr size_t length() const { return length_; }
empty()52   constexpr bool empty() const { return length_ == 0; }
53 
54   // Access
55   constexpr const char& operator[](size_t pos) const { return str_[pos]; }
at(size_t pos)56   constexpr const char& at(size_t pos) const { return str_[pos]; }
front()57   constexpr const char& front() const { return str_[0]; }
back()58   constexpr const char& back() const { return str_[length_ - 1]; }
data()59   constexpr const char* data() const { return str_; }
60 
61   // Iterator
begin()62   constexpr const char* begin() const { return str_; }
end()63   constexpr const char* end() const { return str_ + length_; }
64 
65   // Equal
66   constexpr bool operator==(Key other_key) const {
67     return length() == other_key.length() &&
68            std::memcmp(str_, other_key.data(), length()) == 0;
69   }
70 
71   // Not Equal
72   constexpr bool operator!=(Key other_key) const {
73     return length() != other_key.length() ||
74            std::memcmp(str_, other_key.data(), length()) != 0;
75   }
76 
77  private:
78   // constexpr version of strlen
CStringLength(const char * str)79   static constexpr size_t CStringLength(const char* str) {
80     size_t length = 0;
81     while (str[length] != '\0') {
82       length += 1;
83     }
84     return length;
85   }
86 
87   const char* str_;
88   size_t length_;
89 };
90 
91 }  // namespace kvs
92 }  // namespace pw
93