return n == 0 ? "/" : path.substr(0, n);
}
+bool
+ends_with(const std::string& string, const std::string& suffix)
+{
+ return suffix.length() <= string.length()
+ && string.compare(
+ string.length() - suffix.length(), suffix.length(), suffix)
+ == 0;
+}
+
std::string
read_file(const std::string& path)
{
// Get directory name of path.
std::string dir_name(const std::string& path);
+// Return true if suffix is a suffix of string.
+bool ends_with(const std::string& string, const std::string& suffix);
+
// Read file data as a string.
//
// Throws Error on error.
CHECK(util::dir_name("/foo/bar/f.txt") == "/foo/bar");
}
+TEST_CASE("util::ends_with")
+{
+ CHECK(util::ends_with("", ""));
+ CHECK(util::ends_with("x", ""));
+ CHECK(util::ends_with("x", "x"));
+ CHECK(util::ends_with("xy", ""));
+ CHECK(util::ends_with("xy", "y"));
+ CHECK(util::ends_with("xy", "xy"));
+ CHECK(util::ends_with("xyz", ""));
+ CHECK(util::ends_with("xyz", "z"));
+ CHECK(util::ends_with("xyz", "yz"));
+ CHECK(util::ends_with("xyz", "xyz"));
+
+ CHECK_FALSE(util::ends_with("", "x"));
+ CHECK_FALSE(util::ends_with("x", "y"));
+ CHECK_FALSE(util::ends_with("x", "xy"));
+}
+
TEST_CASE("util::read_file and util::write_file")
{
util::write_file("test", "foo\nbar\n");