diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 99ef053..4156146 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -93,10 +93,13 @@ enum PatternValidatorType { template struct SchemaValidationContext { SchemaValidationContext(const BaseSchema* s) : - schema(s), valueSchema(), notValidator(), objectDependencies(), + schema(s), + valueSchema(), patternPropertiesSchemas(), + notValidator(), patternPropertiesSchemaCount(), valuePatternValidatorType(kPatternValidatorOnly), + objectDependencies(), inArray(false) { } @@ -115,10 +118,10 @@ struct SchemaValidationContext { SchemaValidatorArray dependencyValidators; SchemaValidatorArray patternPropertiesValidators; const BaseSchema** patternPropertiesSchemas; + GenericSchemaValidator, CrtAllocator>* notValidator; SizeType patternPropertiesSchemaCount; PatternValidatorType valuePatternValidatorType; PatternValidatorType objectPatternValidatorType; - GenericSchemaValidator, CrtAllocator>* notValidator; SizeType objectRequiredCount; SizeType arrayElementIndex; bool* objectDependencies; @@ -612,6 +615,12 @@ public: } private: +#if RAPIDJSON_SCHEMA_USE_STDREGEX + typedef std::basic_regex* RegexType; +#else + typedef char RegexType; +#endif + typedef GenericSchemaValidator, CrtAllocator> SchemaValidatorType; static const BaseSchema* GetTypeless() { static BaseSchema typeless(Value(kObjectType).Move()); @@ -661,25 +670,25 @@ private: #if RAPIDJSON_SCHEMA_USE_STDREGEX template - static std::basic_regex* CreatePattern(const ValueType& value) { + static RegexType* CreatePattern(const ValueType& value) { if (value.IsString()) try { - return new std::basic_regex(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript); + return new RegexType(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript); } catch (const std::regex_error&) { } return 0; } - static bool IsPatternMatch(const std::basic_regex* pattern, const Ch *str, SizeType length) { + static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType length) { std::match_results r; return std::regex_search(str, str + length, r, *pattern); } #else template - void* CreatePattern(const ValueType&) { return 0; } + RegexType* CreatePattern(const ValueType&) { return 0; } - static bool IsPatternMatch(const void*, const Ch *, SizeType) { return true; } + static bool IsPatternMatch(const RegexType*, const Ch *, SizeType) { return true; } #endif // RAPIDJSON_SCHEMA_USE_STDREGEX void AddType(const Value& type) { @@ -781,11 +790,7 @@ private: } BaseSchema* schema; -#if RAPIDJSON_SCHEMA_USE_STDREGEX - std::basic_regex* pattern; -#else - void *pattern; -#endif + RegexType* pattern; }; MemoryPoolAllocator<> allocator_; @@ -816,11 +821,7 @@ private: SizeType maxItems_; bool additionalItems_; -#if RAPIDJSON_SCHEMA_USE_STDREGEX - std::basic_regex* pattern_; -#else - void* pattern_; -#endif + RegexType* pattern_; SizeType minLength_; SizeType maxLength_; diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index dd1e163..8755371 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -149,6 +149,7 @@ TEST(SchemaValidator, String_LengthRange) { VALIDATE(s, "\"ABCD\"", false); } +#if RAPIDJSON_SCHEMA_HAS_REGEX TEST(SchemaValidator, String_Pattern) { Document sd; sd.Parse("{\"type\":\"string\",\"pattern\":\"^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$\"}"); @@ -159,6 +160,7 @@ TEST(SchemaValidator, String_Pattern) { VALIDATE(s, "\"(888)555-1212 ext. 532\"", false); VALIDATE(s, "\"(800)FLOWERS\"", false); } +#endif TEST(SchemaValidator, Integer) { Document sd;