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
138 throw std::out_of_range(
"StringView::at");
166 std::swap(
ptr_, s.ptr_);
167 std::swap(
size_, s.size_);
173 explicit operator std::string()
const {
182 throw std::out_of_range(
"StringView::copy");
184 std::copy(
data(),
data() + rsize, s);
190 throw std::out_of_range(
"StringView::substr");
198 const int cmp = std::strncmp(
ptr_, x.ptr_, std::min(
size_, x.size_));
199 return cmp != 0 ? cmp
200 : (
size_ == x.size_ ? 0 :
size_ < x.size_ ? -1 : 1);
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_ &&
317 if (pos >
size_ - s.size_)
318 pos =
size_ - s.size_;
321 for (
const char* cur =
ptr_ + pos; ; --cur) {
322 if (std::strncmp(cur, s.ptr_, s.size_) == 0)
372 pos =
size_ - (pos + 1);
374 std::find_first_of(
crbegin() + pos,
crend(), s.cbegin(), s.cend());
425 pos =
size_ - (pos + 1);
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());
623struct hash<
tlx::StringView> {
StringView is a reference to a part of a string, consisting of only a char pointer and a length.
size_type find_first_of(const char *s, size_type pos=0) const noexcept
size_type find_first_not_of(const char *s, size_type pos=0) const noexcept
bool operator==(const StringView &other) const noexcept
Equality operator to compare a StringView with another StringView.
size_type size() const noexcept
const_reference front() const
int compare(size_type pos1, size_type n1, StringView x) const noexcept
void swap(StringView &s) noexcept
size_type rfind(const char *s, size_type pos=npos) const noexcept
size_type find(StringView s, size_type pos=0) const noexcept
size_type find_first_not_of(StringView s, size_type pos=0) const noexcept
const_iterator begin() const noexcept
void remove_suffix(size_type n)
Iterator find_not_of(Iterator first, Iterator last, StringView s) const noexcept
size_type copy(char *s, size_type n, size_type pos=0) const
size_type rfind(StringView s, size_type pos=npos) const noexcept
static constexpr size_type npos
int compare(size_type pos1, size_type n1, const char *x) const
size_type find_last_not_of(StringView s, size_type pos=npos) const noexcept
bool operator>=(const StringView &other) const noexcept
Less than or equal to.
bool empty() const noexcept
bool starts_with(char c) const noexcept
std::reverse_iterator< const_iterator > const_reverse_iterator
size_type find_last_of(const char *s, size_type pos=npos) const noexcept
StringView(const StringView &rhs) noexcept=default
copy construction
size_type find_first_not_of(const char *s, size_type pos, size_type n) const noexcept
size_type find_first_of(const char *s, size_type pos, size_type n) const noexcept
bool operator<=(const StringView &other) const noexcept
Less than or equal to.
const_iterator end() const noexcept
bool ends_with(StringView x) const noexcept
const_reference back() const
size_type find_last_not_of(char c, size_type pos=npos) const noexcept
size_type length() const noexcept
size_type find(const char *s, size_type pos, size_type n) const noexcept
size_type rfind(char c, size_type pos=npos) const noexcept
size_type rfind(const char *s, size_type pos, size_type n) const noexcept
int compare(size_type pos1, size_type n1, const char *x, size_type n2) const
size_type find_first_not_of(char c, size_type pos=0) const noexcept
const_reverse_iterator reverse_iterator
size_type find_first_of(char c, size_type pos=0) const noexcept
size_type find_last_not_of(const char *s, size_type pos, size_type n) const noexcept
StringView(const std::string::const_iterator &begin, size_t size) noexcept
assign a string iterator with given length
int compare(size_type pos1, size_type n1, StringView x, size_type pos2, size_type n2) const
StringView(std::string &&)=delete
constructing a StringView from a temporary string is a bad idea
size_type find(const char *s, size_type pos=0) const noexcept
size_type find_last_not_of(const char *s, size_type pos=npos) const noexcept
size_type find_last_of(const char *s, size_type pos, size_type n) const noexcept
const_reference at(size_t pos) const
size_type reverse_distance(r_iter first, r_iter last) const noexcept
const_iterator cend() const noexcept
bool ends_with(char c) const noexcept
const char & const_reference
const_pointer const_iterator
StringView substr(size_type pos, size_type n=npos) const
std::string to_string() const
Returns the data of this StringView as a std::string.
StringView(const char *str, size_type size) noexcept
assign a C-style string with given length
const_pointer data() const noexcept
size_type find_last_of(StringView s, size_type pos=npos) const noexcept
const_reverse_iterator crbegin() const noexcept
StringView(const std::string &str) noexcept
assign a whole string
const_iterator cbegin() const noexcept
size_type find_first_of(StringView s, size_type pos=0) const noexcept
size_type max_size() const noexcept
StringView & operator=(const StringView &rhs) noexcept=default
assignment
const_reverse_iterator rend() const noexcept
StringView(const std::string::const_iterator &begin, const std::string::const_iterator &end) noexcept
assign a string between two iterators
const_reverse_iterator crend() const noexcept
bool starts_with(StringView x) const noexcept
StringView(const char *str) noexcept
assign a whole C-style string
std::ptrdiff_t difference_type
bool operator<(const StringView &other) const noexcept
Less operator to compare a StringView with another StringView lexicographically.
size_type find_last_of(char c, size_type pos=npos) const noexcept
friend std::ostream & operator<<(std::ostream &os, const StringView &v)
const_reference operator[](size_type pos) const noexcept
const char * const_pointer
StringView() noexcept
default construction
int compare(const char *x) const
bool operator!=(const StringView &other) const noexcept
Inequality operator to compare a StringView with another StringView.
void remove_prefix(size_type n)
int compare(StringView x) const noexcept
bool operator>(const StringView &other) const noexcept
Greater than.
size_type find(char c, size_type pos=0) const noexcept
const_reverse_iterator rbegin() const noexcept
static bool operator>=(const StringView &x, const std::string &y) noexcept
static bool operator<=(const StringView &x, const std::string &y) noexcept
StringView string_view
make alias due to STL similarity
static bool operator==(const StringView &a, const std::string &b) noexcept
equality operator to compare a StringView with a std::string
static bool operator<(const StringView &a, const std::string &b) noexcept
less operator to compare a StringView with a std::string lexicographically
static bool operator!=(const StringView &a, const std::string &b) noexcept
inequality operator to compare a StringView with a std::string
static bool operator>(const StringView &x, const std::string &y) noexcept
static std::uint32_t hash_djb2(const unsigned char *str)
Simple, fast, but "insecure" string hash method by Dan Bernstein from http://www.cse....