Skip to content

Commit

Permalink
Add force parameter to RemovePrefix and RemoveSuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
qris committed Jul 23, 2017
1 parent d379d8b commit a20533c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
18 changes: 16 additions & 2 deletions lib/common/Utils.cpp
Expand Up @@ -119,24 +119,38 @@ bool EndsWith(const std::string& suffix, const std::string& haystack)
haystack.substr(haystack.size() - suffix.size()) == suffix;
}

std::string RemovePrefix(const std::string& prefix, const std::string& haystack)
std::string RemovePrefix(const std::string& prefix, const std::string& haystack,
bool force)
{
if(StartsWith(prefix, haystack))
{
return haystack.substr(prefix.size());
}
else if(force)
{
THROW_EXCEPTION_MESSAGE(CommonException, Internal,
"String '" << haystack << "' was expected to start with prefix "
"'" << prefix << "', but did not.");
}
else
{
return "";
}
}

std::string RemoveSuffix(const std::string& suffix, const std::string& haystack)
std::string RemoveSuffix(const std::string& suffix, const std::string& haystack,
bool force)
{
if(EndsWith(suffix, haystack))
{
return haystack.substr(0, haystack.size() - suffix.size());
}
else if(force)
{
THROW_EXCEPTION_MESSAGE(CommonException, Internal,
"String '" << haystack << "' was expected to end with suffix "
"'" << suffix << "', but did not.");
}
else
{
return "";
Expand Down
6 changes: 4 additions & 2 deletions lib/common/Utils.h
Expand Up @@ -20,8 +20,10 @@ std::string GetBoxBackupVersion();
void SplitString(std::string String, char SplitOn, std::vector<std::string> &rOutput);
bool StartsWith(const std::string& prefix, const std::string& haystack);
bool EndsWith(const std::string& prefix, const std::string& haystack);
std::string RemovePrefix(const std::string& prefix, const std::string& haystack);
std::string RemoveSuffix(const std::string& suffix, const std::string& haystack);
std::string RemovePrefix(const std::string& prefix, const std::string& haystack,
bool force = true);
std::string RemoveSuffix(const std::string& suffix, const std::string& haystack,
bool force = true);

void DumpStackBacktrace(const std::string& filename);
void DumpStackBacktrace(const std::string& filename, size_t size, void * const * array);
Expand Down
38 changes: 38 additions & 0 deletions test/common/testcommon.cpp
Expand Up @@ -952,6 +952,43 @@ void test_box_strtoui64()
TEST_EQUAL(input + 5, endptr);
}

// Test that RemovePrefix and RemoveSuffix work properly
void test_remove_prefix_suffix()
{
TEST_EQUAL("food", RemovePrefix("", "food", false)); // !force
TEST_EQUAL("food", RemoveSuffix("", "food", false)); // !force
TEST_EQUAL("", RemovePrefix("d", "food", false)); // !force
TEST_EQUAL("", RemoveSuffix("f", "food", false)); // !force
TEST_EQUAL("", RemovePrefix("dz", "food", false)); // !force
TEST_EQUAL("", RemoveSuffix("fz", "food", false)); // !force
TEST_EQUAL("ood", RemovePrefix("f", "food", false)); // !force
TEST_EQUAL("foo", RemoveSuffix("d", "food", false)); // !force
TEST_EQUAL("od", RemovePrefix("fo", "food", false)); // !force
TEST_EQUAL("fo", RemoveSuffix("od", "food", false)); // !force
TEST_EQUAL("", RemovePrefix("food", "food", false)); // !force
TEST_EQUAL("", RemoveSuffix("food", "food", false)); // !force
TEST_EQUAL("", RemovePrefix("foodz", "food", false)); // !force
TEST_EQUAL("", RemoveSuffix("foodz", "food", false)); // !force
TEST_EQUAL("", RemoveSuffix("zfood", "food", false)); // !force

TEST_EQUAL("food", RemovePrefix("", "food", true)); // force
TEST_EQUAL("food", RemoveSuffix("", "food", true)); // force
TEST_CHECK_THROWS(RemovePrefix("d", "food", true), CommonException, Internal); // force
TEST_CHECK_THROWS(RemoveSuffix("f", "food", true), CommonException, Internal); // force
TEST_CHECK_THROWS(RemovePrefix("dz", "food", true), CommonException, Internal); // force
TEST_CHECK_THROWS(RemoveSuffix("fz", "food", true), CommonException, Internal); // force
TEST_EQUAL("ood", RemovePrefix("f", "food", true)); // force
TEST_EQUAL("foo", RemoveSuffix("d", "food", true)); // force
TEST_EQUAL("od", RemovePrefix("fo", "food", true)); // force
TEST_EQUAL("fo", RemoveSuffix("od", "food", true)); // force
TEST_CHECK_THROWS(RemovePrefix("foodz", "food", true), CommonException, Internal); // force
TEST_CHECK_THROWS(RemoveSuffix("foodz", "food", true), CommonException, Internal); // force
TEST_CHECK_THROWS(RemoveSuffix("zfood", "food", true), CommonException, Internal); // force

// Test that force defaults to true:
TEST_CHECK_THROWS(RemovePrefix("d", "food"), CommonException, Internal);
}

int test(int argc, const char *argv[])
{
test_stream_large_files();
Expand All @@ -966,6 +1003,7 @@ int test(int argc, const char *argv[])
test_conversions();
test_archive();
test_box_strtoui64();
test_remove_prefix_suffix();

return 0;
}

0 comments on commit a20533c

Please sign in to comment.