| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#ifndef XGBOOST_STRING_VIEW_H_ |
| 5 |
#define XGBOOST_STRING_VIEW_H_ |
| 6 |
#include <xgboost/logging.h> |
| 7 |
#include <xgboost/span.h> |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <cstddef> |
| 11 |
#include <iterator> |
| 12 |
#include <ostream> |
| 13 |
#include <string> |
| 14 |
|
| 15 |
namespace xgboost { |
| 16 |
struct StringView { |
| 17 |
private: |
| 18 |
using CharT = char; |
| 19 |
using Traits = std::char_traits<CharT>; |
| 20 |
CharT const* str_{nullptr}; |
| 21 |
std::size_t size_{0}; |
| 22 |
|
| 23 |
public: |
| 24 |
using value_type = CharT; |
| 25 |
using iterator = const CharT*; |
| 26 |
using const_iterator = iterator; |
| 27 |
using reverse_iterator = std::reverse_iterator<const_iterator>; |
| 28 |
using const_reverse_iterator = reverse_iterator; |
| 29 |
|
| 30 |
public: |
| 31 |
constexpr StringView() = default; |
| 32 |
constexpr StringView(value_type const* str, std::size_t size) : str_{str}, size_{size} {} |
| 33 |
StringView(std::string const& str) : str_{str.c_str()}, size_{str.size()} {} |
| 34 |
constexpr StringView(value_type const* str) |
| 35 |
: str_{str}, size_{str == nullptr ? 0ul : Traits::length(str)} {} |
| 36 |
|
| 37 |
[[nodiscard]] value_type const& operator[](std::size_t p) const { return str_[p]; } |
| 38 |
[[nodiscard]] explicit operator std::string() const { return {this->c_str(), this->size()}; } |
| 39 |
[[nodiscard]] value_type const& at(std::size_t p) const { |
| 40 |
CHECK_LT(p, size_); |
| 41 |
return str_[p]; |
| 42 |
} |
| 43 |
[[nodiscard]] constexpr std::size_t size() const { return size_; } |
| 44 |
[[nodiscard]] constexpr bool empty() const { return size() == 0; } |
| 45 |
[[nodiscard]] StringView substr(std::size_t beg, std::size_t n) const { |
| 46 |
CHECK_LE(beg, size_); |
| 47 |
std::size_t len = std::min(n, size_ - beg); |
| 48 |
return {str_ + beg, len}; |
| 49 |
} |
| 50 |
[[nodiscard]] value_type const* c_str() const { return str_; } |
| 51 |
|
| 52 |
[[nodiscard]] constexpr const_iterator cbegin() const { return str_; } |
| 53 |
[[nodiscard]] constexpr const_iterator cend() const { return str_ + size(); } |
| 54 |
[[nodiscard]] constexpr iterator begin() const { return str_; } |
| 55 |
[[nodiscard]] constexpr iterator end() const { return str_ + size(); } |
| 56 |
|
| 57 |
[[nodiscard]] const_reverse_iterator rbegin() const noexcept { |
| 58 |
return const_reverse_iterator(this->end()); |
| 59 |
} |
| 60 |
[[nodiscard]] const_reverse_iterator crbegin() const noexcept { |
| 61 |
return const_reverse_iterator(this->end()); |
| 62 |
} |
| 63 |
[[nodiscard]] const_reverse_iterator rend() const noexcept { |
| 64 |
return const_reverse_iterator(this->begin()); |
| 65 |
} |
| 66 |
[[nodiscard]] const_reverse_iterator crend() const noexcept { |
| 67 |
return const_reverse_iterator(this->begin()); |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
inline std::ostream& operator<<(std::ostream& os, StringView const v) { |
| 72 |
for (auto c : v) { |
| 73 |
os.put(c); |
| 74 |
} |
| 75 |
return os; |
| 76 |
} |
| 77 |
|
| 78 |
inline bool operator==(StringView l, StringView r) { |
| 79 |
if (l.size() != r.size()) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
return std::equal(l.cbegin(), l.cend(), r.cbegin()); |
| 83 |
} |
| 84 |
|
| 85 |
inline bool operator!=(StringView l, StringView r) { return !(l == r); } |
| 86 |
|
| 87 |
inline bool operator<(StringView l, StringView r) { |
| 88 |
return common::Span<StringView::value_type const>{l.c_str(), l.size()} < |
| 89 |
common::Span<StringView::value_type const>{r.c_str(), r.size()}; |
| 90 |
} |
| 91 |
|
| 92 |
inline bool operator<(std::string const& l, StringView r) { return StringView{l} < r; } |
| 93 |
|
| 94 |
inline bool operator<(StringView l, std::string const& r) { return l < StringView{r}; } |
| 95 |
} |
| 96 |
#endif |