Browse Source

Fix bad string trimming creating bogus string_views

default_compile_flags
vector-of-bool 5 years ago
parent
commit
55b7d56430
3 changed files with 15 additions and 2 deletions
  1. +3
    -1
      src/dds/util/string.hpp
  2. +7
    -0
      src/dds/util/string.test.cpp
  3. +5
    -1
      src/libman/parse.test.cpp

+ 3
- 1
src/dds/util/string.hpp View File

++iter; ++iter;
} }
auto riter = s.rbegin(); auto riter = s.rbegin();
auto rend = s.rend();
auto rend = std::make_reverse_iterator(iter);
while (riter != rend && std::isspace(*riter)) { while (riter != rend && std::isspace(*riter)) {
++riter; ++riter;
} }
return sview(iter, new_end); 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 std::string trim(std::string&& s) { return std::string(trim(s)); }


inline bool ends_with(std::string_view s, std::string_view key) { inline bool ends_with(std::string_view s, std::string_view key) {

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

CHECK(!ends_with("foo.bar", "foo")); CHECK(!ends_with("foo.bar", "foo"));
} }


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

void test_contains() { void test_contains() {
CHECK(contains("foo", "foo")); CHECK(contains("foo", "foo"));
CHECK(contains("foo", "")); CHECK(contains("foo", ""));
} }


void run_tests() { void run_tests() {
test_trim();
test_starts_with(); test_starts_with();
test_ends_with(); test_ends_with();
test_contains(); test_contains();

+ 5
- 1
src/libman/parse.test.cpp View File

auto kvs = parse_string(lm_src); auto kvs = parse_string(lm_src);
CHECK(kvs.size() == 0); CHECK(kvs.size() == 0);


lm_src = "foo: bar";
CHECK(parse_string(" ").size() == 0);
CHECK(parse_string("\n ").size() == 0);
CHECK(parse_string("#comment\n ").size() == 0);

lm_src = "foo: bar\n ";
kvs = parse_string(lm_src); kvs = parse_string(lm_src);
CHECK(kvs.size() == 1); CHECK(kvs.size() == 1);
REQUIRE(kvs.find("foo")); REQUIRE(kvs.find("foo"));

Loading…
Cancel
Save