#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace LCompilers {
bool startswith(const std::string &s, const std::string &e)
{
if (s.size() < e.size()) return false;
return s.substr(0, e.size()) == e;
}
bool endswith(const std::string &s, const std::string &e)
{
if (s.size() < e.size()) return false;
return s.substr(s.size()-e.size()) == e;
}
std::string to_lower(const std::string &s) {
std::string res = s;
std::transform(res.begin(), res.end(), res.begin(),
[](unsigned char c){ return std::tolower(c); });
return res;
}
char *s2c(Allocator &al, const std::string &s) {
Str x; x.from_str_view(s);
return x.c_str(al);
}
// Splits the string `s` using the separator `split_string`
std::vector string_split(const std::string &s, const std::string &split_string)
{
std::vector result;
size_t old_pos = 0;
size_t new_pos;
while ((new_pos = s.find(split_string, old_pos)) != std::string::npos) {
std::string substr = s.substr(old_pos, new_pos-old_pos);
if (substr.size() > 0) result.push_back(substr);
old_pos = new_pos+split_string.size();
}
result.push_back(s.substr(old_pos));
return result;
}
// Splits the string `s` using any space or newline
std::vector split(const std::string &s)
{
std::vector result;
std::string split_chars = " \n";
size_t old_pos = 0;
size_t new_pos;
while ((new_pos = s.find_first_of(split_chars, old_pos)) != std::string::npos) {
std::string substr = s.substr(old_pos, new_pos-old_pos);
if (substr.size() > 0) result.push_back(substr);
old_pos = new_pos+1;
}
result.push_back(s.substr(old_pos));
return result;
}
std::string join(const std::string j, const std::vector &l)
{
std::string result;
for (size_t i=0; i= oldlen)) {
newlen = oldlen-start;
} else {
newlen = end-start;
}
std::vector nv(newlen);
for (int i=0; i= 0 && path[pos] != '/') pos--;
if (pos == -1) {
return "";
} else {
return path.substr(0, pos);
}
}
bool is_relative_path(const std::string &path) {
return !startswith(path, "/");
}
std::string join_paths(const std::vector &paths) {
std::string p;
std::string delim = "/";
for (auto &path : paths) {
if (path.size() > 0) {
if (p.size() > 0 && !endswith(p, delim)) {
p.append(delim);
}
p.append(path);
}
}
return p;
}
std::string str_escape_c(const std::string &s) {
std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); c++) {
switch (*c) {
case '"': o