xgboost

string_view.h
Go to the documentation of this file.
1
4 #ifndef XGBOOST_STRING_VIEW_H_
5 #define XGBOOST_STRING_VIEW_H_
6 #include <xgboost/logging.h>
7
8 #include <algorithm>
9 #include <iterator>
10 #include <ostream>
11 #include <string>
12
13 namespace xgboost {
14 struct StringView {
15 private:
16 using CharT = char; // unsigned char
17 using Traits = std::char_traits<CharT>;
18 CharT const* str_{nullptr};
19 size_t size_{0};
20
21 public:
22 using iterator = const CharT*; // NOLINT
23 using const_iterator = iterator; // NOLINT
24 using reverse_iterator = std::reverse_iterator<const_iterator>; // NOLINT
25 using const_reverse_iterator = reverse_iterator; // NOLINT
26
27 public:
28 constexpr StringView() = default;
29 constexpr StringView(CharT const* str, size_t size) : str_{str}, size_{size} {}
30 explicit StringView(std::string const& str) : str_{str.c_str()}, size_{str.size()} {}
31 StringView(CharT const* str) : str_{str}, size_{Traits::length(str)} {} // NOLINT
32
33 CharT const& operator[](size_t p) const { return str_[p]; }
34 CharT const& at(size_t p) const { // NOLINT
35 CHECK_LT(p, size_);
36 return str_[p];
37 }
38 constexpr size_t size() const { return size_; } // NOLINT
39 StringView substr(size_t beg, size_t n) const { // NOLINT
40 CHECK_LE(beg, size_);
41 size_t len = std::min(n, size_ - beg);
42 return {str_ + beg, len};
43 }
44 CharT const* c_str() const { return str_; } // NOLINT
45
46 constexpr CharT const* cbegin() const { return str_; } // NOLINT
47 constexpr CharT const* cend() const { return str_ + size(); } // NOLINT
48 constexpr CharT const* begin() const { return str_; } // NOLINT
49 constexpr CharT const* end() const { return str_ + size(); } // NOLINT
50
51 const_reverse_iterator rbegin() const noexcept { // NOLINT
52 return const_reverse_iterator(this->end());
53 }
54 const_reverse_iterator crbegin() const noexcept { // NOLINT
55 return const_reverse_iterator(this->end());
56 }
57 const_reverse_iterator rend() const noexcept { // NOLINT
58 return const_reverse_iterator(this->begin());
59 }
60 const_reverse_iterator crend() const noexcept { // NOLINT
61 return const_reverse_iterator(this->begin());
62 }
63 };
64
65 inline std::ostream& operator<<(std::ostream& os, StringView const v) {
66 for (auto c : v) {
67 os.put(c);
68 }
69 return os;
70 }
71
72 inline bool operator==(StringView l, StringView r) {
73 if (l.size() != r.size()) {
74 return false;
75 }
76 return std::equal(l.cbegin(), l.cend(), r.cbegin());
77 }
78
79 inline bool operator!=(StringView l, StringView r) { return !(l == r); }
80 } // namespace xgboost
81 #endif // XGBOOST_STRING_VIEW_H_
xgboost::StringView::cend
constexpr CharT const * cend() const
Definition: string_view.h:47
xgboost::StringView::end
constexpr CharT const * end() const
Definition: string_view.h:49
xgboost::StringView::const_iterator
iterator const_iterator
Definition: string_view.h:23
xgboost::StringView::operator[]
CharT const & operator[](size_t p) const
Definition: string_view.h:33
xgboost::StringView::size
constexpr size_t size() const
Definition: string_view.h:38
xgboost::StringView
Definition: string_view.h:14
xgboost::StringView::StringView
StringView(CharT const *str)
Definition: string_view.h:31
xgboost::StringView::reverse_iterator
std::reverse_iterator< const_iterator > reverse_iterator
Definition: string_view.h:24
xgboost::operator<<
std::basic_ostream< E, T > & operator<<(std::basic_ostream< E, T > &os, IntrusivePtr< Y > const &p)
Definition: intrusive_ptr.h:200
xgboost::StringView::iterator
const CharT * iterator
Definition: string_view.h:22
xgboost::StringView::const_reverse_iterator
reverse_iterator const_reverse_iterator
Definition: string_view.h:25
xgboost::StringView::crbegin
const_reverse_iterator crbegin() const noexcept
Definition: string_view.h:54
xgboost::StringView::at
CharT const & at(size_t p) const
Definition: string_view.h:34
xgboost::operator!=
bool operator!=(IntrusivePtr< T > const &x, IntrusivePtr< U > const &y) noexcept
Definition: intrusive_ptr.h:155
xgboost::StringView::begin
constexpr CharT const * begin() const
Definition: string_view.h:48
xgboost::StringView::rbegin
const_reverse_iterator rbegin() const noexcept
Definition: string_view.h:51
xgboost::StringView::substr
StringView substr(size_t beg, size_t n) const
Definition: string_view.h:39
xgboost::StringView::StringView
constexpr StringView()=default
xgboost::StringView::crend
const_reverse_iterator crend() const noexcept
Definition: string_view.h:60
xgboost::StringView::c_str
CharT const * c_str() const
Definition: string_view.h:44
xgboost::StringView::cbegin
constexpr CharT const * cbegin() const
Definition: string_view.h:46
xgboost::StringView::rend
const_reverse_iterator rend() const noexcept
Definition: string_view.h:57
xgboost::StringView::StringView
constexpr StringView(CharT const *str, size_t size)
Definition: string_view.h:29
xgboost::operator==
bool operator==(IntrusivePtr< T > const &x, IntrusivePtr< U > const &y) noexcept
Definition: intrusive_ptr.h:150
xgboost::StringView::StringView
StringView(std::string const &str)
Definition: string_view.h:30
xgboost
namespace of xgboost
Definition: base.h:110