summaryrefslogtreecommitdiff
path: root/src/util.hh
blob: 93ea32003bd3639f5546362e858c31f2b2e2f7c4 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once

#include <string>

#include "types.hh"

using namespace std;


namespace util {

inline string
json_escape(const string& s)
{
  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();
}

inline vector<symbol_t>
tokenize(string s)
{
  istringstream ss(s);
  vector<symbol_t> r;
  while (ss.good()) {
    string buf;
    ss >> buf;
    r.push_back(buf);
  }

  return r;
}

} // namespace util