nlib
StringView.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_STRINGVIEW_H_
17 #define INCLUDE_NN_NLIB_STRINGVIEW_H_
18 
19 #include <algorithm>
20 #include <iterator>
21 #include <utility>
22 
23 #include "nn/nlib/Config.h"
24 #include "nn/nlib/UniquePtr.h"
25 #include "nn/nlib/Cstring.h"
26 
27 NLIB_NAMESPACE_BEGIN
28 
30  public:
31  // types
32  typedef char charT;
33  typedef charT value_type;
34  typedef const charT* pointer;
35  typedef const charT* const_pointer;
36  typedef const charT& reference;
37  typedef const charT& const_reference;
38  typedef const_pointer const_iterator;
39  typedef const_iterator iterator;
40  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
41  typedef const_reverse_iterator reverse_iterator;
42  typedef size_t size_type;
43  typedef ptrdiff_t difference_type;
44  static const size_type npos = size_type(-1);
45 
46  // [string.view.cons], construct/copy
47  NLIB_CEXPR StringView() NLIB_NOEXCEPT : len_(0), base_("") {}
48  // this ctor calls strlen(), use StringView(str, len) if you know the the length of the string.
49  explicit StringView(const charT* str) NLIB_NOEXCEPT : len_(nlib_strlen(str)), base_(str) {
50  NLIB_ASSERT(this->IsValid_());
51  }
52  StringView(const charT* str, size_type len) NLIB_NOEXCEPT : len_(len), base_(str) {
53  NLIB_ASSERT(this->IsValid_());
54  }
55 
56  // [string.view.iterators], iterators
57  const_iterator begin() const NLIB_NOEXCEPT {
58  NLIB_ASSERT(this->IsValid_());
59  return base_;
60  }
61  const_iterator end() const NLIB_NOEXCEPT { return begin() + len_; }
62  const_iterator cbegin() const NLIB_NOEXCEPT { return begin(); }
63  const_iterator cend() const NLIB_NOEXCEPT { return cbegin() + len_; }
64 
65  const_reverse_iterator rbegin() const NLIB_NOEXCEPT { return const_reverse_iterator(end()); }
66  const_reverse_iterator rend() const NLIB_NOEXCEPT { return const_reverse_iterator(begin()); }
67  const_reverse_iterator crbegin() const NLIB_NOEXCEPT { return const_reverse_iterator(end()); }
68  const_reverse_iterator crend() const NLIB_NOEXCEPT { return const_reverse_iterator(begin()); }
69 
70  // [string.view.capacity], capacity
71  size_type size() const NLIB_NOEXCEPT {
72  NLIB_ASSERT(this->IsValid_());
73  return len_;
74  }
75  size_type length() const NLIB_NOEXCEPT {
76  NLIB_ASSERT(this->IsValid_());
77  return len_;
78  }
79  size_type max_size() const NLIB_NOEXCEPT { return static_cast<size_type>(-1) / sizeof(charT); }
80  bool empty() const NLIB_NOEXCEPT { return this->size() == 0; }
81 
82  // [string.view.access], element access
83  const charT& operator[](size_type pos) const {
84  NLIB_ASSERT(this->IsValid_());
85  return base_[pos];
86  }
87  const charT& at(size_type pos) const {
88  NLIB_ASSERT(this->IsValid_());
89  return base_[pos];
90  }
91  const charT& front() const { return (*this)[0]; }
92  const charT& back() const { return (*this)[this->length() - 1]; }
93  const charT* data() const NLIB_NOEXCEPT { return base_; }
94 
95  // [string.view.modifiers], modifiers:
97  len_ = 0;
98  base_ = "";
99  }
100  void remove_prefix(size_type n) NLIB_NOEXCEPT {
101  if (n >= len_) {
102  base_ += len_;
103  len_ = 0;
104  } else {
105  base_ += n;
106  len_ -= n;
107  }
108  }
109  void remove_suffix(size_type n) NLIB_NOEXCEPT {
110  if (n >= len_) {
111  len_ = 0;
112  } else {
113  len_ -= n;
114  }
115  }
116 
117  // [string.view.ops], string operations:
118  StringView substr(size_type pos, size_type n = npos) const NLIB_NOEXCEPT {
119  NLIB_ASSERT(this->IsValid_());
120  if (pos >= len_) return StringView(base_ + len_, 0);
121  const charT* new_base = base_ + pos;
122  size_type r = static_cast<size_type>(len_ - pos);
123  size_type new_len = r > n ? n : r;
124  return StringView(new_base, new_len);
125  }
126  int compare(const StringView& s) const NLIB_NOEXCEPT;
127  int compare(const charT* s) const NLIB_NOEXCEPT { return this->compare(StringView(s)); }
128  bool starts_with(const StringView& s) const NLIB_NOEXCEPT {
129  NLIB_ASSERT(this->IsValid_());
130  if (s.length() > this->length()) return false;
131  return nlib_memcmp(s.data(), base_, sizeof(*base_) * s.length()) == 0;
132  }
133  bool starts_with(charT c) const NLIB_NOEXCEPT { return front() == c; }
134  NLIB_DEPRECATED bool starts_with(const charT* s) const NLIB_NOEXCEPT {
135  return this->starts_with(StringView(s));
136  }
137  bool ends_with(const StringView& s) const NLIB_NOEXCEPT {
138  NLIB_ASSERT(this->IsValid_());
139  if (s.length() > len_) return false;
140  return nlib_memcmp(s.data(), base_ + len_ - s.length(), sizeof(*base_) * s.length()) ==
141  0;
142  }
143  NLIB_DEPRECATED bool ends_with(charT c) const NLIB_NOEXCEPT { return back() == c; }
144  NLIB_DEPRECATED bool ends_with(const charT* s) const NLIB_NOEXCEPT {
145  return this->ends_with(StringView(s));
146  }
147  bool ToCstring(charT* str, size_type buf_size) const NLIB_NOEXCEPT;
148  template <size_type N>
149  NLIB_DEPRECATED bool ToCstring(charT (&str)[N]) const NLIB_NOEXCEPT {
150  return ToCstring(str, N);
151  }
152 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
153  template<class DUMMY = void>
154 #endif
155  NLIB_DEPRECATED bool ToCstring(UniquePtr<charT[]>& str) const NLIB_NOEXCEPT { // NOLINT
156  // to avoid crossing the DLL boundary
157  NLIB_ASSERT(this->IsValid_());
158  str.reset(new (std::nothrow) charT[len_ + 1]);
159  if (!str) return false;
160  return ToCstring(str.get(), len_ + 1);
161  }
162 #ifdef __cpp_rvalue_references
163  NLIB_DEPRECATED UniquePtr<charT[]> ToCstring() const NLIB_NOEXCEPT {
164  NLIB_ASSERT(this->IsValid_());
165  charT* p = new (std::nothrow) charT[len_ + 1];
166  if (!p) return nullptr;
167  (void)ToCstring(p, len_ + 1);
168  return UniquePtr<char[]>(p);
169  }
170 #endif
171 
172  errno_t ToInteger(int32_t* v, size_type* idx = nullptr, int base = 10) const NLIB_NOEXCEPT;
173  NLIB_DEPRECATED errno_t ToInteger(int64_t* v, size_type* idx = nullptr, int base = 10) const NLIB_NOEXCEPT; // NOLINT
174  errno_t ToInteger(uint32_t* v, size_type* idx = nullptr, int base = 10) const NLIB_NOEXCEPT;
175  NLIB_DEPRECATED errno_t ToInteger(uint64_t* v, size_type* idx = nullptr, int base = 10) const NLIB_NOEXCEPT; // NOLINT
176  NLIB_DEPRECATED errno_t ToInteger(int8_t* v, size_type* idx = nullptr, int base = 10) const NLIB_NOEXCEPT; // NOLINT
177  NLIB_DEPRECATED errno_t ToInteger(int16_t* v, size_type* idx = nullptr, int base = 10) const NLIB_NOEXCEPT; // NOLINT
178  NLIB_DEPRECATED errno_t ToInteger(uint8_t* v, size_type* idx = nullptr, int base = 10) const NLIB_NOEXCEPT; // NOLINT
179  NLIB_DEPRECATED errno_t ToInteger(uint16_t* v, size_type* idx = nullptr, int base = 10) const NLIB_NOEXCEPT; // NOLINT
180  NLIB_DEPRECATED errno_t ToFloat(float* v, size_type* idx = nullptr) const NLIB_NOEXCEPT;
181  NLIB_DEPRECATED errno_t ToDouble(double* v, size_type* idx = nullptr) const NLIB_NOEXCEPT;
182 
183  bool TrimLeft() NLIB_NOEXCEPT;
184  bool TrimRight() NLIB_NOEXCEPT;
185  NLIB_DEPRECATED void Trim() NLIB_NOEXCEPT {
186  TrimLeft();
187  TrimRight();
188  }
190  bool Proceed(const StringView& kwd) NLIB_NOEXCEPT {
191  if (!this->starts_with(kwd)) return false;
192  base_ += kwd.length();
193  len_ -= kwd.length();
194  return true;
195  }
196  NLIB_DEPRECATED bool Proceed(const charT* kwd) NLIB_NOEXCEPT { return this->Proceed(StringView(kwd)); } // NOLINT
197  NLIB_DEPRECATED bool Proceed(charT ch) NLIB_NOEXCEPT;
199  /*
200  size_type find(const StringView& s) const {
201  StringView::iterator it = std::search(
202  begin(), end(), s.begin(), s.end());
203  return it == end() ? npos : it - begin();
204  }
205  size_type find(charT c) const;
206  size_type find(const charT* s) const;
207  size_type rfind(const StringView& s) const {
208  StringView::iterator it = std::find_end(
209  begin(), end(), s.begin(), s.end());
210  return it == end() ? npos : it - begin();
211  }
212  size_type rfind(charT c) const;
213  size_type rfind(const charT* s) const;
214  size_type find_first_of(const StringView& s) const {
215  StringView::iterator it = std::find_first_of(
216  begin(), end(), s.begin(), s.end());
217  return it == end() ? npos : it - begin();
218  }
219  size_type find_first_of(charT c) const;
220  size_type find_first_of(const charT* s) const;
221  size_type find_last_of(const StringView& s) const {
222  StringView::reverse_iterator it = std::find_first_of(
223  rbegin(), rend(), s.begin(), s.end());
224  return it == rend() ? std::string::npos : it.base() - 1 - begin();
225  }
226  size_type find_last_of(charT c) const;
227  size_type find_last_of(const charT* s) const;
228  size_type find_first_not_of(const StringView& s) const;
229  size_type find_first_not_of(charT c) const;
230  size_type find_first_not_of(const charT* s) const;
231  size_type find_last_not_of(const StringView& s) const;
232  size_type find_last_not_of(charT c) const;
233  size_type find_last_not_of(const charT* s) const;
234  */
235 
236  private:
237  bool IsValid_() const NLIB_NOEXCEPT {
238  if (!base_) return false;
239  return true;
240  }
241  size_type len_;
242  const charT* base_;
243 };
244 
245 
246 inline errno_t StringView::ToInteger(int8_t* v, size_type* idx, int base) const NLIB_NOEXCEPT {
247  int32_t tmp;
248  errno_t e = ToInteger(&tmp, idx, base);
249  if (e != 0) return e;
250  if (tmp > 127 || tmp < -128) return ERANGE;
251  *v = static_cast<int8_t>(tmp);
252  return 0;
253 }
254 
255 
256 inline errno_t StringView::ToInteger(int16_t* v, size_type* idx, int base) const NLIB_NOEXCEPT {
257  int32_t tmp;
258  errno_t e = ToInteger(&tmp, idx, base);
259  if (e != 0) return e;
260  if (tmp > 32767 || tmp < -32768) return ERANGE;
261  *v = static_cast<int16_t>(tmp);
262  return 0;
263 }
264 
265 
266 inline errno_t StringView::ToInteger(uint8_t* v, size_type* idx, int base) const NLIB_NOEXCEPT {
267  uint32_t tmp;
268  errno_t e = ToInteger(&tmp, idx, base);
269  if (e != 0) return e;
270  if (tmp > 255) return ERANGE;
271  *v = static_cast<uint8_t>(tmp);
272  return 0;
273 }
274 
275 
276 inline errno_t StringView::ToInteger(uint16_t* v, size_type* idx, int base) const NLIB_NOEXCEPT {
277  uint32_t tmp;
278  errno_t e = ToInteger(&tmp, idx, base);
279  if (e != 0) return e;
280  if (tmp > 65535) return ERANGE;
281  *v = static_cast<uint16_t>(tmp);
282  return 0;
283 }
284 
285 inline bool StringView::Proceed(charT ch) NLIB_NOEXCEPT {
286  if (this->starts_with(ch)) {
287  ++base_;
288  --len_;
289  return true;
290  } else {
291  return false;
292  }
293 }
294 
295 inline StringView StringView::GetName() NLIB_NOEXCEPT {
296  // isalpha()[isalnum()]*
297  NLIB_ASSERT(this->IsValid_());
298  const charT* s = base_;
299  const charT* s_end = base_ + len_;
300  charT c = (s != s_end) ? *s : '\0';
301  if (!nlib_isalpha(c) && c != '_') return *this;
302  for (;;) {
303  ++s;
304  c = (s != s_end) ? *s : '\0';
305  if (!nlib_isalnum(c) && c != '_') break;
306  }
307  size_type len = s - base_;
308  StringView rval(base_, len);
309  base_ = s;
310  len_ = len_ - len;
311  return rval;
312 }
313 
314 // [string.view.comparison], non-member StringView comparison functions
315 inline bool operator==(const StringView& lhs, const StringView& rhs) NLIB_NOEXCEPT {
316  return lhs.compare(rhs) == 0;
317 }
318 NLIB_EQUAL_OPERATOR(StringView)
319 inline bool operator<(const StringView& lhs, const StringView& rhs) NLIB_NOEXCEPT {
320  return lhs.compare(rhs) < 0;
321 }
322 NLIB_COMPARE_OPERATOR(StringView)
323 
324 // Free functions
325 inline bool StartsWith(const StringView& str, const StringView& prefix) NLIB_NOEXCEPT {
326  if (prefix.length() > str.length()) return false;
327  return nlib_memcmp(prefix.data(), str.data(), prefix.length()) == 0;
328 }
329 inline bool StartsWith(const StringView& str, char c) NLIB_NOEXCEPT { return str.front() == c; }
330 inline bool StartsWith(const StringView& str, const char* prefix) NLIB_NOEXCEPT {
331  return StartsWith(str, StringView(prefix));
332 }
333 inline bool EndsWith(const StringView& str, const StringView& suffix) NLIB_NOEXCEPT {
334  if (suffix.length() > str.length()) return false;
335  return nlib_memcmp(suffix.data(), str.end() - suffix.length(), suffix.length()) == 0;
336 }
337 inline bool EndsWith(const StringView& str, char c) NLIB_NOEXCEPT { return str.back() == c; }
338 inline bool EndsWith(const StringView& str, const char* suffix) NLIB_NOEXCEPT {
339  return EndsWith(str, StringView(suffix));
340 }
341 inline bool ToCstring(char* buf, size_t buf_size, const StringView& str) NLIB_NOEXCEPT {
342  if (!buf || buf_size == 0) return false;
343  size_t len = str.length();
344  errno_t e = nlib_memcpy(buf, buf_size - 1, str.begin(), len);
345  if (e == 0) {
346  buf[len] = '\0';
347  return true;
348  } else {
349  return false;
350  }
351 }
352 template<size_t N>
353 inline bool ToCstring(char (&buf)[N], const StringView& str) NLIB_NOEXCEPT {
354  return ToCstring(&buf[0], N, str);
355 }
356 #ifdef __cpp_rvalue_references
358  size_t buf_size = str.length() + 1;
359  char* p = new (std::nothrow) char[buf_size];
360  if (!p) return nullptr;
361  (void)ToCstring(p, buf_size, str);
362  return UniquePtr<char[]>(p);
363 }
364 #endif
365 inline std::pair<errno_t, size_t>
366 ToInteger(int8_t* v, const StringView& str, int base) NLIB_NOEXCEPT {
367  const char* eptr;
368  errno_t e = nlib_int8_from_chars(v, &eptr, str.begin(), str.end(), base);
369  return std::make_pair(e, eptr - str.begin());
370 }
371 inline std::pair<errno_t, size_t>
372 ToInteger(int16_t* v, const StringView& str, int base) NLIB_NOEXCEPT {
373  const char* eptr;
374  errno_t e = nlib_int16_from_chars(v, &eptr, str.begin(), str.end(), base);
375  return std::make_pair(e, eptr - str.begin());
376 }
377 inline std::pair<errno_t, size_t>
378 ToInteger(int32_t* v, const StringView& str, int base) NLIB_NOEXCEPT {
379  const char* eptr;
380  errno_t e = nlib_int32_from_chars(v, &eptr, str.begin(), str.end(), base);
381  return std::make_pair(e, eptr - str.begin());
382 }
383 inline std::pair<errno_t, size_t>
384 ToInteger(int64_t* v, const StringView& str, int base) NLIB_NOEXCEPT {
385  const char* eptr;
386  errno_t e = nlib_int64_from_chars(v, &eptr, str.begin(), str.end(), base);
387  return std::make_pair(e, eptr - str.begin());
388 }
389 inline std::pair<errno_t, size_t>
390 ToInteger(uint8_t* v, const StringView& str, int base) NLIB_NOEXCEPT {
391  const char* eptr;
392  errno_t e = nlib_uint8_from_chars(v, &eptr, str.begin(), str.end(), base);
393  return std::make_pair(e, eptr - str.begin());
394 }
395 inline std::pair<errno_t, size_t>
396 ToInteger(uint16_t* v, const StringView& str, int base) NLIB_NOEXCEPT {
397  const char* eptr;
398  errno_t e = nlib_uint16_from_chars(v, &eptr, str.begin(), str.end(), base);
399  return std::make_pair(e, eptr - str.begin());
400 }
401 inline std::pair<errno_t, size_t>
402 ToInteger(uint32_t* v, const StringView& str, int base) NLIB_NOEXCEPT {
403  const char* eptr;
404  errno_t e = nlib_uint32_from_chars(v, &eptr, str.begin(), str.end(), base);
405  return std::make_pair(e, eptr - str.begin());
406 }
407 inline std::pair<errno_t, size_t>
408 ToInteger(uint64_t* v, const StringView& str, int base) NLIB_NOEXCEPT {
409  const char* eptr;
410  errno_t e = nlib_uint64_from_chars(v, &eptr, str.begin(), str.end(), base);
411  return std::make_pair(e, eptr - str.begin());
412 }
413 inline std::pair<errno_t, size_t>
414 ToFloat(float* v, const StringView& str) NLIB_NOEXCEPT {
415  const char* eptr;
416  errno_t e = nlib_float_from_chars(v, &eptr, str.begin(), str.end());
417  return std::make_pair(e, eptr - str.begin());
418 }
419 inline std::pair<errno_t, size_t>
420 ToDouble(double* v, const StringView& str) NLIB_NOEXCEPT {
421  const char* eptr;
422  errno_t e = nlib_double_from_chars(v, &eptr, str.begin(), str.end());
423  return std::make_pair(e, eptr - str.begin());
424 }
425 template<class T>
426 inline std::pair<errno_t, size_t>
427 ToInteger(T* v, const StringView& str) NLIB_NOEXCEPT {
428  return ToInteger(v, str, 10);
429 }
430 inline bool TrimLeft(StringView& str) NLIB_NOEXCEPT { // NOLINT
431  StringView::iterator it = str.begin();
432  StringView::iterator itend = str.end();
433  for (; it != itend; ++it) {
434  if (!nlib_isspace(*it)) break;
435  }
436  ptrdiff_t cnt = std::distance(str.begin(), it);
437  if (cnt == 0) return false;
438  str.remove_prefix(cnt);
439  return true;
440 }
441 inline bool TrimRight(StringView& str) NLIB_NOEXCEPT { // NOLINT
442  StringView::reverse_iterator it = str.rbegin();
443  StringView::reverse_iterator itend = str.rend();
444  for (; it != itend; ++it) {
445  if (!nlib_isspace(*it)) break;
446  }
447  ptrdiff_t cnt = std::distance(str.rbegin(), it);
448  if (cnt == 0) return false;
449  str.remove_suffix(cnt);
450  return true;
451 }
452 inline void Trim(StringView& str) NLIB_NOEXCEPT { // NOLINT
453  TrimLeft(str);
454  TrimRight(str);
455 }
456 inline StringView GetLine(StringView& str) NLIB_NOEXCEPT { // NOLINT
457  const char* beg = str.begin();
458  const char* p = static_cast<const char*>(nlib_memchr(beg, '\n', str.length()));
459  if (!p) {
460  StringView rval(str);
461  str.remove_prefix(str.length());
462  return rval;
463  }
464  size_t width = p - beg;
465  if (width > 0 && *(p - 1) == '\r') --width;
466  StringView rval(beg, width);
467  str.remove_prefix(p - beg + 1);
468  return rval;
469 }
470 inline bool Proceed(StringView& str, const StringView& prefix) NLIB_NOEXCEPT { // NOLINT
471  if (!str.starts_with(prefix)) return false;
472  str.remove_prefix(prefix.length());
473  return true;
474 }
475 inline bool Proceed(StringView& str, const char* prefix) NLIB_NOEXCEPT { // NOLINT
476  return Proceed(str, StringView(prefix));
477 }
478 inline bool Proceed(StringView& str, char c) NLIB_NOEXCEPT { // NOLINT
479  if (!str.starts_with(c)) return false;
480  str.remove_prefix(1);
481  return true;
482 }
483 inline StringView GetName(StringView& str) NLIB_NOEXCEPT { // NOLINT
484  StringView::iterator it = str.begin();
485  StringView::iterator it_end = str.end();
486  if (it != it_end) {
487  if (!nlib_isalpha(*it) && *it != '_') return str;
488  while (++it != it_end) {
489  if (!nlib_isalnum(*it) && *it != '_') break;
490  }
491  }
492  ptrdiff_t len = std::distance(str.begin(), it);
493  StringView rval(str.begin(), len);
494  str.remove_prefix(len);
495  return rval;
496 }
497 
498 NLIB_NAMESPACE_END
499 
500 #endif // INCLUDE_NN_NLIB_STRINGVIEW_H_
void remove_prefix(size_type n) noexcept
Removes the first n characters.
Definition: StringView.h:100
std::pair< errno_t, size_t > ToInteger(T *v, const StringView &str) noexcept
Returns ToInteger(v, str, 10). This means that a character is converted to a decimal numerical value...
Definition: StringView.h:427
bool ends_with(const StringView &s) const noexcept
Checks whether the string has s as its suffix.
Definition: StringView.h:137
std::reverse_iterator< const_iterator > const_reverse_iterator
A string reverse iterator.
Definition: StringView.h:40
const charT * data() const noexcept
Returns the pointer to the first character.
Definition: StringView.h:93
size_t size_type
A non-negative integer type, currently defined in size_t using typedef.
Definition: StringView.h:42
void clear() noexcept
Sets an empty string.
Definition: StringView.h:96
static int nlib_isalpha(int ch)
If ch is an ASCII character &#39;A&#39;-&#39;Z&#39; or &#39;a&#39;-&#39;z&#39;, the function returns non-zero. Othewise, the function returns 0.
Definition: Platform.h:2409
ptrdiff_t difference_type
The type returned when you take the difference between iterators.
Definition: StringView.h:43
errno_t nlib_uint64_from_chars(uint64_t *result, const char **endptr, const char *first, const char *last, int base)
Converts a string to the uint64_t type. For more information, refer to the description for nlib_int32...
bool empty() const noexcept
Checks whether it is an empty string.
Definition: StringView.h:80
int nlib_memcmp(const void *buf1, const void *buf2, size_t n)
Compares the n bytes from the starts of buf1 and buf2 as unsigned char data.
StringView substr(size_type pos, size_type n=npos) const noexcept
Returns a substring [pos, pos + n).
Definition: StringView.h:118
std::pair< errno_t, size_t > ToFloat(float *v, const StringView &str) noexcept
Internally calls nlib_float_from_chars() to convert a string to a numerical value. The returned value is a pair of the error value and the number of read characters.
Definition: StringView.h:414
errno_t nlib_float_from_chars(float *result, const char **endptr, const char *first, const char *last)
Converts a string to the float type. For more information, refer to the description for nlib_double_f...
bool ends_with(charT c) const noexcept
Checks whether the string ends with the character specified for c.
Definition: StringView.h:143
const_reverse_iterator crbegin() const noexcept
Returns the reverse iterator pointing to the last character.
Definition: StringView.h:67
size_type length() const noexcept
Returns the length of the string.
Definition: StringView.h:75
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Definition: UniquePtr.h:109
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are equal.
Definition: NMalloc.h:149
Defines that class that is corresponding to std::unique_ptr.
char charT
Currently a char-type specific class.
Definition: StringView.h:32
#define NLIB_DEPRECATED
Indicates that a function or something has been deprecated.
Definition: Config.h:109
bool starts_with(charT c) const noexcept
Checks whether the string begins with the character specified for c.
Definition: StringView.h:133
UniquePtr< char[]> ToCstring(const StringView &str) noexcept
Allocates memory and copies a string as a null terminated string. Returns true if the string was succ...
Definition: StringView.h:357
const charT & operator[](size_type pos) const
Gets the nth character, where n is specified by pos.
Definition: StringView.h:83
errno_t nlib_uint8_from_chars(uint8_t *result, const char **endptr, const char *first, const char *last, int base)
Converts a string to the uint8_t type. For more information, refer to the description for nlib_int32_...
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
bool EndsWith(const StringView &str, const char *suffix) noexcept
Returns true if suffix is the suffix of str.
Definition: StringView.h:338
bool starts_with(const StringView &s) const noexcept
Checks whether the string has s as its prefix.
Definition: StringView.h:128
const charT & back() const
Gets the last character.
Definition: StringView.h:92
bool ends_with(const charT *s) const noexcept
Checks whether the string has s as its suffix.
Definition: StringView.h:144
constexpr StringView() noexcept
Instantiates the object with default parameters (default constructor). Initialized as an empty string...
Definition: StringView.h:47
bool TrimRight(StringView &str) noexcept
Removes any white spaces from the end of the string.
Definition: StringView.h:441
const void * nlib_memchr(const void *s, int c, size_t n)
Searches the n bytes from the start of the memory region (s, s + n) and returns a pointer to byte c...
const_iterator end() const noexcept
Returns the iterator pointing to the character following the last character.
Definition: StringView.h:61
const charT * pointer
The type for a character pointer. The same as const_pointer.
Definition: StringView.h:34
size_type max_size() const noexcept
Returns the maximum value for the string length.
Definition: StringView.h:79
errno_t nlib_int32_from_chars(int32_t *result, const char **endptr, const char *first, const char *last, int base)
Converts a string to the int32_t type.
const_iterator cend() const noexcept
Returns the iterator pointing to the character following the last character.
Definition: StringView.h:63
const_iterator iterator
A string iterator. The same as const_iterator.
Definition: StringView.h:39
int compare(const charT *s) const noexcept
Compares strings.
Definition: StringView.h:127
const charT & at(size_type pos) const
Gets the nth character, where n is specified by pos.
Definition: StringView.h:87
errno_t nlib_int8_from_chars(int8_t *result, const char **endptr, const char *first, const char *last, int base)
Converts a string to the int8_t type. For more information, refer to the description for nlib_int32_f...
errno_t nlib_int64_from_chars(int64_t *result, const char **endptr, const char *first, const char *last, int base)
Converts a string to the int64_t type. For more information, refer to the description for nlib_int32_...
static int nlib_isalnum(int ch)
If ch is an ASCII character &#39;0&#39;-&#39;9&#39;, &#39;A&#39;-&#39;Z&#39;, or &#39;a&#39;-&#39;z&#39;, the function returns non-zero. Otherwise, the function returns 0.
Definition: Platform.h:2406
static errno_t nlib_memcpy(void *s1, size_t s1max, const void *s2, size_t n)
An implementation corresponding to N1078 memcpy_s.
Definition: Platform.h:2437
const_reverse_iterator reverse_iterator
A string reverse iterator. The same as const_reverse_iterator.
Definition: StringView.h:41
static int nlib_isspace(int ch)
If ch is an ASCII character &#39; &#39;, &#39;\t&#39;, or &#39;\n&#39;, the function returns non-zero. Otherwise, the function returns 0.
Definition: Platform.h:2419
const_iterator begin() const noexcept
Returns the iterator pointing to the first character.
Definition: StringView.h:57
bool Proceed(StringView &str, char c) noexcept
If str starts with c, it is allowed to go forward by the length of that string.
Definition: StringView.h:478
bool StartsWith(const StringView &str, const char *prefix) noexcept
Returns true if prefix is the prefix of str.
Definition: StringView.h:330
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
const_iterator cbegin() const noexcept
Returns the iterator pointing to the first character.
Definition: StringView.h:62
StringView(const charT *str, size_type len) noexcept
Initializes using the specified calculated string length.
Definition: StringView.h:52
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:107
A file that contains the configuration information for each development environment.
const_reverse_iterator crend() const noexcept
Returns the reverse iterator pointing to the character before the first character.
Definition: StringView.h:68
const_pointer const_iterator
A character string iterator.
Definition: StringView.h:38
StringView GetName(StringView &str) noexcept
Obtains a string that is generally handled as a variable name. Its own object ( str) moves to the pos...
Definition: StringView.h:483
The class for using the member functions of std::string without constructing std::string. This class will be defined as the string_view of C++17 using typedef. For more information about free functions taking StringView as the argument, seehere..
Definition: StringView.h:29
bool starts_with(const charT *s) const noexcept
Checks whether the string has s as its prefix.
Definition: StringView.h:134
std::pair< errno_t, size_t > ToDouble(double *v, const StringView &str) noexcept
Internally calls nlib_double_from_chars() to convert a string to a numerical value. The returned value is a pair of the error value and the number of read characters.
Definition: StringView.h:420
void Trim(StringView &str) noexcept
Removes any white spaces from the beginning and end of the string.
Definition: StringView.h:452
bool Proceed(const StringView &kwd) noexcept
Advances by the amount of the string length of kwd, providing that the prefix matches kwd...
Definition: StringView.h:190
void remove_suffix(size_type n) noexcept
Removes the last n characters.
Definition: StringView.h:109
size_t nlib_strlen(const char *s)
Internally calls strlen(). In some cases, it may operate as an independent implementation.
const charT * const_pointer
The type for a character pointer.
Definition: StringView.h:35
const charT & const_reference
A reference to a character.
Definition: StringView.h:37
errno_t nlib_int16_from_chars(int16_t *result, const char **endptr, const char *first, const char *last, int base)
Converts a string to the int16_t type. For more information, refer to the description for nlib_int32_...
Wraps functions like strlen and strcpy so they can be safely used.
const_reverse_iterator rbegin() const noexcept
Returns the reverse iterator pointing to the last character.
Definition: StringView.h:65
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:245
bool ToCstring(charT(&str)[N]) const noexcept
Definition: StringView.h:149
charT value_type
The type for a character.
Definition: StringView.h:33
StringView(const charT *str) noexcept
Initialized to reference str. The string length is calculated internally.
Definition: StringView.h:49
bool TrimLeft(StringView &str) noexcept
Removes any white spaces from the beginning of the string.
Definition: StringView.h:430
const charT & front() const
Gets the first character.
Definition: StringView.h:91
size_type size() const noexcept
Returns the length of the string.
Definition: StringView.h:71
const_reverse_iterator rend() const noexcept
Returns the reverse iterator pointing to the character before the first character.
Definition: StringView.h:66
bool ToCstring(UniquePtr< charT[]> &str) const noexcept
Definition: StringView.h:155
errno_t nlib_double_from_chars(double *result, const char **endptr, const char *first, const char *last)
Converts a string to the double type.
bool Proceed(const charT *kwd) noexcept
Advances by the amount of the string length of kwd, providing that the prefix matches kwd...
Definition: StringView.h:196
void Trim() noexcept
Trims the white space from the start and the end of the string.
Definition: StringView.h:185
errno_t nlib_uint16_from_chars(uint16_t *result, const char **endptr, const char *first, const char *last, int base)
Converts a string to the uint16_t type. For more information, refer to the description for nlib_int32...
errno_t nlib_uint32_from_chars(uint32_t *result, const char **endptr, const char *first, const char *last, int base)
Converts a string to the uint32_t type. For more information, refer to the description for nlib_int32...
const charT & reference
A reference to a character. The same as const_reference.
Definition: StringView.h:36
StringView GetLine(StringView &str) noexcept
Obtains the strings from the start to the end of the line. Its own object ( str) moves to the beginni...
Definition: StringView.h:456
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37