22 #ifndef TLX_CONTAINER_STRING_VIEW_HEADER 23 #define TLX_CONTAINER_STRING_VIEW_HEADER 79 :
ptr_(str),
size_(str ? std::strlen(str) : 0) { }
91 const std::string::const_iterator&
end) noexcept
97 const_iterator
begin() const noexcept {
return ptr_; }
102 const_reverse_iterator
rbegin() const noexcept {
105 const_reverse_iterator
crbegin() const noexcept {
108 const_reverse_iterator
rend() const noexcept {
111 const_reverse_iterator
crend() const noexcept {
130 const_pointer
data() const noexcept {
return ptr_; }
136 const_reference
at(
size_t pos)
const {
138 throw std::out_of_range(
"StringView::at");
173 explicit operator std::string()
const {
180 size_type
copy(
char* s, size_type n, size_type pos = 0)
const {
182 throw std::out_of_range(
"StringView::copy");
184 std::copy(
data(),
data() + rsize, s);
190 throw std::out_of_range(
"StringView::substr");
199 return cmp != 0 ? cmp
200 : (
size_ == x.size_ ? 0 :
size_ < x.size_ ? -1 : 1);
208 size_type n2)
const {
216 int compare(size_type pos1, size_type n1,
const char* x)
const {
221 size_type pos1, size_type n1,
const char* x, size_type n2)
const {
232 return size_ == other.size_ &&
244 return std::lexicographical_compare(
250 return other.operator < (*this);
255 return !other.operator < (*this);
273 return size_ >= x.size_ && std::equal(
ptr_,
ptr_ + x.size_, x.ptr_);
281 return size_ >= x.size_ &&
295 const_iterator iter =
297 return iter ==
cend() ? npos : std::distance(
cbegin(), iter);
299 size_type
find(
char c, size_type pos = 0) const noexcept {
302 size_type
find(
const char* s, size_type pos, size_type n)
const noexcept {
305 size_type
find(
const char* s, size_type pos = 0) const noexcept {
321 for (
const char* cur =
ptr_ + pos; ; --cur) {
322 if (std::strncmp(cur, s.
ptr_, s.
size_) == 0)
328 size_type
rfind(
char c, size_type pos = npos) const noexcept {
331 size_type
rfind(
const char* s, size_type pos, size_type n)
const noexcept {
334 size_type
rfind(
const char* s, size_type pos = npos) const noexcept {
346 const_iterator iter =
348 return iter ==
cend() ? npos : std::distance(
cbegin(), iter);
354 size_type pos, size_type n)
const noexcept {
372 pos =
size_ - (pos + 1);
373 const_reverse_iterator iter =
381 size_type pos, size_type n)
const noexcept {
384 size_type
find_last_of(
const char* s, size_type pos = npos) const noexcept {
394 size_type pos = 0) const noexcept {
400 return iter ==
cend() ? npos : std::distance(
cbegin(), iter);
406 size_type n)
const noexcept {
410 size_type pos = 0) const noexcept {
420 size_type pos = npos) const noexcept {
425 pos =
size_ - (pos + 1);
433 size_type pos, size_type n)
const noexcept {
437 size_type pos = npos) const noexcept {
450 template <
typename r_iter>
454 return size_ - 1 - std::distance(first, last);
457 template <
typename Iterator>
460 for ( ; first != last; ++first)
461 if (0 == std::char_traits<char>::find(s.ptr_, s.size_, *first))
481 return a.
size() == b.size() &&
482 std::equal(a.begin(), a.end(), b.begin());
488 return a.
size() == b.size() &&
489 std::equal(a.begin(), a.end(), b.begin());
495 return !(a.operator == (b));
501 return !(b.operator == (a));
508 return std::lexicographical_compare(
509 a.begin(), a.end(), b.begin(), b.end());
516 return std::lexicographical_compare(
517 a.begin(), a.end(), b.begin(), b.end());
631 #endif // !TLX_CONTAINER_STRING_VIEW_HEADER bool operator<(const StringView &other) const noexcept
Less operator to compare a StringView with another StringView lexicographically.
const_reverse_iterator reverse_iterator
size_type find_first_not_of(char c, size_type pos=0) const noexcept
const_pointer data() const noexcept
size_type find(char c, size_type pos=0) const noexcept
std::ptrdiff_t difference_type
StringView(const std::string &str) noexcept
assign a whole string
size_type find_first_of(StringView s, size_type pos=0) const noexcept
bool ends_with(char c) const noexcept
size_type find_first_not_of(const char *s, size_type pos=0) const noexcept
size_type rfind(const char *s, size_type pos, size_type n) const noexcept
bool ends_with(StringView x) const noexcept
int compare(StringView x) const noexcept
int compare(size_type pos1, size_type n1, const char *x, size_type n2) const
int compare(const char *x) const
void remove_prefix(size_type n)
static constexpr size_type npos
size_type find_first_of(char c, size_type pos=0) const noexcept
StringView substr(size_type pos, size_type n=npos) const
size_type find_first_not_of(StringView s, size_type pos=0) const noexcept
size_type find_last_of(StringView s, size_type pos=npos) const noexcept
size_type rfind(const char *s, size_type pos=npos) const noexcept
int compare(size_type pos1, size_type n1, const char *x) const
size_type copy(char *s, size_type n, size_type pos=0) const
const_reverse_iterator crend() const noexcept
const_iterator begin() const noexcept
size_type rfind(char c, size_type pos=npos) const noexcept
bool operator>=(const StringView &other) const noexcept
Less than or equal to.
StringView & operator=(const StringView &rhs) noexcept=default
assignment
static uint32_t hash_djb2(const unsigned char *str)
Simple, fast, but "insecure" string hash method by Dan Bernstein from http://www.cse.yorku.ca/~oz/hash.html.
size_type find_last_not_of(const char *s, size_type pos=npos) const noexcept
bool operator<=(const StringView &other) const noexcept
Less than or equal to.
std::string to_string() const
Returns the data of this StringView as a std::string.
const_reference back() const
StringView(const std::string::const_iterator &begin, const std::string::const_iterator &end) noexcept
assign a string between two iterators
size_type reverse_distance(r_iter first, r_iter last) const noexcept
void swap(StringView &s) noexcept
const_iterator cend() const noexcept
const_iterator cbegin() const noexcept
bool starts_with(char c) const noexcept
size_type find(const char *s, size_type pos, size_type n) const noexcept
static uint32_t min(uint32_t x, uint32_t y)
StringView(const std::string::const_iterator &begin, size_t size) noexcept
assign a string iterator with given length
const_iterator end() const noexcept
void remove_suffix(size_type n)
StringView(const char *str, size_type size) noexcept
assign a C-style string with given length
void swap(CountingPtr< A, D > &a1, CountingPtr< A, D > &a2) noexcept
swap enclosed object with another counting pointer (no reference counts need change) ...
size_type length() const noexcept
int compare(size_type pos1, size_type n1, StringView x, size_type pos2, size_type n2) const
size_type find_first_of(const char *s, size_type pos, size_type n) const noexcept
bool operator==(const StringView &other) const noexcept
Equality operator to compare a StringView with another StringView.
size_type rfind(StringView s, size_type pos=npos) const noexcept
size_type size() const noexcept
size_type find_first_not_of(const char *s, size_type pos, size_type n) const noexcept
const_reverse_iterator rbegin() const noexcept
std::reverse_iterator< const_iterator > const_reverse_iterator
StringView is a reference to a part of a string, consisting of only a char pointer and a length...
size_type find_last_of(const char *s, size_type pos, size_type n) const noexcept
bool operator!=(const StringView &other) const noexcept
Inequality operator to compare a StringView with another StringView.
const_reverse_iterator crbegin() const noexcept
const_reverse_iterator rend() const noexcept
bool empty() const noexcept
int compare(size_type pos1, size_type n1, StringView x) const noexcept
const_reference operator[](size_type pos) const noexcept
size_type find_last_not_of(char c, size_type pos=npos) const noexcept
Iterator find_not_of(Iterator first, Iterator last, StringView s) const noexcept
StringView(const char *str) noexcept
assign a whole C-style string
size_type max_size() const noexcept
const char & const_reference
size_type find_last_not_of(const char *s, size_type pos, size_type n) const noexcept
size_type find(const char *s, size_type pos=0) const noexcept
size_type find_last_of(const char *s, size_type pos=npos) const noexcept
StringView() noexcept
default construction
bool operator>(const StringView &other) const noexcept
Greater than.
bool starts_with(StringView x) const noexcept
const_pointer const_iterator
size_type find_last_not_of(StringView s, size_type pos=npos) const noexcept
const_reference at(size_t pos) const
const_reference front() const
size_type find_first_of(const char *s, size_type pos=0) const noexcept
size_type find_last_of(char c, size_type pos=npos) const noexcept
friend std::ostream & operator<<(std::ostream &os, const StringView &v)
const char * const_pointer
size_type find(StringView s, size_type pos=0) const noexcept