Enable boost regex usage for string-format-check.cpp (#286)
This commit is contained in:
parent
693b74eddf
commit
f4194d7e24
@ -11,6 +11,16 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#ifdef JSON_SCHEMA_BOOST_REGEX
|
||||||
|
# include <boost/regex.hpp>
|
||||||
|
# define REGEX_NAMESPACE boost
|
||||||
|
#elif defined(JSON_SCHEMA_NO_REGEX)
|
||||||
|
# define NO_STD_REGEX
|
||||||
|
#else
|
||||||
|
# include <regex>
|
||||||
|
# define REGEX_NAMESPACE std
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Many of the RegExes are from @see http://jmrware.com/articles/2009/uri_regexp/URI_regex.html
|
* Many of the RegExes are from @see http://jmrware.com/articles/2009/uri_regexp/URI_regex.html
|
||||||
*/
|
*/
|
||||||
@ -30,10 +40,10 @@ void range_check(const T value, const T min, const T max)
|
|||||||
/** @see date_time_check */
|
/** @see date_time_check */
|
||||||
void rfc3339_date_check(const std::string &value)
|
void rfc3339_date_check(const std::string &value)
|
||||||
{
|
{
|
||||||
const static std::regex dateRegex{R"(^([0-9]{4})\-([0-9]{2})\-([0-9]{2})$)"};
|
const static REGEX_NAMESPACE::regex dateRegex{R"(^([0-9]{4})\-([0-9]{2})\-([0-9]{2})$)"};
|
||||||
|
|
||||||
std::smatch matches;
|
REGEX_NAMESPACE::smatch matches;
|
||||||
if (!std::regex_match(value, matches, dateRegex)) {
|
if (!REGEX_NAMESPACE::regex_match(value, matches, dateRegex)) {
|
||||||
throw std::invalid_argument(value + " is not a date string according to RFC 3339.");
|
throw std::invalid_argument(value + " is not a date string according to RFC 3339.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,10 +66,10 @@ void rfc3339_date_check(const std::string &value)
|
|||||||
/** @see date_time_check */
|
/** @see date_time_check */
|
||||||
void rfc3339_time_check(const std::string &value)
|
void rfc3339_time_check(const std::string &value)
|
||||||
{
|
{
|
||||||
const static std::regex timeRegex{R"(^([0-9]{2})\:([0-9]{2})\:([0-9]{2})(\.[0-9]+)?(?:[Zz]|((?:\+|\-)[0-9]{2})\:([0-9]{2}))$)"};
|
const static REGEX_NAMESPACE::regex timeRegex{R"(^([0-9]{2})\:([0-9]{2})\:([0-9]{2})(\.[0-9]+)?(?:[Zz]|((?:\+|\-)[0-9]{2})\:([0-9]{2}))$)"};
|
||||||
|
|
||||||
std::smatch matches;
|
REGEX_NAMESPACE::smatch matches;
|
||||||
if (!std::regex_match(value, matches, timeRegex)) {
|
if (!REGEX_NAMESPACE::regex_match(value, matches, timeRegex)) {
|
||||||
throw std::invalid_argument(value + " is not a time string according to RFC 3339.");
|
throw std::invalid_argument(value + " is not a time string according to RFC 3339.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,10 +141,10 @@ void rfc3339_time_check(const std::string &value)
|
|||||||
*/
|
*/
|
||||||
void rfc3339_date_time_check(const std::string &value)
|
void rfc3339_date_time_check(const std::string &value)
|
||||||
{
|
{
|
||||||
const static std::regex dateTimeRegex{R"(^([0-9]{4}\-[0-9]{2}\-[0-9]{2})[Tt]([0-9]{2}\:[0-9]{2}\:[0-9]{2}(?:\.[0-9]+)?(?:[Zz]|(?:\+|\-)[0-9]{2}\:[0-9]{2}))$)"};
|
const static REGEX_NAMESPACE::regex dateTimeRegex{R"(^([0-9]{4}\-[0-9]{2}\-[0-9]{2})[Tt]([0-9]{2}\:[0-9]{2}\:[0-9]{2}(?:\.[0-9]+)?(?:[Zz]|(?:\+|\-)[0-9]{2}\:[0-9]{2}))$)"};
|
||||||
|
|
||||||
std::smatch matches;
|
REGEX_NAMESPACE::smatch matches;
|
||||||
if (!std::regex_match(value, matches, dateTimeRegex)) {
|
if (!REGEX_NAMESPACE::regex_match(value, matches, dateTimeRegex)) {
|
||||||
throw std::invalid_argument(value + " is not a date-time string according to RFC 3339.");
|
throw std::invalid_argument(value + " is not a date-time string according to RFC 3339.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,9 +330,9 @@ void rfc3986_uri_check(const std::string &value)
|
|||||||
R"((?:\#((?:[A-Za-z0-9\-._~!$&'()*+,;=:@\/?]|%[0-9A-Fa-f]{2})*))?)"};
|
R"((?:\#((?:[A-Za-z0-9\-._~!$&'()*+,;=:@\/?]|%[0-9A-Fa-f]{2})*))?)"};
|
||||||
const static std::string uriFormat{scheme + hierPart + query + fragment};
|
const static std::string uriFormat{scheme + hierPart + query + fragment};
|
||||||
|
|
||||||
const static std::regex uriRegex{uriFormat};
|
const static REGEX_NAMESPACE::regex uriRegex{uriFormat};
|
||||||
|
|
||||||
if (!std::regex_match(value, uriRegex)) {
|
if (!REGEX_NAMESPACE::regex_match(value, uriRegex)) {
|
||||||
throw std::invalid_argument(value + " is not a URI string according to RFC 3986.");
|
throw std::invalid_argument(value + " is not a URI string according to RFC 3986.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -362,28 +372,28 @@ void default_string_format_check(const std::string &format, const std::string &v
|
|||||||
throw std::invalid_argument(value + " is not a valid idn-email according to RFC 6531.");
|
throw std::invalid_argument(value + " is not a valid idn-email according to RFC 6531.");
|
||||||
}
|
}
|
||||||
} else if (format == "hostname") {
|
} else if (format == "hostname") {
|
||||||
static const std::regex hostRegex{hostname};
|
static const REGEX_NAMESPACE::regex hostRegex{hostname};
|
||||||
if (!std::regex_match(value, hostRegex)) {
|
if (!REGEX_NAMESPACE::regex_match(value, hostRegex)) {
|
||||||
throw std::invalid_argument(value + " is not a valid hostname according to RFC 3986 Appendix A.");
|
throw std::invalid_argument(value + " is not a valid hostname according to RFC 3986 Appendix A.");
|
||||||
}
|
}
|
||||||
} else if (format == "ipv4") {
|
} else if (format == "ipv4") {
|
||||||
const static std::regex ipv4Regex{"^" + ipv4Address + "$"};
|
const static REGEX_NAMESPACE::regex ipv4Regex{"^" + ipv4Address + "$"};
|
||||||
if (!std::regex_match(value, ipv4Regex)) {
|
if (!REGEX_NAMESPACE::regex_match(value, ipv4Regex)) {
|
||||||
throw std::invalid_argument(value + " is not an IPv4 string according to RFC 2673.");
|
throw std::invalid_argument(value + " is not an IPv4 string according to RFC 2673.");
|
||||||
}
|
}
|
||||||
} else if (format == "ipv6") {
|
} else if (format == "ipv6") {
|
||||||
static const std::regex ipv6Regex{ipv6Address};
|
static const REGEX_NAMESPACE::regex ipv6Regex{ipv6Address};
|
||||||
if (!std::regex_match(value, ipv6Regex)) {
|
if (!REGEX_NAMESPACE::regex_match(value, ipv6Regex)) {
|
||||||
throw std::invalid_argument(value + " is not an IPv6 string according to RFC 5954.");
|
throw std::invalid_argument(value + " is not an IPv6 string according to RFC 5954.");
|
||||||
}
|
}
|
||||||
} else if (format == "uuid") {
|
} else if (format == "uuid") {
|
||||||
static const std::regex uuidRegex{uuid};
|
static const REGEX_NAMESPACE::regex uuidRegex{uuid};
|
||||||
if (!std::regex_match(value, uuidRegex)) {
|
if (!REGEX_NAMESPACE::regex_match(value, uuidRegex)) {
|
||||||
throw std::invalid_argument(value + " is not an uuid string according to RFC 4122.");
|
throw std::invalid_argument(value + " is not an uuid string according to RFC 4122.");
|
||||||
}
|
}
|
||||||
} else if (format == "regex") {
|
} else if (format == "regex") {
|
||||||
try {
|
try {
|
||||||
std::regex re(value, std::regex::ECMAScript);
|
REGEX_NAMESPACE::regex re(value, std::regex::ECMAScript);
|
||||||
} catch (std::exception &exception) {
|
} catch (std::exception &exception) {
|
||||||
throw exception;
|
throw exception;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user