Commit 761561a2 authored by Milo Yip's avatar Milo Yip

Fix clang compilation and a memory leak

parent 1948fb57
......@@ -616,22 +616,6 @@ struct Transcoder<Encoding, Encoding> {
}
};
//! Returns number of code points in a encoded string.
template<typename Encoding>
bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
GenericStringStream<Encoding> 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)
......
......@@ -33,6 +33,22 @@ inline SizeType StrLen(const Ch* s) {
return SizeType(p - s);
}
//! Returns number of code points in a encoded string.
template<typename Encoding>
bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
GenericStringStream<Encoding> 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
......
......@@ -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<Encoding>(str, length, &count) && (count < minLength_ || count > maxLength_))
if (internal::CountStringCodePoint<Encoding>(str, length, &count) && (count < minLength_ || count > maxLength_))
return false;
}
......
......@@ -13,6 +13,7 @@ set(UNITTEST_SOURCES
readertest.cpp
schematest.cpp
simdtest.cpp
strfunctest.cpp
stringbuffertest.cpp
strtodtest.cpp
unittest.cpp
......
......@@ -424,14 +424,3 @@ TEST(EncodingsTest, UTF32) {
}
}
}
TEST(EncodingsTest, CountStringCodePoint) {
SizeType count;
EXPECT_TRUE(CountStringCodePoint<UTF8<> >("", 0, &count));
EXPECT_EQ(0u, count);
EXPECT_TRUE(CountStringCodePoint<UTF8<> >("Hello", 5, &count));
EXPECT_EQ(5u, count);
EXPECT_TRUE(CountStringCodePoint<UTF8<> >("\xC2\xA2\xE2\x82\xAC\xF0\x9D\x84\x9E", 9, &count)); // cents euro G-clef
EXPECT_EQ(3u, count);
EXPECT_FALSE(CountStringCodePoint<UTF8<> >("\xC2\xA2\xE2\x82\xAC\xF0\x9D\x84\x9E\x80", 10, &count));
}
// 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<UTF8<> >("", 0, &count));
EXPECT_EQ(0u, count);
EXPECT_TRUE(CountStringCodePoint<UTF8<> >("Hello", 5, &count));
EXPECT_EQ(5u, count);
EXPECT_TRUE(CountStringCodePoint<UTF8<> >("\xC2\xA2\xE2\x82\xAC\xF0\x9D\x84\x9E", 9, &count)); // cents euro G-clef
EXPECT_EQ(3u, count);
EXPECT_FALSE(CountStringCodePoint<UTF8<> >("\xC2\xA2\xE2\x82\xAC\xF0\x9D\x84\x9E\x80", 10, &count));
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment