reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

commit fb724c0430ebb9fabd318e1f51247cf0801524d6
parent a6d7754ab95509e27b0dad1dedcaf476e7d53890
Author: cfillion <cfillion@users.noreply.github.com>
Date:   Sun, 17 Jan 2016 21:11:09 -0500

split path components to ensure no invalid separator can hitchhike

Diffstat:
Msrc/path.cpp | 38++++++++++++++++++++++++++++++++++----
Mtest/path.cpp | 27+++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/src/path.cpp b/src/path.cpp @@ -17,6 +17,8 @@ #include "path.hpp" +#include <vector> + using namespace std; #ifndef _WIN32 @@ -25,15 +27,43 @@ static const char SEPARATOR = '/'; static const char SEPARATOR = '\\'; #endif -void Path::prepend(const string &part) +static vector<string> Split(const string &input) { - if(!part.empty()) + vector<string> list; + + size_t last = 0; + + while(true) { + const size_t pos = input.find_first_of("\\/", last); + + if(pos == string::npos) { + list.push_back(input.substr(last)); + break; + } + else { + list.push_back(input.substr(last, pos-last)); + last = pos + 1; + } + } + + return list; +} + +void Path::prepend(const string &parts) +{ + if(parts.empty()) + return; + + for(const string &part : Split(parts)) m_parts.push_front(part); } -void Path::append(const string &part) +void Path::append(const string &parts) { - if(!part.empty()) + if(parts.empty()) + return; + + for(const string &part : Split(parts)) m_parts.push_back(part); } diff --git a/test/path.cpp b/test/path.cpp @@ -103,3 +103,30 @@ TEST_CASE("custom separator", M) { REQUIRE(a.join('-') == "hello-world"); } + +TEST_CASE("split input", M) { + SECTION("slash") { + Path a; + a.append("hello/world"); + + REQUIRE(a.size() == 2); + REQUIRE(a[0] == "hello"); + REQUIRE(a[1] == "world"); + } + + SECTION("backslash") { + Path a; + a.append("hello\\world"); + + REQUIRE(a.size() == 2); + REQUIRE(a[0] == "hello"); + REQUIRE(a[1] == "world"); + } + + SECTION("prepend") { + Path a; + a.prepend("hello/world"); + + REQUIRE(a.size() == 2); + } +}