Skip to content

Commit 11edf31

Browse files
author
Hana Dusíková
committed
api for std::data, std::size, and basic comparison
1 parent 1f4be1e commit 11edf31

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

include/ctre/return_type.hpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ template <size_t Id, typename Name = void> struct captured_content {
125125
return to_string();
126126
}
127127

128+
constexpr CTRE_FORCE_INLINE const auto * data() const noexcept {
129+
return &*(begin());
130+
}
128131

129132
constexpr CTRE_FORCE_INLINE const auto & select_by_id(id_tag<Id>) const noexcept {
130133
return *this;
@@ -168,6 +171,19 @@ template <size_t Id, typename Name = void> struct captured_content {
168171
}
169172
}
170173
#endif
174+
175+
constexpr friend CTRE_FORCE_INLINE bool operator==(const storage & lhs, std::basic_string_view<char_type> rhs) noexcept {
176+
return lhs.view() == rhs;
177+
}
178+
constexpr friend CTRE_FORCE_INLINE bool operator!=(const storage & lhs, std::basic_string_view<char_type> rhs) noexcept {
179+
return lhs.view() != rhs;
180+
}
181+
constexpr friend CTRE_FORCE_INLINE bool operator==(std::basic_string_view<char_type> lhs, const storage & rhs) noexcept {
182+
return lhs == rhs.view();
183+
}
184+
constexpr friend CTRE_FORCE_INLINE bool operator!=(std::basic_string_view<char_type> lhs, const storage & rhs) noexcept {
185+
return lhs != rhs.view();
186+
}
171187
};
172188
};
173189

@@ -289,7 +305,7 @@ template <typename Iterator, typename... Captures> class regex_results {
289305
return _captures.template get<Name>();
290306
}
291307

292-
static constexpr size_t size() noexcept {
308+
static constexpr CTRE_FORCE_INLINE size_t count() noexcept {
293309
return sizeof...(Captures) + 1;
294310
}
295311

@@ -327,6 +343,14 @@ template <typename Iterator, typename... Captures> class regex_results {
327343
return _captures.template get<0>().view();
328344
}
329345

346+
constexpr CTRE_FORCE_INLINE const auto * data() const noexcept {
347+
return _captures.template get<0>().data();
348+
}
349+
350+
constexpr CTRE_FORCE_INLINE size_t size() const noexcept {
351+
return _captures.template get<0>().view().size();
352+
}
353+
330354
constexpr CTRE_FORCE_INLINE auto str() const noexcept {
331355
return _captures.template get<0>().to_string();
332356
}
@@ -350,6 +374,18 @@ template <typename Iterator, typename... Captures> class regex_results {
350374
_captures.template get<Id>().set_end(pos).matched();
351375
return *this;
352376
}
377+
constexpr friend CTRE_FORCE_INLINE bool operator==(const regex_results & lhs, std::basic_string_view<char_type> rhs) noexcept {
378+
return lhs.view() == rhs;
379+
}
380+
constexpr friend CTRE_FORCE_INLINE bool operator!=(const regex_results & lhs, std::basic_string_view<char_type> rhs) noexcept {
381+
return lhs.view() != rhs;
382+
}
383+
constexpr friend CTRE_FORCE_INLINE bool operator==(std::basic_string_view<char_type> lhs, const regex_results & rhs) noexcept {
384+
return lhs == rhs.view();
385+
}
386+
constexpr friend CTRE_FORCE_INLINE bool operator!=(std::basic_string_view<char_type> lhs, const regex_results & rhs) noexcept {
387+
return lhs != rhs.view();
388+
}
353389
};
354390

355391
template <typename Iterator, typename... Captures> regex_results(Iterator, ctll::list<Captures...>) -> regex_results<Iterator, Captures...>;
@@ -365,7 +401,7 @@ template <typename Iterator, typename... Captures> regex_results(Iterator, ctll:
365401
#endif
366402

367403
namespace std {
368-
template <typename... Captures> struct tuple_size<ctre::regex_results<Captures...>> : public std::integral_constant<size_t, ctre::regex_results<Captures...>::size()> { };
404+
template <typename... Captures> struct tuple_size<ctre::regex_results<Captures...>> : public std::integral_constant<size_t, ctre::regex_results<Captures...>::count()> { };
369405

370406
template <size_t N, typename... Captures> struct tuple_element<N, ctre::regex_results<Captures...>> {
371407
public:

tests/results.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <ctre.hpp>
2+
#include <iostream>
3+
#include <charconv>
4+
5+
static constexpr auto pattern = ctll::fixed_string("(?<first>[0-9])[0-9]++");
6+
7+
int main() {
8+
using namespace std::string_view_literals;
9+
auto input = "123,456,768"sv;
10+
11+
for (auto match: ctre::range<pattern>(input)) {
12+
13+
if (match == "456") std::cout << "bingo: ";
14+
if (match != "768") std::cout << "bad: ";
15+
16+
if ("456" == match) std::cout << "bingo: ";
17+
if ("768" != match) std::cout << "bad: ";
18+
19+
auto capture = match.get<1>();
20+
21+
if (capture == "456") std::cout << "bingo: ";
22+
if (capture != "768") std::cout << "bad: ";
23+
24+
if ("456" == capture) std::cout << "bingo: ";
25+
if ("768" != capture) std::cout << "bad: ";
26+
27+
int value = 0;
28+
auto [ptr, err] = std::from_chars(std::data(match), std::data(match)+std::size(match), value);
29+
if (err == std::errc{}) {
30+
std::cout << value << "\n";
31+
} else {
32+
std::cout << "error\n";
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)