3 #ifndef INCLUDE_NN_NLIB_STRINGVIEW_H_ 4 #define INCLUDE_NN_NLIB_STRINGVIEW_H_ 30 static const size_type npos = size_type(-1);
36 NLIB_ASSERT(this->IsValid_());
39 NLIB_ASSERT(this->IsValid_());
44 NLIB_ASSERT(this->IsValid_());
52 const_reverse_iterator
rend() const
NLIB_NOEXCEPT {
return const_reverse_iterator(begin()); }
58 NLIB_ASSERT(this->IsValid_());
62 NLIB_ASSERT(this->IsValid_());
70 NLIB_ASSERT(this->IsValid_());
73 const charT&
at(size_type pos)
const {
74 NLIB_ASSERT(this->IsValid_());
77 const charT&
front()
const {
return (*
this)[0]; }
78 const charT&
back()
const {
return (*
this)[this->length() - 1]; }
105 NLIB_ASSERT(this->IsValid_());
106 if (pos >= len_)
return StringView(base_ + len_, 0);
107 const charT* new_base = base_ + pos;
108 size_type r =
static_cast<size_type
>(len_ - pos);
109 size_type newLen = r > n ? n : r;
115 NLIB_ASSERT(this->IsValid_());
116 if (s.
length() > this->length())
return false;
119 bool starts_with(charT c)
const NLIB_NOEXCEPT {
return front() == c; }
124 NLIB_ASSERT(this->IsValid_());
125 if (s.
length() > len_)
return false;
129 bool ends_with(charT c)
const NLIB_NOEXCEPT {
return back() == c; }
131 bool ToCstring(charT* str, size_type buf_size)
const NLIB_NOEXCEPT;
132 template <
size_type N>
134 return ToCstring(str, N);
136 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 137 template<
class DUMMY =
void>
141 NLIB_ASSERT(this->IsValid_());
142 str.reset(
new (std::nothrow) charT[len_ + 1]);
143 if (!str)
return false;
144 return ToCstring(str.get(), len_ + 1);
166 if (!this->starts_with(kwd))
return false;
168 len_ -= kwd.length();
212 bool IsValid_()
const NLIB_NOEXCEPT {
213 if (!base_)
return false;
221 inline errno_t StringView::ToInteger(int8_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
223 errno_t e = ToInteger(&tmp, idx, base);
224 if (e != 0)
return e;
225 if (tmp > 127 || tmp < -128)
return ERANGE;
226 *v =
static_cast<int8_t
>(tmp);
231 inline errno_t StringView::ToInteger(int16_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
233 errno_t e = ToInteger(&tmp, idx, base);
234 if (e != 0)
return e;
235 if (tmp > 32767 || tmp < -32768)
return ERANGE;
236 *v =
static_cast<int16_t
>(tmp);
241 inline errno_t StringView::ToInteger(uint8_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
243 errno_t e = ToInteger(&tmp, idx, base);
244 if (e != 0)
return e;
245 if (tmp > 255)
return ERANGE;
246 *v =
static_cast<uint8_t
>(tmp);
251 inline errno_t StringView::ToInteger(uint16_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
253 errno_t e = ToInteger(&tmp, idx, base);
254 if (e != 0)
return e;
255 if (tmp > 65535)
return ERANGE;
256 *v =
static_cast<uint16_t
>(tmp);
260 inline bool StringView::Proceed(charT ch) NLIB_NOEXCEPT {
261 if (this->starts_with(ch)) {
272 NLIB_ASSERT(this->IsValid_());
273 const charT* s = base_;
274 const charT* s_end = base_ + len_;
275 charT c = (s != s_end) ? *s :
'\0';
279 c = (s != s_end) ? *s :
'\0';
282 size_type len = s - base_;
291 return lhs.compare(rhs) == 0;
294 return !(lhs == rhs);
297 return lhs.compare(rhs) < 0;
300 return lhs.compare(rhs) > 0;
303 return lhs.compare(rhs) <= 0;
306 return lhs.compare(rhs) >= 0;
311 #endif // INCLUDE_NN_NLIB_STRINGVIEW_H_ void remove_prefix(size_type n) noexcept
Removes the first n characters.
bool ends_with(const StringView &s) const noexcept
Checks whether the string has s as its suffix.
std::reverse_iterator< const_iterator > const_reverse_iterator
A string reverse iterator.
const charT * data() const noexcept
Returns the pointer to the first character.
size_t size_type
A non-negative integer type, currently defined in size_t using typedef.
void clear() noexcept
Sets an empty string.
ptrdiff_t difference_type
The type returned when you take the difference between iterators.
bool empty() const noexcept
Checks whether it is an empty string.
StringView substr(size_type pos, size_type n=npos) const noexcept
Returns a substring [pos, pos + n).
bool ends_with(charT c) const noexcept
Checks whether the string ends with the character specified for c.
const_reverse_iterator crbegin() const noexcept
Returns the reverse iterator pointing to the last character.
size_type length() const noexcept
Returns the length of the string.
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
bool operator<=(const StringView &lhs, const StringView &rhs) noexcept
Compares strings in dictionary order.
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are equal.
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.
char charT
Currently a char-type specific class.
bool starts_with(charT c) const noexcept
Checks whether the string begins with the character specified for c.
const charT & operator[](size_type pos) const
Gets the nth character, where n is specified by pos.
bool starts_with(const StringView &s) const noexcept
Checks whether the string has s as its prefix.
const charT & back() const
Gets the last character.
bool ends_with(const charT *s) const noexcept
Checks whether the string has s as its suffix.
constexpr StringView() noexcept
Instantiates the object with default parameters (default constructor). Initialized as an empty string...
const_iterator end() const noexcept
Returns the iterator pointing to the character following the last character.
const charT * pointer
The type for a character pointer. The same as const_pointer.
size_type max_size() const noexcept
Returns the maximum value for the string length.
const_iterator cend() const noexcept
Returns the iterator pointing to the character following the last character.
const_iterator iterator
A string iterator. The same as const_iterator.
int compare(const charT *s) const noexcept
Compares strings.
const charT & at(size_type pos) const
Gets the nth character, where n is specified by pos.
bool operator<(const StringView &lhs, const StringView &rhs) noexcept
Compares strings in dictionary order.
const_reverse_iterator reverse_iterator
A string reverse iterator. The same as const_reverse_iterator.
bool operator>=(const StringView &lhs, const StringView &rhs) noexcept
Compares strings in dictionary order.
const_iterator begin() const noexcept
Returns the iterator pointing to the first character.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
const_iterator cbegin() const noexcept
Returns the iterator pointing to the first character.
StringView(const charT *str, size_type len) noexcept
Initializes using the specified calculated string length.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
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.
const_pointer const_iterator
A character string iterator.
The class for using the member functions of std::string without constructing std::string.
bool starts_with(const charT *s) const noexcept
Checks whether the string has s as its prefix.
bool operator>(const StringView &lhs, const StringView &rhs) noexcept
Compares strings in dictionary order.
bool Proceed(const StringView &kwd) noexcept
Advances by the amount of the string length of kwd, providing that the prefix matches kwd...
void remove_suffix(size_type n) noexcept
Removes the last n characters.
const charT * const_pointer
The type for a character pointer.
const charT & const_reference
A reference to a character.
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.
bool ToCstring(charT(&str)[N]) const noexcept
charT value_type
The type for a character.
StringView(const charT *str) noexcept
Initialized to reference str. The string length is calculated internally.
const charT & front() const
Gets the first character.
size_type size() const noexcept
Returns the length of the string.
const_reverse_iterator rend() const noexcept
Returns the reverse iterator pointing to the character before the first character.
bool ToCstring(UniquePtr< charT[]> &str) const noexcept
bool Proceed(const charT *kwd) noexcept
Advances by the amount of the string length of kwd, providing that the prefix matches kwd...
void Trim() noexcept
Trims the white space from the start and the end of the string.
const charT & reference
A reference to a character. The same as const_reference.