Update PatternConstraint to use custom allocator for pattern string

This commit is contained in:
Tristan Penman 2015-12-28 20:34:09 +11:00
parent 2bb7ce19b2
commit db33cb833f
3 changed files with 46 additions and 9 deletions

View File

@ -474,14 +474,43 @@ private:
};
/**
* @brief Represents a 'pattern' constraint.
* @brief Represents a 'pattern' constraint
*/
struct PatternConstraint: BasicConstraint<PatternConstraint>
class PatternConstraint: public BasicConstraint<PatternConstraint>
{
PatternConstraint(const std::string &pattern)
: pattern(pattern) { }
public:
PatternConstraint()
: pattern(Allocator::rebind<char>::other(allocator)) { }
const std::string pattern;
PatternConstraint(CustomAlloc allocFn, CustomFree freeFn)
: BasicConstraint(allocFn, freeFn),
pattern(Allocator::rebind<char>::other(allocator)) { }
template<typename AllocatorType>
bool getPattern(std::basic_string<char, std::char_traits<char>,
AllocatorType> &result) const
{
result.assign(this->pattern.c_str());
return true;
}
template<typename AllocatorType>
std::basic_string<char, std::char_traits<char>, AllocatorType> getPattern(
const AllocatorType &alloc = AllocatorType()) const
{
return std::basic_string<char, std::char_traits<char>, AllocatorType>(
pattern.c_str(), alloc);
}
template<typename AllocatorType>
void setPattern(const std::basic_string<char, std::char_traits<char>,
AllocatorType> &pattern)
{
this->pattern.assign(pattern.c_str());
}
private:
String pattern;
};
/**

View File

@ -1215,7 +1215,9 @@ private:
constraints::PatternConstraint makePatternConstraint(
const AdapterType &node)
{
return constraints::PatternConstraint(node.getString());
constraints::PatternConstraint constraint;
constraint.setPattern(node.getString());
return constraint;
}
/**

View File

@ -755,11 +755,17 @@ public:
return true;
}
const boost::regex r(constraint.pattern, boost::regex::perl);
if (!boost::regex_search(target.asString(), r)) {
const boost::regex patternRegex(
constraint.getPattern<std::string::allocator_type>(),
boost::regex::perl);
if (!boost::regex_search(target.asString(), patternRegex)) {
if (results) {
results->pushError(context, "Failed to match regex specified by 'pattern' constraint.");
results->pushError(context,
"Failed to match regex specified by 'pattern' "
"constraint.");
}
return false;
}