1#pragma once
2
3#include <regex>
4#include <string>
5
6enum common_regex_match_type {
7 COMMON_REGEX_MATCH_TYPE_NONE,
8 COMMON_REGEX_MATCH_TYPE_PARTIAL,
9 COMMON_REGEX_MATCH_TYPE_FULL,
10};
11
12struct common_string_range {
13 size_t begin;
14 size_t end;
15 common_string_range(size_t begin, size_t end) : begin(begin), end(end) {
16 if (begin > end) {
17 throw std::runtime_error("Invalid range");
18 }
19 }
20 // prevent default ctor
21 common_string_range() = delete;
22 bool empty() const {
23 return begin == end;
24 }
25 bool operator==(const common_string_range & other) const {
26 return begin == other.begin && end == other.end;
27 }
28};
29
30struct common_regex_match {
31 common_regex_match_type type = COMMON_REGEX_MATCH_TYPE_NONE;
32 std::vector<common_string_range> groups;
33
34 bool operator==(const common_regex_match & other) const {
35 return type == other.type && groups == other.groups;
36 }
37 bool operator!=(const common_regex_match & other) const {
38 return !(*this == other);
39 }
40};
41
42class common_regex {
43 std::string pattern;
44 std::regex rx;
45 std::regex rx_reversed_partial;
46
47 public:
48 explicit common_regex(const std::string & pattern);
49
50 common_regex_match search(const std::string & input, size_t pos, bool as_match = false) const;
51
52 const std::string & str() const { return pattern; }
53};
54
55// For testing only (pretty print of failures).
56std::string regex_to_reversed_partial_regex(const std::string & pattern);
57