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 
22 #include "nn/nlib/Config.h"
23 #include "nn/nlib/UniquePtr.h"
24 #include "nn/nlib/Cstring.h"
25 
26 NLIB_NAMESPACE_BEGIN
27 
29  public:
30  // types
31  typedef char charT;
32  typedef charT value_type;
33  typedef const charT* pointer;
34  typedef const charT* const_pointer;
35  typedef const charT& reference;
36  typedef const charT& const_reference;
37  typedef const_pointer const_iterator;
38  typedef const_iterator iterator;
39  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
40  typedef const_reverse_iterator reverse_iterator;
41  typedef size_t size_type;
42  typedef ptrdiff_t difference_type;
43  static const size_type npos = size_type(-1);
44 
45  // [string.view.cons], construct/copy
46  NLIB_CEXPR StringView() NLIB_NOEXCEPT : len_(0), base_("") {}
47  // this ctor calls strlen(), use StringView(str, len) if you know the the length of the string.
48  explicit StringView(const charT* str) NLIB_NOEXCEPT : len_(nlib_strlen(str)), base_(str) {
49  NLIB_ASSERT(this->IsValid_());
50  }
51  StringView(const charT* str, size_type len) NLIB_NOEXCEPT : len_(len), base_(str) {
52  NLIB_ASSERT(this->IsValid_());
53  }
54 
55  // [string.view.iterators], iterators
56  const_iterator begin() const NLIB_NOEXCEPT {
57  NLIB_ASSERT(this->IsValid_());
58  return base_;
59  }
60  const_iterator end() const NLIB_NOEXCEPT { return begin() + len_; }
61  const_iterator cbegin() const NLIB_NOEXCEPT { return begin(); }
62  const_iterator cend() const NLIB_NOEXCEPT { return cbegin() + len_; }
63 
64  const_reverse_iterator rbegin() const NLIB_NOEXCEPT { return const_reverse_iterator(end()); }
65  const_reverse_iterator rend() const NLIB_NOEXCEPT { return const_reverse_iterator(begin()); }
66  const_reverse_iterator crbegin() const NLIB_NOEXCEPT { return const_reverse_iterator(end()); }
67  const_reverse_iterator crend() const NLIB_NOEXCEPT { return const_reverse_iterator(begin()); }
68 
69  // [string.view.capacity], capacity
70  size_type size() const NLIB_NOEXCEPT {
71  NLIB_ASSERT(this->IsValid_());
72  return len_;
73  }
74  size_type length() const NLIB_NOEXCEPT {
75  NLIB_ASSERT(this->IsValid_());
76  return len_;
77  }
78  size_type max_size() const NLIB_NOEXCEPT { return static_cast<size_type>(-1) / sizeof(charT); }
79  bool empty() const NLIB_NOEXCEPT { return this->size() == 0; }
80 
81  // [string.view.access], element access
82  const charT& operator[](size_type pos) const {
83  NLIB_ASSERT(this->IsValid_());
84  return base_[pos];
85  }
86  const charT& at(size_type pos) const {
87  NLIB_ASSERT(this->IsValid_());
88  return base_[pos];
89  }
90  const charT& front() const { return (*this)[0]; }
91  const charT& back() const { return (*this)[this->length() - 1]; }
92  const charT* data() const NLIB_NOEXCEPT { return base_; }
93 
94  // [string.view.modifiers], modifiers:
96  len_ = 0;
97  base_ = "";
98  }
99  void remove_prefix(size_type n) NLIB_NOEXCEPT {
100  if (n >= len_) {
101  base_ += len_;
102  len_ = 0;
103  } else {
104  base_ += n;
105  len_ -= n;
106  }
107  }
108  void remove_suffix(size_type n) NLIB_NOEXCEPT {
109  if (n >= len_) {
110  len_ = 0;
111  } else {
112  len_ -= n;
113  }
114  }
115 
116  // [string.view.ops], string operations:
117  StringView substr(size_type pos, size_type n = npos) const NLIB_NOEXCEPT {
118  NLIB_ASSERT(this->IsValid_());
119  if (pos >= len_) return StringView(base_ + len_, 0);
120  const charT* new_base = base_ + pos;
121  size_type r = static_cast<size_type>(len_ - pos);
122  size_type new_len = r > n ? n : r;
123  return StringView(new_base, new_len);
124  }
125  int compare(const StringView& s) const NLIB_NOEXCEPT;
126  int compare(const charT* s) const NLIB_NOEXCEPT { return this->compare(StringView(s)); }
127  bool starts_with(const StringView& s) const NLIB_NOEXCEPT {
128  NLIB_ASSERT(this->IsValid_());
129  if (s.length() > this->length()) return false;
130  return nlib_memcmp(s.data(), base_, sizeof(*base_) * s.length()) == 0;
131  }
132  bool starts_with(charT c) const NLIB_NOEXCEPT { return front() == c; }
133  bool starts_with(const charT* s) const NLIB_NOEXCEPT {
134  return this->starts_with(StringView(s));
135  }
136  bool ends_with(const StringView& s) const NLIB_NOEXCEPT {
137  NLIB_ASSERT(this->IsValid_());
138  if (s.length() > len_) return false;
139  return nlib_memcmp(s.data(), base_ + len_ - s.length(), sizeof(*base_) * s.length()) ==
140  0;
141  }
142  bool ends_with(charT c) const NLIB_NOEXCEPT { return back() == c; }
143  bool ends_with(const charT* s) const NLIB_NOEXCEPT { return this->ends_with(StringView(s)); }
144  bool ToCstring(charT* str, size_type buf_size) const NLIB_NOEXCEPT;
145  template <size_type N>
146  bool ToCstring(charT (&str)[N]) const NLIB_NOEXCEPT {
147  return ToCstring(str, N);
148  }
149 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
150  template<class DUMMY = void>
151 #endif
152  bool ToCstring(UniquePtr<charT[]>& str) const NLIB_NOEXCEPT { // NOLINT
153  // to avoid crossing the DLL boundary
154  NLIB_ASSERT(this->IsValid_());
155  str.reset(new (std::nothrow) charT[len_ + 1]);
156  if (!str) return false;
157  return ToCstring(str.get(), len_ + 1);
158  }
159 
160  errno_t ToInteger(int32_t* v, size_type* idx = NULL, int base = 10) const NLIB_NOEXCEPT;
161  errno_t ToInteger(int64_t* v, size_type* idx = NULL, int base = 10) const NLIB_NOEXCEPT;
162  errno_t ToInteger(uint32_t* v, size_type* idx = NULL, int base = 10) const NLIB_NOEXCEPT;
163  errno_t ToInteger(uint64_t* v, size_type* idx = NULL, int base = 10) const NLIB_NOEXCEPT;
164  errno_t ToInteger(int8_t* v, size_type* idx = NULL, int base = 10) const NLIB_NOEXCEPT;
165  errno_t ToInteger(int16_t* v, size_type* idx = NULL, int base = 10) const NLIB_NOEXCEPT;
166  errno_t ToInteger(uint8_t* v, size_type* idx = NULL, int base = 10) const NLIB_NOEXCEPT;
167  errno_t ToInteger(uint16_t* v, size_type* idx = NULL, int base = 10) const NLIB_NOEXCEPT;
168  errno_t ToFloat(float* v, size_type* idx = NULL) const NLIB_NOEXCEPT;
169  errno_t ToDouble(double* v, size_type* idx = NULL) const NLIB_NOEXCEPT;
170 
171  bool TrimLeft() NLIB_NOEXCEPT;
172  bool TrimRight() NLIB_NOEXCEPT;
173  void Trim() NLIB_NOEXCEPT {
174  TrimLeft();
175  TrimRight();
176  }
177  StringView GetLine() NLIB_NOEXCEPT;
178  bool Proceed(const StringView& kwd) NLIB_NOEXCEPT {
179  if (!this->starts_with(kwd)) return false;
180  base_ += kwd.length();
181  len_ -= kwd.length();
182  return true;
183  }
184  bool Proceed(const charT* kwd) NLIB_NOEXCEPT { return this->Proceed(StringView(kwd)); }
185  bool Proceed(charT ch) NLIB_NOEXCEPT;
186  StringView GetName() NLIB_NOEXCEPT;
187  /*
188  size_type find(const StringView& s) const {
189  StringView::iterator it = std::search(
190  begin(), end(), s.begin(), s.end());
191  return it == end() ? npos : it - begin();
192  }
193  size_type find(charT c) const;
194  size_type find(const charT* s) const;
195  size_type rfind(const StringView& s) const {
196  StringView::iterator it = std::find_end(
197  begin(), end(), s.begin(), s.end());
198  return it == end() ? npos : it - begin();
199  }
200  size_type rfind(charT c) const;
201  size_type rfind(const charT* s) const;
202  size_type find_first_of(const StringView& s) const {
203  StringView::iterator it = std::find_first_of(
204  begin(), end(), s.begin(), s.end());
205  return it == end() ? npos : it - begin();
206  }
207  size_type find_first_of(charT c) const;
208  size_type find_first_of(const charT* s) const;
209  size_type find_last_of(const StringView& s) const {
210  StringView::reverse_iterator it = std::find_first_of(
211  rbegin(), rend(), s.begin(), s.end());
212  return it == rend() ? std::string::npos : it.base() - 1 - begin();
213  }
214  size_type find_last_of(charT c) const;
215  size_type find_last_of(const charT* s) const;
216  size_type find_first_not_of(const StringView& s) const;
217  size_type find_first_not_of(charT c) const;
218  size_type find_first_not_of(const charT* s) const;
219  size_type find_last_not_of(const StringView& s) const;
220  size_type find_last_not_of(charT c) const;
221  size_type find_last_not_of(const charT* s) const;
222  */
223 
224  private:
225  bool IsValid_() const NLIB_NOEXCEPT {
226  if (!base_) return false;
227  return true;
228  }
229  size_type len_;
230  const charT* base_;
231 };
232 
233 
234 inline errno_t StringView::ToInteger(int8_t* v, size_type* idx, int base) const NLIB_NOEXCEPT {
235  int32_t tmp;
236  errno_t e = ToInteger(&tmp, idx, base);
237  if (e != 0) return e;
238  if (tmp > 127 || tmp < -128) return ERANGE;
239  *v = static_cast<int8_t>(tmp);
240  return 0;
241 }
242 
243 
244 inline errno_t StringView::ToInteger(int16_t* v, size_type* idx, int base) const NLIB_NOEXCEPT {
245  int32_t tmp;
246  errno_t e = ToInteger(&tmp, idx, base);
247  if (e != 0) return e;
248  if (tmp > 32767 || tmp < -32768) return ERANGE;
249  *v = static_cast<int16_t>(tmp);
250  return 0;
251 }
252 
253 
254 inline errno_t StringView::ToInteger(uint8_t* v, size_type* idx, int base) const NLIB_NOEXCEPT {
255  uint32_t tmp;
256  errno_t e = ToInteger(&tmp, idx, base);
257  if (e != 0) return e;
258  if (tmp > 255) return ERANGE;
259  *v = static_cast<uint8_t>(tmp);
260  return 0;
261 }
262 
263 
264 inline errno_t StringView::ToInteger(uint16_t* v, size_type* idx, int base) const NLIB_NOEXCEPT {
265  uint32_t tmp;
266  errno_t e = ToInteger(&tmp, idx, base);
267  if (e != 0) return e;
268  if (tmp > 65535) return ERANGE;
269  *v = static_cast<uint16_t>(tmp);
270  return 0;
271 }
272 
273 inline bool StringView::Proceed(charT ch) NLIB_NOEXCEPT {
274  if (this->starts_with(ch)) {
275  ++base_;
276  --len_;
277  return true;
278  } else {
279  return false;
280  }
281 }
282 
283 inline StringView StringView::GetName() NLIB_NOEXCEPT {
284  // isalpha()[isalnum()]*
285  NLIB_ASSERT(this->IsValid_());
286  const charT* s = base_;
287  const charT* s_end = base_ + len_;
288  charT c = (s != s_end) ? *s : '\0';
289  if (!nlib_isalpha(c) && c != '_') return *this;
290  for (;;) {
291  ++s;
292  c = (s != s_end) ? *s : '\0';
293  if (!nlib_isalnum(c) && c != '_') break;
294  }
295  size_type len = s - base_;
296  StringView rval(base_, len);
297  base_ = s;
298  len_ = len_ - len;
299  return rval;
300 }
301 
302 // [string.view.comparison], non-member StringView comparison functions
303 inline bool operator==(const StringView& lhs, const StringView& rhs) NLIB_NOEXCEPT {
304  return lhs.compare(rhs) == 0;
305 }
306 inline bool operator!=(const StringView& lhs, const StringView& rhs) NLIB_NOEXCEPT {
307  return !(lhs == rhs);
308 }
309 inline bool operator<(const StringView& lhs, const StringView& rhs) NLIB_NOEXCEPT {
310  return lhs.compare(rhs) < 0;
311 }
312 inline bool operator>(const StringView& lhs, const StringView& rhs) NLIB_NOEXCEPT {
313  return lhs.compare(rhs) > 0;
314 }
315 inline bool operator<=(const StringView& lhs, const StringView& rhs) NLIB_NOEXCEPT {
316  return lhs.compare(rhs) <= 0;
317 }
318 inline bool operator>=(const StringView& lhs, const StringView& rhs) NLIB_NOEXCEPT {
319  return lhs.compare(rhs) >= 0;
320 }
321 
322 NLIB_NAMESPACE_END
323 
324 #endif // INCLUDE_NN_NLIB_STRINGVIEW_H_
void remove_prefix(size_type n) noexcept
Removes the first n characters.
Definition: StringView.h:99
bool ends_with(const StringView &s) const noexcept
Checks whether the string has s as its suffix.
Definition: StringView.h:136
std::reverse_iterator< const_iterator > const_reverse_iterator
A string reverse iterator.
Definition: StringView.h:39
const charT * data() const noexcept
Returns the pointer to the first character.
Definition: StringView.h:92
size_t size_type
A non-negative integer type, currently defined in size_t using typedef.
Definition: StringView.h:41
void clear() noexcept
Sets an empty string.
Definition: StringView.h:95
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:2329
ptrdiff_t difference_type
The type returned when you take the difference between iterators.
Definition: StringView.h:42
bool empty() const noexcept
Checks whether it is an empty string.
Definition: StringView.h:79
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:117
bool ends_with(charT c) const noexcept
Checks whether the string ends with the character specified for c.
Definition: StringView.h:142
const_reverse_iterator crbegin() const noexcept
Returns the reverse iterator pointing to the last character.
Definition: StringView.h:66
size_type length() const noexcept
Returns the length of the string.
Definition: StringView.h:74
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 StringView &lhs, const StringView &rhs) noexcept
Compares strings in dictionary order.
Definition: StringView.h:315
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are equal.
Definition: NMalloc.h:145
Defines that class that is corresponding to std::unique_ptr.
bool operator!=(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are not equal.
Definition: NMalloc.h:150
char charT
Currently a char-type specific class.
Definition: StringView.h:31
bool starts_with(charT c) const noexcept
Checks whether the string begins with the character specified for c.
Definition: StringView.h:132
const charT & operator[](size_type pos) const
Gets the nth character, where n is specified by pos.
Definition: StringView.h:82
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
bool starts_with(const StringView &s) const noexcept
Checks whether the string has s as its prefix.
Definition: StringView.h:127
const charT & back() const
Gets the last character.
Definition: StringView.h:91
bool ends_with(const charT *s) const noexcept
Checks whether the string has s as its suffix.
Definition: StringView.h:143
constexpr StringView() noexcept
Instantiates the object with default parameters (default constructor). Initialized as an empty string...
Definition: StringView.h:46
const_iterator end() const noexcept
Returns the iterator pointing to the character following the last character.
Definition: StringView.h:60
const charT * pointer
The type for a character pointer. The same as const_pointer.
Definition: StringView.h:33
size_type max_size() const noexcept
Returns the maximum value for the string length.
Definition: StringView.h:78
const_iterator cend() const noexcept
Returns the iterator pointing to the character following the last character.
Definition: StringView.h:62
const_iterator iterator
A string iterator. The same as const_iterator.
Definition: StringView.h:38
int compare(const charT *s) const noexcept
Compares strings.
Definition: StringView.h:126
const charT & at(size_type pos) const
Gets the nth character, where n is specified by pos.
Definition: StringView.h:86
bool operator<(const StringView &lhs, const StringView &rhs) noexcept
Compares strings in dictionary order.
Definition: StringView.h:309
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:2326
const_reverse_iterator reverse_iterator
A string reverse iterator. The same as const_reverse_iterator.
Definition: StringView.h:40
bool operator>=(const StringView &lhs, const StringView &rhs) noexcept
Compares strings in dictionary order.
Definition: StringView.h:318
const_iterator begin() const noexcept
Returns the iterator pointing to the first character.
Definition: StringView.h:56
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
const_iterator cbegin() const noexcept
Returns the iterator pointing to the first character.
Definition: StringView.h:61
StringView(const charT *str, size_type len) noexcept
Initializes using the specified calculated string length.
Definition: StringView.h:51
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:93
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:67
const_pointer const_iterator
A character string iterator.
Definition: StringView.h:37
The class for using the member functions of std::string without constructing std::string.
Definition: StringView.h:28
bool starts_with(const charT *s) const noexcept
Checks whether the string has s as its prefix.
Definition: StringView.h:133
bool operator>(const StringView &lhs, const StringView &rhs) noexcept
Compares strings in dictionary order.
Definition: StringView.h:312
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:178
void remove_suffix(size_type n) noexcept
Removes the last n characters.
Definition: StringView.h:108
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:34
const charT & const_reference
A reference to a character.
Definition: StringView.h:36
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:64
bool ToCstring(charT(&str)[N]) const noexcept
Definition: StringView.h:146
charT value_type
The type for a character.
Definition: StringView.h:32
StringView(const charT *str) noexcept
Initialized to reference str. The string length is calculated internally.
Definition: StringView.h:48
const charT & front() const
Gets the first character.
Definition: StringView.h:90
size_type size() const noexcept
Returns the length of the string.
Definition: StringView.h:70
const_reverse_iterator rend() const noexcept
Returns the reverse iterator pointing to the character before the first character.
Definition: StringView.h:65
bool ToCstring(UniquePtr< charT[]> &str) const noexcept
Definition: StringView.h:152
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:184
void Trim() noexcept
Trims the white space from the start and the end of the string.
Definition: StringView.h:173
const charT & reference
A reference to a character. The same as const_reference.
Definition: StringView.h:35
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37