Ver código fonte

Fix bad string trimming creating bogus string_views

default_compile_flags
vector-of-bool 5 anos atrás
pai
commit
55b7d56430
3 arquivos alterados com 15 adições e 2 exclusões
  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 Ver arquivo

@@ -21,7 +21,7 @@ inline std::string_view trim(std::string_view s) {
++iter;
}
auto riter = s.rbegin();
auto rend = s.rend();
auto rend = std::make_reverse_iterator(iter);
while (riter != rend && std::isspace(*riter)) {
++riter;
}
@@ -29,6 +29,8 @@ 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) {

+ 7
- 0
src/dds/util/string.test.cpp Ver arquivo

@@ -33,6 +33,12 @@ void test_ends_with() {
CHECK(!ends_with("foo.bar", "foo"));
}

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

void test_contains() {
CHECK(contains("foo", "foo"));
CHECK(contains("foo", ""));
@@ -48,6 +54,7 @@ void test_split() {
}

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

+ 5
- 1
src/libman/parse.test.cpp Ver arquivo

@@ -10,7 +10,11 @@ void test_simple() {
auto kvs = parse_string(lm_src);
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);
CHECK(kvs.size() == 1);
REQUIRE(kvs.find("foo"));

Carregando…
Cancelar
Salvar