Browse Source

Move `replace` to generate utils

default_compile_flags
vector-of-bool 5 years ago
parent
commit
0e6ee6838a
2 changed files with 21 additions and 23 deletions
  1. +0
    -23
      src/dds/toolchain.cpp
  2. +21
    -0
      src/dds/util.hpp

+ 0
- 23
src/dds/toolchain.cpp View File

@@ -165,29 +165,6 @@ vector<string> dds::split_shell_string(std::string_view shell) {
return acc;
}

namespace {

std::string replace(std::string_view str, std::string_view key, std::string_view repl) {
std::string ret;
std::string_view::size_type pos = 0;
std::string_view::size_type prev_pos = 0;
while (pos = str.find(key, pos), pos != key.npos) {
ret.append(str.begin() + prev_pos, str.begin() + pos);
ret.append(repl);
prev_pos = pos += key.size();
}
ret.append(str.begin() + prev_pos, str.end());
return ret;
}

vector<string> replace(vector<string> strings, std::string_view key, std::string_view repl) {
for (auto& item : strings) {
item = replace(item, key, repl);
}
return strings;
}
} // namespace

vector<string> toolchain::include_args(const fs::path& p) const noexcept {
return replace(_inc_template, "<PATH>", p.string());
}

+ 21
- 0
src/dds/util.hpp View File

@@ -51,6 +51,27 @@ inline std::vector<std::string> split(std::string_view str, std::string_view sep
return ret;
}

inline std::string replace(std::string_view str, std::string_view key, std::string_view repl) {
std::string ret;
std::string_view::size_type pos = 0;
std::string_view::size_type prev_pos = 0;
while (pos = str.find(key, pos), pos != key.npos) {
ret.append(str.begin() + prev_pos, str.begin() + pos);
ret.append(repl);
prev_pos = pos += key.size();
}
ret.append(str.begin() + prev_pos, str.end());
return ret;
}

inline std::vector<std::string>
replace(std::vector<std::string> strings, std::string_view key, std::string_view repl) {
for (auto& item : strings) {
item = replace(item, key, repl);
}
return strings;
}

template <typename Container, typename Predicate>
void erase_if(Container& c, Predicate&& p) {
auto erase_point = std::remove_if(c.begin(), c.end(), p);

Loading…
Cancel
Save