Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtefsutils] Add LexicallyNormal function (#663) #1077

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
7 changes: 7 additions & 0 deletions libs/rtefsutils/include/RteFsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ class RteFsUtils
*/
static std::string ParentPath(const std::string& path);

/**
* @brief get lexically normalized path
* @param path path to be processed
* @return string containing the lexically normalized path
*/
static std::string LexicallyNormal(const std::string& path);

/**
* @brief determine relative path in respect to base directory
* @param path path to be processed
Expand Down
9 changes: 9 additions & 0 deletions libs/rtefsutils/src/RteFsUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,15 @@ string RteFsUtils::ParentPath(const string& path) {
return fs::path(path).parent_path().generic_string();
}

string RteFsUtils::LexicallyNormal(const string& path) {
string lexicallyNormal = fs::path(path).lexically_normal().generic_string();
if ((lexicallyNormal.length() > 1) && (lexicallyNormal.back() == '/')) {
// remove trailing separator for alignment with canonical behaviour
lexicallyNormal.pop_back();
}
return lexicallyNormal;
}

string RteFsUtils::RelativePath(const string& path, const string& base, bool withHeadingDot) {
if (path.empty() || base.empty()) {
return "";
Expand Down
13 changes: 13 additions & 0 deletions libs/rtefsutils/test/src/RteFsUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,19 @@ TEST_F(RteFsUtilsTest, ParentPath) {
EXPECT_EQ(filePath, dirnameSubdir);
}

TEST_F(RteFsUtilsTest, LexicallyNormal) {
string dirPath;

dirPath = RteFsUtils::LexicallyNormal(dirnameDotDotSubdir);
EXPECT_EQ(dirPath, dirnameSubdir);

dirPath = RteFsUtils::LexicallyNormal(dirnameDotSubdir);
EXPECT_EQ(dirPath, dirnameSubdir);

dirPath = RteFsUtils::LexicallyNormal(dirnameSubdirWithTrailing);
EXPECT_EQ(dirPath, dirnameSubdir);
}

TEST_F(RteFsUtilsTest, RelativePath) {
string relPath;
string absSubdir = RteFsUtils::AbsolutePath(dirnameSubdir).generic_string();
Expand Down
2 changes: 1 addition & 1 deletion tools/projmgr/src/ProjMgrYamlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void ProjMgrYamlParser::EnsurePortability(const string& file, const YAML::Mark&
value.clear();
} else {
const string parentDir = RteFsUtils::ParentPath(file);
const string original = fs::path(parentDir).append(value).lexically_normal().generic_string();
const string original = RteFsUtils::LexicallyNormal(fs::path(parentDir).append(value).generic_string());
if (RteFsUtils::Exists(original)) {
error_code ec;
string canonical = fs::canonical(original, ec).generic_string();
Expand Down
Loading