Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef set<int> result_type;
- template <typename T, typename F>
- T ConvertTo(F f)
- {
- std::stringstream st;
- st << f;
- T t;
- st >> t;
- return t;
- }
- result_type parse(const string &rsSource)
- {
- result_type xResult;
- string::size_type i = 0, iSourceSize = rsSource.size();
- int iRangeBegin;
- enum
- {
- wmSingle,
- wmRange
- } eWorkMode = wmSingle;
- while (i < uiSourceSize)
- {
- string::size_type j = i;
- for ( ; (i < uiSourceSize) && (rsSource[i] >= '0') && (rsSource[i] <= '9') ; i++ ) {} // this is intent
- if (i == uiSourceSize || rsSource[i] == ',')
- {
- if (eWorkMode == wmSingle)
- xResult.insert(ConvertTo<int>(rsSource.substr(j, i-j)));
- else
- {
- int iRangeEnd = ConvertTo<int>(rsSource.substr(j, i-j));
- for (int n = iRangeBegin; n <= iRangeEnd; n++)
- xResult.insert(n);
- }
- }
- else if (rsSource[i] == '-')
- {
- eWorkMode = wmRange;
- iRangeBegin = ConvertTo<int>(rsSource.substr(j, i-j));
- }
- }
- if (eWorkMode == wmRange)
- return result_type(); // what shit is it?!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement