string_view_constexpr_test1.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright (c) Marshall Clow 2017-2017.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. For more information, see http://www.boost.org
  6. */
  7. #include <new> // for placement new
  8. #include <iostream>
  9. #include <cstddef> // for NULL, std::size_t, std::ptrdiff_t
  10. #include <cstring> // for std::strchr and std::strcmp
  11. #include <cstdlib> // for std::malloc and std::free
  12. #include <boost/config.hpp>
  13. #include <boost/utility/string_view.hpp>
  14. #if __cplusplus >= 201402L
  15. struct constexpr_char_traits
  16. {
  17. typedef char char_type;
  18. typedef int int_type;
  19. typedef std::streamoff off_type;
  20. typedef std::streampos pos_type;
  21. typedef std::mbstate_t state_type;
  22. static void assign(char_type& c1, const char_type& c2) noexcept { c1 = c2; }
  23. static constexpr bool eq(char_type c1, char_type c2) noexcept { return c1 == c2; }
  24. static constexpr bool lt(char_type c1, char_type c2) noexcept { return c1 < c2; }
  25. static constexpr int compare(const char_type* s1, const char_type* s2, size_t n) noexcept;
  26. static constexpr size_t length(const char_type* s) noexcept;
  27. static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a) noexcept;
  28. static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n) noexcept;
  29. static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n) noexcept;
  30. static constexpr char_type* assign(char_type* s, size_t n, char_type a) noexcept;
  31. static constexpr int_type not_eof(int_type c) noexcept { return eq_int_type(c, eof()) ? ~eof() : c; }
  32. static constexpr char_type to_char_type(int_type c) noexcept { return char_type(c); }
  33. static constexpr int_type to_int_type(char_type c) noexcept { return int_type(c); }
  34. static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept { return c1 == c2; }
  35. static constexpr int_type eof() noexcept { return EOF; }
  36. };
  37. // yields:
  38. // 0 if for each i in [0,n), X::eq(s1[i],s2[i]) is true;
  39. // else, a negative value if, for some j in [0,n), X::lt(s1[j],s2[j]) is true and
  40. // for each i in [0,j) X::eq(s2[i],s2[i]) is true;
  41. // else a positive value.
  42. constexpr int constexpr_char_traits::compare(const char_type* s1, const char_type* s2, size_t n) noexcept
  43. {
  44. for (; n != 0; --n, ++s1, ++s2)
  45. {
  46. if (lt(*s1, *s2))
  47. return -1;
  48. if (lt(*s2, *s1))
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. // yields: the smallest i such that X::eq(s[i],charT()) is true.
  54. constexpr size_t constexpr_char_traits::length(const char_type* s) noexcept
  55. {
  56. size_t len = 0;
  57. for (; !eq(*s, char_type(0)); ++s)
  58. ++len;
  59. return len;
  60. }
  61. typedef boost::basic_string_view<char, constexpr_char_traits> string_view;
  62. int main()
  63. {
  64. constexpr string_view sv1;
  65. constexpr string_view sv2{"abc", 3}; // ptr, len
  66. constexpr string_view sv3{"def"}; // ptr
  67. constexpr const char *s1 = "";
  68. constexpr const char *s2 = "abc";
  69. static_assert( (sv1 == sv1), "" );
  70. static_assert(!(sv1 == sv2), "" );
  71. static_assert( (sv1 != sv2), "" );
  72. static_assert( (sv1 < sv2), "" );
  73. static_assert( (sv1 <= sv2), "" );
  74. static_assert(!(sv1 > sv2), "" );
  75. static_assert(!(sv1 >= sv2), "" );
  76. static_assert(!(s1 == sv2), "" );
  77. static_assert( (s1 != sv2), "" );
  78. static_assert( (s1 < sv2), "" );
  79. static_assert( (s1 <= sv2), "" );
  80. static_assert(!(s1 > sv2), "" );
  81. static_assert(!(s1 >= sv2), "" );
  82. static_assert(!(sv1 == s2), "" );
  83. static_assert( (sv1 != s2), "" );
  84. static_assert( (sv1 < s2), "" );
  85. static_assert( (sv1 <= s2), "" );
  86. static_assert(!(sv1 > s2), "" );
  87. static_assert(!(sv1 >= s2), "" );
  88. static_assert( sv1.compare(sv2) < 0, "" );
  89. static_assert( sv1.compare(sv1) == 0, "" );
  90. static_assert( sv3.compare(sv1) > 0, "" );
  91. static_assert( sv1.compare(s2) < 0, "" );
  92. static_assert( sv1.compare(s1) == 0, "" );
  93. static_assert( sv3.compare(s1) > 0, "" );
  94. }
  95. #endif