diff --git a/include/rapidjson/encodings.h b/include/rapidjson/encodings.h index fa9979e..bc3cd81 100644 --- a/include/rapidjson/encodings.h +++ b/include/rapidjson/encodings.h @@ -616,22 +616,6 @@ struct Transcoder { } }; -//! Returns number of code points in a encoded string. -template -bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) { - GenericStringStream is(s); - const typename Encoding::Ch* end = s + length; - SizeType count = 0; - while (is.src_ < end) { - unsigned codepoint; - if (!Encoding::Decode(is, &codepoint)) - return false; - count++; - } - *outCount = count; - return true; -} - RAPIDJSON_NAMESPACE_END #if defined(__GNUC__) || defined(_MSV_VER) diff --git a/include/rapidjson/internal/strfunc.h b/include/rapidjson/internal/strfunc.h index f6c99db..040ca1e 100644 --- a/include/rapidjson/internal/strfunc.h +++ b/include/rapidjson/internal/strfunc.h @@ -33,6 +33,22 @@ inline SizeType StrLen(const Ch* s) { return SizeType(p - s); } +//! Returns number of code points in a encoded string. +template +bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) { + GenericStringStream is(s); + const typename Encoding::Ch* end = s + length; + SizeType count = 0; + while (is.src_ < end) { + unsigned codepoint; + if (!Encoding::Decode(is, &codepoint)) + return false; + count++; + } + *outCount = count; + return true; +} + } // namespace internal RAPIDJSON_NAMESPACE_END diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 3871e5c..7521962 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -306,6 +306,7 @@ public: #if RAPIDJSON_SCHEMA_HAS_REGEX delete [] patternProperties_; #endif + delete additionalItemsSchema_; delete itemsList_; for (SizeType i = 0; i < itemsTupleCount_; i++) delete itemsTuple_[i]; @@ -431,7 +432,7 @@ public: // return false; if (minLength_ != 0 || maxLength_ != SizeType(~0)) { SizeType count; - if (CountStringCodePoint(str, length, &count) && (count < minLength_ || count > maxLength_)) + if (internal::CountStringCodePoint(str, length, &count) && (count < minLength_ || count > maxLength_)) return false; } diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index cd69a76..61696d5 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -13,6 +13,7 @@ set(UNITTEST_SOURCES readertest.cpp schematest.cpp simdtest.cpp + strfunctest.cpp stringbuffertest.cpp strtodtest.cpp unittest.cpp diff --git a/test/unittest/encodingstest.cpp b/test/unittest/encodingstest.cpp index f3e90d2..b697d91 100644 --- a/test/unittest/encodingstest.cpp +++ b/test/unittest/encodingstest.cpp @@ -424,14 +424,3 @@ TEST(EncodingsTest, UTF32) { } } } - -TEST(EncodingsTest, CountStringCodePoint) { - SizeType count; - EXPECT_TRUE(CountStringCodePoint >("", 0, &count)); - EXPECT_EQ(0u, count); - EXPECT_TRUE(CountStringCodePoint >("Hello", 5, &count)); - EXPECT_EQ(5u, count); - EXPECT_TRUE(CountStringCodePoint >("\xC2\xA2\xE2\x82\xAC\xF0\x9D\x84\x9E", 9, &count)); // cents euro G-clef - EXPECT_EQ(3u, count); - EXPECT_FALSE(CountStringCodePoint >("\xC2\xA2\xE2\x82\xAC\xF0\x9D\x84\x9E\x80", 10, &count)); -} diff --git a/test/unittest/strfunctest.cpp b/test/unittest/strfunctest.cpp new file mode 100644 index 0000000..3e1a1ce --- /dev/null +++ b/test/unittest/strfunctest.cpp @@ -0,0 +1,31 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include "unittest.h" + +#include "rapidjson/internal/strfunc.h" + +using namespace rapidjson; +using namespace rapidjson::internal; + +TEST(StrFunc, CountStringCodePoint) { + SizeType count; + EXPECT_TRUE(CountStringCodePoint >("", 0, &count)); + EXPECT_EQ(0u, count); + EXPECT_TRUE(CountStringCodePoint >("Hello", 5, &count)); + EXPECT_EQ(5u, count); + EXPECT_TRUE(CountStringCodePoint >("\xC2\xA2\xE2\x82\xAC\xF0\x9D\x84\x9E", 9, &count)); // cents euro G-clef + EXPECT_EQ(3u, count); + EXPECT_FALSE(CountStringCodePoint >("\xC2\xA2\xE2\x82\xAC\xF0\x9D\x84\x9E\x80", 10, &count)); +} \ No newline at end of file