Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
141 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) 2007-2017 Hartmut Kaiser | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#if !defined(HPX_UTIL_REGEX_FROM_PATTERN_DEC_13_2017_1016AM) | ||
#define HPX_UTIL_REGEX_FROM_PATTERN_DEC_13_2017_1016AM | ||
|
||
#include <hpx/config.hpp> | ||
#include <hpx/error_code.hpp> | ||
|
||
#include <string> | ||
|
||
namespace hpx { namespace util | ||
{ | ||
HPX_EXPORT std::string regex_from_pattern(std::string const& pattern, | ||
error_code& ec = throws); | ||
}} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) 2007-2017 Hartmut Kaiser | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include <hpx/config.hpp> | ||
#include <hpx/error_code.hpp> | ||
#include <hpx/util/regex_from_pattern.hpp> | ||
|
||
#include <string> | ||
|
||
#include <boost/regex.hpp> | ||
|
||
namespace hpx { namespace util | ||
{ | ||
namespace detail | ||
{ | ||
/////////////////////////////////////////////////////////////////////// | ||
inline std::string | ||
regex_from_character_set(std::string::const_iterator& it, | ||
std::string::const_iterator end, error_code& ec) | ||
{ | ||
std::string::const_iterator start = it; | ||
std::string result(1, *it); // copy '[' | ||
if (*++it == '!') { | ||
result.append(1, '^'); // negated character set | ||
} | ||
else if (*it == ']') { | ||
HPX_THROWS_IF(ec, bad_parameter, "regex_from_character_set", | ||
"Invalid pattern (empty character set) at: " + | ||
std::string(start, end)); | ||
return ""; | ||
} | ||
else { | ||
result.append(1, *it); // append this character | ||
} | ||
|
||
// copy while in character set | ||
while (++it != end) { | ||
result.append(1, *it); | ||
if (*it == ']') | ||
break; | ||
} | ||
|
||
if (it == end || *it != ']') { | ||
HPX_THROWS_IF(ec, bad_parameter, "regex_from_character_set", | ||
"Invalid pattern (missing closing ']') at: " + | ||
std::string(start, end)); | ||
return ""; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
std::string regex_from_pattern(std::string const& pattern, error_code& ec) | ||
{ | ||
std::string result; | ||
std::string::const_iterator end = pattern.end(); | ||
for (std::string::const_iterator it = pattern.begin(); it != end; ++it) | ||
{ | ||
char c = *it; | ||
switch (c) { | ||
case '*': | ||
result.append(".*"); | ||
break; | ||
|
||
case '?': | ||
result.append(1, '.'); | ||
break; | ||
|
||
case '[': | ||
{ | ||
std::string r = | ||
detail::regex_from_character_set(it, end, ec); | ||
if (ec) return ""; | ||
result.append(r); | ||
} | ||
break; | ||
|
||
case '\\': | ||
if (++it == end) { | ||
HPX_THROWS_IF(ec, bad_parameter, | ||
"regex_from_pattern", | ||
"Invalid escape sequence at: " + pattern); | ||
return ""; | ||
} | ||
result.append(1, *it); | ||
break; | ||
|
||
default: | ||
result.append(1, c); | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
}} |