++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) { |
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(); |
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")); |