diff options
author | Patrick Simianer <p@simianer.de> | 2014-09-16 10:23:14 +0100 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-09-16 10:23:14 +0100 |
commit | 129a22cfcc7651daa4b11ed52e7870249f6373a5 (patch) | |
tree | 78de4649396ab0d37a325b7598f9873c2d65f4c9 /src/util.hh | |
parent | df70006a07fb67b17fb39aa56762c50c2e7b8131 (diff) |
spring cleaning
Diffstat (limited to 'src/util.hh')
-rw-r--r-- | src/util.hh | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/util.hh b/src/util.hh new file mode 100644 index 0000000..93ea320 --- /dev/null +++ b/src/util.hh @@ -0,0 +1,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 + |