mirror of
https://github.com/tristanpenman/valijson.git
synced 2025-03-04 19:13:30 +01:00
Update PatternConstraint to use custom allocator for pattern string
This commit is contained in:
parent
2bb7ce19b2
commit
db33cb833f
@ -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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user