fork(4) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <utility>
  5. using namespace std;
  6.  
  7. string string_replace( const string & s, const string & findS, const std::string & replaceS )
  8. {
  9. string result = s;
  10. auto pos = s.find( findS );
  11. if ( pos == string::npos ) {
  12. return result;
  13. }
  14. result.replace( pos, findS.length(), replaceS );
  15. return string_replace( result, findS, replaceS );
  16. }
  17.  
  18. string parse(const string& s) {
  19. static vector< pair< string, string > > patterns = {
  20. { "\\\\" , "\\" },
  21. { "\\n", "\n" },
  22. { "\\r", "\r" },
  23. { "\\t", "\t" },
  24. { "\\\"", "\"" }
  25. };
  26. string result = s;
  27. for ( const auto & p : patterns ) {
  28. result = string_replace( result, p.first, p.second );
  29. }
  30. return result;
  31. }
  32.  
  33. int main() {
  34. string s1 = R"(hello\n\"this is a string with escape sequences\"\n)";
  35. string s2 = "hello\n\"this is a string with escape sequences\"\n";
  36. cout << parse(s1) << endl;
  37. cout << ( parse(s1) == s2 ) << endl;
  38. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
hello
"this is a string with escape sequences"

1