sisi4s
Loading...
Searching...
No Matches
RangeParser.hpp
Go to the documentation of this file.
1#ifndef RANGE_PARSER_DEFINED
2#define RANGE_PARSER_DEFINED
3
4namespace sisi4s {
5
11public:
16 RangeParser(const std::string &rawRange_)
17 : rawRange(rawRange_) {
18 parse();
19 }
23 std::vector<int> getRange() const { return parsedRange; }
24
30 std::vector<int> atomicRangeToRange(const std::string &atomicRange) {
31 std::vector<int> range;
32 int low(0), high(0);
33 int dash_pos(atomicRange.find("-"));
34 if (dash_pos == -1) {
35 low = std::stoi(atomicRange);
36 high = low;
37 } else {
38 low = std::stoi(atomicRange.substr(0, dash_pos));
39 high = std::stoi(atomicRange.substr(dash_pos + 1,
40 atomicRange.length() - dash_pos - 1));
41 }
42#ifdef DEBUG
43 LOG(1, "Parser") << "Low " << low << std::endl;
44 LOG(1, "Parser") << "high " << high << std::endl;
45#endif
46 for (int i(std::min(low, high)); i <= std::max(low, high); i++) {
47 range.push_back(i);
48 }
49 return range;
50 }
51
59 void parse() {
60 std::string buff("");
61 char comma = ',', space = ' ';
62 for (unsigned int i(0); i < rawRange.length(); i++) {
63 if (rawRange[i] == comma) {
64 } else if (rawRange[i] == space) {
65 continue;
66 } else {
67 buff.append(rawRange, i, 1);
68 if (i != rawRange.length() - 1) { continue; }
69 }
70 // Parse the numbers here
71 auto tempRange(atomicRangeToRange(buff));
72 parsedRange.insert(parsedRange.end(), tempRange.begin(), tempRange.end());
73 buff.clear();
74 }
75 }
76
77protected:
78 const std::string rawRange;
79 std::vector<int> parsedRange;
80};
81
82inline std::ostream &operator<<(std::ostream &stream,
83 const RangeParser &parser) {
84 for (const auto &i : parser.getRange()) stream << i << " ";
85 return stream;
86}
87
88} // namespace sisi4s
89
90#endif
std::ostream & operator<<(std::ostream &s, const FcidumpReader::FcidumpHeader &h)
Definition FcidumpWriter.cxx:25
#define LOG(...)
Definition Log.hpp:119
Class to parse a string of comma separated range delimiters. Note: It only works for unsigned integer...
Definition RangeParser.hpp:10
std::vector< int > getRange() const
Get the range in the form of a vector of integers.
Definition RangeParser.hpp:23
RangeParser(const std::string &rawRange_)
Constructor from a string, it will parse the string automatically.
Definition RangeParser.hpp:16
void parse()
Main parser method, it will parse a comma separated string of range delimiters.
Definition RangeParser.hpp:59
const std::string rawRange
Definition RangeParser.hpp:78
std::vector< int > parsedRange
Definition RangeParser.hpp:79
std::vector< int > atomicRangeToRange(const std::string &atomicRange)
Parse individual range atoms. For example this method will map "2" into std::vector<int>{2} and "2-3"...
Definition RangeParser.hpp:30
Definition Algorithm.hpp:10