commit 5c649b6399a287f5921230229627cad0120899a1
parent 9285bf4fc907f5d989a1dd5eb8978bd1fc2028c9
Author: cfillion <cfillion@users.noreply.github.com>
Date: Tue, 19 Jan 2016 10:01:03 -0800
fix undefined behavior in Path::dirname and Path::removeLast
Diffstat:
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/src/path.cpp b/src/path.cpp
@@ -83,27 +83,34 @@ void Path::clear()
void Path::removeLast()
{
- m_parts.pop_back();
+ if(!empty())
+ m_parts.pop_back();
}
string Path::basename() const
{
- if(m_parts.empty())
+ if(empty())
return {};
return m_parts.back();
}
-string Path::join(const bool skipLast, const char sep) const
+string Path::dirname() const
{
- string path;
+ if(empty())
+ return {};
+
+ Path dir(*this);
+ dir.removeLast();
- auto end = m_parts.end();
+ return dir.join();
+}
- if(skipLast)
- end--;
+string Path::join(const char sep) const
+{
+ string path;
- for(auto it = m_parts.begin(); it != end; it++) {
+ for(auto it = m_parts.begin(); it != m_parts.end(); it++) {
const string &part = *it;
if(!path.empty())
diff --git a/src/path.hpp b/src/path.hpp
@@ -34,8 +34,8 @@ public:
size_t size() const { return m_parts.size(); }
std::string basename() const;
- std::string dirname() const { return join(true, 0); }
- std::string join(const char sep = 0) const { return join(false, sep); }
+ std::string dirname() const;
+ std::string join(const char sep = 0) const;
bool operator==(const Path &) const;
bool operator!=(const Path &) const;
@@ -46,7 +46,6 @@ public:
const std::string &operator[](const size_t index) const;
private:
- std::string join(const bool, const char) const;
const std::string &at(const size_t) const;
std::list<std::string> m_parts;