16 #ifndef INCLUDE_NN_NLIB_STRINGVIEW_H_ 17 #define INCLUDE_NN_NLIB_STRINGVIEW_H_ 28 #if __has_include(<string_view>) && (!defined(_MSVC_LANG) || _MSVC_LANG > 201402L) \ 29 && (defined(_MSC_VER) || __cplusplus > 201402L) 30 #include <string_view> 33 typedef std::string_view StringView;
36 #elif __has_include(<experimental/string_view>) 37 #include <experimental/string_view> 40 typedef std::experimental::string_view StringView;
67 NLIB_ASSERT(this->IsValid_());
70 NLIB_ASSERT(this->IsValid_());
75 NLIB_ASSERT(this->IsValid_());
89 NLIB_ASSERT(this->IsValid_());
93 NLIB_ASSERT(this->IsValid_());
101 NLIB_ASSERT(this->IsValid_());
105 NLIB_ASSERT(this->IsValid_());
109 const charT&
back()
const {
return (*
this)[this->length() - 1]; }
136 NLIB_ASSERT(this->IsValid_());
137 if (pos >= len_)
return StringView(base_ + len_, 0);
138 const charT* new_base = base_ + pos;
147 return it == end() ? npos : it - begin();
149 size_type find(charT c)
const {
return find(StringView(&c, 1)); }
150 size_type find(
const charT* s)
const {
return find(StringView(s,
nlib_strlen(s))); }
151 size_type rfind(
const StringView& s)
const {
152 StringView::iterator it = std::find_end(begin(), end(), s.begin(), s.end());
153 return it == end() ? npos : it - begin();
155 size_type rfind(charT c)
const {
return rfind(StringView(&c, 1)); }
156 size_type rfind(
const charT* s)
const {
return rfind(StringView(s,
nlib_strlen(s))); }
157 size_type find_first_of(
const StringView& s)
const {
158 StringView::iterator it = std::find_first_of(begin(), end(), s.begin(), s.end());
159 return it == end() ? npos : it - begin();
161 size_type find_first_of(charT c)
const {
return find_first_of(StringView(&c, 1)); }
162 size_type find_first_of(
const charT* s)
const {
163 return find_first_of(StringView(s,
nlib_strlen(s)));
165 size_type find_last_of(
const StringView& s)
const {
166 StringView::reverse_iterator it = std::find_first_of(rbegin(), rend(), s.begin(), s.end());
167 return it == rend() ? std::string::npos : it.base() - 1 - begin();
169 size_type find_last_of(charT c)
const {
return find_last_of(StringView(&c, 1)); }
170 size_type find_last_of(
const charT* s)
const {
171 return find_last_of(StringView(s,
nlib_strlen(s)));
184 if (!base_)
return false;
193 return lhs.compare(rhs) == 0;
195 NLIB_EQUAL_OPERATOR(StringView)
197 return lhs.compare(rhs) < 0;
199 NLIB_COMPARE_OPERATOR(StringView)
208 if (prefix.length() > str.length())
return false;
209 return nlib_memcmp(prefix.data(), str.data(), prefix.length()) == 0;
212 return str.front() == c;
218 if (suffix.length() > str.length())
return false;
219 return nlib_memcmp(suffix.data(), &*str.end() - suffix.length(), suffix.length()) == 0;
222 return str.back() == c;
228 if (!buf || buf_size == 0)
return false;
229 size_t len = str.length();
230 errno_t e = nlib_memcpy(buf, buf_size - 1, str.data(), len);
242 #ifdef __cpp_rvalue_references 244 size_t buf_size = str.length() + 1;
245 char* p =
new (std::nothrow)
char[buf_size];
246 if (!p)
return nullptr;
251 inline std::pair<errno_t, size_t>
255 return std::make_pair(e, eptr - str.data());
257 inline std::pair<errno_t, size_t>
261 return std::make_pair(e, eptr - str.data());
263 inline std::pair<errno_t, size_t>
267 return std::make_pair(e, eptr - str.data());
269 inline std::pair<errno_t, size_t>
273 return std::make_pair(e, eptr - str.data());
275 inline std::pair<errno_t, size_t>
279 return std::make_pair(e, eptr - str.data());
281 inline std::pair<errno_t, size_t>
285 return std::make_pair(e, eptr - str.data());
287 inline std::pair<errno_t, size_t>
291 return std::make_pair(e, eptr - str.data());
293 inline std::pair<errno_t, size_t>
297 return std::make_pair(e, eptr - str.data());
302 return std::make_pair(e, eptr - str.data());
307 return std::make_pair(e, eptr - str.data());
316 for (; it != itend; ++it) {
319 ptrdiff_t cnt = std::distance(str.begin(), it);
320 if (cnt == 0)
return false;
321 str.remove_prefix(cnt);
327 for (; it != itend; ++it) {
330 ptrdiff_t cnt = std::distance(str.rbegin(), it);
331 if (cnt == 0)
return false;
332 str.remove_suffix(cnt);
340 const char* beg = str.data();
341 const char* p =
static_cast<const char*
>(
nlib_memchr(beg,
'\n', str.length()));
344 str.remove_prefix(str.length());
347 size_t width = p - beg;
348 if (width > 0 && *(p - 1) ==
'\r') --width;
350 str.remove_prefix(p - beg + 1);
355 str.remove_prefix(prefix.length());
363 str.remove_prefix(1);
371 while (++it != it_end) {
375 ptrdiff_t len = std::distance(str.begin(), it);
383 #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...
std::reverse_iterator< const_iterator > const_reverse_iterator
Read-only 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
Returns true if it is an empty string, otherwise returns false.
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.
const_reverse_iterator crbegin() const noexcept
Gets the read-only reverse iterator pointing to the last element.
size_type length() const noexcept
Returns the length of the string.
In the C++11 environment (which supports alias templates), std::unique_ptr is made an alias template...
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.
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.
const charT & back() const
Gets a reference to the last element.
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
Gets the read-only iterator pointing beyond the last element.
const charT * 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
Gets the read-only iterator pointing beyond the last element.
const_iterator 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
The same as const_reverse_iterator
const_iterator begin() const noexcept
Gets the read-only iterator pointing to the first element.
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
Gets the read-only iterator pointing to the first element.
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
Gets the read-only reverse iterator pointing ahead of the first element.
const_pointer const_iterator
Read-only random-access 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..
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.
void remove_suffix(size_type n) noexcept
Removes the last n characters.
const charT * const_pointer
Read-only pointer to an element.
const charT & const_reference
Read-only reference to an element.
Wraps functions like strlen and strcpy so they can be safely used.
const_reverse_iterator rbegin() const noexcept
Gets the read-only reverse iterator pointing to the last element.
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 a reference to the first element.
size_type size() const noexcept
Returns the length of the string.
const_reverse_iterator rend() const noexcept
Gets the read-only reverse iterator pointing ahead of the first element.
const charT & reference
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...