summaryrefslogtreecommitdiffstats
path: root/third_party/wasm2c/src/string-view.h
blob: 534e675c73473b7bf477d057a0c899c15b726d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright 2017 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef WABT_STRING_VIEW_H_
#define WABT_STRING_VIEW_H_

#include <cassert>
#include <functional>
#include <iterator>
#include <ostream>
#include <string>

#include "src/hash-util.h"

namespace wabt {

// This is a simplified implemention of C++17's basic_string_view template.
//
// Missing features:
// * Not a template
// * No allocator support
// * Some functions are not implemented
// * Asserts instead of exceptions
// * Some functions are not constexpr because we don't compile in C++17 mode

class string_view {
 public:
  typedef std::char_traits<char> traits_type;
  typedef char value_type;
  typedef char* pointer;
  typedef const char* const_pointer;
  typedef char& reference;
  typedef const char& const_reference;
  typedef const char* const_iterator;
  typedef const_iterator iterator;
  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  typedef const_reverse_iterator reverse_iterator;
  typedef std::size_t size_type;
  typedef std::ptrdiff_t difference_type;
  static const size_type npos = size_type(-1);

  // construction and assignment
  constexpr string_view() noexcept;
  constexpr string_view(const string_view&) noexcept = default;
  string_view& operator=(const string_view&) noexcept = default;
  string_view(const std::string& str) noexcept;
  /*constexpr*/ string_view(const char* str);
  constexpr string_view(const char* str, size_type len);

  // iterator support
  constexpr const_iterator begin() const noexcept;
  constexpr const_iterator end() const noexcept;
  constexpr const_iterator cbegin() const noexcept;
  constexpr const_iterator cend() const noexcept;
  const_reverse_iterator rbegin() const noexcept;
  const_reverse_iterator rend() const noexcept;
  const_reverse_iterator crbegin() const noexcept;
  const_reverse_iterator crend() const noexcept;

  // capacity
  constexpr size_type size() const noexcept;
  constexpr size_type length() const noexcept;
  constexpr size_type max_size() const noexcept;
  constexpr bool empty() const noexcept;

  // element access
  constexpr const_reference operator[](size_type pos) const;
  /*constexpr*/ const_reference at(size_type pos) const;
  /*constexpr*/ const_reference front() const;
  /*constexpr*/ const_reference back() const;
  constexpr const_pointer data() const noexcept;

  // modifiers
  /*constexpr*/ void remove_prefix(size_type n);
  /*constexpr*/ void remove_suffix(size_type n);
  /*constexpr*/ void swap(string_view& s) noexcept;

  // string operations
  explicit operator std::string() const;
  std::string to_string() const;
  size_type copy(char* s, size_type n, size_type pos = 0) const;
  /*constexpr*/ string_view substr(size_type pos = 0, size_type n = npos) const;
  /*constexpr*/ int compare(string_view s) const noexcept;
  /*constexpr*/ int compare(size_type pos1, size_type n1, string_view s) const;
  /*constexpr*/ int compare(size_type pos1,
                            size_type n1,
                            string_view s,
                            size_type pos2,
                            size_type n2) const;
  /*constexpr*/ int compare(const char* s) const;
  /*constexpr*/ int compare(size_type pos1, size_type n1, const char* s) const;
  /*constexpr*/ int compare(size_type pos1,
                            size_type n1,
                            const char* s,
                            size_type n2) const;
  /*constexpr*/ size_type find(string_view s, size_type pos = 0) const noexcept;
  /*constexpr*/ size_type find(char c, size_type pos = 0) const noexcept;
  /*constexpr*/ size_type find(const char* s, size_type pos, size_type n) const;
  /*constexpr*/ size_type find(const char* s, size_type pos = 0) const;
  /*constexpr*/ size_type rfind(string_view s, size_type pos = npos) const
      noexcept;
  /*constexpr*/ size_type rfind(char c, size_type pos = npos) const noexcept;
  /*constexpr*/ size_type rfind(const char* s,
                                size_type pos,
                                size_type n) const;
  /*constexpr*/ size_type rfind(const char* s, size_type pos = npos) const;
  /*constexpr*/ size_type find_first_of(string_view s, size_type pos = 0) const
      noexcept;
  /*constexpr*/ size_type find_first_of(char c, size_type pos = 0) const
      noexcept;
  /*constexpr*/ size_type find_first_of(const char* s,
                                        size_type pos,
                                        size_type n) const;
  /*constexpr*/ size_type find_first_of(const char* s, size_type pos = 0) const;
  /*constexpr*/ size_type find_last_of(string_view s,
                                       size_type pos = npos) const noexcept;
  /*constexpr*/ size_type find_last_of(char c, size_type pos = npos) const
      noexcept;
  /*constexpr*/ size_type find_last_of(const char* s,
                                       size_type pos,
                                       size_type n) const;
  /*constexpr*/ size_type find_last_of(const char* s,
                                       size_type pos = npos) const;

// TODO(binji): These are defined by C++17 basic_string_view but not
// implemented here.
#if 0
  constexpr size_type find_first_not_of(string_view s, size_type pos = 0) const
      noexcept;
  constexpr size_type find_first_not_of(char c, size_type pos = 0) const
      noexcept;
  constexpr size_type find_first_not_of(const char* s,
                                        size_type pos,
                                        size_type n) const;
  constexpr size_type find_first_not_of(const char* s, size_type pos = 0) const;
  constexpr size_type find_last_not_of(string_view s,
                                       size_type pos = npos) const noexcept;
  constexpr size_type find_last_not_of(char c, size_type pos = npos) const
      noexcept;
  constexpr size_type find_last_not_of(const char* s,
                                       size_type pos,
                                       size_type n) const;
  constexpr size_type find_last_not_of(const char* s,
                                       size_type pos = npos) const;
#endif

 private:
  const char* data_;
  size_type size_;
};

// non-member comparison functions
inline bool operator==(string_view x, string_view y) noexcept {
  return x.compare(y) == 0;
}

inline bool operator!=(string_view x, string_view y) noexcept {
  return x.compare(y) != 0;
}

inline bool operator<(string_view x, string_view y) noexcept {
  return x.compare(y) < 0;
}

inline bool operator>(string_view x, string_view y) noexcept {
  return x.compare(y) > 0;
}

inline bool operator<=(string_view x, string_view y) noexcept {
  return x.compare(y) <= 0;
}

inline bool operator>=(string_view x, string_view y) noexcept {
  return x.compare(y) >= 0;
}

// non-member append.
inline std::string& operator+=(std::string& x, string_view y) {
  x.append(y.data(), y.size());
  return x;
}

inline std::string operator+(string_view x, string_view y) {
  std::string s;
  s.reserve(x.size() + y.size());
  s.append(x.data(), x.size());
  s.append(y.data(), y.size());
  return s;
}

inline std::string operator+(const std::string& x, string_view y) {
  return string_view(x) + y;
}

inline std::string operator+(string_view x, const std::string& y) {
  return x + string_view(y);
}

inline std::string operator+(const char* x, string_view y) {
  return string_view(x) + y;
}

inline std::string operator+(string_view x, const char* y) {
  return x + string_view(y);
}

inline void cat_concatenate(std::string&) {}

template<typename T, typename ...Ts>
void cat_concatenate(std::string& s, const T& t, const Ts&... args) {
    s += t;
    cat_concatenate(s, args...);
}

inline size_t cat_compute_size() { return 0; }

template<typename T, typename ...Ts>
size_t cat_compute_size(const T& t, const Ts&... args) {
    return string_view(t).size() + cat_compute_size(args...);
}

// Is able to concatenate any combination of string/string_view/char*
template<typename ...Ts> std::string cat(const Ts&... args) {
    std::string s;
    s.reserve(cat_compute_size(args...));
    cat_concatenate(s, args...);
    return s;
}

inline constexpr string_view::string_view() noexcept
    : data_(nullptr), size_(0) {}

inline string_view::string_view(const std::string& str) noexcept
    : data_(str.data()), size_(str.size()) {}

inline string_view::string_view(const char* str)
    : data_(str), size_(traits_type::length(str)) {}

inline constexpr string_view::string_view(const char* str, size_type len)
    : data_(str), size_(len) {}

inline constexpr string_view::const_iterator string_view::begin() const
    noexcept {
  return data_;
}

inline constexpr string_view::const_iterator string_view::end() const noexcept {
  return data_ + size_;
}

inline constexpr string_view::const_iterator string_view::cbegin() const
    noexcept {
  return data_;
}

inline constexpr string_view::const_iterator string_view::cend() const
    noexcept {
  return data_ + size_;
}

inline string_view::const_reverse_iterator string_view::rbegin() const
    noexcept {
  return const_reverse_iterator(end());
}

inline string_view::const_reverse_iterator string_view::rend() const noexcept {
  return const_reverse_iterator(begin());
}

inline string_view::const_reverse_iterator string_view::crbegin() const
    noexcept {
  return const_reverse_iterator(cend());
}

inline string_view::const_reverse_iterator string_view::crend() const noexcept {
  return const_reverse_iterator(cbegin());
}

constexpr inline string_view::size_type string_view::size() const noexcept {
  return size_;
}

constexpr inline string_view::size_type string_view::length() const noexcept {
  return size_;
}

constexpr inline bool string_view::empty() const noexcept {
  return size_ == 0;
}

constexpr inline string_view::const_reference string_view::operator[](
    size_type pos) const {
  return data_[pos];
}

inline string_view::const_reference string_view::at(size_type pos) const {
  assert(pos < size_);
  return data_[pos];
}

inline string_view::const_reference string_view::front() const {
  assert(!empty());
  return *data_;
}

inline string_view::const_reference string_view::back() const {
  assert(!empty());
  return data_[size_ - 1];
}

constexpr inline string_view::const_pointer string_view::data() const noexcept {
  return data_;
}

inline std::ostream& operator<<(std::ostream& os, string_view sv) {
    os.write(sv.data(), sv.size());
    return os;
}

}  // namespace wabt

namespace std {

// hash support
template <>
struct hash<::wabt::string_view> {
  ::wabt::hash_code operator()(const ::wabt::string_view& sv) {
    return ::wabt::HashRange(sv.begin(), sv.end());
  }
};

}  // namespace std

#endif  // WABT_STRING_VIEW_H_