瀏覽代碼

Fix bad string trimming creating bogus string_views

default_compile_flags
vector-of-bool 5 年之前
父節點
當前提交
55b7d56430
共有 3 個文件被更改,包括 15 次插入2 次删除
  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 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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"));

Loading…
取消
儲存