diff options
author | Hosein Ghahramanzadeh <hgsilverman@gmail.com> | 2020-01-16 23:25:08 (GMT) |
---|---|---|
committer | Hosein Ghahramanzadeh <hgsilverman@gmail.com> | 2020-01-24 21:20:48 (GMT) |
commit | c09fbb239338da11d81e4a854ae805b8e87b381b (patch) | |
tree | a7bbd0c66a078daecf09193b2ea240dd3b6402d7 /googletest | |
parent | 8b4817e3df3746a20502a84580f661ac448821be (diff) | |
download | googletest-c09fbb239338da11d81e4a854ae805b8e87b381b.zip googletest-c09fbb239338da11d81e4a854ae805b8e87b381b.tar.gz googletest-c09fbb239338da11d81e4a854ae805b8e87b381b.tar.bz2 |
Fix always false condition and clean function bodyrefs/pull/2677/head
An always false condition was remove from Normalize member function of
FilePath and the body of the function which was poorly writen is
improved.
Diffstat (limited to 'googletest')
-rw-r--r-- | googletest/src/gtest-filepath.cc | 40 |
1 files changed, 13 insertions, 27 deletions
diff --git a/googletest/src/gtest-filepath.cc b/googletest/src/gtest-filepath.cc index 9aad12f..478a415 100644 --- a/googletest/src/gtest-filepath.cc +++ b/googletest/src/gtest-filepath.cc @@ -349,33 +349,19 @@ FilePath FilePath::RemoveTrailingPathSeparator() const { // For example, "bar///foo" becomes "bar/foo". Does not eliminate other // redundancies that might be in a pathname involving "." or "..". void FilePath::Normalize() { - if (pathname_.c_str() == nullptr) { - pathname_ = ""; - return; - } - const char* src = pathname_.c_str(); - char* const dest = new char[pathname_.length() + 1]; - char* dest_ptr = dest; - memset(dest_ptr, 0, pathname_.length() + 1); - - while (*src != '\0') { - *dest_ptr = *src; - if (!IsPathSeparator(*src)) { - src++; - } else { -#if GTEST_HAS_ALT_PATH_SEP_ - if (*dest_ptr == kAlternatePathSeparator) { - *dest_ptr = kPathSeparator; - } -#endif - while (IsPathSeparator(*src)) - src++; - } - dest_ptr++; - } - *dest_ptr = '\0'; - pathname_ = dest; - delete[] dest; + std::string normalized_pathname; + normalized_pathname.reserve(pathname_.length()); + + for (const auto character : pathname_) + if (!IsPathSeparator(character)) + normalized_pathname.push_back(character); + else if (normalized_pathname.empty() || + normalized_pathname.back() != kPathSeparator) + normalized_pathname.push_back(kPathSeparator); + else + continue; + + pathname_ = normalized_pathname; } } // namespace internal |