-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub_rule.h
More file actions
88 lines (75 loc) · 2.16 KB
/
sub_rule.h
File metadata and controls
88 lines (75 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef Sub_rule_h
#define Sub_rule_h
#include <vector>
#include <string>
#include "pcast.h"
class Sub_rule { // Sub rule for testing one single input
private:
bool invert; // Whether to invert final answer (not-transition)
bool wildcard;
std::vector<std::string> single_values;
std::vector<std::string> ranges;
public:
Sub_rule(bool _invert, std::vector<std::string> _single_values, std::vector<std::string> _ranges){
wildcard = false;
invert = _invert;
single_values = _single_values;
ranges = _ranges;
}
Sub_rule(){ // Constructor without arguments means wildcard
wildcard = true;
invert = false;
}
template <typename Input>
bool match(Input input){
bool match = wildcard; // Ends up returning "true" (short circuits) if wildcard, depends on input if not
for(int i = 0; i < single_values.size(); i++){ // Match any single value
match = match || (input == pcast::cast<Input>(single_values.at(i)));
}
for(int i = 0; i < ranges.size(); i += 2){ // Match any range
match = match || (input >= pcast::cast<Input>(ranges.at(i)) && input <= pcast::cast<Input>(ranges.at(i+1)));
}
return (invert ? !match : match); // Return and invert if appropriate
}
#ifdef PMATCH_DEBUG
std::string debug_escape_characters(std::string chars){
for(int i = 0; i < chars.length(); ++i){
switch(chars.at(i)){
case '-':
case ',':
case '\\':
case '^':
chars.insert(i, "\\");
++i;
break;
}
}
return chars;
}
void debug_output_structure(){
if(wildcard){
std::cout << ".";
}
else{
std::cout << "[" << (invert ? "^" : "" );
for(int i = 0; i < single_values.size(); ++i){
std::cout << debug_escape_characters(single_values.at(i));
if(i != single_values.size()-1){
std::cout << ",";
}
}
if(single_values.size() && ranges.size()){
std::cout << ",";
}
for(int i = 0; i < ranges.size(); i += 2){
std::cout << debug_escape_characters(ranges.at(i)) << "-" << debug_escape_characters(ranges.at(i+1));
if(i != ranges.size()-2){
std::cout << ",";
}
}
std::cout << "]";
}
}
#endif
};
#endif