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 >= m_Len)
return StringView(m_Base + m_Len, 0);
107 const charT* newBase = m_Base + pos;
108 size_type r =
static_cast<size_type
>(m_Len - pos);
109 size_type newLen = r > n ? n : r;
115 NLIB_ASSERT(this->IsValid_());
116 if (s.length() > this->length())
return false;
117 return nlib_memcmp(s.data(), m_Base,
sizeof(*m_Base) * s.length()) == 0;
119 bool starts_with(charT c)
const NLIB_NOEXCEPT {
return front() == c; }
124 NLIB_ASSERT(this->IsValid_());
125 if (s.length() > m_Len)
return false;
126 return nlib_memcmp(s.data(), m_Base + m_Len - s.length(),
sizeof(*m_Base) * s.length()) ==
129 bool ends_with(charT c)
const NLIB_NOEXCEPT {
return back() == c; }
133 template <
size_type N>
135 return ToCstring(str, N);
140 int base = 10) const NLIB_NOEXCEPT;
142 int base = 10) const NLIB_NOEXCEPT;
144 int base = 10) const NLIB_NOEXCEPT;
146 int base = 10) const NLIB_NOEXCEPT;
147 errno_t ToInteger(int8_t* v, size_type* idx = NULL,
int base = 10) const NLIB_NOEXCEPT;
148 errno_t ToInteger(int16_t* v, size_type* idx = NULL,
int base = 10) const NLIB_NOEXCEPT;
149 errno_t ToInteger(uint8_t* v, size_type* idx = NULL,
int base = 10) const NLIB_NOEXCEPT;
150 errno_t ToInteger(uint16_t* v, size_type* idx = NULL,
int base = 10) const NLIB_NOEXCEPT;
156 void Trim() NLIB_NOEXCEPT {
162 if (!this->starts_with(kwd))
return false;
164 m_Len -= kwd.length();
208 bool IsValid_() const NLIB_NOEXCEPT {
209 if (!m_Base)
return false;
216 inline errno_t StringView::ToInteger(int8_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
218 errno_t e = ToInteger(&tmp, idx, base);
219 if (e != 0)
return e;
220 if (tmp > 127 || tmp < -128)
return ERANGE;
221 *v =
static_cast<int8_t
>(tmp);
225 inline errno_t StringView::ToInteger(int16_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
227 errno_t e = ToInteger(&tmp, idx, base);
228 if (e != 0)
return e;
229 if (tmp > 32767 || tmp < -32768)
return ERANGE;
230 *v =
static_cast<int16_t
>(tmp);
234 inline errno_t StringView::ToInteger(uint8_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 > 255)
return ERANGE;
239 *v =
static_cast<uint8_t
>(tmp);
243 inline errno_t StringView::ToInteger(uint16_t* v, size_type* idx,
int base)
const NLIB_NOEXCEPT {
245 errno_t e = ToInteger(&tmp, idx, base);
246 if (e != 0)
return e;
247 if (tmp > 65535)
return ERANGE;
248 *v =
static_cast<uint16_t
>(tmp);
252 inline bool StringView::Proceed(charT ch) NLIB_NOEXCEPT {
253 if (this->starts_with(ch)) {
264 NLIB_ASSERT(this->IsValid_());
265 const charT* s = m_Base;
266 const charT* s_end = m_Base + m_Len;
267 charT c = (s != s_end) ? *s :
'\0';
268 if (!
IsAlpha(c) && c !=
'_')
return *
this;
271 c = (s != s_end) ? *s :
'\0';
272 if (!
IsAlnum(c) && c !=
'_')
break;
274 size_type len = s - m_Base;
283 return lhs.compare(rhs) == 0;
286 return !(lhs == rhs);
289 return lhs.compare(rhs) < 0;
292 return lhs.compare(rhs) > 0;
295 return lhs.compare(rhs) <= 0;
298 return lhs.compare(rhs) >= 0;
303 #endif // INCLUDE_NN_NLIB_STRINGVIEW_H_
void remove_prefix(size_type n) noexcept
Removes the first n characters.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
std::reverse_iterator< const_iterator > const_reverse_iterator
A string reverse iterator.
size_type max_size() const noexcept
Returns the maximum value for the string length.
size_t size_type
A non-negative integer type, currently defined in size_t using typedef.
const_iterator end() const noexcept
Returns the iterator pointing to the character following the last character.
void clear() noexcept
Sets an empty string.
ptrdiff_t difference_type
The type returned when you take the difference between iterators.
const_iterator cbegin() const noexcept
Returns the iterator pointing to the first character.
const charT & front() const
Gets the first character.
const_reverse_iterator crend() const noexcept
Returns the reverse iterator pointing to the character before the first character.
bool starts_with(const StringView &s) const noexcept
Checks whether the string has s as its prefix.
size_type length() const noexcept
Returns the length of the string.
int IsAlnum(int c) noexcept
Returns std::isalnum(static_cast(c)).
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.
const charT & back() const
Gets the last character.
constexpr StringView() noexcept
Instantiates the object with default parameters (default constructor). Initialized as an empty string...
const charT & operator[](size_type pos) const
Gets the nth character, where n is specified by pos.
bool empty() const noexcept
Checks whether it is an empty string.
int IsAlpha(int c) noexcept
Returns std::isalpha(static_cast(c)).
const charT * pointer
The type for a character pointer. The same as const_pointer.
const_iterator iterator
A string iterator. The same as const_iterator.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
bool operator<(const StringView &lhs, const StringView &rhs) noexcept
Compares strings in dictionary order.
const charT & at(size_type pos) const
Gets the nth character, where n is specified by pos.
bool ToCstring(charT(&str)[N]) const noexcept
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_reverse_iterator crbegin() const noexcept
Returns the reverse iterator pointing to the last character.
StringView(const charT *str, size_type len) noexcept
Initializes using the specified calculated string length.
A file that contains the configuration information for each development environment.
int compare(const charT *s) const noexcept
Compares strings.
StringView substr(size_type pos, size_type n=npos) const noexcept
Returns a substring [pos, pos + n).
const_pointer const_iterator
A character string iterator.
const charT * data() const noexcept
Returns the pointer to the first character.
The class for using the member functions of std::string without constructing std::string.
size_type size() const noexcept
Returns the length of the string.
bool operator>(const StringView &lhs, const StringView &rhs) noexcept
Compares strings in dictionary order.
const_reverse_iterator rbegin() const noexcept
Returns the reverse iterator pointing to the last character.
void remove_suffix(size_type n) noexcept
Removes the last n characters.
const_iterator begin() const noexcept
Returns the iterator pointing to the first character.
const charT * const_pointer
The type for a character pointer.
const charT & const_reference
A reference to a character.
bool starts_with(charT c) const noexcept
Checks whether the string begins with the character specified for c.
Wraps functions like strlen and strcpy so they can be safely used.
bool ends_with(charT c) const noexcept
Checks whether the string ends with the character specified for c.
const_reverse_iterator rend() const noexcept
Returns the reverse iterator pointing to the character before the first character.
bool ends_with(const charT *s) const noexcept
Checks whether the string has s as its suffix.
charT value_type
The type for a character.
StringView(const charT *str) noexcept
Initialized to reference str. The string length is calculated internally.
const_iterator cend() const noexcept
Returns the iterator pointing to the character following the last character.
bool starts_with(const charT *s) const noexcept
Checks whether the string has s as its prefix.
bool ends_with(const StringView &s) const noexcept
Checks whether the string has s as its suffix.
bool Proceed(const charT *kwd) noexcept
Advances by the amount of the string length of kwd, providing that the prefix matches kwd...
const charT & reference
A reference to a character. The same as const_reference.