commit 5138c52672c37803b8c1961cf08b67a9f3b33d57
parent 4e691a52ed7841e72e475c3a624b94877b9dbac4
Author: cfillion <cfillion@users.noreply.github.com>
Date: Mon, 18 Jan 2016 19:27:40 -0500
some more refactoring
Diffstat:
4 files changed, 35 insertions(+), 17 deletions(-)
diff --git a/src/config.hpp b/src/config.hpp
@@ -20,7 +20,6 @@
#include <string>
-#include "registry.hpp"
#include "remote.hpp"
class Path;
diff --git a/src/path.cpp b/src/path.cpp
@@ -53,6 +53,11 @@ static vector<string> Split(const string &input)
return list;
}
+Path::Path(const std::string &path)
+{
+ append(path);
+}
+
void Path::prepend(const string &parts)
{
if(parts.empty())
@@ -136,7 +141,7 @@ Path Path::operator+(const Path &o) const
return path;
}
-string &Path::operator[](const size_t index)
+const string &Path::at(const size_t index) const
{
auto it = m_parts.begin();
@@ -145,3 +150,13 @@ string &Path::operator[](const size_t index)
return *it;
}
+
+string &Path::operator[](const size_t index)
+{
+ return const_cast<string &>(at(index));
+}
+
+const string &Path::operator[](const size_t index) const
+{
+ return at(index);
+}
diff --git a/src/path.hpp b/src/path.hpp
@@ -23,6 +23,8 @@
class Path {
public:
+ Path(const std::string &path = std::string());
+
void prepend(const std::string &part);
void append(const std::string &part);
void clear();
@@ -40,9 +42,11 @@ public:
Path operator+(const std::string &) const;
Path operator+(const Path &) const;
std::string &operator[](const size_t index);
+ 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;
};
diff --git a/test/path.cpp b/test/path.cpp
@@ -9,8 +9,8 @@ using namespace std;
static const char *M = "[path]";
TEST_CASE("compare paths", M) {
- const Path a = Path() + "hello" + "world";
- const Path b = Path() + "chunky" + "bacon";
+ const Path a = Path("hello");
+ const Path b = Path("world");
REQUIRE_FALSE(a == b);
REQUIRE(a != b);
@@ -57,11 +57,8 @@ TEST_CASE("prepend and append path components", M) {
}
TEST_CASE("concatenate paths", M) {
- Path a;
- a.append("hello");
-
- Path b;
- b.append("world");
+ const Path a("hello");
+ const Path b("world");
Path c;
c.append("hello");
@@ -106,8 +103,7 @@ TEST_CASE("custom separator", M) {
TEST_CASE("split input", M) {
SECTION("slash") {
- Path a;
- a.append("hello/world");
+ const Path a("hello/world");
REQUIRE(a.size() == 2);
REQUIRE(a[0] == "hello");
@@ -115,8 +111,7 @@ TEST_CASE("split input", M) {
}
SECTION("backslash") {
- Path a;
- a.append("hello\\world");
+ const Path a("hello\\world");
REQUIRE(a.size() == 2);
REQUIRE(a[0] == "hello");
@@ -130,9 +125,15 @@ TEST_CASE("split input", M) {
REQUIRE(a.size() == 2);
}
- SECTION("skip empty parts") {
+ SECTION("append") {
Path a;
- a.append("hello//world/");
+ a.append("hello/world");
+
+ REQUIRE(a.size() == 2);
+ }
+
+ SECTION("skip empty parts") {
+ const Path a("hello//world/");
REQUIRE(a.size() == 2);
REQUIRE(a[0] == "hello");
@@ -142,8 +143,7 @@ TEST_CASE("split input", M) {
#ifndef _WIN32
TEST_CASE("absolute path (unix)", M) {
- Path a;
- a.append("/usr/bin/zsh");
+ const Path a("/usr/bin/zsh");
REQUIRE(a.size() == 3);
REQUIRE(a[0] == "/usr");