summaryrefslogtreecommitdiff
path: root/fast/util.hh
blob: c3e087e62d421b836013dd9bca87800366474428 (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
30
#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