fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <map>
  5. #include <sstream>
  6. #include <string>
  7. using namespace std;
  8.  
  9. template <typename T>
  10. T findNonEscaped(T start, T end, const char ch) {
  11. T result = find(start, end, ch);
  12.  
  13. while (result != end && result[-1] == '\\') result = find(start, end, ch);
  14.  
  15. return result;
  16. }
  17.  
  18. template <typename T>
  19. T extractParenthesis(T start, T end) {
  20. T finish = findNonEscaped(start, end, ')');
  21.  
  22. for (auto i = findNonEscaped(next(start), end, '('); i != end && i < finish; i = findNonEscaped(next(i), end, '(')) finish = findNonEscaped(next(finish), end, ')');
  23. return finish;
  24. }
  25.  
  26. int main() {
  27. const auto input = "data1(\" value 1 \") data2 (\"value 2\") anything3(\" data3(\"value\") \")"s;
  28. map<string, string> output;
  29.  
  30. for (auto openParenthesis = findNonEscaped(input.cbegin(), input.cend(), '('), closeParenthesis = input.cbegin(); openParenthesis != input.cend(); openParenthesis = findNonEscaped(openParenthesis, input.cend(), '(')) {
  31. decltype(output)::key_type key;
  32. istringstream ss{ string{ make_reverse_iterator(openParenthesis), make_reverse_iterator(closeParenthesis) } };
  33.  
  34. ss >> key;
  35. closeParenthesis = extractParenthesis(openParenthesis, input.cend());
  36. output[decltype(output)::key_type{ key.crbegin(), key.crend() }] = decltype(output)::mapped_type{ next(findNonEscaped(next(openParenthesis), closeParenthesis, '"')), prev(findNonEscaped(make_reverse_iterator(closeParenthesis), make_reverse_iterator(next(openParenthesis)), '"').base()) };
  37. openParenthesis = closeParenthesis;
  38. }
  39.  
  40. for (auto& i : output) {
  41. cout << i.first << " : " << i.second << endl;
  42. }
  43. }
Success #stdin #stdout 0s 3420KB
stdin
Standard input is empty
stdout
anything3 :   data3("value") 
data1 :  value 1 
data2 : value 2