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