summaryrefslogtreecommitdiff
path: root/fast/util.hh
blob: 9ce19da5efe8f3c6229c17bbcc29d8f2c4d26a80 (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 "weaver.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