gcc-4.8: if less than 4.9 use boost::regex, if detected

This commit is contained in:
Patrick Boettcher 2017-03-06 10:44:20 +01:00
parent 46bafd05b8
commit dd2d95cf0a
2 changed files with 27 additions and 13 deletions

View File

@ -35,6 +35,22 @@ target_link_libraries(json-schema-validator
PUBLIC
json-hpp)
# regex with boost if gcc < 4.8 - default is std::regex
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9.0")
find_package(Boost COMPONENTS regex)
if(NOT Boost_FOUND)
message(WARNING "GCC less then 4.9 and boost-regex NOT found - no regex used")
target_compile_definitions(json-schema-validator PRIVATE -DJSON_SCHEMA_NO_REGEX)
else()
message(WARNING "GCC less then 4.9 and boost-regex FOUND - using boost::regex")
target_compile_definitions(json-schema-validator PRIVATE -DJSON_SCHEMA_BOOST_REGEX)
target_include_directories(json-schema-validator PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(json-schema-validator PRIVATE ${Boost_LIBRARIES})
endif()
endif()
endif()
if(NOT TARGET json-hpp) # if used as a subdirectory do not install json-schema.hpp
install(
FILES

View File

@ -30,16 +30,14 @@
using nlohmann::json;
using nlohmann::json_uri;
#if defined(__GNUC__)
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#if GCC_VERSION < 40900
#define NO_STD_REGEX
#endif
#undef GCC_VERSION
#endif
#ifndef NO_STD_REGEX
#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
namespace
@ -609,9 +607,9 @@ void json_validator::validate_object(const json &instance, const json &schema, c
for (auto pp = patternProperties.begin();
pp != patternProperties.end(); ++pp) {
#ifndef NO_STD_REGEX
std::regex re(pp.key(), std::regex::ECMAScript);
REGEX_NAMESPACE::regex re(pp.key(), REGEX_NAMESPACE::regex::ECMAScript);
if (std::regex_search(child.key(), re)) {
if (REGEX_NAMESPACE::regex_search(child.key(), re)) {
validate(child.value(), pp.value(), child_name);
property_or_patternProperties_has_validated = true;
}
@ -719,8 +717,8 @@ void json_validator::validate_string(const json &instance, const json &schema, c
// pattern
attr = schema.find("pattern");
if (attr != schema.end()) {
std::regex re(attr.value().get<std::string>(), std::regex::ECMAScript);
if (!std::regex_search(instance.get<std::string>(), re))
REGEX_NAMESPACE::regex re(attr.value().get<std::string>(), REGEX_NAMESPACE::regex::ECMAScript);
if (!REGEX_NAMESPACE::regex_search(instance.get<std::string>(), re))
throw std::invalid_argument(instance.get<std::string>() + " does not match regex pattern: " + attr.value().get<std::string>() + " for " + name);
}
#endif