From 821c6ab73cf389f69d226150717094049c75223a Mon Sep 17 00:00:00 2001 From: "miloyip@gmail.com" Date: Tue, 13 Nov 2012 02:58:56 +0000 Subject: [PATCH] Fixed Issue 44 SetStringRaw doesn't work with wchar_t git-svn-id: https://rapidjson.googlecode.com/svn/trunk@65 c5894555-1306-4e8d-425f-1f6f381ee07c --- include/rapidjson/document.h | 4 ++-- test/unittest/documenttest.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 9782107..fc65838 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -650,9 +650,9 @@ private: void SetStringRaw(const Ch* s, SizeType length, Allocator& allocator) { RAPIDJSON_ASSERT(s != NULL); flags_ = kCopyStringFlag; - data_.s.str = (Ch *)allocator.Malloc(length + 1); + data_.s.str = (Ch *)allocator.Malloc((length + 1) * sizeof(Ch)); data_.s.length = length; - memcpy((void*)data_.s.str, s, length); + memcpy((void*)data_.s.str, s, length * sizeof(Ch)); ((Ch*)data_.s.str)[length] = '\0'; } diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index 9a4dcef..869a71c 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -67,3 +67,18 @@ TEST(Document, AcceptWriter) { EXPECT_EQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}", os.str()); } + +// Issue 44: SetStringRaw doesn't work with wchar_t +TEST(Document, UTF16_Document) { + GenericDocument< UTF16<> > json; + json.Parse(L"[{\"created_at\":\"Wed Oct 30 17:13:20 +0000 2012\"}]"); + + ASSERT_TRUE(json.IsArray()); + GenericValue< UTF16<> >& v = json[0u]; + ASSERT_TRUE(v.IsObject()); + + GenericValue< UTF16<> >& s = v[L"created_at"]; + ASSERT_TRUE(s.IsString()); + + EXPECT_EQ(0, wcscmp(L"Wed Oct 30 17:13:20 +0000 2012", s.GetString())); +}