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

Update namespace.cc: re-implement the member function NormalizePath #778

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
27 changes: 17 additions & 10 deletions src/nameserver/namespace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -731,23 +731,30 @@ bool NameSpace::RebuildBlockMap(std::function<void (const FileInfo&)> callback)
}

std::string NameSpace::NormalizePath(const std::string& path) {
// Is there a better implementation?
std::string ret;
if (path.empty() || path[0] != '/') {
ret = "/";
}
bool slash = false;
for (uint32_t i = 0; i < path.size(); i++) {
uint32_t len = path.size();
uint32_t i = 1;
for (; i < len; ) {
if (path[i] == '/') {
if (slash) continue;
slash = true;
} else {
slash = false;
if (path[i-1] == '/') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码风格

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I get it.

ret.push_back(path[i-1]);
}
i++;
}
else {
ret.push_back(path[i-1]);
ret.push_back(path[i]);
i += 2;
}
ret.push_back(path[i]);
}
if (ret.size() > 1U && ret[ret.size() - 1] == '/') {
ret.resize(ret.size() - 1);
if (i == len && path[len-1] != '/') {
ret.push_back(path[len-1]);
}
if (ret.empty()) {
ret = "/";
}
return ret;
}
Expand Down