16 #ifndef INCLUDE_NN_NLIB_STRINGVIEW_H_ 17 #define INCLUDE_NN_NLIB_STRINGVIEW_H_ 43 static const size_type npos = size_type(-1);
49 NLIB_ASSERT(this->IsValid_());
52 NLIB_ASSERT(this->IsValid_());
57 NLIB_ASSERT(this->IsValid_());
65 const_reverse_iterator
rend() const
NLIB_NOEXCEPT {
return const_reverse_iterator(begin()); }
71 NLIB_ASSERT(this->IsValid_());
75 NLIB_ASSERT(this->IsValid_());
83 NLIB_ASSERT(this->IsValid_());
86 const charT&
at(size_type pos)
const {
87 NLIB_ASSERT(this->IsValid_());
90 const charT&
front()
const {
return (*
this)[0]; }
91 const charT&
back()
const {
return (*
this)[this->length() - 1]; }
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;
128 NLIB_ASSERT(this->IsValid_());
129 if (s.
length() > this->length())
return false;
132 bool starts_with(charT c)
const NLIB_NOEXCEPT {
return front() == c; }
137 NLIB_ASSERT(this->IsValid_());
138 if (s.
length() > len_)
return false;
142 bool ends_with(charT c)
const NLIB_NOEXCEPT {
return back() == c; }
144 bool ToCstring(charT* str, size_type buf_size)
const NLIB_NOEXCEPT;
145 template <
size_type N>
147 return ToCstring(str, N);
149 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 150 template<
class DUMMY =
void>
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);
179 if (!this->starts_with(kwd))
return false;
181 len_ -= kwd.length();
225 bool IsValid_()
const NLIB_NOEXCEPT {
226 if (!base_)
return false;
234 inline errno_t StringView::ToInteger(int8_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
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);
244 inline errno_t StringView::ToInteger(int16_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
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);
254 inline errno_t StringView::ToInteger(uint8_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
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);
264 inline errno_t StringView::ToInteger(uint16_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
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);
273 inline bool StringView::Proceed(charT ch) NLIB_NOEXCEPT {
274 if (this->starts_with(ch)) {
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';
292 c = (s != s_end) ? *s :
'\0';
295 size_type len = s - base_;
304 return lhs.compare(rhs) == 0;
307 return !(lhs == rhs);
310 return lhs.compare(rhs) < 0;
313 return lhs.compare(rhs) > 0;
316 return lhs.compare(rhs) <= 0;
319 return lhs.compare(rhs) >= 0;
324 #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.