Skip to content

Commit

Permalink
Move wildcards_regex from cp_task to string_utils and helpers namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloop committed Sep 25, 2020
1 parent bed090c commit d96bafc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 44 deletions.
42 changes: 42 additions & 0 deletions src/helpers/string_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
#include "string_utils.h"
#include <cctype>

namespace {

void replace_substring(std::string &data, const std::string &from, const std::string &to)
{
std::size_t pos = data.find(from);
while(pos != std::string::npos) {
data.replace(pos, from.size(), to);
pos = data.find(from, pos + to.size());
}
}

void escape_regex(std::string &regex)
{
replace_substring(regex, "\\", "\\\\");
replace_substring(regex, "^", "\\^");
replace_substring(regex, ".", "\\.");
replace_substring(regex, "$", "\\$");
replace_substring(regex, "|", "\\|");
replace_substring(regex, "(", "\\(");
replace_substring(regex, ")", "\\)");
replace_substring(regex, "[", "\\[");
replace_substring(regex, "]", "\\]");
replace_substring(regex, "*", "\\*");
replace_substring(regex, "+", "\\+");
replace_substring(regex, "?", "\\?");
replace_substring(regex, "/", "\\/");
}

} // namespace

std::string helpers::random_alphanum_string(std::size_t length)
{
auto randchar = []() -> char {
Expand All @@ -23,3 +53,15 @@ void helpers::filter_non_printable_chars(std::string &text)
[](char c) { return !isprint(static_cast<unsigned char>(c)) && !iscntrl(static_cast<unsigned char>(c)); }),
text.end());
}

std::regex helpers::wildcards_regex(std::string wildcard_pattern)
{
// Escape all regex special chars
escape_regex(wildcard_pattern);

// Convert chars '*?' back to their regex equivalents
replace_substring(wildcard_pattern, "\\?", ".");
replace_substring(wildcard_pattern, "\\*", ".*");

return std::regex(wildcard_pattern);
}
8 changes: 8 additions & 0 deletions src/helpers/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <string>
#include <algorithm>
#include <regex>

namespace helpers
{
Expand All @@ -18,6 +19,13 @@ namespace helpers
* @param text
*/
void filter_non_printable_chars(std::string &text);

/**
* Constructs appropriate regular expression for given wildcard.
* @param string with wildcard pattern
* @return regex
*/
std::regex wildcards_regex(std::string wildcard_pattern);
} // namespace helpers


Expand Down
46 changes: 2 additions & 44 deletions src/tasks/internal/cp_task.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "cp_task.h"
#include <regex>
#include "helpers/string_utils.h"
#include "helpers/filesystem.h"

#define BOOST_FILESYSTEM_NO_DEPRECATED
Expand All @@ -8,48 +8,6 @@

namespace fs = boost::filesystem;

namespace {

void replace_substring(std::string &data, const std::string &from, const std::string &to)
{
std::size_t pos = data.find(from);
while(pos != std::string::npos) {
data.replace(pos, from.size(), to);
pos = data.find(from, pos + to.size());
}
}

void escape_regex(std::string &regex)
{
replace_substring(regex, "\\", "\\\\");
replace_substring(regex, "^", "\\^");
replace_substring(regex, ".", "\\.");
replace_substring(regex, "$", "\\$");
replace_substring(regex, "|", "\\|");
replace_substring(regex, "(", "\\(");
replace_substring(regex, ")", "\\)");
replace_substring(regex, "[", "\\[");
replace_substring(regex, "]", "\\]");
replace_substring(regex, "*", "\\*");
replace_substring(regex, "+", "\\+");
replace_substring(regex, "?", "\\?");
replace_substring(regex, "/", "\\/");
}

std::regex wildcards_regex(std::string wildcard_pattern)
{
// Escape all regex special chars
escape_regex(wildcard_pattern);

// Convert chars '*?' back to their regex equivalents
replace_substring(wildcard_pattern, "\\?", ".");
replace_substring(wildcard_pattern, "\\*", ".*");

return std::regex(wildcard_pattern);
}

} // namespace


cp_task::cp_task(std::size_t id, std::shared_ptr<task_metadata> task_meta) : task_base(id, task_meta)
{
Expand Down Expand Up @@ -79,7 +37,7 @@ std::shared_ptr<task_results> cp_task::run()
fs::path input(task_meta_->cmd_args[0]);
auto filename_matcher = input.filename().string();
auto base_dir = input.remove_filename();
auto pattern = wildcards_regex(filename_matcher);
auto pattern = helpers::wildcards_regex(filename_matcher);

// parse output path and check if it is existing directory or not
fs::path output(task_meta_->cmd_args[1]);
Expand Down

0 comments on commit d96bafc

Please sign in to comment.