tree-sitter/src/compiler/generate_code/helpers.cpp

42 lines
1.2 KiB
C++
Raw Normal View History

2014-03-09 21:37:21 -07:00
#include "compiler/generate_code/helpers.h"
namespace tree_sitter {
namespace generate_code {
static void str_replace(string &input, const string &search, const string &replace) {
size_t pos = 0;
while (1) {
pos = input.find(search, pos);
if (pos == string::npos) break;
input.erase(pos, search.length());
input.insert(pos, replace);
pos += replace.length();
}
}
2014-03-09 19:49:35 -07:00
string join(vector<string> lines, string separator) {
string result;
bool started = false;
for (auto line : lines) {
if (started) result += separator;
started = true;
result += line;
}
return result;
}
2014-03-09 19:49:35 -07:00
string join(vector<string> lines) {
return join(lines, "\n");
}
2014-03-09 19:49:35 -07:00
string indent(string input) {
string tab = " ";
str_replace(input, "\n", "\n" + tab);
return tab + input;
}
2014-03-09 19:49:35 -07:00
string escape_string(string input) {
str_replace(input, "\"", "\\\"");
return input;
}
}
}