Commit 6495b60e authored by Tudor Bosman's avatar Tudor Bosman Committed by Jordan DeLong

Fix name handling when baseDir and subDir are both empty, simplify paths

Test Plan: fbconfig -r folly/experimental/symbolizer folly/experimental/exception_tracer && fbmake runtests_opt

Reviewed By: wez@fb.com

FB internal diff: D1104426
parent c84a6adf
...@@ -110,22 +110,48 @@ void skipPadding(folly::StringPiece& sp, const char* start, size_t alignment) { ...@@ -110,22 +110,48 @@ void skipPadding(folly::StringPiece& sp, const char* start, size_t alignment) {
} }
} }
void stripSlashes(folly::StringPiece& sp, bool keepInitialSlash) { // Simplify a path -- as much as we can while not moving data around...
if (sp.empty()) { void simplifyPath(folly::StringPiece& sp) {
return; // Strip leading slashes and useless patterns (./), leaving one initial
} // slash.
for (;;) {
if (sp.empty()) {
return;
}
const char* p = sp.begin(); // Strip leading slashes, leaving one.
for (; p != sp.end() && *p == '/'; ++p); while (sp.startsWith("//")) {
sp.advance(1);
}
const char* q = sp.end(); if (sp.startsWith("/./")) {
for (; q != p && q[-1] == '/'; --q); // Note 2, not 3, to keep it absolute
sp.advance(2);
continue;
}
if (keepInitialSlash && p != sp.begin()) { if (sp.removePrefix("./")) {
--p; continue;
}
break;
} }
sp.assign(p, q); // Strip trailing slashes and useless patterns (/.).
for (;;) {
if (sp.empty()) {
return;
}
// Strip trailing slashes
while (sp.removeSuffix('/')) { }
if (sp.removeSuffix("/.")) {
continue;
}
break;
}
} }
} // namespace } // namespace
...@@ -159,12 +185,17 @@ Dwarf::Path::Path(folly::StringPiece baseDir, folly::StringPiece subDir, ...@@ -159,12 +185,17 @@ Dwarf::Path::Path(folly::StringPiece baseDir, folly::StringPiece subDir,
swap(baseDir_, subDir_); swap(baseDir_, subDir_);
} }
stripSlashes(baseDir_, true); // keep leading slash if it exists simplifyPath(baseDir_);
stripSlashes(subDir_, false); simplifyPath(subDir_);
stripSlashes(file_, false); simplifyPath(file_);
} }
size_t Dwarf::Path::size() const { size_t Dwarf::Path::size() const {
if (baseDir_.empty()) {
assert(subDir_.empty());
return file_.size();
}
return return
baseDir_.size() + !subDir_.empty() + subDir_.size() + !file_.empty() + baseDir_.size() + !subDir_.empty() + subDir_.size() + !file_.empty() +
file_.size(); file_.size();
...@@ -192,7 +223,9 @@ size_t Dwarf::Path::toBuffer(char* buf, size_t bufSize) const { ...@@ -192,7 +223,9 @@ size_t Dwarf::Path::toBuffer(char* buf, size_t bufSize) const {
append(subDir_); append(subDir_);
} }
if (!file_.empty()) { if (!file_.empty()) {
append("/"); if (!baseDir_.empty()) {
append("/");
}
append(file_); append(file_);
} }
if (bufSize) { if (bufSize) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment