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 <algorithm>
17 #include <cstddef>
18 #include <iterator>
19 
20 #include "pw_polyfill/standard_library/namespace.h"
21 
22 #define __cpp_lib_string_view 201606L
23 
24 _PW_POLYFILL_BEGIN_NAMESPACE_STD
25 
26 template <typename T>
27 class basic_string_view {
28  public:
29   using traits_type = void;  // No traits object is used.
30   using value_type = T;
31   using pointer = T*;
32   using const_pointer = const T*;
33   using reference = T&;
34   using const_reference = const T&;
35   using const_iterator = const T*;
36   using iterator = const_iterator;
37   using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;
38   using reverse_iterator = const_reverse_iterator;
39   using size_type = size_t;
40   using difference_type = ptrdiff_t;
41 
42   static constexpr size_type npos = size_type(-1);
43 
basic_string_view()44   constexpr basic_string_view() noexcept : string_(nullptr), size_(0) {}
45   constexpr basic_string_view(const basic_string_view&) noexcept = default;
basic_string_view(const T * string,size_type count)46   constexpr basic_string_view(const T* string, size_type count)
47       : string_(string), size_(count) {}
basic_string_view(const T * string)48   constexpr basic_string_view(const T* string)
49       : string_(string), size_(CStringLength(string)) {}
50 
51   constexpr basic_string_view& operator=(const basic_string_view&) noexcept =
52       default;
53 
begin()54   constexpr const_iterator begin() const noexcept { return string_; }
cbegin()55   constexpr const_iterator cbegin() const noexcept { return begin(); }
56 
end()57   constexpr const_iterator end() const noexcept { return string_ + size_; }
cend()58   constexpr const_iterator cend() const noexcept { return end(); }
59 
60   // NOT IMPLEMENTED: Reverse iterators not supported.
61   constexpr const_reverse_iterator rbegin() const noexcept;
62   constexpr const_reverse_iterator crbegin() const noexcept;
63 
64   constexpr const_reverse_iterator rend() const noexcept;
65   constexpr const_reverse_iterator crend() const noexcept;
66 
67   constexpr const_reference operator[](size_type pos) const {
68     return data()[pos];
69   }
70 
71   // NOT IMPLEMENTED: at() has no bounds checking.
at(size_type pos)72   constexpr const_reference at(size_type pos) const { return data()[pos]; }
73 
front()74   constexpr const_reference front() const { return data()[0]; }
75 
back()76   constexpr const_reference back() const { return data()[size() - 1]; }
77 
data()78   constexpr const_pointer data() const noexcept { return string_; }
79 
size()80   constexpr size_type size() const noexcept { return size_; }
length()81   constexpr size_type length() const noexcept { return size(); }
82 
max_size()83   constexpr size_type max_size() const noexcept { return ~size_t{0}; }
84 
empty()85   [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0u; }
86 
remove_prefix(size_type characters)87   constexpr void remove_prefix(size_type characters) {
88     string_ += characters;
89     size_ -= characters;
90   }
91 
remove_suffix(size_type characters)92   constexpr void remove_suffix(size_type characters) { size_ -= characters; }
93 
swap(basic_string_view & other)94   constexpr void swap(basic_string_view& other) noexcept {
95     pointer temp_string = string_;
96     string_ = other.string_;
97     other.string_ = temp_string;
98 
99     size_type temp_size = size_;
100     size_ = other.size_;
101     other.size_ = temp_size;
102   }
103 
104   // NOT IMPLEMENTED: copy does no bounds checking.
105   constexpr size_type copy(T* dest, size_type count, size_type pos = 0) const {
106     const size_type to_copy = min(count, size() - pos);
107     for (size_type i = pos; i < pos + to_copy; ++i) {
108       *dest++ = string_[i];
109     }
110     return to_copy;
111   }
112 
113   constexpr basic_string_view substr(size_type pos = 0,
114                                      size_type count = npos) const {
115     return basic_string_view(string_ + pos, min(count, size() - pos));
116   }
117 
118   // NOT IMPLEMENTED: These functions and their overloads are not defined.
119   constexpr int compare(basic_string_view view) const noexcept;
120   constexpr bool starts_with(basic_string_view view) const noexcept;
121   constexpr bool ends_with(basic_string_view view) const noexcept;
122   constexpr size_type find(basic_string_view view,
123                            size_type pos = 0) const noexcept;
124   constexpr size_type rfind(basic_string_view view,
125                             size_type pos = npos) const noexcept;
126   constexpr size_type find_first_of(basic_string_view view,
127                                     size_type pos = 0) const noexcept;
128   constexpr size_type find_last_of(basic_string_view view,
129                                    size_type pos = npos) const noexcept;
130   constexpr size_type find_first_not_of(basic_string_view view,
131                                         size_type pos = 0) const noexcept;
132   constexpr size_type find_last_not_of(basic_string_view view,
133                                        size_type pos = npos) const noexcept;
134 
135  private:
CStringLength(const T * string)136   static constexpr size_type CStringLength(const T* string) {
137     size_type length = 0;
138     while (string[length] != T()) {
139       length += 1;
140     }
141     return length;
142   }
143 
144   const_pointer string_;
145   size_type size_;
146 };
147 
148 template <typename T>
149 constexpr bool operator==(basic_string_view<T> lhs, basic_string_view<T> rhs) {
150   if (lhs.size() != rhs.size()) {
151     return false;
152   }
153   for (typename basic_string_view<T>::size_type i = 0; i < lhs.size(); ++i) {
154     if (lhs[i] != rhs[i]) {
155       return false;
156     }
157   }
158   return true;
159 }
160 
161 template <typename T>
162 constexpr bool operator!=(basic_string_view<T> lhs, basic_string_view<T> rhs) {
163   return !(lhs == rhs);
164 }
165 
166 // NOT IMPLEMENTED: Other comparison operators are not defined.
167 
168 using string_view = basic_string_view<char>;
169 using wstring_view = basic_string_view<wchar_t>;
170 using u16string_view = basic_string_view<char16_t>;
171 using u32string_view = basic_string_view<char32_t>;
172 
173 // NOT IMPLEMENTED: string_view literals cannot be implemented since they do not
174 //                  start with _.
175 
176 _PW_POLYFILL_END_NAMESPACE_STD
177