Debian Codegraph @

src/include/xgboost/string_view.h

raw symbols SCIP
1 /**
2 * Copyright 2021-2023, XGBoost Contributors
3 */
4 #ifndef XGBOOST_STRING_VIEW_H_
5 #define XGBOOST_STRING_VIEW_H_
6 #include <xgboost/logging.h> // CHECK_LT
7 #include <xgboost/span.h> // Span
8
9 #include <algorithm> // for equal, min
10 #include <cstddef> // for size_t
11 #include <iterator> // for reverse_iterator
12 #include <ostream> // for ostream
13 #include <string> // for char_traits, 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; // NOLINT
25 using iterator = const CharT*; // NOLINT
26 using const_iterator = iterator; // NOLINT
27 using reverse_iterator = std::reverse_iterator<const_iterator>; // NOLINT
28 using const_reverse_iterator = reverse_iterator; // NOLINT
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()} {} // NOLINT
34 constexpr StringView(value_type const* str) // NOLINT
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 { // NOLINT
40 CHECK_LT(p, size_);
41 return str_[p];
42 }
43 [[nodiscard]] constexpr std::size_t size() const { return size_; } // NOLINT
44 [[nodiscard]] constexpr bool empty() const { return size() == 0; } // NOLINT
45 [[nodiscard]] StringView substr(std::size_t beg, std::size_t n) const { // NOLINT
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_; } // NOLINT
51
52 [[nodiscard]] constexpr const_iterator cbegin() const { return str_; } // NOLINT
53 [[nodiscard]] constexpr const_iterator cend() const { return str_ + size(); } // NOLINT
54 [[nodiscard]] constexpr iterator begin() const { return str_; } // NOLINT
55 [[nodiscard]] constexpr iterator end() const { return str_ + size(); } // NOLINT
56
57 [[nodiscard]] const_reverse_iterator rbegin() const noexcept { // NOLINT
58 return const_reverse_iterator(this->end());
59 }
60 [[nodiscard]] const_reverse_iterator crbegin() const noexcept { // NOLINT
61 return const_reverse_iterator(this->end());
62 }
63 [[nodiscard]] const_reverse_iterator rend() const noexcept { // NOLINT
64 return const_reverse_iterator(this->begin());
65 }
66 [[nodiscard]] const_reverse_iterator crend() const noexcept { // NOLINT
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 } // namespace xgboost
96 #endif // XGBOOST_STRING_VIEW_H_