Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions openfst/compat/file_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ std::pair<std::string_view, std::string_view> SplitPath(std::string_view path) {
absl::ClippedSubstr(path, pos + 1));
}

// Return the parts of the basename of path, split on the final ".".
// If there is no "." in the basename or "." is the final character in the
// basename, the second value will be empty.
std::pair<std::string_view, std::string_view> SplitBasename(
std::string_view path) {
path = Basename(path);

const size_t pos = path.find_last_of('.');
if (pos == std::string_view::npos)
return std::make_pair(path, absl::ClippedSubstr(path, path.size(), 0));
return std::make_pair(path.substr(0, pos),
absl::ClippedSubstr(path, pos + 1));
}

} // namespace

std::string JoinPath(std::string_view path1, std::string_view path2) {
Expand Down Expand Up @@ -81,4 +95,12 @@ std::string_view Basename(std::string_view path) {
return SplitPath(path).second;
}

std::string_view Dirname(absl::string_view path) {
return SplitPath(path).first;
}

std::string_view Extension(std::string_view path) {
return SplitBasename(path).second;
}

} // namespace fst
11 changes: 11 additions & 0 deletions openfst/compat/file_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ std::string JoinPathRespectAbsolute(std::string_view path1,
// "/" in the path, the result is the same as the input.
std::string_view Basename(std::string_view path);

// Returns the part of the path before the final "/", EXCEPT:
// * If there is a single leading "/" in the path, the result will be the
// leading "/".
// * If there is no "/" in the path, the result is the empty prefix of the
// input string.
std::string_view Dirname(std::string_view path);

// Returns the part of the basename of path after the final ".". If
// there is no "." in the basename, the result is empty.
std::string_view Extension(std::string_view path);

} // namespace fst

#endif // OPENFST_COMPAT_FILE_PATH_H_
24 changes: 24 additions & 0 deletions openfst/compat/file_path_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,29 @@ TEST(FilePathTest, CheckBasename) {
EXPECT_EQ("hello", Basename("/a/b/c/hello"));
}

TEST(FilePathTest, CheckDirname) {
EXPECT_EQ("/hello", Dirname("/hello/"));
EXPECT_EQ("/", Dirname("/hello"));
EXPECT_EQ("/hello", Dirname("/hello/world"));
EXPECT_EQ("hello", Dirname("hello/world"));
EXPECT_EQ("hello", Dirname("hello/"));
EXPECT_EQ("", Dirname("world"));
EXPECT_EQ("/", Dirname("/"));
EXPECT_EQ("", Dirname(""));
}

TEST(FilePathTest, CheckExtension) {
EXPECT_EQ("gif", Extension("foo.gif"));
EXPECT_EQ("", Extension("foo."));
EXPECT_EQ("", Extension(""));
EXPECT_EQ("", Extension("/"));
EXPECT_EQ("", Extension("foo"));
EXPECT_EQ("", Extension("foo/"));
EXPECT_EQ("gif", Extension("/a/path/to/foo.gif"));
EXPECT_EQ("html", Extension("/a/path.bar/to/foo.html"));
EXPECT_EQ("", Extension("/a/path.bar/to/foo"));
EXPECT_EQ("baz", Extension("/a/path.bar/to/foo.bar.baz"));
}

} // namespace
} // namespace fst
Loading