Newer
Older
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/UserStringParser.h"
#include <boost/lexical_cast.hpp>
namespace Mantid
{
namespace Kernel
{
///constructor
UserStringParser::UserStringParser()
{
}
///Destructor
UserStringParser::~UserStringParser()
{
}
/**This method parses a given string of numbers and returns a vector of vector of numbers.
*@param userString - the string to parse
*@returns a vector containing vectors of numbers.
*/
std::vector<std::vector<unsigned int> > UserStringParser::parse(const std::string& userString)
{
std::vector<std::vector<unsigned int> > numbers;
//first separate commas
std::vector<std::string> commaseparatedstrings;
if(userString.find(",")!=std::string::npos)
{
commaseparatedstrings=separateComma(userString);
}
if(!commaseparatedstrings.empty())
{
std::vector<std::string>::const_iterator citr;
for(citr=commaseparatedstrings.begin();citr!=commaseparatedstrings.end();++citr)
{
parse((*citr),numbers);
}
}
else
{
parse(userString,numbers);
}
return numbers;
}
/**This method parses a given string of numbers and returns a vector of vector of numbers.
*@param userString - the input string to parse
*@param numbers- a vector containing vectors of numbers.
*/
void UserStringParser::parse(const std::string& userString,
std::vector<std::vector<unsigned int> >& numbers)
{
//look for separators
std::string separators("-+:");
//if input contains no separator string
if(userString.find_first_of(separators)==std::string::npos)
{
numbers.push_back(std::vector<unsigned int>(1,toUInt(userString)));
}
else if(Contains(userString,'-'))
{
std::vector<unsigned int>value =separateDelimiters(userString,"-:");
if(!value.empty())
{
numbers.push_back(value);
}
}
else if (Contains(userString,'+'))
{
std::vector<unsigned int>value =separateDelimiters(userString,"+");
if(!value.empty())
{
numbers.push_back(value);
}
}
else if (Contains(userString,':'))
{
std::vector<std::vector<unsigned int> >colonseparated= separateColon(userString);
std::vector<std::vector<unsigned int> >::const_iterator citr1;
for(citr1=colonseparated.begin();citr1!=colonseparated.end();++citr1)
{
numbers.push_back((*citr1));
}
}
}
/** This method checks input string contains character ch
* @param input - the input string
* @param ch - character ch to search
* @returns - true if the string contains character ch.
*/
bool UserStringParser::Contains(const std::string& input,char ch)
{
std::string::size_type pos = input.find(ch);
return (pos==std::string::npos?false:true);
}
/**This method parses a given string of numbers into comma separated tokens.
*@param input - the string to parse
*@returns a vector containing comma separated tokens.
*/
std::vector<std::string> UserStringParser::separateComma(const std::string& input)
{
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> commasep(",");
std::vector<std::string> commaseparatedvalues;
tokenizer tokens(input,commasep);
for(tokenizer::iterator tokItr=tokens.begin();tokItr!=tokens.end();++tokItr)
{
commaseparatedvalues.push_back(*tokItr);
}
return commaseparatedvalues;
}
/**This method parses a given string of numbers into colon separated tokens.
*@param input - the string to parse
*@returns a vector of vector containing colon separated tokens.
*/
std::vector<std::vector<unsigned int> > UserStringParser::separateColon(const std::string& input)
{
unsigned int startNum=0;
unsigned int endNum=0;
unsigned int step=1;
std::vector<std::vector<unsigned int> > separatedValues;
Tokenize(input,":",startNum,endNum,step);
for(unsigned int num=startNum;num<=endNum;num+=step)
{
separatedValues.push_back(std::vector<unsigned int>(1,num));
}
return separatedValues;
}
/**This method parses a given string of numbers into tokens using the separator character symbol.
*@param input - the string to parse
*@param delimiters - the string used as separator
*@returns a vector of vector containing colon separated tokens.
*/
std::vector<unsigned int> UserStringParser::separateDelimiters(const std::string& input,const std::string& delimiters)
{
unsigned int startNum=0;
unsigned int endNum=0;
unsigned int step=1;
std::vector<unsigned int> separatedValues;
Tokenize(input,delimiters,startNum,endNum,step);
for(unsigned int num=startNum;num<=endNum;num+=step)
{
separatedValues.push_back(num);
}
return separatedValues;
}
/**This method parses a given string of numbers into tokens based on the given separator
*returns numbers corresponding to separated tokens and separators if any
*@param input - the string to parse
*@param delimiter - character to separate
*@param start - a number corresponding to the string before delimiter
*@param end - a number corresponding to the string after delimiter
*@param step - number used to increment from the start num generate the series of numbers .
*/
void UserStringParser::Tokenize(const std::string& input,const std::string& delimiter,
unsigned int& start, unsigned int& end,unsigned int& step)
{
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> seps(delimiter.c_str());
std::vector<std::string> temp;
tokenizer tokens(input,seps);
for(tokenizer::const_iterator tokItr=tokens.begin();tokItr!=tokens.end();++tokItr)
{
temp.push_back(*tokItr);
}
if(temp.empty())
{
return ;
}
//validate the separated tokens
if(!isValidStepSeparator(input,temp))
{
throw std::runtime_error("Non supported format found in the input string "+input+ " Step string should be preceded by :");
}
//convert the parsed string to number
convertToNumbers(input,temp,start,end,step);
}
/**This method checks the separator preceded by the step string is valid
* colon ':' is valid separator for step string
*@param input - input string to validate
*@param tokens - vector containing separated values.
*@returns true if the input is valid
*/
bool UserStringParser::isValidStepSeparator(const std::string& input,std::vector<std::string>& tokens)
{
std::string step_separator;
if(tokens.size()==3)
{
std::string step=tokens[2];
std::string::size_type index=input.rfind(step);
if(index!=std::string::npos)
{
step_separator=input.substr(index-1,1);
}
//step values must be preceded by colon ':'
return(!step_separator.compare(":")?true:false);
}
return true;
}
/**This method checks the input string is valid format
*@param input - input string to validate
*@param tokens - vector containing separated values.
*@param start - start number
*@param end - end number
*@param step - step used for incrementing
*/
void UserStringParser::convertToNumbers(const std::string& input,const std::vector<std::string>& tokens,
unsigned int& start, unsigned int& end, unsigned int& step)
{
if(tokens.empty())
{
return;
}
try
{
start = toUInt(tokens.at(0));
end= toUInt(tokens.at(1));
if(tokens.size()==3)
{
step=toUInt(tokens.at(2));
}
if(end<start)
{
throw std::runtime_error("Invalid Input String: End number "+ tokens.at(1)+" can not be lower than start number"+ tokens.at(0));
}
if(start+step>end)
{
throw std::runtime_error("Invalid Input String: End number "+ tokens.at(1)+ " can not be lower than the sum of start number "+ tokens.at(0) +" and step number" + tokens.at(2));
}
}
catch(std::runtime_error& e)
{
throw std::runtime_error(e.what());
}
catch(std::out_of_range&)
{
throw std::runtime_error("Error when parsing the input string "+input);
}
}
/**This method converts a string to unsigned int
*@param input - the string to parse
*@returns an unsigned number equivalent of the input
*/
unsigned int UserStringParser::toUInt(const std::string& input)
{
try
{
return boost::lexical_cast<unsigned int>(input);
}
catch(boost::bad_lexical_cast&)
{
throw std::runtime_error("Error when parsing the input string "+input);
}
}
}
}