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{
23public:
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]] constexpr bool starts_with(std::string_view sv) const {
61 return view().starts_with(sv);
62 }
63 [[nodiscard]] constexpr bool starts_with(char c) const {
64 return view().starts_with(c);
65 }
66 [[nodiscard]] constexpr bool starts_with(const char* s) const {
67 return view().starts_with(s);
68 }
69 [[nodiscard]] constexpr bool ends_with(std::string_view sv) const {
70 return view().ends_with(sv);
71 }
72 [[nodiscard]] constexpr bool ends_with(char c) const {
73 return view().ends_with(c);
74 }
75 [[nodiscard]] constexpr bool ends_with(const char* s) const {
76 return view().ends_with(s);
77 }
78
79 [[nodiscard]] explicit operator std::string() const {
80 return {dat, siz};
81 }
82 [[nodiscard]] constexpr std::string_view view() const {
83 return {dat, siz};
84 }
85 [[nodiscard]] constexpr /*implicit*/ operator std::string_view() const {
86 return view();
87 }
88
89private:
90 const char* dat;
91 size_type siz;
92};
93
94static_assert(std::is_trivially_destructible_v<zstring_view>);
95static_assert(std::is_trivially_copyable_v<zstring_view>);
96static_assert(std::is_trivially_copy_constructible_v<zstring_view>);
97static_assert(std::is_trivially_move_constructible_v<zstring_view>);
98static_assert(std::is_trivially_assignable_v<zstring_view, zstring_view>);
99static_assert(std::is_trivially_copy_assignable_v<zstring_view>);
100static_assert(std::is_trivially_move_assignable_v<zstring_view>);
101
102[[nodiscard]] constexpr auto begin(const zstring_view& x) { return x.begin(); }
103[[nodiscard]] constexpr auto end (const zstring_view& x) { return x.end(); }
104
105[[nodiscard]] constexpr bool operator==(const zstring_view& x, const zstring_view& y) {
106 return std::string_view(x) == std::string_view(y);
107}
108[[nodiscard]] inline bool operator==(const zstring_view& x, const std::string& y) {
109 return std::string_view(x) == std::string_view(y);
110}
111[[nodiscard]] constexpr bool operator==(const zstring_view& x, const std::string_view& y) {
112 return std::string_view(x) == y;
113}
114[[nodiscard]] constexpr bool operator==(const zstring_view& x, const char* y) {
115 return std::string_view(x) == std::string_view(y);
116}
117
118inline std::ostream& operator<<(std::ostream& os, const zstring_view& str)
119{
120 os << std::string_view(str);
121 return os;
122}
123#endif
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
Definition: zstring_view.hh:22
constexpr bool ends_with(char c) const
Definition: zstring_view.hh:72
constexpr bool starts_with(char c) const
Definition: zstring_view.hh:63
constexpr auto begin() const
Definition: zstring_view.hh:36
constexpr bool starts_with(const char *s) const
Definition: zstring_view.hh:66
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
constexpr bool ends_with(std::string_view sv) const
Definition: zstring_view.hh:69
zstring_view(const char *s)
Definition: zstring_view.hh:29
constexpr auto end() const
Definition: zstring_view.hh:37
constexpr bool ends_with(const char *s) const
Definition: zstring_view.hh:75
static constexpr auto npos
Definition: zstring_view.hh:27
constexpr const char * c_str() const
Definition: zstring_view.hh:49
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 bool starts_with(std::string_view sv) const
Definition: zstring_view.hh:60
constexpr auto empty() const
Definition: zstring_view.hh:40
constexpr std::string_view view() const
Definition: zstring_view.hh:82
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:127
constexpr bool operator==(const zstring_view &x, const zstring_view &y)
std::ostream & operator<<(std::ostream &os, const zstring_view &str)
constexpr auto begin(const zstring_view &x)
constexpr auto end(const zstring_view &x)