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 #include "pw_string/internal/length.h" 25 26 namespace pw { 27 namespace kvs { 28 29 // Key is a simplified string_view used for KVS. This helps KVS work on 30 // platforms without C++17. 31 class Key { 32 public: 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) 37 : str_{str}, 38 length_{string::internal::ClampedLength( 39 str, std::numeric_limits<size_t>::max())} {} Key(const char * str,size_t len)40 constexpr Key(const char* str, size_t len) : str_{str}, length_{len} {} Key(const std::string & str)41 Key(const std::string& str) : str_{str.data()}, length_{str.length()} {} 42 43 #if __cplusplus >= 201703L Key(const std::string_view & str)44 constexpr Key(const std::string_view& str) 45 : str_{str.data()}, length_{str.length()} {} string_view()46 operator std::string_view() { return std::string_view{str_, length_}; } 47 #endif // __cplusplus >= 201703L 48 49 // Assignment 50 constexpr Key& operator=(const Key&) = default; 51 52 // Traits size()53 constexpr size_t size() const { return length_; } length()54 constexpr size_t length() const { return length_; } empty()55 constexpr bool empty() const { return length_ == 0; } 56 57 // Access 58 constexpr const char& operator[](size_t pos) const { return str_[pos]; } at(size_t pos)59 constexpr const char& at(size_t pos) const { return str_[pos]; } front()60 constexpr const char& front() const { return str_[0]; } back()61 constexpr const char& back() const { return str_[length_ - 1]; } data()62 constexpr const char* data() const { return str_; } 63 64 // Iterator begin()65 constexpr const char* begin() const { return str_; } end()66 constexpr const char* end() const { return str_ + length_; } 67 68 // Equal 69 constexpr bool operator==(Key other_key) const { 70 return length() == other_key.length() && 71 std::memcmp(str_, other_key.data(), length()) == 0; 72 } 73 74 // Not Equal 75 constexpr bool operator!=(Key other_key) const { 76 return length() != other_key.length() || 77 std::memcmp(str_, other_key.data(), length()) != 0; 78 } 79 80 private: 81 const char* str_; 82 size_t length_; 83 }; 84 85 } // namespace kvs 86 } // namespace pw 87