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