diff --git a/include/rapidjson/encodedstream.h b/include/rapidjson/encodedstream.h index f2f3ea3..25936d2 100644 --- a/include/rapidjson/encodedstream.h +++ b/include/rapidjson/encodedstream.h @@ -109,6 +109,7 @@ public: \param type UTF encoding type if it is not detected from the stream. */ AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); DetectType(); static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) }; takeFunc_ = f[type_]; @@ -189,8 +190,6 @@ private: case kUTF32BE: RAPIDJSON_ASSERT(sizeof(Ch) >= 4); break; - default: - RAPIDJSON_ASSERT(false); // Invalid type } } @@ -220,6 +219,8 @@ public: \param putBOM Whether to write BOM at the beginning of the stream. */ AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + // RUntime check whether the size of character type is sufficient. It only perform checks with assertion. switch (type_) { case kUTF16LE: @@ -233,8 +234,6 @@ public: case kUTF8: // Do nothing break; - default: - RAPIDJSON_ASSERT(false); // Invalid UTFType } static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) }; diff --git a/test/unittest/encodedstreamtest.cpp b/test/unittest/encodedstreamtest.cpp index c2920a0..af2171a 100644 --- a/test/unittest/encodedstreamtest.cpp +++ b/test/unittest/encodedstreamtest.cpp @@ -119,10 +119,11 @@ protected: } EXPECT_EQ('\0', s.Peek()); free(data); + EXPECT_EQ(size, eis.Tell()); } } - void TestAutoUTFInputStream(const char *filename) { + void TestAutoUTFInputStream(const char *filename, bool expectHasBOM) { // Test FileReadStream { char buffer[16]; @@ -130,6 +131,7 @@ protected: ASSERT_TRUE(fp != 0); FileReadStream fs(fp, buffer, sizeof(buffer)); AutoUTFInputStream eis(fs); + EXPECT_EQ(expectHasBOM, eis.HasBOM()); StringStream s(json_); while (eis.Peek() != '\0') { unsigned expected, actual; @@ -147,6 +149,7 @@ protected: char* data = ReadFile(filename, true, &size); MemoryStream ms(data, size); AutoUTFInputStream eis(ms); + EXPECT_EQ(expectHasBOM, eis.HasBOM()); StringStream s(json_); while (eis.Peek() != '\0') { @@ -264,16 +267,25 @@ TEST_F(EncodedStreamTest, EncodedInputStream) { } TEST_F(EncodedStreamTest, AutoUTFInputStream) { - TestAutoUTFInputStream("utf8.json"); - TestAutoUTFInputStream("utf8bom.json"); - TestAutoUTFInputStream("utf16le.json"); - TestAutoUTFInputStream("utf16lebom.json"); - TestAutoUTFInputStream("utf16be.json"); - TestAutoUTFInputStream("utf16bebom.json"); - TestAutoUTFInputStream("utf32le.json"); - TestAutoUTFInputStream("utf32lebom.json"); - TestAutoUTFInputStream("utf32be.json"); - TestAutoUTFInputStream("utf32bebom.json"); + TestAutoUTFInputStream("utf8.json", false); + TestAutoUTFInputStream("utf8bom.json", true); + TestAutoUTFInputStream("utf16le.json", false); + TestAutoUTFInputStream("utf16lebom.json",true); + TestAutoUTFInputStream("utf16be.json", false); + TestAutoUTFInputStream("utf16bebom.json",true); + TestAutoUTFInputStream("utf32le.json", false); + TestAutoUTFInputStream("utf32lebom.json",true); + TestAutoUTFInputStream("utf32be.json", false); + TestAutoUTFInputStream("utf32bebom.json", true); + + { + // Auto detection fail, use user defined UTF type + const char json[] = "{}"; + MemoryStream ms(json, sizeof(json)); + AutoUTFInputStream eis(ms, kUTF8); + EXPECT_FALSE(eis.HasBOM()); + EXPECT_EQ(kUTF8, eis.GetType()); + } } TEST_F(EncodedStreamTest, EncodedOutputStream) {