summaryrefslogtreecommitdiff
path: root/fast/util.hh
blob: 2a28f16b154841c02db21c20de88fdf03e2c1e88 (plain)
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
#pragma once

#include <string>

using namespace std;


namespace util {

  inline string
  json_escape(const string& s) { // FIXME: only inline?
    ostringstream os;
    for (auto it = s.cbegin(); it != s.cend(); it++) {
      switch (*it) {
        case '"':  os << "\\\""; break;
        case '\\': os << "\\\\"; break;
        case '\b': os << "\\b";  break;
        case '\f': os << "\\f";  break;
        case '\n': os << "\\n";  break;
        case '\r': os << "\\r";  break;
        case '\t': os << "\\t";  break;
        default:   os << *it;    break;
     }
   }
   return os.str();
 };

} // namespace util