2014-03-27 12:54:54 -07:00
|
|
|
#include "compiler/util/string_helpers.h"
|
2014-04-14 23:11:10 -07:00
|
|
|
#include <vector>
|
2014-03-27 12:54:54 -07:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
|
|
|
|
using std::string;
|
2014-04-14 23:11:10 -07:00
|
|
|
using std::vector;
|
|
|
|
|
using std::set;
|
2014-03-28 13:51:32 -07:00
|
|
|
|
2014-03-27 12:54:54 -07:00
|
|
|
namespace util {
|
|
|
|
|
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-28 13:51:32 -07:00
|
|
|
|
2014-03-27 12:54:54 -07:00
|
|
|
string escape_string(string input) {
|
|
|
|
|
str_replace(&input, "\"", "\\\"");
|
|
|
|
|
str_replace(&input, "\n", "\\n");
|
|
|
|
|
return input;
|
|
|
|
|
}
|
2014-06-11 16:43:03 -07:00
|
|
|
|
|
|
|
|
string escape_char(char character) {
|
|
|
|
|
switch (character) {
|
|
|
|
|
case '\0':
|
|
|
|
|
return "\\0";
|
|
|
|
|
case '"':
|
|
|
|
|
return "\\\"";
|
|
|
|
|
case '\'':
|
|
|
|
|
return "\\'";
|
|
|
|
|
case '\n':
|
|
|
|
|
return "\\n";
|
|
|
|
|
case '\r':
|
|
|
|
|
return "\\r";
|
|
|
|
|
case '\t':
|
|
|
|
|
return "\\t";
|
|
|
|
|
case '\\':
|
|
|
|
|
return "\\\\";
|
|
|
|
|
default:
|
|
|
|
|
return string() + character;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-17 13:20:43 -07:00
|
|
|
|
2014-04-14 23:11:10 -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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string join(vector<string> lines) {
|
|
|
|
|
return join(lines, "\n");
|
|
|
|
|
}
|
2014-04-17 13:20:43 -07:00
|
|
|
|
2014-04-14 23:11:10 -07:00
|
|
|
string indent(string input) {
|
|
|
|
|
string tab = " ";
|
|
|
|
|
util::str_replace(&input, "\n", "\n" + tab);
|
|
|
|
|
return tab + input;
|
|
|
|
|
}
|
2014-03-27 12:54:54 -07:00
|
|
|
}
|
2014-04-28 21:46:43 -07:00
|
|
|
}
|