openMSX
zstring_view.hh
Go to the documentation of this file.
1 #ifndef ZSTRING_VIEW_HH
2 #define ZSTRING_VIEW_HH
3 
4 #include <cassert>
5 #include <cstring>
6 #include <string>
7 #include <string_view>
8 #include <type_traits>
9 
21 {
22 public:
23  using size_type = size_t;
24  using const_iterator = const char*;
25 
26  static constexpr auto npos = std::string_view::npos;
27 
28  /*constexpr*/ zstring_view(const char* s)
29  : dat(s), siz(size_type(strlen(s))) {}
30  constexpr zstring_view(const char* s, size_t n)
31  : dat(s), siz(n) { assert(s[n] == '\0'); }
32  /*constexpr*/ zstring_view(const std::string& s)
33  : dat(s.c_str()), siz(s.size()) {}
34 
35  [[nodiscard]] constexpr auto begin() const { return dat; }
36  [[nodiscard]] constexpr auto end() const { return dat + siz; }
37 
38  [[nodiscard]] constexpr auto size() const { return siz; }
39  [[nodiscard]] constexpr auto empty() const { return siz == 0; }
40 
41  [[nodiscard]] constexpr char operator[](size_type i) const {
42  assert(i < siz);
43  return dat[i];
44  }
45  [[nodiscard]] constexpr char front() const { return *dat; }
46  [[nodiscard]] constexpr char back() const { return *(dat + siz - 1); }
47  [[nodiscard]] constexpr const char* data() const { return dat; }
48  [[nodiscard]] constexpr const char* c_str() const { return dat; }
49 
50  [[nodiscard]] constexpr auto find(char c, size_type pos = 0) const {
51  return view().find(c, pos);
52  }
53 
54  [[nodiscard]] constexpr zstring_view substr(size_type pos) const {
55  assert(pos <= siz);
56  return zstring_view(dat + pos, siz - pos);
57  }
58 
59  [[nodiscard]] explicit operator std::string() const {
60  return {dat, siz};
61  }
62  [[nodiscard]] constexpr std::string_view view() const {
63  return {dat, siz};
64  }
65  [[nodiscard]] constexpr /*implicit*/ operator std::string_view() const {
66  return view();
67  }
68 
69 private:
70  const char* dat;
71  size_type siz;
72 };
73 
74 static_assert(std::is_trivially_destructible_v<zstring_view>);
75 static_assert(std::is_trivially_copyable_v<zstring_view>);
76 static_assert(std::is_trivially_copy_constructible_v<zstring_view>);
77 static_assert(std::is_trivially_move_constructible_v<zstring_view>);
78 static_assert(std::is_trivially_assignable_v<zstring_view, zstring_view>);
79 static_assert(std::is_trivially_copy_assignable_v<zstring_view>);
80 static_assert(std::is_trivially_move_assignable_v<zstring_view>);
81 
82 [[nodiscard]] constexpr auto begin(const zstring_view& x) { return x.begin(); }
83 [[nodiscard]] constexpr auto end (const zstring_view& x) { return x.end(); }
84 
85 [[nodiscard]] constexpr bool operator==(const zstring_view& x, const zstring_view& y) {
86  return std::string_view(x) == std::string_view(y);
87 }
88 [[nodiscard]] inline bool operator==(const zstring_view& x, const std::string& y) {
89  return std::string_view(x) == std::string_view(y);
90 }
91 [[nodiscard]] inline bool operator==(const std::string& x, const zstring_view& y) {
92  return std::string_view(x) == std::string_view(y);
93 }
94 [[nodiscard]] constexpr bool operator==(const zstring_view& x, const std::string_view& y) {
95  return std::string_view(x) == y;
96 }
97 [[nodiscard]] constexpr bool operator==(const std::string_view& x, const zstring_view& y) {
98  return x == std::string_view(y);
99 }
100 [[nodiscard]] constexpr bool operator==(const zstring_view& x, const char* y) {
101  return std::string_view(x) == std::string_view(y);
102 }
103 [[nodiscard]] constexpr bool operator==(const char* x, const zstring_view& y) {
104  return std::string_view(x) == std::string_view(y);
105 }
106 [[nodiscard]] constexpr bool operator!=(const zstring_view& x, const zstring_view& y) {
107  return std::string_view(x) != std::string_view(y);
108 }
109 [[nodiscard]] inline bool operator!=(const zstring_view& x, const std::string& y) {
110  return std::string_view(x) != std::string_view(y);
111 }
112 [[nodiscard]] inline bool operator!=(const std::string& x, const zstring_view& y) {
113  return std::string_view(x) != std::string_view(y);
114 }
115 [[nodiscard]] constexpr bool operator!=(const zstring_view& x, const std::string_view& y) {
116  return std::string_view(x) != y;
117 }
118 [[nodiscard]] constexpr bool operator!=(const std::string_view& x, const zstring_view& y) {
119  return x != std::string_view(y);
120 }
121 [[nodiscard]] constexpr bool operator!=(const zstring_view& x, const char* y) {
122  return std::string_view(x) != std::string_view(y);
123 }
124 [[nodiscard]] constexpr bool operator!=(const char* x, const zstring_view& y) {
125  return std::string_view(x) != std::string_view(y);
126 }
127 
128 #endif
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
Definition: zstring_view.hh:21
constexpr auto begin() const
Definition: zstring_view.hh:35
constexpr const char * c_str() const
Definition: zstring_view.hh:48
constexpr auto size() const
Definition: zstring_view.hh:38
constexpr char back() const
Definition: zstring_view.hh:46
zstring_view(const std::string &s)
Definition: zstring_view.hh:32
size_t size_type
Definition: zstring_view.hh:23
constexpr zstring_view(const char *s, size_t n)
Definition: zstring_view.hh:30
constexpr const char * data() const
Definition: zstring_view.hh:47
constexpr char operator[](size_type i) const
Definition: zstring_view.hh:41
const char * const_iterator
Definition: zstring_view.hh:24
zstring_view(const char *s)
Definition: zstring_view.hh:28
constexpr auto end() const
Definition: zstring_view.hh:36
static constexpr auto npos
Definition: zstring_view.hh:26
constexpr char front() const
Definition: zstring_view.hh:45
constexpr auto find(char c, size_type pos=0) const
Definition: zstring_view.hh:50
constexpr zstring_view substr(size_type pos) const
Definition: zstring_view.hh:54
constexpr auto empty() const
Definition: zstring_view.hh:39
constexpr std::string_view view() const
Definition: zstring_view.hh:62
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:124
constexpr bool operator==(const zstring_view &x, const zstring_view &y)
Definition: zstring_view.hh:85
constexpr bool operator!=(const zstring_view &x, const zstring_view &y)
constexpr auto begin(const zstring_view &x)
Definition: zstring_view.hh:82
constexpr auto end(const zstring_view &x)
Definition: zstring_view.hh:83