12#include <boost/lexical_cast.hpp>
22 std::vector<std::vector<unsigned int>> numbers;
24 std::vector<std::string> commaseparatedstrings;
25 if (userString.find(
',') != std::string::npos) {
28 if (!commaseparatedstrings.empty()) {
29 std::vector<std::string>::const_iterator citr;
30 for (citr = commaseparatedstrings.begin(); citr != commaseparatedstrings.end(); ++citr) {
31 parse((*citr), numbers);
35 parse(userString, numbers);
48 std::string separators(
"-+:");
50 if (userString.find_first_of(separators) == std::string::npos) {
51 numbers.emplace_back(1,
toUInt(userString));
52 }
else if (
Contains(userString,
'-')) {
55 numbers.emplace_back(
value);
57 }
else if (
Contains(userString,
'+')) {
60 numbers.emplace_back(
value);
62 }
else if (
Contains(userString,
':')) {
63 std::vector<std::vector<unsigned int>> colonseparated =
separateColon(userString);
64 std::vector<std::vector<unsigned int>>::const_iterator citr1;
65 for (citr1 = colonseparated.begin(); citr1 != colonseparated.end(); ++citr1) {
66 numbers.emplace_back((*citr1));
77 std::string::size_type pos = input.find(ch);
78 return (pos != std::string::npos);
96 unsigned int startNum = 0;
97 unsigned int endNum = 0;
98 unsigned int step = 1;
99 std::vector<std::vector<unsigned int>> separatedValues;
100 Tokenize(input,
":", startNum, endNum, step);
101 for (
unsigned int num = startNum; num <= endNum; num += step) {
102 separatedValues.emplace_back(1, num);
105 return separatedValues;
115 const std::string &delimiters) {
116 unsigned int startNum = 0;
117 unsigned int endNum = 0;
118 unsigned int step = 1;
119 std::vector<unsigned int> separatedValues;
120 Tokenize(input, delimiters, startNum, endNum, step);
122 for (
unsigned int num = startNum; num <= endNum; num += step) {
123 separatedValues.emplace_back(num);
125 return separatedValues;
139 unsigned int &end,
unsigned int &step) {
144 throw std::runtime_error(
"Non supported format found in the input string " + input +
145 " Step string should be preceded by :");
158 std::string step_separator;
159 if (tokens.size() == 3) {
160 std::string step = tokens[2];
162 std::string::size_type
index = input.rfind(step);
163 if (
index != std::string::npos) {
164 step_separator = input.substr(
index - 1, 1);
167 return (step_separator ==
":");
180 unsigned int &start,
unsigned int &end,
unsigned int &step) {
181 if (tokens.empty()) {
185 start =
toUInt(tokens.at(0));
186 end =
toUInt(tokens.at(1));
187 if (tokens.size() == 3) {
188 step =
toUInt(tokens.at(2));
191 throw std::runtime_error(
"Invalid Input String: End number " + tokens.at(1) +
192 " can not be lower than start number" + tokens.at(0));
194 if (start + step > end) {
195 throw std::runtime_error(
"Invalid Input String: End number " + tokens.at(1) +
196 " can not be lower than the sum of start number " + tokens.at(0) +
" and step number" +
199 }
catch (std::runtime_error &e) {
200 throw std::runtime_error(e.what());
201 }
catch (std::out_of_range &) {
202 throw std::runtime_error(
"Error when parsing the input string " + input);
211 return boost::lexical_cast<unsigned int>(input);
212 }
catch (boost::bad_lexical_cast &) {
213 throw std::runtime_error(
"Error when parsing the input string " + input);
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
@ TOK_TRIM
remove leading and trailing whitespace from tokens
const TokenVec & asVector()
Returns a vector of tokenized strings.
bool isValidStepSeparator(const std::string &input, const std::vector< std::string > &tokens)
This method checks the separator preceded by the step string is valid colon ':' is valid separator fo...
void Tokenize(const std::string &input, const std::string &delimiter, unsigned int &start, unsigned int &end, unsigned int &step)
This method removes the separator string from the input string and converts the tokens to unisgned in...
std::vector< std::vector< unsigned int > > separateColon(const std::string &input)
separates a given string to vector of vector of numbers using colon as the delimeter
std::vector< std::vector< unsigned int > > parse(const std::string &userString)
parses a given string into a vector of vector of numbers
std::vector< std::string > separateComma(const std::string &)
separate a given string to a vector of comma separated strings
void convertToNumbers(const std::string &input, const std::vector< std::string > &tokens, unsigned int &start, unsigned int &end, unsigned int &step)
converts the parsed tokens to numbers
unsigned int toUInt(const std::string &input)
converts a string to int.
bool Contains(const std::string &input, char ch)
This method checks the input string contains the character ch.
std::vector< unsigned int > separateDelimiters(const std::string &input, const std::string &delimiters)
separate delimiter string from input string and return a vector of numbers created from the separated...