Browse Source

Fix bad string trimming, and safer trimming of temporary strings

default_compile_flags
vector-of-bool 5 years ago
parent
commit
404738a7a3
5 changed files with 24 additions and 22 deletions
  1. +11
    -10
      src/dds/deps.cpp
  2. +4
    -5
      src/dds/util/string.hpp
  3. +3
    -3
      src/dds/util/string.test.cpp
  4. +3
    -1
      src/libman/index.cpp
  5. +3
    -3
      src/libman/parse.cpp

+ 11
- 10
src/dds/deps.cpp View File

@@ -26,8 +26,8 @@ dependency dependency::parse_depends_string(std::string_view str) {
++str_iter;
}

auto name = trim(std::string_view(str_begin, str_iter - str_begin));
auto version_str = trim(std::string_view(str_iter, str_end - str_iter));
auto name = trim_view(std::string_view(str_begin, str_iter - str_begin));
auto version_str = trim_view(std::string_view(str_iter, str_end - str_iter));

semver::version version;
try {
@@ -54,24 +54,25 @@ auto tie_sdist(const sdist& sd) {
return std::tuple(sd.manifest.name, sd.manifest.version.to_string());
}

auto sdist_compare = [](const sdist& lhs, const sdist& rhs) {
return tie_sdist(lhs) < tie_sdist(rhs);
};
auto sdist_compare
= [](const sdist& lhs, const sdist& rhs) { return tie_sdist(lhs) < tie_sdist(rhs); };

void detail::sort_sdists(std::vector<sdist>& sd) { std::sort(sd.begin(), sd.end(), sdist_compare); }

namespace {

const sdist* get_sdist(const std::vector<sdist>& sorted_sds, std::string_view name, std::string_view version) {
auto found = std::partition_point(sorted_sds.begin(), sorted_sds.end(), [&](const auto& candidate) {
return tie_sdist(candidate) < std::tie(name, version);
});
const sdist*
get_sdist(const std::vector<sdist>& sorted_sds, std::string_view name, std::string_view version) {
auto found
= std::partition_point(sorted_sds.begin(), sorted_sds.end(), [&](const auto& candidate) {
return tie_sdist(candidate) < std::tie(name, version);
});
if (found->manifest.name == name && found->manifest.version.to_string() == version) {
return &*found;
}
return nullptr;
}
}
} // namespace

void detail::do_find_deps(const std::vector<sdist>& sdists,
const dependency& dep,

+ 4
- 5
src/dds/util/string.hpp View File

@@ -11,10 +11,13 @@ inline namespace string_utils {

inline std::string_view sview(std::string_view::const_iterator beg,
std::string_view::const_iterator end) {
if (beg == end) {
return "";
}
return std::string_view(&*beg, static_cast<std::size_t>(std::distance(beg, end)));
}

inline std::string_view trim(std::string_view s) {
inline std::string_view trim_view(std::string_view s) {
auto iter = s.begin();
auto end = s.end();
while (iter != end && std::isspace(*iter)) {
@@ -29,10 +32,6 @@ inline std::string_view trim(std::string_view s) {
return sview(iter, new_end);
}

inline std::string_view trim(const char* str) { return trim(std::string_view(str)); }

inline std::string trim(std::string&& s) { return std::string(trim(s)); }

inline bool ends_with(std::string_view s, std::string_view key) {
auto found = s.rfind(key);
return found != s.npos && found == s.size() - key.size();

+ 3
- 3
src/dds/util/string.test.cpp View File

@@ -34,9 +34,9 @@ void test_ends_with() {
}

void test_trim() {
CHECK(trim("foo") == "foo");
CHECK(trim("foo ") == "foo");
CHECK(trim(" ").size() == 0);
CHECK(trim_view("foo") == "foo");
CHECK(trim_view("foo ") == "foo");
CHECK(trim_view(" ").size() == 0);
}

void test_contains() {

+ 3
- 1
src/libman/index.cpp View File

@@ -26,7 +26,9 @@ lm::index index::from_file(path_ref fpath) {

for (const auto& pkg_line : package_lines) {
auto items = dds::split(pkg_line, ";");
std::transform(items.begin(), items.end(), items.begin(), [](auto s) { return trim(s); });
std::transform(items.begin(), items.end(), items.begin(), [](auto s) {
return std::string(trim_view(s));
});
if (items.size() != 2) {
throw std::runtime_error(
fmt::format("Invalid 'Package' field in index file ({}): 'Package: {}'",

+ 3
- 3
src/libman/parse.cpp View File

@@ -16,7 +16,7 @@ using namespace lm;
namespace {

void parse_line(std::vector<pair>& pairs, const std::string_view whole_line) {
const auto line = trim(whole_line);
const auto line = trim_view(whole_line);
if (line.empty() || line[0] == '#') {
return;
}
@@ -46,8 +46,8 @@ void parse_line(std::vector<pair>& pairs, const std::string_view whole_line) {
// `iter` now points to the space between the key and value
auto key = sview(begin, iter - 1); // -1 to trim the colon in the key
auto value = sview(iter, end);
key = trim(key);
value = trim(value);
key = trim_view(key);
value = trim_view(value);
pairs.emplace_back(key, value);
}


Loading…
Cancel
Save