tree-sitter/src/compiler/util/string_helpers.cc

24 lines
691 B
C++
Raw Normal View History

#include "compiler/util/string_helpers.h"
namespace tree_sitter {
using std::string;
2014-03-28 13:51:32 -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
string escape_string(string input) {
str_replace(&input, "\"", "\\\"");
str_replace(&input, "\n", "\\n");
return input;
}
}
}