diff --git a/CMakeLists.txt b/CMakeLists.txt index 8104556..42f2e21 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/json-validator.cpp b/src/json-validator.cpp index 503f6ae..811b772 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -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 + #define REGEX_NAMESPACE boost +#elif defined(JSON_SCHEMA_NO_REGEX) + #define NO_STD_REGEX +#else #include + #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::regex::ECMAScript); - if (!std::regex_search(instance.get(), re)) + REGEX_NAMESPACE::regex re(attr.value().get(), REGEX_NAMESPACE::regex::ECMAScript); + if (!REGEX_NAMESPACE::regex_search(instance.get(), re)) throw std::invalid_argument(instance.get() + " does not match regex pattern: " + attr.value().get() + " for " + name); } #endif