string_span 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #ifndef GSL_STRING_SPAN_H
  17. #define GSL_STRING_SPAN_H
  18. #include <gsl/gsl_assert> // for Ensures, Expects
  19. #include <gsl/gsl_util> // for narrow_cast
  20. #include <gsl/span_ext> // for operator!=, operator==, dynamic_extent
  21. #include <algorithm> // for equal, lexicographical_compare
  22. #include <array> // for array
  23. #include <cstddef> // for size_t, nullptr_t
  24. #include <cstdint> // for PTRDIFF_MAX
  25. #include <cstring>
  26. #include <string> // for basic_string, allocator, char_traits
  27. #include <type_traits> // for declval, is_convertible, enable_if_t, add_...
  28. #if defined(_MSC_VER) && !defined(__clang__)
  29. #pragma warning(push)
  30. // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
  31. #pragma warning(disable : 26446) // TODO: bug in parser - attributes and templates
  32. #pragma warning(disable : 26481) // TODO: suppress does not work inside templates sometimes
  33. #endif // _MSC_VER
  34. namespace gsl
  35. {
  36. //
  37. // czstring and wzstring
  38. //
  39. // These are "tag" typedefs for C-style strings (i.e. null-terminated character arrays)
  40. // that allow static analysis to help find bugs.
  41. //
  42. // There are no additional features/semantics that we can find a way to add inside the
  43. // type system for these types that will not either incur significant runtime costs or
  44. // (sometimes needlessly) break existing programs when introduced.
  45. //
  46. template <typename CharT, std::size_t Extent = dynamic_extent>
  47. using basic_zstring = CharT*;
  48. template <std::size_t Extent = dynamic_extent>
  49. using czstring = basic_zstring<const char, Extent>;
  50. template <std::size_t Extent = dynamic_extent>
  51. using cwzstring = basic_zstring<const wchar_t, Extent>;
  52. template <std::size_t Extent = dynamic_extent>
  53. using cu16zstring = basic_zstring<const char16_t, Extent>;
  54. template <std::size_t Extent = dynamic_extent>
  55. using cu32zstring = basic_zstring<const char32_t, Extent>;
  56. template <std::size_t Extent = dynamic_extent>
  57. using zstring = basic_zstring<char, Extent>;
  58. template <std::size_t Extent = dynamic_extent>
  59. using wzstring = basic_zstring<wchar_t, Extent>;
  60. template <std::size_t Extent = dynamic_extent>
  61. using u16zstring = basic_zstring<char16_t, Extent>;
  62. template <std::size_t Extent = dynamic_extent>
  63. using u32zstring = basic_zstring<char32_t, Extent>;
  64. namespace details
  65. {
  66. template <class CharT>
  67. std::size_t string_length(const CharT* str, std::size_t n)
  68. {
  69. if (str == nullptr || n == dynamic_extent) return 0;
  70. const span<const CharT> str_span{str, n};
  71. std::size_t len = 0;
  72. while (len < n && str_span[len]) len++;
  73. return len;
  74. }
  75. } // namespace details
  76. //
  77. // ensure_sentinel()
  78. //
  79. // Provides a way to obtain an span from a contiguous sequence
  80. // that ends with a (non-inclusive) sentinel value.
  81. //
  82. // Will fail-fast if sentinel cannot be found before max elements are examined.
  83. //
  84. template <typename T, const T Sentinel>
  85. span<T, dynamic_extent> ensure_sentinel(T* seq,
  86. std::size_t max = static_cast<std::size_t>(-1))
  87. {
  88. Ensures(seq != nullptr);
  89. GSL_SUPPRESS(
  90. f.23) // NO-FORMAT: attribute // TODO: false positive // TODO: suppress does not work
  91. auto cur = seq;
  92. Ensures(cur != nullptr); // workaround for removing the warning
  93. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute // TODO: suppress does not work
  94. while (static_cast<std::size_t>(cur - seq) < max && *cur != Sentinel) ++cur;
  95. Ensures(*cur == Sentinel);
  96. return {seq, static_cast<std::size_t>(cur - seq)};
  97. }
  98. //
  99. // ensure_z - creates a span for a zero terminated strings.
  100. // Will fail fast if a null-terminator cannot be found before
  101. // the limit of size_type.
  102. //
  103. template <typename CharT>
  104. span<CharT, dynamic_extent> ensure_z(CharT* const& sz,
  105. std::size_t max = static_cast<std::size_t>(-1))
  106. {
  107. return ensure_sentinel<CharT, CharT(0)>(sz, max);
  108. }
  109. template <typename CharT, std::size_t N>
  110. span<CharT, dynamic_extent> ensure_z(CharT (&sz)[N])
  111. {
  112. return ensure_z(&sz[0], N);
  113. }
  114. template <class Cont>
  115. span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_extent>
  116. ensure_z(Cont& cont)
  117. {
  118. return ensure_z(cont.data(), cont.size());
  119. }
  120. template <typename CharT, std::size_t>
  121. class basic_string_span;
  122. namespace details
  123. {
  124. template <typename T>
  125. struct is_basic_string_span_oracle : std::false_type
  126. {
  127. };
  128. template <typename CharT, std::size_t Extent>
  129. struct is_basic_string_span_oracle<basic_string_span<CharT, Extent>> : std::true_type
  130. {
  131. };
  132. template <typename T>
  133. struct is_basic_string_span : is_basic_string_span_oracle<std::remove_cv_t<T>>
  134. {
  135. };
  136. } // namespace details
  137. //
  138. // string_span and relatives
  139. //
  140. template <typename CharT, std::size_t Extent = dynamic_extent>
  141. class basic_string_span
  142. {
  143. public:
  144. using element_type = CharT;
  145. using value_type = std::remove_cv_t<element_type>;
  146. using pointer = std::add_pointer_t<element_type>;
  147. using reference = std::add_lvalue_reference_t<element_type>;
  148. using const_reference = std::add_lvalue_reference_t<std::add_const_t<element_type>>;
  149. using impl_type = span<element_type, Extent>;
  150. using size_type = typename impl_type::size_type;
  151. using iterator = typename impl_type::iterator;
  152. using reverse_iterator = typename impl_type::reverse_iterator;
  153. // default (empty)
  154. constexpr basic_string_span() noexcept = default;
  155. // copy
  156. constexpr basic_string_span(const basic_string_span& other) noexcept = default;
  157. // assign
  158. constexpr basic_string_span& operator=(const basic_string_span& other) noexcept = default;
  159. constexpr basic_string_span(pointer ptr, size_type length) : span_(ptr, length) {}
  160. constexpr basic_string_span(pointer firstElem, pointer lastElem) : span_(firstElem, lastElem) {}
  161. // From static arrays - if 0-terminated, remove 0 from the view
  162. // All other containers allow 0s within the length, so we do not remove them
  163. template <std::size_t N>
  164. constexpr basic_string_span(element_type (&arr)[N]) : span_(remove_z(arr))
  165. {}
  166. template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
  167. constexpr basic_string_span(std::array<ArrayElementType, N>& arr) noexcept : span_(arr)
  168. {}
  169. template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
  170. constexpr basic_string_span(const std::array<ArrayElementType, N>& arr) noexcept : span_(arr)
  171. {}
  172. // Container signature should work for basic_string after C++17 version exists
  173. template <class Traits, class Allocator>
  174. // GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute // TODO: parser bug
  175. constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator>& str)
  176. : span_(&str[0], str.length())
  177. {}
  178. template <class Traits, class Allocator>
  179. constexpr basic_string_span(const std::basic_string<element_type, Traits, Allocator>& str)
  180. : span_(&str[0], str.length())
  181. {}
  182. // from containers. Containers must have a pointer type and data() function signatures
  183. template <class Container,
  184. class = std::enable_if_t<
  185. !details::is_basic_string_span<Container>::value &&
  186. std::is_convertible<typename Container::pointer, pointer>::value &&
  187. std::is_convertible<typename Container::pointer,
  188. decltype(std::declval<Container>().data())>::value>>
  189. constexpr basic_string_span(Container& cont) : span_(cont)
  190. {}
  191. template <class Container,
  192. class = std::enable_if_t<
  193. !details::is_basic_string_span<Container>::value &&
  194. std::is_convertible<typename Container::pointer, pointer>::value &&
  195. std::is_convertible<typename Container::pointer,
  196. decltype(std::declval<Container>().data())>::value>>
  197. constexpr basic_string_span(const Container& cont) : span_(cont)
  198. {}
  199. // from string_span
  200. template <
  201. class OtherValueType, std::size_t OtherExtent,
  202. class = std::enable_if_t<std::is_convertible<
  203. typename basic_string_span<OtherValueType, OtherExtent>::impl_type, impl_type>::value>>
  204. constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other)
  205. : span_(other.data(), other.length())
  206. {}
  207. template <size_type Count>
  208. constexpr basic_string_span<element_type, Count> first() const
  209. {
  210. return {span_.template first<Count>()};
  211. }
  212. constexpr basic_string_span<element_type, dynamic_extent> first(size_type count) const
  213. {
  214. return {span_.first(count)};
  215. }
  216. template <size_type Count>
  217. constexpr basic_string_span<element_type, Count> last() const
  218. {
  219. return {span_.template last<Count>()};
  220. }
  221. constexpr basic_string_span<element_type, dynamic_extent> last(size_type count) const
  222. {
  223. return {span_.last(count)};
  224. }
  225. template <size_type Offset, size_type Count>
  226. constexpr basic_string_span<element_type, Count> subspan() const
  227. {
  228. return {span_.template subspan<Offset, Count>()};
  229. }
  230. constexpr basic_string_span<element_type, dynamic_extent>
  231. subspan(size_type offset, size_type count = dynamic_extent) const
  232. {
  233. return {span_.subspan(offset, count)};
  234. }
  235. constexpr reference operator[](size_type idx) const { return span_[idx]; }
  236. constexpr reference operator()(size_type idx) const { return span_[idx]; }
  237. constexpr pointer data() const { return span_.data(); }
  238. constexpr size_type length() const noexcept { return span_.size(); }
  239. constexpr size_type size() const noexcept { return span_.size(); }
  240. constexpr size_type size_bytes() const noexcept { return span_.size_bytes(); }
  241. constexpr size_type length_bytes() const noexcept { return span_.length_bytes(); }
  242. constexpr bool empty() const noexcept { return size() == 0; }
  243. constexpr iterator begin() const noexcept { return span_.begin(); }
  244. constexpr iterator end() const noexcept { return span_.end(); }
  245. constexpr reverse_iterator rbegin() const noexcept { return span_.rbegin(); }
  246. constexpr reverse_iterator rend() const noexcept { return span_.rend(); }
  247. private:
  248. static impl_type remove_z(pointer const& sz, std::size_t max)
  249. {
  250. return impl_type(sz, details::string_length(sz, max));
  251. }
  252. template <std::size_t N>
  253. static impl_type remove_z(element_type (&sz)[N])
  254. {
  255. return remove_z(&sz[0], N);
  256. }
  257. impl_type span_;
  258. };
  259. template <std::size_t Extent = dynamic_extent>
  260. using string_span = basic_string_span<char, Extent>;
  261. template <std::size_t Extent = dynamic_extent>
  262. using cstring_span = basic_string_span<const char, Extent>;
  263. template <std::size_t Extent = dynamic_extent>
  264. using wstring_span = basic_string_span<wchar_t, Extent>;
  265. template <std::size_t Extent = dynamic_extent>
  266. using cwstring_span = basic_string_span<const wchar_t, Extent>;
  267. template <std::size_t Extent = dynamic_extent>
  268. using u16string_span = basic_string_span<char16_t, Extent>;
  269. template <std::size_t Extent = dynamic_extent>
  270. using cu16string_span = basic_string_span<const char16_t, Extent>;
  271. template <std::size_t Extent = dynamic_extent>
  272. using u32string_span = basic_string_span<char32_t, Extent>;
  273. template <std::size_t Extent = dynamic_extent>
  274. using cu32string_span = basic_string_span<const char32_t, Extent>;
  275. //
  276. // to_string() allow (explicit) conversions from string_span to string
  277. //
  278. template <typename CharT, std::size_t Extent>
  279. std::basic_string<typename std::remove_const<CharT>::type>
  280. to_string(basic_string_span<CharT, Extent> view)
  281. {
  282. return {view.data(), narrow_cast<std::size_t>(view.length())};
  283. }
  284. template <typename CharT, typename Traits = typename std::char_traits<CharT>,
  285. typename Allocator = std::allocator<CharT>, typename gCharT, std::size_t Extent>
  286. std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view)
  287. {
  288. return {view.data(), narrow_cast<std::size_t>(view.length())};
  289. }
  290. template <class ElementType, std::size_t Extent>
  291. basic_string_span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
  292. as_bytes(basic_string_span<ElementType, Extent> s) noexcept
  293. {
  294. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  295. return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
  296. }
  297. template <class ElementType, std::size_t Extent,
  298. class = std::enable_if_t<!std::is_const<ElementType>::value>>
  299. basic_string_span<byte, details::calculate_byte_size<ElementType, Extent>::value>
  300. as_writable_bytes(basic_string_span<ElementType, Extent> s) noexcept
  301. {
  302. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  303. return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
  304. }
  305. // zero-terminated string span, used to convert
  306. // zero-terminated spans to legacy strings
  307. template <typename CharT, std::size_t Extent = dynamic_extent>
  308. class basic_zstring_span
  309. {
  310. public:
  311. using value_type = CharT;
  312. using const_value_type = std::add_const_t<CharT>;
  313. using pointer = std::add_pointer_t<value_type>;
  314. using const_pointer = std::add_pointer_t<const_value_type>;
  315. using zstring_type = basic_zstring<value_type, Extent>;
  316. using const_zstring_type = basic_zstring<const_value_type, Extent>;
  317. using impl_type = span<value_type, Extent>;
  318. using string_span_type = basic_string_span<value_type, Extent>;
  319. constexpr basic_zstring_span(impl_type s) : span_(s)
  320. {
  321. // expects a zero-terminated span
  322. Expects(s[s.size() - 1] == '\0');
  323. }
  324. // copy
  325. constexpr basic_zstring_span(const basic_zstring_span& other) = default;
  326. // move
  327. constexpr basic_zstring_span(basic_zstring_span&& other) = default;
  328. // assign
  329. constexpr basic_zstring_span& operator=(const basic_zstring_span& other) = default;
  330. // move assign
  331. constexpr basic_zstring_span& operator=(basic_zstring_span&& other) = default;
  332. constexpr bool empty() const noexcept { return span_.size() == 0; }
  333. constexpr string_span_type as_string_span() const noexcept
  334. {
  335. const auto sz = span_.size();
  336. return {span_.data(), sz > 1 ? sz - 1 : 0};
  337. }
  338. constexpr string_span_type ensure_z() const { return gsl::ensure_z(span_); }
  339. constexpr const_zstring_type assume_z() const noexcept { return span_.data(); }
  340. private:
  341. impl_type span_;
  342. };
  343. template <std::size_t Max = dynamic_extent>
  344. using zstring_span = basic_zstring_span<char, Max>;
  345. template <std::size_t Max = dynamic_extent>
  346. using wzstring_span = basic_zstring_span<wchar_t, Max>;
  347. template <std::size_t Max = dynamic_extent>
  348. using u16zstring_span = basic_zstring_span<char16_t, Max>;
  349. template <std::size_t Max = dynamic_extent>
  350. using u32zstring_span = basic_zstring_span<char32_t, Max>;
  351. template <std::size_t Max = dynamic_extent>
  352. using czstring_span = basic_zstring_span<const char, Max>;
  353. template <std::size_t Max = dynamic_extent>
  354. using cwzstring_span = basic_zstring_span<const wchar_t, Max>;
  355. template <std::size_t Max = dynamic_extent>
  356. using cu16zstring_span = basic_zstring_span<const char16_t, Max>;
  357. template <std::size_t Max = dynamic_extent>
  358. using cu32zstring_span = basic_zstring_span<const char32_t, Max>;
  359. // operator ==
  360. template <class CharT, std::size_t Extent, class T,
  361. class = std::enable_if_t<
  362. details::is_basic_string_span<T>::value ||
  363. std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
  364. bool operator==(const gsl::basic_string_span<CharT, Extent>& one, const T& other)
  365. {
  366. const gsl::basic_string_span<std::add_const_t<CharT>> tmp(other);
  367. return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end());
  368. }
  369. template <class CharT, std::size_t Extent, class T,
  370. class = std::enable_if_t<
  371. !details::is_basic_string_span<T>::value &&
  372. std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
  373. bool operator==(const T& one, const gsl::basic_string_span<CharT, Extent>& other)
  374. {
  375. const gsl::basic_string_span<std::add_const_t<CharT>> tmp(one);
  376. return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end());
  377. }
  378. // operator !=
  379. template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  380. typename = std::enable_if_t<std::is_convertible<
  381. T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
  382. bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other)
  383. {
  384. return !(one == other);
  385. }
  386. template <
  387. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  388. typename = std::enable_if_t<
  389. std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
  390. !gsl::details::is_basic_string_span<T>::value>>
  391. bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other)
  392. {
  393. return !(one == other);
  394. }
  395. // operator<
  396. template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  397. typename = std::enable_if_t<std::is_convertible<
  398. T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
  399. bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other)
  400. {
  401. const gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
  402. return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
  403. }
  404. template <
  405. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  406. typename = std::enable_if_t<
  407. std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
  408. !gsl::details::is_basic_string_span<T>::value>>
  409. bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other)
  410. {
  411. gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
  412. return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
  413. }
  414. #ifndef _MSC_VER
  415. // VS treats temp and const containers as convertible to basic_string_span,
  416. // so the cases below are already covered by the previous operators
  417. template <
  418. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  419. typename DataType = typename T::value_type,
  420. typename = std::enable_if_t<
  421. !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
  422. std::is_convertible<DataType*, CharT*>::value &&
  423. std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
  424. DataType>::value>>
  425. bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other)
  426. {
  427. gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
  428. return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
  429. }
  430. template <
  431. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  432. typename DataType = typename T::value_type,
  433. typename = std::enable_if_t<
  434. !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
  435. std::is_convertible<DataType*, CharT*>::value &&
  436. std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
  437. DataType>::value>>
  438. bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other)
  439. {
  440. gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
  441. return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
  442. }
  443. #endif
  444. // operator <=
  445. template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  446. typename = std::enable_if_t<std::is_convertible<
  447. T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
  448. bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other)
  449. {
  450. return !(other < one);
  451. }
  452. template <
  453. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  454. typename = std::enable_if_t<
  455. std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
  456. !gsl::details::is_basic_string_span<T>::value>>
  457. bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other)
  458. {
  459. return !(other < one);
  460. }
  461. #ifndef _MSC_VER
  462. // VS treats temp and const containers as convertible to basic_string_span,
  463. // so the cases below are already covered by the previous operators
  464. template <
  465. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  466. typename DataType = typename T::value_type,
  467. typename = std::enable_if_t<
  468. !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
  469. std::is_convertible<DataType*, CharT*>::value &&
  470. std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
  471. DataType>::value>>
  472. bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other)
  473. {
  474. return !(other < one);
  475. }
  476. template <
  477. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  478. typename DataType = typename T::value_type,
  479. typename = std::enable_if_t<
  480. !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
  481. std::is_convertible<DataType*, CharT*>::value &&
  482. std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
  483. DataType>::value>>
  484. bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other)
  485. {
  486. return !(other < one);
  487. }
  488. #endif
  489. // operator>
  490. template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  491. typename = std::enable_if_t<std::is_convertible<
  492. T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
  493. bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other)
  494. {
  495. return other < one;
  496. }
  497. template <
  498. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  499. typename = std::enable_if_t<
  500. std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
  501. !gsl::details::is_basic_string_span<T>::value>>
  502. bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other)
  503. {
  504. return other < one;
  505. }
  506. #ifndef _MSC_VER
  507. // VS treats temp and const containers as convertible to basic_string_span,
  508. // so the cases below are already covered by the previous operators
  509. template <
  510. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  511. typename DataType = typename T::value_type,
  512. typename = std::enable_if_t<
  513. !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
  514. std::is_convertible<DataType*, CharT*>::value &&
  515. std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
  516. DataType>::value>>
  517. bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other)
  518. {
  519. return other < one;
  520. }
  521. template <
  522. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  523. typename DataType = typename T::value_type,
  524. typename = std::enable_if_t<
  525. !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
  526. std::is_convertible<DataType*, CharT*>::value &&
  527. std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
  528. DataType>::value>>
  529. bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other)
  530. {
  531. return other < one;
  532. }
  533. #endif
  534. // operator >=
  535. template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  536. typename = std::enable_if_t<std::is_convertible<
  537. T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
  538. bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other)
  539. {
  540. return !(one < other);
  541. }
  542. template <
  543. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  544. typename = std::enable_if_t<
  545. std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
  546. !gsl::details::is_basic_string_span<T>::value>>
  547. bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other)
  548. {
  549. return !(one < other);
  550. }
  551. #ifndef _MSC_VER
  552. // VS treats temp and const containers as convertible to basic_string_span,
  553. // so the cases below are already covered by the previous operators
  554. template <
  555. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  556. typename DataType = typename T::value_type,
  557. typename = std::enable_if_t<
  558. !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
  559. std::is_convertible<DataType*, CharT*>::value &&
  560. std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
  561. DataType>::value>>
  562. bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other)
  563. {
  564. return !(one < other);
  565. }
  566. template <
  567. typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
  568. typename DataType = typename T::value_type,
  569. typename = std::enable_if_t<
  570. !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
  571. std::is_convertible<DataType*, CharT*>::value &&
  572. std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
  573. DataType>::value>>
  574. bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other)
  575. {
  576. return !(one < other);
  577. }
  578. #endif
  579. } // namespace gsl
  580. #if defined(_MSC_VER) && !defined(__clang__)
  581. #pragma warning(pop)
  582. #endif // _MSC_VER
  583. #endif // GSL_STRING_SPAN_H