|
12 | 12 |
|
13 | 13 | namespace OS2DSRules {
|
14 | 14 |
|
15 |
| - namespace CPRDetector { |
16 |
| - |
17 |
| - using Predicate = std::function<bool(char)>; |
18 |
| - |
19 |
| - template<typename T> |
20 |
| - requires std::same_as<T, char> |
21 |
| - constexpr Predicate make_predicate(T d) noexcept { |
22 |
| - return [d](T c) { return c == d; }; |
23 |
| - } |
24 |
| - |
25 |
| - template<typename T, typename... Args> |
26 |
| - constexpr Predicate make_predicate(T d, Args... ds) noexcept { |
27 |
| - if constexpr(sizeof...(ds) > 0) { |
28 |
| - return [d, ds...](T c) { |
29 |
| - return make_predicate(d)(c) || make_predicate(ds...)(c); |
30 |
| - }; |
31 |
| - } else |
32 |
| - return make_predicate(d); |
33 |
| - } |
34 |
| - |
35 |
| - constexpr bool is_nonzero_digit(char c) noexcept { |
36 |
| - return '0' < c && c <= '9'; |
37 |
| - } |
38 |
| - |
39 |
| - constexpr bool is_digit(char c) noexcept { |
40 |
| - return '0' <= c && c <= '9'; |
41 |
| - } |
42 |
| - |
43 |
| - const auto is_separator = make_predicate(' ', '-', '/'); |
44 |
| - |
45 |
| - const auto is_previous_ok = make_predicate(char(0), ' ', '\n', '\t', '\0'); |
46 |
| - |
47 |
| - constexpr bool is_space(const char c) noexcept { |
48 |
| - return c == ' '; |
49 |
| - } |
50 |
| - |
51 |
| - const int modulus11_factors[] = {4, 3, 2, 7, 6, 5, 4, 3, 2, 1}; |
52 |
| - |
53 |
| - class CPRDetector { |
54 |
| - private: |
55 |
| - enum class CPRDetectorState : unsigned char { |
56 |
| - Empty, |
57 |
| - First, |
58 |
| - Second, |
59 |
| - Third, |
60 |
| - Fourth, |
61 |
| - Fifth, |
62 |
| - Sixth, |
63 |
| - Seventh, |
64 |
| - Eighth, |
65 |
| - Match, |
66 |
| - }; |
67 |
| - |
68 |
| - bool check_mod11_; |
69 |
| - bool examine_context_; |
70 |
| - void reset(CPRDetectorState& state) noexcept; |
71 |
| - char update(char, CPRDetectorState, CPRDetectorState&, Predicate) noexcept; |
72 |
| - bool check_day_month(const std::string&, CPRDetectorState&) noexcept; |
73 |
| - void check_leap_year(const std::string&, CPRDetectorState&) noexcept; |
74 |
| - void check_and_append_cpr(std::string&, MatchResults&, size_t, size_t) noexcept; |
75 |
| - bool check_mod11(const MatchResult&) noexcept; |
76 |
| - |
77 |
| - public: |
78 |
| - constexpr CPRDetector(bool check_mod11 = false, bool examine_context = false) noexcept |
79 |
| - : check_mod11_(check_mod11), examine_context_(examine_context) {} |
80 |
| - |
81 |
| - constexpr CPRDetector(const CPRDetector&) noexcept; |
82 |
| - constexpr CPRDetector& operator=(const CPRDetector&) noexcept; |
83 |
| - |
84 |
| - constexpr CPRDetector(CPRDetector&&) noexcept; |
85 |
| - constexpr CPRDetector& operator=(CPRDetector&&) noexcept; |
86 |
| - ~CPRDetector() = default; |
87 |
| - |
88 |
| - MatchResults find_matches(const std::string&) noexcept; |
89 |
| - |
90 |
| - static const Sensitivity sensitivity = Sensitivity::Critical; |
| 15 | +namespace CPRDetector { |
| 16 | + |
| 17 | +using Predicate = std::function<bool(char)>; |
| 18 | + |
| 19 | +template <typename T> |
| 20 | + requires std::same_as<T, char> |
| 21 | +constexpr Predicate make_predicate(T d) noexcept { |
| 22 | + return [d](T c) { return c == d; }; |
| 23 | +} |
| 24 | + |
| 25 | +template <typename T, typename... Args> |
| 26 | +constexpr Predicate make_predicate(T d, Args... ds) noexcept { |
| 27 | + if constexpr (sizeof...(ds) > 0) { |
| 28 | + return [d, ds...](T c) { |
| 29 | + return make_predicate(d)(c) || make_predicate(ds...)(c); |
91 | 30 | };
|
92 |
| - }; // namespace CPRDetector |
| 31 | + } else |
| 32 | + return make_predicate(d); |
| 33 | +} |
| 34 | + |
| 35 | +constexpr bool is_nonzero_digit(char c) noexcept { return '0' < c && c <= '9'; } |
| 36 | + |
| 37 | +constexpr bool is_digit(char c) noexcept { return '0' <= c && c <= '9'; } |
| 38 | + |
| 39 | +const auto is_separator = make_predicate(' ', '-', '/'); |
| 40 | + |
| 41 | +const auto is_previous_ok = make_predicate(char(0), ' ', '\n', '\t', '\0'); |
| 42 | + |
| 43 | +constexpr bool is_space(const char c) noexcept { return c == ' '; } |
| 44 | + |
| 45 | +const int modulus11_factors[] = {4, 3, 2, 7, 6, 5, 4, 3, 2, 1}; |
| 46 | + |
| 47 | +class CPRDetector { |
| 48 | +private: |
| 49 | + enum class CPRDetectorState : unsigned char { |
| 50 | + Empty, |
| 51 | + First, |
| 52 | + Second, |
| 53 | + Third, |
| 54 | + Fourth, |
| 55 | + Fifth, |
| 56 | + Sixth, |
| 57 | + Seventh, |
| 58 | + Eighth, |
| 59 | + Match, |
| 60 | + }; |
| 61 | + |
| 62 | + bool check_mod11_; |
| 63 | + bool examine_context_; |
| 64 | + void reset(CPRDetectorState &state) noexcept; |
| 65 | + char update(char, CPRDetectorState, CPRDetectorState &, Predicate) noexcept; |
| 66 | + bool check_day_month(const std::string &, CPRDetectorState &) noexcept; |
| 67 | + void check_leap_year(const std::string &, CPRDetectorState &) noexcept; |
| 68 | + void check_and_append_cpr(std::string &, MatchResults &, size_t, |
| 69 | + size_t) noexcept; |
| 70 | + bool check_mod11(const MatchResult &) noexcept; |
| 71 | + |
| 72 | +public: |
| 73 | + constexpr CPRDetector(bool check_mod11 = false, |
| 74 | + bool examine_context = false) noexcept |
| 75 | + : check_mod11_(check_mod11), examine_context_(examine_context) {} |
| 76 | + |
| 77 | + constexpr CPRDetector(const CPRDetector &) noexcept; |
| 78 | + constexpr CPRDetector &operator=(const CPRDetector &) noexcept; |
| 79 | + |
| 80 | + constexpr CPRDetector(CPRDetector &&) noexcept; |
| 81 | + constexpr CPRDetector &operator=(CPRDetector &&) noexcept; |
| 82 | + ~CPRDetector() = default; |
| 83 | + |
| 84 | + MatchResults find_matches(const std::string &) noexcept; |
| 85 | + |
| 86 | + static const Sensitivity sensitivity = Sensitivity::Critical; |
| 87 | +}; |
93 | 88 |
|
| 89 | +}; // namespace CPRDetector |
94 | 90 | }; // namespace OS2DSRules
|
95 | 91 |
|
96 | 92 | #endif
|
0 commit comments