openMSX
string_view.hh
Go to the documentation of this file.
1 #ifndef STRING_VIEW_HH
2 #define STRING_VIEW_HH
3 
4 #include <cassert>
5 #include <cstring>
6 #include <iosfwd>
7 #include <iterator>
8 #include <string>
9 #include <type_traits>
10 
17 {
18 public:
19  using size_type = size_t;
20  using difference_type = std::ptrdiff_t;
21  using const_iterator = const char*;
22  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
23 
24  static const size_type npos = size_type(-1);
25 
26  // construct/copy/assign
27  string_view() = default;
28  /*implicit*/ string_view(const char* s)
29  : dat(s), siz(s ? size_type(strlen(s)) : 0) {}
30  string_view(const char* s, size_type len)
31  : dat(s), siz(len) { if (!dat) assert(siz == 0); }
32  string_view(const char* first, const char* last)
33  : dat(first), siz(last - first) { if (!dat) assert(siz == 0); }
34  /*implicit*/ string_view(const std::string& s)
35  : dat(s.data()), siz(s.size()) {}
36 
37  // iterators
38  auto begin() const { return dat; }
39  auto end() const { return dat + siz; }
40  auto rbegin() const { return const_reverse_iterator(end()); }
41  auto rend() const { return const_reverse_iterator(begin()); }
42 
43  // capacity
44  size_type size() const { return siz; }
45  bool empty() const { return siz == 0; }
46  //size_type max_size() const;
47  //size_type length() const;
48 
49  // element access
50  char operator[](size_type i) const {
51  assert(i < siz);
52  return dat[i];
53  }
54  //const char& at(size_type i) const;
55  char front() const { return *dat; }
56  char back() const { return *(dat + siz - 1); }
57  const char* data() const { return dat; }
58 
59  // Outgoing conversion operators
60  //explicit operator std::string() const; // c++11
61  std::string str() const;
62 
63  // mutators
64  void clear() { siz = 0; } // no need to change 'dat'
66  assert(n <= siz);
67  dat += n;
68  siz -= n;
69  }
71  assert(n <= siz);
72  siz -= n;
73  }
74  void pop_back() { remove_suffix(1); }
75  void pop_front() { remove_prefix(1); }
76 
77  // string operations with the same semantics as std::string
78  int compare(string_view x) const;
79  string_view substr(size_type pos, size_type n = npos) const;
80  //size_type copy(char* buf) const;
81  size_type find(string_view s) const;
82  size_type find(char c) const;
83  size_type rfind(string_view s) const;
84  size_type rfind(char c) const;
86  size_type find_first_of(char c) const;
87  //size_type find_first_not_of(string_view s) const;
88  //size_type find_first_not_of(char c) const;
90  size_type find_last_of(char c) const;
91  //size_type find_last_not_of(string_view s) const;
92  //size_type find_last_not_of(char c) const;
93 
94  // c++20
95  bool starts_with(string_view x) const;
96  bool starts_with(char x) const;
97  bool ends_with(string_view x) const;
98  bool ends_with(char x) const;
99 
100 private:
101  const char* dat = nullptr;
102  size_type siz = 0;
103 };
104 
105 static_assert(std::is_trivially_destructible<string_view>::value, "");
106 static_assert(std::is_trivially_copyable<string_view>::value, "");
107 static_assert(std::is_trivially_copy_constructible<string_view>::value, "");
108 static_assert(std::is_trivially_move_constructible<string_view>::value, "");
109 static_assert(std::is_trivially_assignable<string_view, string_view>::value, "");
110 static_assert(std::is_trivially_copy_assignable<string_view>::value, "");
111 static_assert(std::is_trivially_move_assignable<string_view>::value, "");
112 
113 // Comparison operators
114 inline bool operator==(string_view x, string_view y) {
115  return (x.size() == y.size()) &&
116  (memcmp(x.data(), y.data(), x.size()) == 0);
117 }
118 
120 inline bool operator!=(string_view x, string_view y) { return !(x == y); }
121 inline bool operator> (string_view x, string_view y) { return (y < x); }
122 inline bool operator<=(string_view x, string_view y) { return !(y < x); }
123 inline bool operator>=(string_view x, string_view y) { return !(x < y); }
124 
125 // numeric conversions
126 //int stoi (string_view s, string_view::size_type* idx = nullptr, int base = 0);
127 //long stol (string_view s, string_view::size_type* idx = nullptr, int base = 0);
128 //unsigned long stoul (string_view s, string_view::size_type* idx = nullptr, int base = 0);
129 //long long stoll (string_view s, string_view::size_type* idx = nullptr, int base = 0);
130 //unsigned long long stoull(string_view s, string_view::size_type* idx = nullptr, int base = 0);
131 //float stof (string_view s, string_view::size_type* idx = nullptr);
132 //double stod (string_view s, string_view::size_type* idx = nullptr);
133 //long double stold (string_view s, string_view::size_type* idx = nullptr);
134 
135 
136 // Faster than the above, but less general (not part of the std proposal):
137 // - Only handles decimal.
138 // - No leading + or - sign (and thus only positive values).
139 // - No leading whitespace.
140 // - No trailing non-digit characters.
141 // - No out-of-range check (so undetected overflow on e.g. 9999999999).
142 // - Empty string parses as zero.
143 // Throws std::invalid_argument if any character is different from [0-9],
144 // similar to the error reporting in the std::stoi() (and related) functions.
145 unsigned fast_stou(string_view s);
146 
147 
148 std::ostream& operator<<(std::ostream& os, string_view s);
149 
150 // begin, end
151 inline auto begin(const string_view& x) { return x.begin(); }
152 inline auto end (const string_view& x) { return x.end(); }
153 
154 
155 #endif
const char * data() const
Definition: string_view.hh:57
auto rend() const
Definition: string_view.hh:41
auto rbegin() const
Definition: string_view.hh:40
unsigned fast_stou(string_view s)
Definition: string_view.cc:145
bool starts_with(string_view x) const
Definition: string_view.cc:116
string_view(const char *s, size_type len)
Definition: string_view.hh:30
size_type find_first_of(string_view s) const
Definition: string_view.cc:87
bool operator>=(string_view x, string_view y)
Definition: string_view.hh:123
void pop_front()
Definition: string_view.hh:75
std::ptrdiff_t difference_type
Definition: string_view.hh:20
void pop_back()
Definition: string_view.hh:74
bool operator>(string_view x, string_view y)
Definition: string_view.hh:121
bool operator==(string_view x, string_view y)
Definition: string_view.hh:114
size_type find_last_of(string_view s) const
Definition: string_view.cc:101
char back() const
Definition: string_view.hh:56
bool ends_with(string_view x) const
Definition: string_view.cc:126
std::ostream & operator<<(std::ostream &os, string_view s)
Definition: string_view.cc:160
static const size_type npos
Definition: string_view.hh:24
int compare(string_view x) const
Definition: string_view.cc:21
void clear()
Definition: string_view.hh:64
char front() const
Definition: string_view.hh:55
void remove_prefix(size_type n)
Definition: string_view.hh:65
constexpr size_t strlen(const char *s) noexcept
Definition: cstd.hh:135
size_type find(string_view s) const
Definition: string_view.cc:38
bool operator<(string_view x, string_view y)
Definition: string_view.cc:138
bool operator<=(string_view x, string_view y)
Definition: string_view.hh:122
size_t size_type
Definition: string_view.hh:19
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: string_view.hh:22
size_type rfind(string_view s) const
Definition: string_view.cc:65
bool empty() const
Definition: string_view.hh:45
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
string_view(const std::string &s)
Definition: string_view.hh:34
auto begin() const
Definition: string_view.hh:38
std::string str() const
Definition: string_view.cc:12
char operator[](size_type i) const
Definition: string_view.hh:50
string_view(const char *s)
Definition: string_view.hh:28
string_view substr(size_type pos, size_type n=npos) const
Definition: string_view.cc:32
string_view()=default
const char * const_iterator
Definition: string_view.hh:21
size_type size() const
Definition: string_view.hh:44
string_view(const char *first, const char *last)
Definition: string_view.hh:32
auto end() const
Definition: string_view.hh:39
bool operator!=(string_view x, string_view y)
Definition: string_view.hh:120
void remove_suffix(size_type n)
Definition: string_view.hh:70