16 #ifndef INCLUDE_NN_NLIB_STRINGVIEW_H_ 17 #define INCLUDE_NN_NLIB_STRINGVIEW_H_ 44 static const size_type npos = size_type(-1);
50 NLIB_ASSERT(this->IsValid_());
53 NLIB_ASSERT(this->IsValid_());
58 NLIB_ASSERT(this->IsValid_());
66 const_reverse_iterator
rend() const
NLIB_NOEXCEPT {
return const_reverse_iterator(begin()); }
72 NLIB_ASSERT(this->IsValid_());
76 NLIB_ASSERT(this->IsValid_());
84 NLIB_ASSERT(this->IsValid_());
87 const charT&
at(size_type pos)
const {
88 NLIB_ASSERT(this->IsValid_());
91 const charT&
front()
const {
return (*
this)[0]; }
92 const charT&
back()
const {
return (*
this)[this->length() - 1]; }
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;
129 NLIB_ASSERT(this->IsValid_());
130 if (s.
length() > this->length())
return false;
133 bool starts_with(charT c)
const NLIB_NOEXCEPT {
return front() == c; }
138 NLIB_ASSERT(this->IsValid_());
139 if (s.
length() > len_)
return false;
148 template <
size_type N>
152 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 153 template<
class DUMMY =
void>
157 NLIB_ASSERT(this->IsValid_());
158 str.reset(
new (std::nothrow) charT[len_ + 1]);
159 if (!str)
return false;
162 #ifdef __cpp_rvalue_references 164 NLIB_ASSERT(this->IsValid_());
165 charT* p =
new (std::nothrow) charT[len_ + 1];
166 if (!p)
return nullptr;
191 if (!this->starts_with(kwd))
return false;
192 base_ += kwd.length();
193 len_ -= kwd.length();
237 bool IsValid_()
const NLIB_NOEXCEPT {
238 if (!base_)
return false;
249 if (e != 0)
return e;
250 if (tmp > 127 || tmp < -128)
return ERANGE;
251 *v =
static_cast<int8_t
>(tmp);
259 if (e != 0)
return e;
260 if (tmp > 32767 || tmp < -32768)
return ERANGE;
261 *v =
static_cast<int16_t
>(tmp);
269 if (e != 0)
return e;
270 if (tmp > 255)
return ERANGE;
271 *v =
static_cast<uint8_t
>(tmp);
279 if (e != 0)
return e;
280 if (tmp > 65535)
return ERANGE;
281 *v =
static_cast<uint16_t
>(tmp);
286 if (this->starts_with(ch)) {
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';
304 c = (s != s_end) ? *s :
'\0';
307 size_type len = s - base_;
316 return lhs.compare(rhs) == 0;
320 return lhs.compare(rhs) < 0;
326 if (prefix.length() > str.length())
return false;
327 return nlib_memcmp(prefix.data(), str.data(), prefix.length()) == 0;
334 if (suffix.length() > str.length())
return false;
335 return nlib_memcmp(suffix.data(), str.end() - suffix.length(), suffix.length()) == 0;
342 if (!buf || buf_size == 0)
return false;
343 size_t len = str.length();
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;
365 inline std::pair<errno_t, size_t>
369 return std::make_pair(e, eptr - str.begin());
371 inline std::pair<errno_t, size_t>
375 return std::make_pair(e, eptr - str.begin());
377 inline std::pair<errno_t, size_t>
381 return std::make_pair(e, eptr - str.begin());
383 inline std::pair<errno_t, size_t>
387 return std::make_pair(e, eptr - str.begin());
389 inline std::pair<errno_t, size_t>
393 return std::make_pair(e, eptr - str.begin());
395 inline std::pair<errno_t, size_t>
399 return std::make_pair(e, eptr - str.begin());
401 inline std::pair<errno_t, size_t>
405 return std::make_pair(e, eptr - str.begin());
407 inline std::pair<errno_t, size_t>
411 return std::make_pair(e, eptr - str.begin());
413 inline std::pair<errno_t, size_t>
417 return std::make_pair(e, eptr - str.begin());
419 inline std::pair<errno_t, size_t>
423 return std::make_pair(e, eptr - str.begin());
426 inline std::pair<errno_t, size_t>
433 for (; it != itend; ++it) {
436 ptrdiff_t cnt = std::distance(str.begin(), it);
437 if (cnt == 0)
return false;
438 str.remove_prefix(cnt);
444 for (; it != itend; ++it) {
447 ptrdiff_t cnt = std::distance(str.rbegin(), it);
448 if (cnt == 0)
return false;
449 str.remove_suffix(cnt);
457 const char* beg = str.begin();
458 const char* p =
static_cast<const char*
>(
nlib_memchr(beg,
'\n', str.length()));
461 str.remove_prefix(str.length());
464 size_t width = p - beg;
465 if (width > 0 && *(p - 1) ==
'\r') --width;
467 str.remove_prefix(p - beg + 1);
471 if (!str.starts_with(prefix))
return false;
472 str.remove_prefix(prefix.length());
479 if (!str.starts_with(c))
return false;
480 str.remove_prefix(1);
488 while (++it != it_end) {
492 ptrdiff_t len = std::distance(str.begin(), it);
500 #endif // INCLUDE_NN_NLIB_STRINGVIEW_H_ void remove_prefix(size_type n) noexcept
Removes the first n characters.
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...
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).
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.
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 HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are equal.
Defines that class that is corresponding to std::unique_ptr.
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.
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...
const charT & operator[](size_type pos) const
Gets the nth character, where n is specified by pos.
bool EndsWith(const StringView &str, const char *suffix) noexcept
Returns true if suffix is the suffix of str.
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...
bool TrimRight(StringView &str) noexcept
Removes any white spaces from the end of the 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.
const_reverse_iterator reverse_iterator
A string reverse iterator. The same as const_reverse_iterator.
const_iterator begin() const noexcept
Returns the iterator pointing to the first character.
bool Proceed(StringView &str, char c) noexcept
If str starts with c, it is allowed to go forward by the length of that string.
bool StartsWith(const StringView &str, const char *prefix) noexcept
Returns true if prefix is the prefix of str.
#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.
StringView GetName(StringView &str) noexcept
Obtains a string that is generally handled as a variable name. Its own object ( str) moves to the pos...
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..
bool starts_with(const charT *s) const noexcept
Checks whether the string has s as its prefix.
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.
void Trim(StringView &str) noexcept
Removes any white spaces from the beginning and end of the string.
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.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
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.
bool TrimLeft(StringView &str) noexcept
Removes any white spaces from the beginning of the string.
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.
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...