| 1 | #include <cstdlib> |
| 2 | #include <cassert> |
| 3 | #include <string_view> |
| 4 | #include <optional> |
| 5 | |
| 6 | #include <ctre.hpp> |
| 7 | |
| 8 | using namespace std::string_view_literals; |
| 9 | using namespace ctre::literals; |
| 10 | |
| 11 | struct date { std::string_view year; std::string_view month; std::string_view day; }; |
| 12 | |
| 13 | static constexpr ctll::fixed_string pattern = "^([0-9]{4})/([0-9]{1,2}+)/([0-9]{1,2}+)$"; |
| 14 | |
| 15 | constexpr std::optional<date> extract_date(std::string_view s) noexcept { |
| 16 | if (auto [whole, year, month, day] = ctre::match<pattern>(s); whole |
| 17 | ) { |
| 18 | return date{year.to_view(), month.to_view(), day.to_view()}; |
| 19 | } else { |
| 20 | return std::nullopt; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | int main() { |
| 25 | |
| 26 | assert(extract_date("2018/08/27"sv).has_value()); |
| 27 | assert(extract_date("2018/08/27"sv)->year == "2018"sv); |
| 28 | assert(extract_date("2018/08/27"sv)->month == "08"sv); |
| 29 | assert(extract_date("2018/08/27"sv)->day == "27"sv); |
| 30 | |
| 31 | return EXIT_SUCCESS; |
| 32 | } |