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