string-view.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright 2017 WebAssembly Community Group participants
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef WABT_STRING_VIEW_H_
  17. #define WABT_STRING_VIEW_H_
  18. #include <cassert>
  19. #include <functional>
  20. #include <iterator>
  21. #include <string>
  22. #include "src/hash-util.h"
  23. namespace wabt {
  24. // This is a simplified implemention of C++17's basic_string_view template.
  25. //
  26. // Missing features:
  27. // * Not a template
  28. // * No allocator support
  29. // * Some functions are not implemented
  30. // * Asserts instead of exceptions
  31. // * Some functions are not constexpr because we don't compile in C++17 mode
  32. class string_view {
  33. public:
  34. typedef std::char_traits<char> traits_type;
  35. typedef char value_type;
  36. typedef char* pointer;
  37. typedef const char* const_pointer;
  38. typedef char& reference;
  39. typedef const char& const_reference;
  40. typedef const char* const_iterator;
  41. typedef const_iterator iterator;
  42. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  43. typedef const_reverse_iterator reverse_iterator;
  44. typedef std::size_t size_type;
  45. typedef std::ptrdiff_t difference_type;
  46. static const size_type npos = size_type(-1);
  47. // construction and assignment
  48. constexpr string_view() noexcept;
  49. constexpr string_view(const string_view&) noexcept = default;
  50. string_view& operator=(const string_view&) noexcept = default;
  51. string_view(const std::string& str) noexcept;
  52. /*constexpr*/ string_view(const char* str);
  53. constexpr string_view(const char* str, size_type len);
  54. // iterator support
  55. constexpr const_iterator begin() const noexcept;
  56. constexpr const_iterator end() const noexcept;
  57. constexpr const_iterator cbegin() const noexcept;
  58. constexpr const_iterator cend() const noexcept;
  59. const_reverse_iterator rbegin() const noexcept;
  60. const_reverse_iterator rend() const noexcept;
  61. const_reverse_iterator crbegin() const noexcept;
  62. const_reverse_iterator crend() const noexcept;
  63. // capacity
  64. constexpr size_type size() const noexcept;
  65. constexpr size_type length() const noexcept;
  66. constexpr size_type max_size() const noexcept;
  67. constexpr bool empty() const noexcept;
  68. // element access
  69. constexpr const_reference operator[](size_type pos) const;
  70. /*constexpr*/ const_reference at(size_type pos) const;
  71. /*constexpr*/ const_reference front() const;
  72. /*constexpr*/ const_reference back() const;
  73. constexpr const_pointer data() const noexcept;
  74. // modifiers
  75. /*constexpr*/ void remove_prefix(size_type n);
  76. /*constexpr*/ void remove_suffix(size_type n);
  77. /*constexpr*/ void swap(string_view& s) noexcept;
  78. // string operations
  79. explicit operator std::string() const;
  80. std::string to_string() const;
  81. size_type copy(char* s, size_type n, size_type pos = 0) const;
  82. /*constexpr*/ string_view substr(size_type pos = 0, size_type n = npos) const;
  83. /*constexpr*/ int compare(string_view s) const noexcept;
  84. /*constexpr*/ int compare(size_type pos1, size_type n1, string_view s) const;
  85. /*constexpr*/ int compare(size_type pos1,
  86. size_type n1,
  87. string_view s,
  88. size_type pos2,
  89. size_type n2) const;
  90. /*constexpr*/ int compare(const char* s) const;
  91. /*constexpr*/ int compare(size_type pos1, size_type n1, const char* s) const;
  92. /*constexpr*/ int compare(size_type pos1,
  93. size_type n1,
  94. const char* s,
  95. size_type n2) const;
  96. /*constexpr*/ size_type find(string_view s, size_type pos = 0) const noexcept;
  97. /*constexpr*/ size_type find(char c, size_type pos = 0) const noexcept;
  98. /*constexpr*/ size_type find(const char* s, size_type pos, size_type n) const;
  99. /*constexpr*/ size_type find(const char* s, size_type pos = 0) const;
  100. /*constexpr*/ size_type rfind(string_view s, size_type pos = npos) const
  101. noexcept;
  102. /*constexpr*/ size_type rfind(char c, size_type pos = npos) const noexcept;
  103. /*constexpr*/ size_type rfind(const char* s,
  104. size_type pos,
  105. size_type n) const;
  106. /*constexpr*/ size_type rfind(const char* s, size_type pos = npos) const;
  107. /*constexpr*/ size_type find_first_of(string_view s, size_type pos = 0) const
  108. noexcept;
  109. /*constexpr*/ size_type find_first_of(char c, size_type pos = 0) const
  110. noexcept;
  111. /*constexpr*/ size_type find_first_of(const char* s,
  112. size_type pos,
  113. size_type n) const;
  114. /*constexpr*/ size_type find_first_of(const char* s, size_type pos = 0) const;
  115. /*constexpr*/ size_type find_last_of(string_view s,
  116. size_type pos = npos) const noexcept;
  117. /*constexpr*/ size_type find_last_of(char c, size_type pos = npos) const
  118. noexcept;
  119. /*constexpr*/ size_type find_last_of(const char* s,
  120. size_type pos,
  121. size_type n) const;
  122. /*constexpr*/ size_type find_last_of(const char* s,
  123. size_type pos = npos) const;
  124. // TODO(binji): These are defined by C++17 basic_string_view but not
  125. // implemented here.
  126. #if 0
  127. constexpr size_type find_first_not_of(string_view s, size_type pos = 0) const
  128. noexcept;
  129. constexpr size_type find_first_not_of(char c, size_type pos = 0) const
  130. noexcept;
  131. constexpr size_type find_first_not_of(const char* s,
  132. size_type pos,
  133. size_type n) const;
  134. constexpr size_type find_first_not_of(const char* s, size_type pos = 0) const;
  135. constexpr size_type find_last_not_of(string_view s,
  136. size_type pos = npos) const noexcept;
  137. constexpr size_type find_last_not_of(char c, size_type pos = npos) const
  138. noexcept;
  139. constexpr size_type find_last_not_of(const char* s,
  140. size_type pos,
  141. size_type n) const;
  142. constexpr size_type find_last_not_of(const char* s,
  143. size_type pos = npos) const;
  144. #endif
  145. private:
  146. const char* data_;
  147. size_type size_;
  148. };
  149. // non-member comparison functions
  150. inline bool operator==(string_view x, string_view y) noexcept {
  151. return x.compare(y) == 0;
  152. }
  153. inline bool operator!=(string_view x, string_view y) noexcept {
  154. return x.compare(y) != 0;
  155. }
  156. inline bool operator<(string_view x, string_view y) noexcept {
  157. return x.compare(y) < 0;
  158. }
  159. inline bool operator>(string_view x, string_view y) noexcept {
  160. return x.compare(y) > 0;
  161. }
  162. inline bool operator<=(string_view x, string_view y) noexcept {
  163. return x.compare(y) <= 0;
  164. }
  165. inline bool operator>=(string_view x, string_view y) noexcept {
  166. return x.compare(y) >= 0;
  167. }
  168. inline constexpr string_view::string_view() noexcept
  169. : data_(nullptr), size_(0) {}
  170. inline string_view::string_view(const std::string& str) noexcept
  171. : data_(str.data()), size_(str.size()) {}
  172. inline string_view::string_view(const char* str)
  173. : data_(str), size_(traits_type::length(str)) {}
  174. inline constexpr string_view::string_view(const char* str, size_type len)
  175. : data_(str), size_(len) {}
  176. inline constexpr string_view::const_iterator string_view::begin() const
  177. noexcept {
  178. return data_;
  179. }
  180. inline constexpr string_view::const_iterator string_view::end() const noexcept {
  181. return data_ + size_;
  182. }
  183. inline constexpr string_view::const_iterator string_view::cbegin() const
  184. noexcept {
  185. return data_;
  186. }
  187. inline constexpr string_view::const_iterator string_view::cend() const
  188. noexcept {
  189. return data_ + size_;
  190. }
  191. inline string_view::const_reverse_iterator string_view::rbegin() const
  192. noexcept {
  193. return const_reverse_iterator(end());
  194. }
  195. inline string_view::const_reverse_iterator string_view::rend() const noexcept {
  196. return const_reverse_iterator(begin());
  197. }
  198. inline string_view::const_reverse_iterator string_view::crbegin() const
  199. noexcept {
  200. return const_reverse_iterator(cend());
  201. }
  202. inline string_view::const_reverse_iterator string_view::crend() const noexcept {
  203. return const_reverse_iterator(cbegin());
  204. }
  205. constexpr inline string_view::size_type string_view::size() const noexcept {
  206. return size_;
  207. }
  208. constexpr inline string_view::size_type string_view::length() const noexcept {
  209. return size_;
  210. }
  211. constexpr inline bool string_view::empty() const noexcept {
  212. return size_ == 0;
  213. }
  214. constexpr inline string_view::const_reference string_view::operator[](
  215. size_type pos) const {
  216. return data_[pos];
  217. }
  218. inline string_view::const_reference string_view::at(size_type pos) const {
  219. assert(pos < size_);
  220. return data_[pos];
  221. }
  222. inline string_view::const_reference string_view::front() const {
  223. assert(!empty());
  224. return *data_;
  225. }
  226. inline string_view::const_reference string_view::back() const {
  227. assert(!empty());
  228. return data_[size_ - 1];
  229. }
  230. constexpr inline string_view::const_pointer string_view::data() const noexcept {
  231. return data_;
  232. }
  233. } // namespace wabt
  234. namespace std {
  235. // hash support
  236. template <>
  237. struct hash<::wabt::string_view> {
  238. ::wabt::hash_code operator()(const ::wabt::string_view& sv) {
  239. return ::wabt::HashRange(sv.begin(), sv.end());
  240. }
  241. };
  242. } // namespace std
  243. #endif // WABT_STRING_VIEW_H_