Merge pull request #3044 from manavrion:improve_file_path_normalize

PiperOrigin-RevId: 339242159
This commit is contained in:
vslashg 2020-10-27 10:17:25 -04:00
commit 3005672db1

View File

@ -349,21 +349,19 @@ FilePath FilePath::RemoveTrailingPathSeparator() const {
// For example, "bar///foo" becomes "bar/foo". Does not eliminate other // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
// redundancies that might be in a pathname involving "." or "..". // redundancies that might be in a pathname involving "." or "..".
void FilePath::Normalize() { void FilePath::Normalize() {
std::string normalized_pathname; auto out = pathname_.begin();
normalized_pathname.reserve(pathname_.length());
for (const char character : pathname_) { for (const char character : pathname_) {
if (!IsPathSeparator(character)) { if (!IsPathSeparator(character)) {
normalized_pathname.push_back(character); *(out++) = character;
} else if (normalized_pathname.empty() || } else if (out == pathname_.begin() || *std::prev(out) != kPathSeparator) {
normalized_pathname.back() != kPathSeparator) { *(out++) = kPathSeparator;
normalized_pathname.push_back(kPathSeparator);
} else { } else {
continue; continue;
} }
} }
pathname_ = normalized_pathname; pathname_.erase(out, pathname_.end());
} }
} // namespace internal } // namespace internal