Add pugixml-1.4 to contrib
authorSam Moore <matches@ucc.asn.au>
Wed, 30 Apr 2014 13:35:16 +0000 (21:35 +0800)
committerSam Moore <matches@ucc.asn.au>
Wed, 30 Apr 2014 13:35:16 +0000 (21:35 +0800)
pugixml is a "lightweight" XML parser implemented in C++

contrib/pugixml-1.4/contrib/foreach.hpp [new file with mode: 0644]
contrib/pugixml-1.4/readme.txt [new file with mode: 0644]
contrib/pugixml-1.4/src/pugiconfig.hpp [new file with mode: 0644]
contrib/pugixml-1.4/src/pugixml.cpp [new file with mode: 0644]
contrib/pugixml-1.4/src/pugixml.hpp [new file with mode: 0644]

diff --git a/contrib/pugixml-1.4/contrib/foreach.hpp b/contrib/pugixml-1.4/contrib/foreach.hpp
new file mode 100644 (file)
index 0000000..26f29a7
--- /dev/null
@@ -0,0 +1,64 @@
+/*\r
+ * Boost.Foreach support for pugixml classes.\r
+ * This file is provided to the public domain.\r
+ * Written by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)\r
+ */\r
+\r
+#ifndef HEADER_PUGIXML_FOREACH_HPP\r
+#define HEADER_PUGIXML_FOREACH_HPP\r
+\r
+#include "pugixml.hpp"\r
+\r
+/*\r
+ * These types add support for BOOST_FOREACH macro to xml_node and xml_document classes (child iteration only).\r
+ * Example usage:\r
+ * BOOST_FOREACH(xml_node n, doc) {}\r
+ */\r
+\r
+namespace boost\r
+{\r
+       template <typename> struct range_mutable_iterator;\r
+       template <typename> struct range_const_iterator;\r
+\r
+       template<> struct range_mutable_iterator<pugi::xml_node>\r
+       {\r
+               typedef pugi::xml_node::iterator type;\r
+       };\r
+\r
+       template<> struct range_const_iterator<pugi::xml_node>\r
+       {\r
+               typedef pugi::xml_node::iterator type;\r
+       };\r
+\r
+       template<> struct range_mutable_iterator<pugi::xml_document>\r
+       {\r
+               typedef pugi::xml_document::iterator type;\r
+       };\r
+\r
+       template<> struct range_const_iterator<pugi::xml_document>\r
+       {\r
+               typedef pugi::xml_document::iterator type;\r
+       };\r
+}\r
+\r
+/*\r
+ * These types add support for BOOST_FOREACH macro to xml_node and xml_document classes (child/attribute iteration).\r
+ * Example usage:\r
+ * BOOST_FOREACH(xml_node n, children(doc)) {}\r
+ * BOOST_FOREACH(xml_node n, attributes(doc)) {}\r
+ */\r
+\r
+namespace pugi\r
+{\r
+       inline xml_object_range<xml_node_iterator> children(const pugi::xml_node& node)\r
+       {\r
+        return node.children();\r
+       }\r
+\r
+       inline xml_object_range<xml_attribute_iterator> attributes(const pugi::xml_node& node)\r
+       {\r
+        return node.attributes();\r
+       }\r
+}\r
+\r
+#endif\r
diff --git a/contrib/pugixml-1.4/readme.txt b/contrib/pugixml-1.4/readme.txt
new file mode 100644 (file)
index 0000000..5a47498
--- /dev/null
@@ -0,0 +1,52 @@
+pugixml 1.4 - an XML processing library\r
+\r
+Copyright (C) 2006-2014, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)\r
+Report bugs and download new versions at http://pugixml.org/\r
+\r
+This is the distribution of pugixml, which is a C++ XML processing library,\r
+which consists of a DOM-like interface with rich traversal/modification\r
+capabilities, an extremely fast XML parser which constructs the DOM tree from\r
+an XML file/buffer, and an XPath 1.0 implementation for complex data-driven\r
+tree queries. Full Unicode support is also available, with Unicode interface\r
+variants and conversions between different Unicode encodings (which happen\r
+automatically during parsing/saving).\r
+\r
+The distribution contains the following folders:\r
+\r
+       contrib/ - various contributions to pugixml\r
+\r
+       docs/ - documentation\r
+               docs/samples - pugixml usage examples\r
+               docs/quickstart.html - quick start guide\r
+               docs/manual.html - complete manual\r
+\r
+       scripts/ - project files for IDE/build systems\r
+\r
+       src/ - header and source files\r
+\r
+       readme.txt - this file.\r
+\r
+This library is distributed under the MIT License:\r
+\r
+Copyright (c) 2006-2014 Arseny Kapoulkine\r
+\r
+Permission is hereby granted, free of charge, to any person\r
+obtaining a copy of this software and associated documentation\r
+files (the "Software"), to deal in the Software without\r
+restriction, including without limitation the rights to use,\r
+copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+copies of the Software, and to permit persons to whom the\r
+Software is furnished to do so, subject to the following\r
+conditions:\r
+\r
+The above copyright notice and this permission notice shall be\r
+included in all copies or substantial portions of the Software.\r
+\r
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\r
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\r
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\r
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\r
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\r
+OTHER DEALINGS IN THE SOFTWARE.\r
diff --git a/contrib/pugixml-1.4/src/pugiconfig.hpp b/contrib/pugixml-1.4/src/pugiconfig.hpp
new file mode 100644 (file)
index 0000000..39550f6
--- /dev/null
@@ -0,0 +1,72 @@
+/**\r
+ * pugixml parser - version 1.4\r
+ * --------------------------------------------------------\r
+ * Copyright (C) 2006-2014, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)\r
+ * Report bugs and download new versions at http://pugixml.org/\r
+ *\r
+ * This library is distributed under the MIT License. See notice at the end\r
+ * of this file.\r
+ *\r
+ * This work is based on the pugxml parser, which is:\r
+ * Copyright (C) 2003, by Kristen Wegner (kristen@tima.net)\r
+ */\r
+\r
+#ifndef HEADER_PUGICONFIG_HPP\r
+#define HEADER_PUGICONFIG_HPP\r
+\r
+// Uncomment this to enable wchar_t mode\r
+// #define PUGIXML_WCHAR_MODE\r
+\r
+// Uncomment this to disable XPath\r
+// #define PUGIXML_NO_XPATH\r
+\r
+// Uncomment this to disable STL\r
+// #define PUGIXML_NO_STL\r
+\r
+// Uncomment this to disable exceptions\r
+// #define PUGIXML_NO_EXCEPTIONS\r
+\r
+// Set this to control attributes for public classes/functions, i.e.:\r
+// #define PUGIXML_API __declspec(dllexport) // to export all public symbols from DLL\r
+// #define PUGIXML_CLASS __declspec(dllimport) // to import all classes from DLL\r
+// #define PUGIXML_FUNCTION __fastcall // to set calling conventions to all public functions to fastcall\r
+// In absence of PUGIXML_CLASS/PUGIXML_FUNCTION definitions PUGIXML_API is used instead\r
+\r
+// Tune these constants to adjust memory-related behavior\r
+// #define PUGIXML_MEMORY_PAGE_SIZE 32768\r
+// #define PUGIXML_MEMORY_OUTPUT_STACK 10240\r
+// #define PUGIXML_MEMORY_XPATH_PAGE_SIZE 4096\r
+\r
+// Uncomment this to switch to header-only version\r
+// #define PUGIXML_HEADER_ONLY\r
+// #include "pugixml.cpp"\r
+\r
+// Uncomment this to enable long long support\r
+// #define PUGIXML_HAS_LONG_LONG\r
+\r
+#endif\r
+\r
+/**\r
+ * Copyright (c) 2006-2014 Arseny Kapoulkine\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person\r
+ * obtaining a copy of this software and associated documentation\r
+ * files (the "Software"), to deal in the Software without\r
+ * restriction, including without limitation the rights to use,\r
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ * copies of the Software, and to permit persons to whom the\r
+ * Software is furnished to do so, subject to the following\r
+ * conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be\r
+ * included in all copies or substantial portions of the Software.\r
+ * \r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\r
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\r
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\r
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\r
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\r
+ * OTHER DEALINGS IN THE SOFTWARE.\r
+ */\r
diff --git a/contrib/pugixml-1.4/src/pugixml.cpp b/contrib/pugixml-1.4/src/pugixml.cpp
new file mode 100644 (file)
index 0000000..4936394
--- /dev/null
@@ -0,0 +1,10639 @@
+/**\r
+ * pugixml parser - version 1.4\r
+ * --------------------------------------------------------\r
+ * Copyright (C) 2006-2014, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)\r
+ * Report bugs and download new versions at http://pugixml.org/\r
+ *\r
+ * This library is distributed under the MIT License. See notice at the end\r
+ * of this file.\r
+ *\r
+ * This work is based on the pugxml parser, which is:\r
+ * Copyright (C) 2003, by Kristen Wegner (kristen@tima.net)\r
+ */\r
+\r
+#ifndef SOURCE_PUGIXML_CPP\r
+#define SOURCE_PUGIXML_CPP\r
+\r
+#include "pugixml.hpp"\r
+\r
+#include <stdlib.h>\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include <assert.h>\r
+\r
+#ifdef PUGIXML_WCHAR_MODE\r
+#      include <wchar.h>\r
+#endif\r
+\r
+#ifndef PUGIXML_NO_XPATH\r
+#      include <math.h>\r
+#      include <float.h>\r
+#      ifdef PUGIXML_NO_EXCEPTIONS\r
+#              include <setjmp.h>\r
+#      endif\r
+#endif\r
+\r
+#ifndef PUGIXML_NO_STL\r
+#      include <istream>\r
+#      include <ostream>\r
+#      include <string>\r
+#endif\r
+\r
+// For placement new\r
+#include <new>\r
+\r
+#ifdef _MSC_VER\r
+#      pragma warning(push)\r
+#      pragma warning(disable: 4127) // conditional expression is constant\r
+#      pragma warning(disable: 4324) // structure was padded due to __declspec(align())\r
+#      pragma warning(disable: 4611) // interaction between '_setjmp' and C++ object destruction is non-portable\r
+#      pragma warning(disable: 4702) // unreachable code\r
+#      pragma warning(disable: 4996) // this function or variable may be unsafe\r
+#      pragma warning(disable: 4793) // function compiled as native: presence of '_setjmp' makes a function unmanaged\r
+#endif\r
+\r
+#ifdef __INTEL_COMPILER\r
+#      pragma warning(disable: 177) // function was declared but never referenced \r
+#      pragma warning(disable: 279) // controlling expression is constant\r
+#      pragma warning(disable: 1478 1786) // function was declared "deprecated"\r
+#      pragma warning(disable: 1684) // conversion from pointer to same-sized integral type\r
+#endif\r
+\r
+#if defined(__BORLANDC__) && defined(PUGIXML_HEADER_ONLY)\r
+#      pragma warn -8080 // symbol is declared but never used; disabling this inside push/pop bracket does not make the warning go away\r
+#endif\r
+\r
+#ifdef __BORLANDC__\r
+#      pragma option push\r
+#      pragma warn -8008 // condition is always false\r
+#      pragma warn -8066 // unreachable code\r
+#endif\r
+\r
+#ifdef __SNC__\r
+// Using diag_push/diag_pop does not disable the warnings inside templates due to a compiler bug\r
+#      pragma diag_suppress=178 // function was declared but never referenced\r
+#      pragma diag_suppress=237 // controlling expression is constant\r
+#endif\r
+\r
+// Inlining controls\r
+#if defined(_MSC_VER) && _MSC_VER >= 1300\r
+#      define PUGI__NO_INLINE __declspec(noinline)\r
+#elif defined(__GNUC__)\r
+#      define PUGI__NO_INLINE __attribute__((noinline))\r
+#else\r
+#      define PUGI__NO_INLINE \r
+#endif\r
+\r
+// Simple static assertion\r
+#define PUGI__STATIC_ASSERT(cond) { static const char condition_failed[(cond) ? 1 : -1] = {0}; (void)condition_failed[0]; }\r
+\r
+// Digital Mars C++ bug workaround for passing char loaded from memory via stack\r
+#ifdef __DMC__\r
+#      define PUGI__DMC_VOLATILE volatile\r
+#else\r
+#      define PUGI__DMC_VOLATILE\r
+#endif\r
+\r
+// Borland C++ bug workaround for not defining ::memcpy depending on header include order (can't always use std::memcpy because some compilers don't have it at all)\r
+#if defined(__BORLANDC__) && !defined(__MEM_H_USING_LIST)\r
+using std::memcpy;\r
+using std::memmove;\r
+#endif\r
+\r
+// In some environments MSVC is a compiler but the CRT lacks certain MSVC-specific features\r
+#if defined(_MSC_VER) && !defined(__S3E__)\r
+#      define PUGI__MSVC_CRT_VERSION _MSC_VER\r
+#endif\r
+\r
+#ifdef PUGIXML_HEADER_ONLY\r
+#      define PUGI__NS_BEGIN namespace pugi { namespace impl {\r
+#      define PUGI__NS_END } }\r
+#      define PUGI__FN inline\r
+#      define PUGI__FN_NO_INLINE inline\r
+#else\r
+#      if defined(_MSC_VER) && _MSC_VER < 1300 // MSVC6 seems to have an amusing bug with anonymous namespaces inside namespaces\r
+#              define PUGI__NS_BEGIN namespace pugi { namespace impl {\r
+#              define PUGI__NS_END } }\r
+#      else\r
+#              define PUGI__NS_BEGIN namespace pugi { namespace impl { namespace {\r
+#              define PUGI__NS_END } } }\r
+#      endif\r
+#      define PUGI__FN\r
+#      define PUGI__FN_NO_INLINE PUGI__NO_INLINE\r
+#endif\r
+\r
+// uintptr_t\r
+#if !defined(_MSC_VER) || _MSC_VER >= 1600\r
+#      include <stdint.h>\r
+#else\r
+#      ifndef _UINTPTR_T_DEFINED\r
+// No native uintptr_t in MSVC6 and in some WinCE versions\r
+typedef size_t uintptr_t;\r
+#define _UINTPTR_T_DEFINED\r
+#      endif\r
+PUGI__NS_BEGIN\r
+       typedef unsigned __int8 uint8_t;\r
+       typedef unsigned __int16 uint16_t;\r
+       typedef unsigned __int32 uint32_t;\r
+PUGI__NS_END\r
+#endif\r
+\r
+// Memory allocation\r
+PUGI__NS_BEGIN\r
+       PUGI__FN void* default_allocate(size_t size)\r
+       {\r
+               return malloc(size);\r
+       }\r
+\r
+       PUGI__FN void default_deallocate(void* ptr)\r
+       {\r
+               free(ptr);\r
+       }\r
+\r
+       template <typename T>\r
+       struct xml_memory_management_function_storage\r
+       {\r
+               static allocation_function allocate;\r
+               static deallocation_function deallocate;\r
+       };\r
+\r
+       template <typename T> allocation_function xml_memory_management_function_storage<T>::allocate = default_allocate;\r
+       template <typename T> deallocation_function xml_memory_management_function_storage<T>::deallocate = default_deallocate;\r
+\r
+       typedef xml_memory_management_function_storage<int> xml_memory;\r
+PUGI__NS_END\r
+\r
+// String utilities\r
+PUGI__NS_BEGIN\r
+       // Get string length\r
+       PUGI__FN size_t strlength(const char_t* s)\r
+       {\r
+               assert(s);\r
+\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               return wcslen(s);\r
+       #else\r
+               return strlen(s);\r
+       #endif\r
+       }\r
+\r
+       // Compare two strings\r
+       PUGI__FN bool strequal(const char_t* src, const char_t* dst)\r
+       {\r
+               assert(src && dst);\r
+\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               return wcscmp(src, dst) == 0;\r
+       #else\r
+               return strcmp(src, dst) == 0;\r
+       #endif\r
+       }\r
+\r
+       // Compare lhs with [rhs_begin, rhs_end)\r
+       PUGI__FN bool strequalrange(const char_t* lhs, const char_t* rhs, size_t count)\r
+       {\r
+               for (size_t i = 0; i < count; ++i)\r
+                       if (lhs[i] != rhs[i])\r
+                               return false;\r
+       \r
+               return lhs[count] == 0;\r
+       }\r
+\r
+       // Get length of wide string, even if CRT lacks wide character support\r
+       PUGI__FN size_t strlength_wide(const wchar_t* s)\r
+       {\r
+               assert(s);\r
+\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               return wcslen(s);\r
+       #else\r
+               const wchar_t* end = s;\r
+               while (*end) end++;\r
+               return static_cast<size_t>(end - s);\r
+       #endif\r
+       }\r
+\r
+#ifdef PUGIXML_WCHAR_MODE\r
+       // Convert string to wide string, assuming all symbols are ASCII\r
+       PUGI__FN void widen_ascii(wchar_t* dest, const char* source)\r
+       {\r
+               for (const char* i = source; *i; ++i) *dest++ = *i;\r
+               *dest = 0;\r
+       }\r
+#endif\r
+PUGI__NS_END\r
+\r
+#if !defined(PUGIXML_NO_STL) || !defined(PUGIXML_NO_XPATH)\r
+// auto_ptr-like buffer holder for exception recovery\r
+PUGI__NS_BEGIN\r
+       struct buffer_holder\r
+       {\r
+               void* data;\r
+               void (*deleter)(void*);\r
+\r
+               buffer_holder(void* data_, void (*deleter_)(void*)): data(data_), deleter(deleter_)\r
+               {\r
+               }\r
+\r
+               ~buffer_holder()\r
+               {\r
+                       if (data) deleter(data);\r
+               }\r
+\r
+               void* release()\r
+               {\r
+                       void* result = data;\r
+                       data = 0;\r
+                       return result;\r
+               }\r
+       };\r
+PUGI__NS_END\r
+#endif\r
+\r
+PUGI__NS_BEGIN\r
+       static const size_t xml_memory_page_size =\r
+       #ifdef PUGIXML_MEMORY_PAGE_SIZE\r
+               PUGIXML_MEMORY_PAGE_SIZE\r
+       #else\r
+               32768\r
+       #endif\r
+               ;\r
+\r
+       static const uintptr_t xml_memory_page_alignment = 32;\r
+       static const uintptr_t xml_memory_page_pointer_mask = ~(xml_memory_page_alignment - 1);\r
+       static const uintptr_t xml_memory_page_name_allocated_mask = 16;\r
+       static const uintptr_t xml_memory_page_value_allocated_mask = 8;\r
+       static const uintptr_t xml_memory_page_type_mask = 7;\r
+\r
+       struct xml_allocator;\r
+\r
+       struct xml_memory_page\r
+       {\r
+               static xml_memory_page* construct(void* memory)\r
+               {\r
+                       if (!memory) return 0; //$ redundant, left for performance\r
+\r
+                       xml_memory_page* result = static_cast<xml_memory_page*>(memory);\r
+\r
+                       result->allocator = 0;\r
+                       result->memory = 0;\r
+                       result->prev = 0;\r
+                       result->next = 0;\r
+                       result->busy_size = 0;\r
+                       result->freed_size = 0;\r
+\r
+                       return result;\r
+               }\r
+\r
+               xml_allocator* allocator;\r
+\r
+               void* memory;\r
+\r
+               xml_memory_page* prev;\r
+               xml_memory_page* next;\r
+\r
+               size_t busy_size;\r
+               size_t freed_size;\r
+\r
+               char data[1];\r
+       };\r
+\r
+       struct xml_memory_string_header\r
+       {\r
+               uint16_t page_offset; // offset from page->data\r
+               uint16_t full_size; // 0 if string occupies whole page\r
+       };\r
+\r
+       struct xml_allocator\r
+       {\r
+               xml_allocator(xml_memory_page* root): _root(root), _busy_size(root->busy_size)\r
+               {\r
+               }\r
+\r
+               xml_memory_page* allocate_page(size_t data_size)\r
+               {\r
+                       size_t size = offsetof(xml_memory_page, data) + data_size;\r
+\r
+                       // allocate block with some alignment, leaving memory for worst-case padding\r
+                       void* memory = xml_memory::allocate(size + xml_memory_page_alignment);\r
+                       if (!memory) return 0;\r
+\r
+                       // align upwards to page boundary\r
+                       void* page_memory = reinterpret_cast<void*>((reinterpret_cast<uintptr_t>(memory) + (xml_memory_page_alignment - 1)) & ~(xml_memory_page_alignment - 1));\r
+\r
+                       // prepare page structure\r
+                       xml_memory_page* page = xml_memory_page::construct(page_memory);\r
+                       assert(page);\r
+\r
+                       page->memory = memory;\r
+                       page->allocator = _root->allocator;\r
+\r
+                       return page;\r
+               }\r
+\r
+               static void deallocate_page(xml_memory_page* page)\r
+               {\r
+                       xml_memory::deallocate(page->memory);\r
+               }\r
+\r
+               void* allocate_memory_oob(size_t size, xml_memory_page*& out_page);\r
+\r
+               void* allocate_memory(size_t size, xml_memory_page*& out_page)\r
+               {\r
+                       if (_busy_size + size > xml_memory_page_size) return allocate_memory_oob(size, out_page);\r
+\r
+                       void* buf = _root->data + _busy_size;\r
+\r
+                       _busy_size += size;\r
+\r
+                       out_page = _root;\r
+\r
+                       return buf;\r
+               }\r
+\r
+               void deallocate_memory(void* ptr, size_t size, xml_memory_page* page)\r
+               {\r
+                       if (page == _root) page->busy_size = _busy_size;\r
+\r
+                       assert(ptr >= page->data && ptr < page->data + page->busy_size);\r
+                       (void)!ptr;\r
+\r
+                       page->freed_size += size;\r
+                       assert(page->freed_size <= page->busy_size);\r
+\r
+                       if (page->freed_size == page->busy_size)\r
+                       {\r
+                               if (page->next == 0)\r
+                               {\r
+                                       assert(_root == page);\r
+\r
+                                       // top page freed, just reset sizes\r
+                                       page->busy_size = page->freed_size = 0;\r
+                                       _busy_size = 0;\r
+                               }\r
+                               else\r
+                               {\r
+                                       assert(_root != page);\r
+                                       assert(page->prev);\r
+\r
+                                       // remove from the list\r
+                                       page->prev->next = page->next;\r
+                                       page->next->prev = page->prev;\r
+\r
+                                       // deallocate\r
+                                       deallocate_page(page);\r
+                               }\r
+                       }\r
+               }\r
+\r
+               char_t* allocate_string(size_t length)\r
+               {\r
+                       // allocate memory for string and header block\r
+                       size_t size = sizeof(xml_memory_string_header) + length * sizeof(char_t);\r
+                       \r
+                       // round size up to pointer alignment boundary\r
+                       size_t full_size = (size + (sizeof(void*) - 1)) & ~(sizeof(void*) - 1);\r
+\r
+                       xml_memory_page* page;\r
+                       xml_memory_string_header* header = static_cast<xml_memory_string_header*>(allocate_memory(full_size, page));\r
+\r
+                       if (!header) return 0;\r
+\r
+                       // setup header\r
+                       ptrdiff_t page_offset = reinterpret_cast<char*>(header) - page->data;\r
+\r
+                       assert(page_offset >= 0 && page_offset < (1 << 16));\r
+                       header->page_offset = static_cast<uint16_t>(page_offset);\r
+\r
+                       // full_size == 0 for large strings that occupy the whole page\r
+                       assert(full_size < (1 << 16) || (page->busy_size == full_size && page_offset == 0));\r
+                       header->full_size = static_cast<uint16_t>(full_size < (1 << 16) ? full_size : 0);\r
+\r
+                       // round-trip through void* to avoid 'cast increases required alignment of target type' warning\r
+                       // header is guaranteed a pointer-sized alignment, which should be enough for char_t\r
+                       return static_cast<char_t*>(static_cast<void*>(header + 1));\r
+               }\r
+\r
+               void deallocate_string(char_t* string)\r
+               {\r
+                       // this function casts pointers through void* to avoid 'cast increases required alignment of target type' warnings\r
+                       // we're guaranteed the proper (pointer-sized) alignment on the input string if it was allocated via allocate_string\r
+\r
+                       // get header\r
+                       xml_memory_string_header* header = static_cast<xml_memory_string_header*>(static_cast<void*>(string)) - 1;\r
+\r
+                       // deallocate\r
+                       size_t page_offset = offsetof(xml_memory_page, data) + header->page_offset;\r
+                       xml_memory_page* page = reinterpret_cast<xml_memory_page*>(static_cast<void*>(reinterpret_cast<char*>(header) - page_offset));\r
+\r
+                       // if full_size == 0 then this string occupies the whole page\r
+                       size_t full_size = header->full_size == 0 ? page->busy_size : header->full_size;\r
+\r
+                       deallocate_memory(header, full_size, page);\r
+               }\r
+\r
+               xml_memory_page* _root;\r
+               size_t _busy_size;\r
+       };\r
+\r
+       PUGI__FN_NO_INLINE void* xml_allocator::allocate_memory_oob(size_t size, xml_memory_page*& out_page)\r
+       {\r
+               const size_t large_allocation_threshold = xml_memory_page_size / 4;\r
+\r
+               xml_memory_page* page = allocate_page(size <= large_allocation_threshold ? xml_memory_page_size : size);\r
+               out_page = page;\r
+\r
+               if (!page) return 0;\r
+\r
+               if (size <= large_allocation_threshold)\r
+               {\r
+                       _root->busy_size = _busy_size;\r
+\r
+                       // insert page at the end of linked list\r
+                       page->prev = _root;\r
+                       _root->next = page;\r
+                       _root = page;\r
+\r
+                       _busy_size = size;\r
+               }\r
+               else\r
+               {\r
+                       // insert page before the end of linked list, so that it is deleted as soon as possible\r
+                       // the last page is not deleted even if it's empty (see deallocate_memory)\r
+                       assert(_root->prev);\r
+\r
+                       page->prev = _root->prev;\r
+                       page->next = _root;\r
+\r
+                       _root->prev->next = page;\r
+                       _root->prev = page;\r
+               }\r
+\r
+               // allocate inside page\r
+               page->busy_size = size;\r
+\r
+               return page->data;\r
+       }\r
+PUGI__NS_END\r
+\r
+namespace pugi\r
+{\r
+       /// A 'name=value' XML attribute structure.\r
+       struct xml_attribute_struct\r
+       {\r
+               /// Default ctor\r
+               xml_attribute_struct(impl::xml_memory_page* page): header(reinterpret_cast<uintptr_t>(page)), name(0), value(0), prev_attribute_c(0), next_attribute(0)\r
+               {\r
+               }\r
+\r
+               uintptr_t header;\r
+\r
+               char_t* name;   ///< Pointer to attribute name.\r
+               char_t* value;  ///< Pointer to attribute value.\r
+\r
+               xml_attribute_struct* prev_attribute_c; ///< Previous attribute (cyclic list)\r
+               xml_attribute_struct* next_attribute;   ///< Next attribute\r
+       };\r
+\r
+       /// An XML document tree node.\r
+       struct xml_node_struct\r
+       {\r
+               /// Default ctor\r
+               /// \param type - node type\r
+               xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast<uintptr_t>(page) | (type - 1)), parent(0), name(0), value(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0)\r
+               {\r
+               }\r
+\r
+               uintptr_t header;\r
+\r
+               xml_node_struct*                parent;                                 ///< Pointer to parent\r
+\r
+               char_t*                                 name;                                   ///< Pointer to element name.\r
+               char_t*                                 value;                                  ///< Pointer to any associated string data.\r
+\r
+               xml_node_struct*                first_child;                    ///< First child\r
+               \r
+               xml_node_struct*                prev_sibling_c;                 ///< Left brother (cyclic list)\r
+               xml_node_struct*                next_sibling;                   ///< Right brother\r
+               \r
+               xml_attribute_struct*   first_attribute;                ///< First attribute\r
+       };\r
+}\r
+\r
+PUGI__NS_BEGIN\r
+       struct xml_extra_buffer\r
+       {\r
+               char_t* buffer;\r
+               xml_extra_buffer* next;\r
+       };\r
+\r
+       struct xml_document_struct: public xml_node_struct, public xml_allocator\r
+       {\r
+               xml_document_struct(xml_memory_page* page): xml_node_struct(page, node_document), xml_allocator(page), buffer(0), extra_buffers(0)\r
+               {\r
+               }\r
+\r
+               const char_t* buffer;\r
+\r
+               xml_extra_buffer* extra_buffers;\r
+       };\r
+\r
+       inline xml_allocator& get_allocator(const xml_node_struct* node)\r
+       {\r
+               assert(node);\r
+\r
+               return *reinterpret_cast<xml_memory_page*>(node->header & xml_memory_page_pointer_mask)->allocator;\r
+       }\r
+PUGI__NS_END\r
+\r
+// Low-level DOM operations\r
+PUGI__NS_BEGIN\r
+       inline xml_attribute_struct* allocate_attribute(xml_allocator& alloc)\r
+       {\r
+               xml_memory_page* page;\r
+               void* memory = alloc.allocate_memory(sizeof(xml_attribute_struct), page);\r
+\r
+               return new (memory) xml_attribute_struct(page);\r
+       }\r
+\r
+       inline xml_node_struct* allocate_node(xml_allocator& alloc, xml_node_type type)\r
+       {\r
+               xml_memory_page* page;\r
+               void* memory = alloc.allocate_memory(sizeof(xml_node_struct), page);\r
+\r
+               return new (memory) xml_node_struct(page, type);\r
+       }\r
+\r
+       inline void destroy_attribute(xml_attribute_struct* a, xml_allocator& alloc)\r
+       {\r
+               uintptr_t header = a->header;\r
+\r
+               if (header & impl::xml_memory_page_name_allocated_mask) alloc.deallocate_string(a->name);\r
+               if (header & impl::xml_memory_page_value_allocated_mask) alloc.deallocate_string(a->value);\r
+\r
+               alloc.deallocate_memory(a, sizeof(xml_attribute_struct), reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask));\r
+       }\r
+\r
+       inline void destroy_node(xml_node_struct* n, xml_allocator& alloc)\r
+       {\r
+               uintptr_t header = n->header;\r
+\r
+               if (header & impl::xml_memory_page_name_allocated_mask) alloc.deallocate_string(n->name);\r
+               if (header & impl::xml_memory_page_value_allocated_mask) alloc.deallocate_string(n->value);\r
+\r
+               for (xml_attribute_struct* attr = n->first_attribute; attr; )\r
+               {\r
+                       xml_attribute_struct* next = attr->next_attribute;\r
+\r
+                       destroy_attribute(attr, alloc);\r
+\r
+                       attr = next;\r
+               }\r
+\r
+               for (xml_node_struct* child = n->first_child; child; )\r
+               {\r
+                       xml_node_struct* next = child->next_sibling;\r
+\r
+                       destroy_node(child, alloc);\r
+\r
+                       child = next;\r
+               }\r
+\r
+               alloc.deallocate_memory(n, sizeof(xml_node_struct), reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask));\r
+       }\r
+\r
+       PUGI__FN_NO_INLINE xml_node_struct* append_node(xml_node_struct* node, xml_allocator& alloc, xml_node_type type = node_element)\r
+       {\r
+               xml_node_struct* child = allocate_node(alloc, type);\r
+               if (!child) return 0;\r
+\r
+               child->parent = node;\r
+\r
+               xml_node_struct* first_child = node->first_child;\r
+                       \r
+               if (first_child)\r
+               {\r
+                       xml_node_struct* last_child = first_child->prev_sibling_c;\r
+\r
+                       last_child->next_sibling = child;\r
+                       child->prev_sibling_c = last_child;\r
+                       first_child->prev_sibling_c = child;\r
+               }\r
+               else\r
+               {\r
+                       node->first_child = child;\r
+                       child->prev_sibling_c = child;\r
+               }\r
+                       \r
+               return child;\r
+       }\r
+\r
+       PUGI__FN_NO_INLINE xml_attribute_struct* append_attribute_ll(xml_node_struct* node, xml_allocator& alloc)\r
+       {\r
+               xml_attribute_struct* a = allocate_attribute(alloc);\r
+               if (!a) return 0;\r
+\r
+               xml_attribute_struct* first_attribute = node->first_attribute;\r
+\r
+               if (first_attribute)\r
+               {\r
+                       xml_attribute_struct* last_attribute = first_attribute->prev_attribute_c;\r
+\r
+                       last_attribute->next_attribute = a;\r
+                       a->prev_attribute_c = last_attribute;\r
+                       first_attribute->prev_attribute_c = a;\r
+               }\r
+               else\r
+               {\r
+                       node->first_attribute = a;\r
+                       a->prev_attribute_c = a;\r
+               }\r
+                       \r
+               return a;\r
+       }\r
+PUGI__NS_END\r
+\r
+// Helper classes for code generation\r
+PUGI__NS_BEGIN\r
+       struct opt_false\r
+       {\r
+               enum { value = 0 };\r
+       };\r
+\r
+       struct opt_true\r
+       {\r
+               enum { value = 1 };\r
+       };\r
+PUGI__NS_END\r
+\r
+// Unicode utilities\r
+PUGI__NS_BEGIN\r
+       inline uint16_t endian_swap(uint16_t value)\r
+       {\r
+               return static_cast<uint16_t>(((value & 0xff) << 8) | (value >> 8));\r
+       }\r
+\r
+       inline uint32_t endian_swap(uint32_t value)\r
+       {\r
+               return ((value & 0xff) << 24) | ((value & 0xff00) << 8) | ((value & 0xff0000) >> 8) | (value >> 24);\r
+       }\r
+\r
+       struct utf8_counter\r
+       {\r
+               typedef size_t value_type;\r
+\r
+               static value_type low(value_type result, uint32_t ch)\r
+               {\r
+                       // U+0000..U+007F\r
+                       if (ch < 0x80) return result + 1;\r
+                       // U+0080..U+07FF\r
+                       else if (ch < 0x800) return result + 2;\r
+                       // U+0800..U+FFFF\r
+                       else return result + 3;\r
+               }\r
+\r
+               static value_type high(value_type result, uint32_t)\r
+               {\r
+                       // U+10000..U+10FFFF\r
+                       return result + 4;\r
+               }\r
+       };\r
+\r
+       struct utf8_writer\r
+       {\r
+               typedef uint8_t* value_type;\r
+\r
+               static value_type low(value_type result, uint32_t ch)\r
+               {\r
+                       // U+0000..U+007F\r
+                       if (ch < 0x80)\r
+                       {\r
+                               *result = static_cast<uint8_t>(ch);\r
+                               return result + 1;\r
+                       }\r
+                       // U+0080..U+07FF\r
+                       else if (ch < 0x800)\r
+                       {\r
+                               result[0] = static_cast<uint8_t>(0xC0 | (ch >> 6));\r
+                               result[1] = static_cast<uint8_t>(0x80 | (ch & 0x3F));\r
+                               return result + 2;\r
+                       }\r
+                       // U+0800..U+FFFF\r
+                       else\r
+                       {\r
+                               result[0] = static_cast<uint8_t>(0xE0 | (ch >> 12));\r
+                               result[1] = static_cast<uint8_t>(0x80 | ((ch >> 6) & 0x3F));\r
+                               result[2] = static_cast<uint8_t>(0x80 | (ch & 0x3F));\r
+                               return result + 3;\r
+                       }\r
+               }\r
+\r
+               static value_type high(value_type result, uint32_t ch)\r
+               {\r
+                       // U+10000..U+10FFFF\r
+                       result[0] = static_cast<uint8_t>(0xF0 | (ch >> 18));\r
+                       result[1] = static_cast<uint8_t>(0x80 | ((ch >> 12) & 0x3F));\r
+                       result[2] = static_cast<uint8_t>(0x80 | ((ch >> 6) & 0x3F));\r
+                       result[3] = static_cast<uint8_t>(0x80 | (ch & 0x3F));\r
+                       return result + 4;\r
+               }\r
+\r
+               static value_type any(value_type result, uint32_t ch)\r
+               {\r
+                       return (ch < 0x10000) ? low(result, ch) : high(result, ch);\r
+               }\r
+       };\r
+\r
+       struct utf16_counter\r
+       {\r
+               typedef size_t value_type;\r
+\r
+               static value_type low(value_type result, uint32_t)\r
+               {\r
+                       return result + 1;\r
+               }\r
+\r
+               static value_type high(value_type result, uint32_t)\r
+               {\r
+                       return result + 2;\r
+               }\r
+       };\r
+\r
+       struct utf16_writer\r
+       {\r
+               typedef uint16_t* value_type;\r
+\r
+               static value_type low(value_type result, uint32_t ch)\r
+               {\r
+                       *result = static_cast<uint16_t>(ch);\r
+\r
+                       return result + 1;\r
+               }\r
+\r
+               static value_type high(value_type result, uint32_t ch)\r
+               {\r
+                       uint32_t msh = static_cast<uint32_t>(ch - 0x10000) >> 10;\r
+                       uint32_t lsh = static_cast<uint32_t>(ch - 0x10000) & 0x3ff;\r
+\r
+                       result[0] = static_cast<uint16_t>(0xD800 + msh);\r
+                       result[1] = static_cast<uint16_t>(0xDC00 + lsh);\r
+\r
+                       return result + 2;\r
+               }\r
+\r
+               static value_type any(value_type result, uint32_t ch)\r
+               {\r
+                       return (ch < 0x10000) ? low(result, ch) : high(result, ch);\r
+               }\r
+       };\r
+\r
+       struct utf32_counter\r
+       {\r
+               typedef size_t value_type;\r
+\r
+               static value_type low(value_type result, uint32_t)\r
+               {\r
+                       return result + 1;\r
+               }\r
+\r
+               static value_type high(value_type result, uint32_t)\r
+               {\r
+                       return result + 1;\r
+               }\r
+       };\r
+\r
+       struct utf32_writer\r
+       {\r
+               typedef uint32_t* value_type;\r
+\r
+               static value_type low(value_type result, uint32_t ch)\r
+               {\r
+                       *result = ch;\r
+\r
+                       return result + 1;\r
+               }\r
+\r
+               static value_type high(value_type result, uint32_t ch)\r
+               {\r
+                       *result = ch;\r
+\r
+                       return result + 1;\r
+               }\r
+\r
+               static value_type any(value_type result, uint32_t ch)\r
+               {\r
+                       *result = ch;\r
+\r
+                       return result + 1;\r
+               }\r
+       };\r
+\r
+       struct latin1_writer\r
+       {\r
+               typedef uint8_t* value_type;\r
+\r
+               static value_type low(value_type result, uint32_t ch)\r
+               {\r
+                       *result = static_cast<uint8_t>(ch > 255 ? '?' : ch);\r
+\r
+                       return result + 1;\r
+               }\r
+\r
+               static value_type high(value_type result, uint32_t ch)\r
+               {\r
+                       (void)ch;\r
+\r
+                       *result = '?';\r
+\r
+                       return result + 1;\r
+               }\r
+       };\r
+\r
+       template <size_t size> struct wchar_selector;\r
+\r
+       template <> struct wchar_selector<2>\r
+       {\r
+               typedef uint16_t type;\r
+               typedef utf16_counter counter;\r
+               typedef utf16_writer writer;\r
+       };\r
+\r
+       template <> struct wchar_selector<4>\r
+       {\r
+               typedef uint32_t type;\r
+               typedef utf32_counter counter;\r
+               typedef utf32_writer writer;\r
+       };\r
+\r
+       typedef wchar_selector<sizeof(wchar_t)>::counter wchar_counter;\r
+       typedef wchar_selector<sizeof(wchar_t)>::writer wchar_writer;\r
+\r
+       template <typename Traits, typename opt_swap = opt_false> struct utf_decoder\r
+       {\r
+               static inline typename Traits::value_type decode_utf8_block(const uint8_t* data, size_t size, typename Traits::value_type result)\r
+               {\r
+                       const uint8_t utf8_byte_mask = 0x3f;\r
+\r
+                       while (size)\r
+                       {\r
+                               uint8_t lead = *data;\r
+\r
+                               // 0xxxxxxx -> U+0000..U+007F\r
+                               if (lead < 0x80)\r
+                               {\r
+                                       result = Traits::low(result, lead);\r
+                                       data += 1;\r
+                                       size -= 1;\r
+\r
+                                       // process aligned single-byte (ascii) blocks\r
+                                       if ((reinterpret_cast<uintptr_t>(data) & 3) == 0)\r
+                                       {\r
+                                               // round-trip through void* to silence 'cast increases required alignment of target type' warnings\r
+                                               while (size >= 4 && (*static_cast<const uint32_t*>(static_cast<const void*>(data)) & 0x80808080) == 0)\r
+                                               {\r
+                                                       result = Traits::low(result, data[0]);\r
+                                                       result = Traits::low(result, data[1]);\r
+                                                       result = Traits::low(result, data[2]);\r
+                                                       result = Traits::low(result, data[3]);\r
+                                                       data += 4;\r
+                                                       size -= 4;\r
+                                               }\r
+                                       }\r
+                               }\r
+                               // 110xxxxx -> U+0080..U+07FF\r
+                               else if (static_cast<unsigned int>(lead - 0xC0) < 0x20 && size >= 2 && (data[1] & 0xc0) == 0x80)\r
+                               {\r
+                                       result = Traits::low(result, ((lead & ~0xC0) << 6) | (data[1] & utf8_byte_mask));\r
+                                       data += 2;\r
+                                       size -= 2;\r
+                               }\r
+                               // 1110xxxx -> U+0800-U+FFFF\r
+                               else if (static_cast<unsigned int>(lead - 0xE0) < 0x10 && size >= 3 && (data[1] & 0xc0) == 0x80 && (data[2] & 0xc0) == 0x80)\r
+                               {\r
+                                       result = Traits::low(result, ((lead & ~0xE0) << 12) | ((data[1] & utf8_byte_mask) << 6) | (data[2] & utf8_byte_mask));\r
+                                       data += 3;\r
+                                       size -= 3;\r
+                               }\r
+                               // 11110xxx -> U+10000..U+10FFFF\r
+                               else if (static_cast<unsigned int>(lead - 0xF0) < 0x08 && size >= 4 && (data[1] & 0xc0) == 0x80 && (data[2] & 0xc0) == 0x80 && (data[3] & 0xc0) == 0x80)\r
+                               {\r
+                                       result = Traits::high(result, ((lead & ~0xF0) << 18) | ((data[1] & utf8_byte_mask) << 12) | ((data[2] & utf8_byte_mask) << 6) | (data[3] & utf8_byte_mask));\r
+                                       data += 4;\r
+                                       size -= 4;\r
+                               }\r
+                               // 10xxxxxx or 11111xxx -> invalid\r
+                               else\r
+                               {\r
+                                       data += 1;\r
+                                       size -= 1;\r
+                               }\r
+                       }\r
+\r
+                       return result;\r
+               }\r
+\r
+               static inline typename Traits::value_type decode_utf16_block(const uint16_t* data, size_t size, typename Traits::value_type result)\r
+               {\r
+                       const uint16_t* end = data + size;\r
+\r
+                       while (data < end)\r
+                       {\r
+                               unsigned int lead = opt_swap::value ? endian_swap(*data) : *data;\r
+\r
+                               // U+0000..U+D7FF\r
+                               if (lead < 0xD800)\r
+                               {\r
+                                       result = Traits::low(result, lead);\r
+                                       data += 1;\r
+                               }\r
+                               // U+E000..U+FFFF\r
+                               else if (static_cast<unsigned int>(lead - 0xE000) < 0x2000)\r
+                               {\r
+                                       result = Traits::low(result, lead);\r
+                                       data += 1;\r
+                               }\r
+                               // surrogate pair lead\r
+                               else if (static_cast<unsigned int>(lead - 0xD800) < 0x400 && data + 1 < end)\r
+                               {\r
+                                       uint16_t next = opt_swap::value ? endian_swap(data[1]) : data[1];\r
+\r
+                                       if (static_cast<unsigned int>(next - 0xDC00) < 0x400)\r
+                                       {\r
+                                               result = Traits::high(result, 0x10000 + ((lead & 0x3ff) << 10) + (next & 0x3ff));\r
+                                               data += 2;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               data += 1;\r
+                                       }\r
+                               }\r
+                               else\r
+                               {\r
+                                       data += 1;\r
+                               }\r
+                       }\r
+\r
+                       return result;\r
+               }\r
+\r
+               static inline typename Traits::value_type decode_utf32_block(const uint32_t* data, size_t size, typename Traits::value_type result)\r
+               {\r
+                       const uint32_t* end = data + size;\r
+\r
+                       while (data < end)\r
+                       {\r
+                               uint32_t lead = opt_swap::value ? endian_swap(*data) : *data;\r
+\r
+                               // U+0000..U+FFFF\r
+                               if (lead < 0x10000)\r
+                               {\r
+                                       result = Traits::low(result, lead);\r
+                                       data += 1;\r
+                               }\r
+                               // U+10000..U+10FFFF\r
+                               else\r
+                               {\r
+                                       result = Traits::high(result, lead);\r
+                                       data += 1;\r
+                               }\r
+                       }\r
+\r
+                       return result;\r
+               }\r
+\r
+               static inline typename Traits::value_type decode_latin1_block(const uint8_t* data, size_t size, typename Traits::value_type result)\r
+               {\r
+                       for (size_t i = 0; i < size; ++i)\r
+                       {\r
+                               result = Traits::low(result, data[i]);\r
+                       }\r
+\r
+                       return result;\r
+               }\r
+\r
+               static inline typename Traits::value_type decode_wchar_block_impl(const uint16_t* data, size_t size, typename Traits::value_type result)\r
+               {\r
+                       return decode_utf16_block(data, size, result);\r
+               }\r
+\r
+               static inline typename Traits::value_type decode_wchar_block_impl(const uint32_t* data, size_t size, typename Traits::value_type result)\r
+               {\r
+                       return decode_utf32_block(data, size, result);\r
+               }\r
+\r
+               static inline typename Traits::value_type decode_wchar_block(const wchar_t* data, size_t size, typename Traits::value_type result)\r
+               {\r
+                       return decode_wchar_block_impl(reinterpret_cast<const wchar_selector<sizeof(wchar_t)>::type*>(data), size, result);\r
+               }\r
+       };\r
+\r
+       template <typename T> PUGI__FN void convert_utf_endian_swap(T* result, const T* data, size_t length)\r
+       {\r
+               for (size_t i = 0; i < length; ++i) result[i] = endian_swap(data[i]);\r
+       }\r
+\r
+#ifdef PUGIXML_WCHAR_MODE\r
+       PUGI__FN void convert_wchar_endian_swap(wchar_t* result, const wchar_t* data, size_t length)\r
+       {\r
+               for (size_t i = 0; i < length; ++i) result[i] = static_cast<wchar_t>(endian_swap(static_cast<wchar_selector<sizeof(wchar_t)>::type>(data[i])));\r
+       }\r
+#endif\r
+PUGI__NS_END\r
+\r
+PUGI__NS_BEGIN\r
+       enum chartype_t\r
+       {\r
+               ct_parse_pcdata = 1,    // \0, &, \r, <\r
+               ct_parse_attr = 2,              // \0, &, \r, ', "\r
+               ct_parse_attr_ws = 4,   // \0, &, \r, ', ", \n, tab\r
+               ct_space = 8,                   // \r, \n, space, tab\r
+               ct_parse_cdata = 16,    // \0, ], >, \r\r
+               ct_parse_comment = 32,  // \0, -, >, \r\r
+               ct_symbol = 64,                 // Any symbol > 127, a-z, A-Z, 0-9, _, :, -, .\r
+               ct_start_symbol = 128   // Any symbol > 127, a-z, A-Z, _, :\r
+       };\r
+\r
+       static const unsigned char chartype_table[256] =\r
+       {\r
+               55,  0,   0,   0,   0,   0,   0,   0,      0,   12,  12,  0,   0,   63,  0,   0,   // 0-15\r
+               0,   0,   0,   0,   0,   0,   0,   0,      0,   0,   0,   0,   0,   0,   0,   0,   // 16-31\r
+               8,   0,   6,   0,   0,   0,   7,   6,      0,   0,   0,   0,   0,   96,  64,  0,   // 32-47\r
+               64,  64,  64,  64,  64,  64,  64,  64,     64,  64,  192, 0,   1,   0,   48,  0,   // 48-63\r
+               0,   192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 192, 192, 192, 192, 192, // 64-79\r
+               192, 192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 0,   0,   16,  0,   192, // 80-95\r
+               0,   192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 192, 192, 192, 192, 192, // 96-111\r
+               192, 192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 0, 0, 0, 0, 0,           // 112-127\r
+\r
+               192, 192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 192, 192, 192, 192, 192, // 128+\r
+               192, 192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 192, 192, 192, 192, 192,\r
+               192, 192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 192, 192, 192, 192, 192,\r
+               192, 192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 192, 192, 192, 192, 192,\r
+               192, 192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 192, 192, 192, 192, 192,\r
+               192, 192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 192, 192, 192, 192, 192,\r
+               192, 192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 192, 192, 192, 192, 192,\r
+               192, 192, 192, 192, 192, 192, 192, 192,    192, 192, 192, 192, 192, 192, 192, 192\r
+       };\r
+\r
+       enum chartypex_t\r
+       {\r
+               ctx_special_pcdata = 1,   // Any symbol >= 0 and < 32 (except \t, \r, \n), &, <, >\r
+               ctx_special_attr = 2,     // Any symbol >= 0 and < 32 (except \t), &, <, >, "\r
+               ctx_start_symbol = 4,     // Any symbol > 127, a-z, A-Z, _\r
+               ctx_digit = 8,                    // 0-9\r
+               ctx_symbol = 16                   // Any symbol > 127, a-z, A-Z, 0-9, _, -, .\r
+       };\r
+       \r
+       static const unsigned char chartypex_table[256] =\r
+       {\r
+               3,  3,  3,  3,  3,  3,  3,  3,     3,  0,  2,  3,  3,  2,  3,  3,     // 0-15\r
+               3,  3,  3,  3,  3,  3,  3,  3,     3,  3,  3,  3,  3,  3,  3,  3,     // 16-31\r
+               0,  0,  2,  0,  0,  0,  3,  0,     0,  0,  0,  0,  0, 16, 16,  0,     // 32-47\r
+               24, 24, 24, 24, 24, 24, 24, 24,    24, 24, 0,  0,  3,  0,  3,  0,     // 48-63\r
+\r
+               0,  20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 20, 20, 20, 20, 20,    // 64-79\r
+               20, 20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 0,  0,  0,  0,  20,    // 80-95\r
+               0,  20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 20, 20, 20, 20, 20,    // 96-111\r
+               20, 20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 0,  0,  0,  0,  0,     // 112-127\r
+\r
+               20, 20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 20, 20, 20, 20, 20,    // 128+\r
+               20, 20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 20, 20, 20, 20, 20,\r
+               20, 20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 20, 20, 20, 20, 20,\r
+               20, 20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 20, 20, 20, 20, 20,\r
+               20, 20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 20, 20, 20, 20, 20,\r
+               20, 20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 20, 20, 20, 20, 20,\r
+               20, 20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 20, 20, 20, 20, 20,\r
+               20, 20, 20, 20, 20, 20, 20, 20,    20, 20, 20, 20, 20, 20, 20, 20\r
+       };\r
+       \r
+#ifdef PUGIXML_WCHAR_MODE\r
+       #define PUGI__IS_CHARTYPE_IMPL(c, ct, table) ((static_cast<unsigned int>(c) < 128 ? table[static_cast<unsigned int>(c)] : table[128]) & (ct))\r
+#else\r
+       #define PUGI__IS_CHARTYPE_IMPL(c, ct, table) (table[static_cast<unsigned char>(c)] & (ct))\r
+#endif\r
+\r
+       #define PUGI__IS_CHARTYPE(c, ct) PUGI__IS_CHARTYPE_IMPL(c, ct, chartype_table)\r
+       #define PUGI__IS_CHARTYPEX(c, ct) PUGI__IS_CHARTYPE_IMPL(c, ct, chartypex_table)\r
+\r
+       PUGI__FN bool is_little_endian()\r
+       {\r
+               unsigned int ui = 1;\r
+\r
+               return *reinterpret_cast<unsigned char*>(&ui) == 1;\r
+       }\r
+\r
+       PUGI__FN xml_encoding get_wchar_encoding()\r
+       {\r
+               PUGI__STATIC_ASSERT(sizeof(wchar_t) == 2 || sizeof(wchar_t) == 4);\r
+\r
+               if (sizeof(wchar_t) == 2)\r
+                       return is_little_endian() ? encoding_utf16_le : encoding_utf16_be;\r
+               else \r
+                       return is_little_endian() ? encoding_utf32_le : encoding_utf32_be;\r
+       }\r
+\r
+       PUGI__FN xml_encoding guess_buffer_encoding(uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)\r
+       {\r
+               // look for BOM in first few bytes\r
+               if (d0 == 0 && d1 == 0 && d2 == 0xfe && d3 == 0xff) return encoding_utf32_be;\r
+               if (d0 == 0xff && d1 == 0xfe && d2 == 0 && d3 == 0) return encoding_utf32_le;\r
+               if (d0 == 0xfe && d1 == 0xff) return encoding_utf16_be;\r
+               if (d0 == 0xff && d1 == 0xfe) return encoding_utf16_le;\r
+               if (d0 == 0xef && d1 == 0xbb && d2 == 0xbf) return encoding_utf8;\r
+\r
+               // look for <, <? or <?xm in various encodings\r
+               if (d0 == 0 && d1 == 0 && d2 == 0 && d3 == 0x3c) return encoding_utf32_be;\r
+               if (d0 == 0x3c && d1 == 0 && d2 == 0 && d3 == 0) return encoding_utf32_le;\r
+               if (d0 == 0 && d1 == 0x3c && d2 == 0 && d3 == 0x3f) return encoding_utf16_be;\r
+               if (d0 == 0x3c && d1 == 0 && d2 == 0x3f && d3 == 0) return encoding_utf16_le;\r
+               if (d0 == 0x3c && d1 == 0x3f && d2 == 0x78 && d3 == 0x6d) return encoding_utf8;\r
+\r
+               // look for utf16 < followed by node name (this may fail, but is better than utf8 since it's zero terminated so early)\r
+               if (d0 == 0 && d1 == 0x3c) return encoding_utf16_be;\r
+               if (d0 == 0x3c && d1 == 0) return encoding_utf16_le;\r
+\r
+               // no known BOM detected, assume utf8\r
+               return encoding_utf8;\r
+       }\r
+\r
+       PUGI__FN xml_encoding get_buffer_encoding(xml_encoding encoding, const void* contents, size_t size)\r
+       {\r
+               // replace wchar encoding with utf implementation\r
+               if (encoding == encoding_wchar) return get_wchar_encoding();\r
+\r
+               // replace utf16 encoding with utf16 with specific endianness\r
+               if (encoding == encoding_utf16) return is_little_endian() ? encoding_utf16_le : encoding_utf16_be;\r
+\r
+               // replace utf32 encoding with utf32 with specific endianness\r
+               if (encoding == encoding_utf32) return is_little_endian() ? encoding_utf32_le : encoding_utf32_be;\r
+\r
+               // only do autodetection if no explicit encoding is requested\r
+               if (encoding != encoding_auto) return encoding;\r
+\r
+               // skip encoding autodetection if input buffer is too small\r
+               if (size < 4) return encoding_utf8;\r
+\r
+               // try to guess encoding (based on XML specification, Appendix F.1)\r
+               const uint8_t* data = static_cast<const uint8_t*>(contents);\r
+\r
+               PUGI__DMC_VOLATILE uint8_t d0 = data[0], d1 = data[1], d2 = data[2], d3 = data[3];\r
+\r
+               return guess_buffer_encoding(d0, d1, d2, d3);\r
+       }\r
+\r
+       PUGI__FN bool get_mutable_buffer(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, bool is_mutable)\r
+       {\r
+               size_t length = size / sizeof(char_t);\r
+\r
+               if (is_mutable)\r
+               {\r
+                       out_buffer = static_cast<char_t*>(const_cast<void*>(contents));\r
+                       out_length = length;\r
+               }\r
+               else\r
+               {\r
+                       char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));\r
+                       if (!buffer) return false;\r
+\r
+                       memcpy(buffer, contents, length * sizeof(char_t));\r
+                       buffer[length] = 0;\r
+\r
+                       out_buffer = buffer;\r
+                       out_length = length + 1;\r
+               }\r
+\r
+               return true;\r
+       }\r
+\r
+#ifdef PUGIXML_WCHAR_MODE\r
+       PUGI__FN bool need_endian_swap_utf(xml_encoding le, xml_encoding re)\r
+       {\r
+               return (le == encoding_utf16_be && re == encoding_utf16_le) || (le == encoding_utf16_le && re == encoding_utf16_be) ||\r
+                          (le == encoding_utf32_be && re == encoding_utf32_le) || (le == encoding_utf32_le && re == encoding_utf32_be);\r
+       }\r
+\r
+       PUGI__FN bool convert_buffer_endian_swap(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, bool is_mutable)\r
+       {\r
+               const char_t* data = static_cast<const char_t*>(contents);\r
+               size_t length = size / sizeof(char_t);\r
+\r
+               if (is_mutable)\r
+               {\r
+                       char_t* buffer = const_cast<char_t*>(data);\r
+\r
+                       convert_wchar_endian_swap(buffer, data, length);\r
+\r
+                       out_buffer = buffer;\r
+                       out_length = length;\r
+               }\r
+               else\r
+               {\r
+                       char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));\r
+                       if (!buffer) return false;\r
+\r
+                       convert_wchar_endian_swap(buffer, data, length);\r
+                       buffer[length] = 0;\r
+\r
+                       out_buffer = buffer;\r
+                       out_length = length + 1;\r
+               }\r
+\r
+               return true;\r
+       }\r
+\r
+       PUGI__FN bool convert_buffer_utf8(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size)\r
+       {\r
+               const uint8_t* data = static_cast<const uint8_t*>(contents);\r
+               size_t data_length = size;\r
+\r
+               // first pass: get length in wchar_t units\r
+               size_t length = utf_decoder<wchar_counter>::decode_utf8_block(data, data_length, 0);\r
+\r
+               // allocate buffer of suitable length\r
+               char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));\r
+               if (!buffer) return false;\r
+\r
+               // second pass: convert utf8 input to wchar_t\r
+               wchar_writer::value_type obegin = reinterpret_cast<wchar_writer::value_type>(buffer);\r
+               wchar_writer::value_type oend = utf_decoder<wchar_writer>::decode_utf8_block(data, data_length, obegin);\r
+\r
+               assert(oend == obegin + length);\r
+               *oend = 0;\r
+\r
+               out_buffer = buffer;\r
+               out_length = length + 1;\r
+\r
+               return true;\r
+       }\r
+\r
+       template <typename opt_swap> PUGI__FN bool convert_buffer_utf16(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, opt_swap)\r
+       {\r
+               const uint16_t* data = static_cast<const uint16_t*>(contents);\r
+               size_t data_length = size / sizeof(uint16_t);\r
+\r
+               // first pass: get length in wchar_t units\r
+               size_t length = utf_decoder<wchar_counter, opt_swap>::decode_utf16_block(data, data_length, 0);\r
+\r
+               // allocate buffer of suitable length\r
+               char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));\r
+               if (!buffer) return false;\r
+\r
+               // second pass: convert utf16 input to wchar_t\r
+               wchar_writer::value_type obegin = reinterpret_cast<wchar_writer::value_type>(buffer);\r
+               wchar_writer::value_type oend = utf_decoder<wchar_writer, opt_swap>::decode_utf16_block(data, data_length, obegin);\r
+\r
+               assert(oend == obegin + length);\r
+               *oend = 0;\r
+\r
+               out_buffer = buffer;\r
+               out_length = length + 1;\r
+\r
+               return true;\r
+       }\r
+\r
+       template <typename opt_swap> PUGI__FN bool convert_buffer_utf32(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, opt_swap)\r
+       {\r
+               const uint32_t* data = static_cast<const uint32_t*>(contents);\r
+               size_t data_length = size / sizeof(uint32_t);\r
+\r
+               // first pass: get length in wchar_t units\r
+               size_t length = utf_decoder<wchar_counter, opt_swap>::decode_utf32_block(data, data_length, 0);\r
+\r
+               // allocate buffer of suitable length\r
+               char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));\r
+               if (!buffer) return false;\r
+\r
+               // second pass: convert utf32 input to wchar_t\r
+               wchar_writer::value_type obegin = reinterpret_cast<wchar_writer::value_type>(buffer);\r
+               wchar_writer::value_type oend = utf_decoder<wchar_writer, opt_swap>::decode_utf32_block(data, data_length, obegin);\r
+\r
+               assert(oend == obegin + length);\r
+               *oend = 0;\r
+\r
+               out_buffer = buffer;\r
+               out_length = length + 1;\r
+\r
+               return true;\r
+       }\r
+\r
+       PUGI__FN bool convert_buffer_latin1(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size)\r
+       {\r
+               const uint8_t* data = static_cast<const uint8_t*>(contents);\r
+               size_t data_length = size;\r
+\r
+               // get length in wchar_t units\r
+               size_t length = data_length;\r
+\r
+               // allocate buffer of suitable length\r
+               char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));\r
+               if (!buffer) return false;\r
+\r
+               // convert latin1 input to wchar_t\r
+               wchar_writer::value_type obegin = reinterpret_cast<wchar_writer::value_type>(buffer);\r
+               wchar_writer::value_type oend = utf_decoder<wchar_writer>::decode_latin1_block(data, data_length, obegin);\r
+\r
+               assert(oend == obegin + length);\r
+               *oend = 0;\r
+\r
+               out_buffer = buffer;\r
+               out_length = length + 1;\r
+\r
+               return true;\r
+       }\r
+\r
+       PUGI__FN bool convert_buffer(char_t*& out_buffer, size_t& out_length, xml_encoding encoding, const void* contents, size_t size, bool is_mutable)\r
+       {\r
+               // get native encoding\r
+               xml_encoding wchar_encoding = get_wchar_encoding();\r
+\r
+               // fast path: no conversion required\r
+               if (encoding == wchar_encoding) return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable);\r
+\r
+               // only endian-swapping is required\r
+               if (need_endian_swap_utf(encoding, wchar_encoding)) return convert_buffer_endian_swap(out_buffer, out_length, contents, size, is_mutable);\r
+\r
+               // source encoding is utf8\r
+               if (encoding == encoding_utf8) return convert_buffer_utf8(out_buffer, out_length, contents, size);\r
+\r
+               // source encoding is utf16\r
+               if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)\r
+               {\r
+                       xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be;\r
+\r
+                       return (native_encoding == encoding) ?\r
+                               convert_buffer_utf16(out_buffer, out_length, contents, size, opt_false()) :\r
+                               convert_buffer_utf16(out_buffer, out_length, contents, size, opt_true());\r
+               }\r
+\r
+               // source encoding is utf32\r
+               if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)\r
+               {\r
+                       xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be;\r
+\r
+                       return (native_encoding == encoding) ?\r
+                               convert_buffer_utf32(out_buffer, out_length, contents, size, opt_false()) :\r
+                               convert_buffer_utf32(out_buffer, out_length, contents, size, opt_true());\r
+               }\r
+\r
+               // source encoding is latin1\r
+               if (encoding == encoding_latin1) return convert_buffer_latin1(out_buffer, out_length, contents, size);\r
+\r
+               assert(!"Invalid encoding");\r
+               return false;\r
+       }\r
+#else\r
+       template <typename opt_swap> PUGI__FN bool convert_buffer_utf16(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, opt_swap)\r
+       {\r
+               const uint16_t* data = static_cast<const uint16_t*>(contents);\r
+               size_t data_length = size / sizeof(uint16_t);\r
+\r
+               // first pass: get length in utf8 units\r
+               size_t length = utf_decoder<utf8_counter, opt_swap>::decode_utf16_block(data, data_length, 0);\r
+\r
+               // allocate buffer of suitable length\r
+               char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));\r
+               if (!buffer) return false;\r
+\r
+               // second pass: convert utf16 input to utf8\r
+               uint8_t* obegin = reinterpret_cast<uint8_t*>(buffer);\r
+               uint8_t* oend = utf_decoder<utf8_writer, opt_swap>::decode_utf16_block(data, data_length, obegin);\r
+\r
+               assert(oend == obegin + length);\r
+               *oend = 0;\r
+\r
+               out_buffer = buffer;\r
+               out_length = length + 1;\r
+\r
+               return true;\r
+       }\r
+\r
+       template <typename opt_swap> PUGI__FN bool convert_buffer_utf32(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, opt_swap)\r
+       {\r
+               const uint32_t* data = static_cast<const uint32_t*>(contents);\r
+               size_t data_length = size / sizeof(uint32_t);\r
+\r
+               // first pass: get length in utf8 units\r
+               size_t length = utf_decoder<utf8_counter, opt_swap>::decode_utf32_block(data, data_length, 0);\r
+\r
+               // allocate buffer of suitable length\r
+               char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));\r
+               if (!buffer) return false;\r
+\r
+               // second pass: convert utf32 input to utf8\r
+               uint8_t* obegin = reinterpret_cast<uint8_t*>(buffer);\r
+               uint8_t* oend = utf_decoder<utf8_writer, opt_swap>::decode_utf32_block(data, data_length, obegin);\r
+\r
+               assert(oend == obegin + length);\r
+               *oend = 0;\r
+\r
+               out_buffer = buffer;\r
+               out_length = length + 1;\r
+\r
+               return true;\r
+       }\r
+\r
+       PUGI__FN size_t get_latin1_7bit_prefix_length(const uint8_t* data, size_t size)\r
+       {\r
+               for (size_t i = 0; i < size; ++i)\r
+                       if (data[i] > 127)\r
+                               return i;\r
+\r
+               return size;\r
+       }\r
+\r
+       PUGI__FN bool convert_buffer_latin1(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, bool is_mutable)\r
+       {\r
+               const uint8_t* data = static_cast<const uint8_t*>(contents);\r
+               size_t data_length = size;\r
+\r
+               // get size of prefix that does not need utf8 conversion\r
+               size_t prefix_length = get_latin1_7bit_prefix_length(data, data_length);\r
+               assert(prefix_length <= data_length);\r
+\r
+               const uint8_t* postfix = data + prefix_length;\r
+               size_t postfix_length = data_length - prefix_length;\r
+\r
+               // if no conversion is needed, just return the original buffer\r
+               if (postfix_length == 0) return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable);\r
+\r
+               // first pass: get length in utf8 units\r
+               size_t length = prefix_length + utf_decoder<utf8_counter>::decode_latin1_block(postfix, postfix_length, 0);\r
+\r
+               // allocate buffer of suitable length\r
+               char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));\r
+               if (!buffer) return false;\r
+\r
+               // second pass: convert latin1 input to utf8\r
+               memcpy(buffer, data, prefix_length);\r
+\r
+               uint8_t* obegin = reinterpret_cast<uint8_t*>(buffer);\r
+               uint8_t* oend = utf_decoder<utf8_writer>::decode_latin1_block(postfix, postfix_length, obegin + prefix_length);\r
+\r
+               assert(oend == obegin + length);\r
+               *oend = 0;\r
+\r
+               out_buffer = buffer;\r
+               out_length = length + 1;\r
+\r
+               return true;\r
+       }\r
+\r
+       PUGI__FN bool convert_buffer(char_t*& out_buffer, size_t& out_length, xml_encoding encoding, const void* contents, size_t size, bool is_mutable)\r
+       {\r
+               // fast path: no conversion required\r
+               if (encoding == encoding_utf8) return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable);\r
+\r
+               // source encoding is utf16\r
+               if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)\r
+               {\r
+                       xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be;\r
+\r
+                       return (native_encoding == encoding) ?\r
+                               convert_buffer_utf16(out_buffer, out_length, contents, size, opt_false()) :\r
+                               convert_buffer_utf16(out_buffer, out_length, contents, size, opt_true());\r
+               }\r
+\r
+               // source encoding is utf32\r
+               if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)\r
+               {\r
+                       xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be;\r
+\r
+                       return (native_encoding == encoding) ?\r
+                               convert_buffer_utf32(out_buffer, out_length, contents, size, opt_false()) :\r
+                               convert_buffer_utf32(out_buffer, out_length, contents, size, opt_true());\r
+               }\r
+\r
+               // source encoding is latin1\r
+               if (encoding == encoding_latin1) return convert_buffer_latin1(out_buffer, out_length, contents, size, is_mutable);\r
+\r
+               assert(!"Invalid encoding");\r
+               return false;\r
+       }\r
+#endif\r
+\r
+       PUGI__FN size_t as_utf8_begin(const wchar_t* str, size_t length)\r
+       {\r
+               // get length in utf8 characters\r
+               return utf_decoder<utf8_counter>::decode_wchar_block(str, length, 0);\r
+       }\r
+\r
+       PUGI__FN void as_utf8_end(char* buffer, size_t size, const wchar_t* str, size_t length)\r
+       {\r
+               // convert to utf8\r
+               uint8_t* begin = reinterpret_cast<uint8_t*>(buffer);\r
+               uint8_t* end = utf_decoder<utf8_writer>::decode_wchar_block(str, length, begin);\r
+       \r
+               assert(begin + size == end);\r
+               (void)!end;\r
+\r
+               // zero-terminate\r
+               buffer[size] = 0;\r
+       }\r
+       \r
+#ifndef PUGIXML_NO_STL\r
+       PUGI__FN std::string as_utf8_impl(const wchar_t* str, size_t length)\r
+       {\r
+               // first pass: get length in utf8 characters\r
+               size_t size = as_utf8_begin(str, length);\r
+\r
+               // allocate resulting string\r
+               std::string result;\r
+               result.resize(size);\r
+\r
+               // second pass: convert to utf8\r
+               if (size > 0) as_utf8_end(&result[0], size, str, length);\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN std::basic_string<wchar_t> as_wide_impl(const char* str, size_t size)\r
+       {\r
+               const uint8_t* data = reinterpret_cast<const uint8_t*>(str);\r
+\r
+               // first pass: get length in wchar_t units\r
+               size_t length = utf_decoder<wchar_counter>::decode_utf8_block(data, size, 0);\r
+\r
+               // allocate resulting string\r
+               std::basic_string<wchar_t> result;\r
+               result.resize(length);\r
+\r
+               // second pass: convert to wchar_t\r
+               if (length > 0)\r
+               {\r
+                       wchar_writer::value_type begin = reinterpret_cast<wchar_writer::value_type>(&result[0]);\r
+                       wchar_writer::value_type end = utf_decoder<wchar_writer>::decode_utf8_block(data, size, begin);\r
+\r
+                       assert(begin + length == end);\r
+                       (void)!end;\r
+               }\r
+\r
+               return result;\r
+       }\r
+#endif\r
+\r
+       inline bool strcpy_insitu_allow(size_t length, uintptr_t allocated, char_t* target)\r
+       {\r
+               assert(target);\r
+               size_t target_length = strlength(target);\r
+\r
+               // always reuse document buffer memory if possible\r
+               if (!allocated) return target_length >= length;\r
+\r
+               // reuse heap memory if waste is not too great\r
+               const size_t reuse_threshold = 32;\r
+\r
+               return target_length >= length && (target_length < reuse_threshold || target_length - length < target_length / 2);\r
+       }\r
+\r
+       PUGI__FN bool strcpy_insitu(char_t*& dest, uintptr_t& header, uintptr_t header_mask, const char_t* source)\r
+       {\r
+               assert(header);\r
+\r
+               size_t source_length = strlength(source);\r
+\r
+               if (source_length == 0)\r
+               {\r
+                       // empty string and null pointer are equivalent, so just deallocate old memory\r
+                       xml_allocator* alloc = reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask)->allocator;\r
+\r
+                       if (header & header_mask) alloc->deallocate_string(dest);\r
+                       \r
+                       // mark the string as not allocated\r
+                       dest = 0;\r
+                       header &= ~header_mask;\r
+\r
+                       return true;\r
+               }\r
+               else if (dest && strcpy_insitu_allow(source_length, header & header_mask, dest))\r
+               {\r
+                       // we can reuse old buffer, so just copy the new data (including zero terminator)\r
+                       memcpy(dest, source, (source_length + 1) * sizeof(char_t));\r
+                       \r
+                       return true;\r
+               }\r
+               else\r
+               {\r
+                       xml_allocator* alloc = reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask)->allocator;\r
+\r
+                       // allocate new buffer\r
+                       char_t* buf = alloc->allocate_string(source_length + 1);\r
+                       if (!buf) return false;\r
+\r
+                       // copy the string (including zero terminator)\r
+                       memcpy(buf, source, (source_length + 1) * sizeof(char_t));\r
+\r
+                       // deallocate old buffer (*after* the above to protect against overlapping memory and/or allocation failures)\r
+                       if (header & header_mask) alloc->deallocate_string(dest);\r
+                       \r
+                       // the string is now allocated, so set the flag\r
+                       dest = buf;\r
+                       header |= header_mask;\r
+\r
+                       return true;\r
+               }\r
+       }\r
+\r
+       struct gap\r
+       {\r
+               char_t* end;\r
+               size_t size;\r
+                       \r
+               gap(): end(0), size(0)\r
+               {\r
+               }\r
+                       \r
+               // Push new gap, move s count bytes further (skipping the gap).\r
+               // Collapse previous gap.\r
+               void push(char_t*& s, size_t count)\r
+               {\r
+                       if (end) // there was a gap already; collapse it\r
+                       {\r
+                               // Move [old_gap_end, new_gap_start) to [old_gap_start, ...)\r
+                               assert(s >= end);\r
+                               memmove(end - size, end, reinterpret_cast<char*>(s) - reinterpret_cast<char*>(end));\r
+                       }\r
+                               \r
+                       s += count; // end of current gap\r
+                               \r
+                       // "merge" two gaps\r
+                       end = s;\r
+                       size += count;\r
+               }\r
+                       \r
+               // Collapse all gaps, return past-the-end pointer\r
+               char_t* flush(char_t* s)\r
+               {\r
+                       if (end)\r
+                       {\r
+                               // Move [old_gap_end, current_pos) to [old_gap_start, ...)\r
+                               assert(s >= end);\r
+                               memmove(end - size, end, reinterpret_cast<char*>(s) - reinterpret_cast<char*>(end));\r
+\r
+                               return s - size;\r
+                       }\r
+                       else return s;\r
+               }\r
+       };\r
+       \r
+       PUGI__FN char_t* strconv_escape(char_t* s, gap& g)\r
+       {\r
+               char_t* stre = s + 1;\r
+\r
+               switch (*stre)\r
+               {\r
+                       case '#':       // &#...\r
+                       {\r
+                               unsigned int ucsc = 0;\r
+\r
+                               if (stre[1] == 'x') // &#x... (hex code)\r
+                               {\r
+                                       stre += 2;\r
+\r
+                                       char_t ch = *stre;\r
+\r
+                                       if (ch == ';') return stre;\r
+\r
+                                       for (;;)\r
+                                       {\r
+                                               if (static_cast<unsigned int>(ch - '0') <= 9)\r
+                                                       ucsc = 16 * ucsc + (ch - '0');\r
+                                               else if (static_cast<unsigned int>((ch | ' ') - 'a') <= 5)\r
+                                                       ucsc = 16 * ucsc + ((ch | ' ') - 'a' + 10);\r
+                                               else if (ch == ';')\r
+                                                       break;\r
+                                               else // cancel\r
+                                                       return stre;\r
+\r
+                                               ch = *++stre;\r
+                                       }\r
+                                       \r
+                                       ++stre;\r
+                               }\r
+                               else    // &#... (dec code)\r
+                               {\r
+                                       char_t ch = *++stre;\r
+\r
+                                       if (ch == ';') return stre;\r
+\r
+                                       for (;;)\r
+                                       {\r
+                                               if (static_cast<unsigned int>(static_cast<unsigned int>(ch) - '0') <= 9)\r
+                                                       ucsc = 10 * ucsc + (ch - '0');\r
+                                               else if (ch == ';')\r
+                                                       break;\r
+                                               else // cancel\r
+                                                       return stre;\r
+\r
+                                               ch = *++stre;\r
+                                       }\r
+                                       \r
+                                       ++stre;\r
+                               }\r
+\r
+                       #ifdef PUGIXML_WCHAR_MODE\r
+                               s = reinterpret_cast<char_t*>(wchar_writer::any(reinterpret_cast<wchar_writer::value_type>(s), ucsc));\r
+                       #else\r
+                               s = reinterpret_cast<char_t*>(utf8_writer::any(reinterpret_cast<uint8_t*>(s), ucsc));\r
+                       #endif\r
+                                       \r
+                               g.push(s, stre - s);\r
+                               return stre;\r
+                       }\r
+\r
+                       case 'a':       // &a\r
+                       {\r
+                               ++stre;\r
+\r
+                               if (*stre == 'm') // &am\r
+                               {\r
+                                       if (*++stre == 'p' && *++stre == ';') // &amp;\r
+                                       {\r
+                                               *s++ = '&';\r
+                                               ++stre;\r
+                                                       \r
+                                               g.push(s, stre - s);\r
+                                               return stre;\r
+                                       }\r
+                               }\r
+                               else if (*stre == 'p') // &ap\r
+                               {\r
+                                       if (*++stre == 'o' && *++stre == 's' && *++stre == ';') // &apos;\r
+                                       {\r
+                                               *s++ = '\'';\r
+                                               ++stre;\r
+\r
+                                               g.push(s, stre - s);\r
+                                               return stre;\r
+                                       }\r
+                               }\r
+                               break;\r
+                       }\r
+\r
+                       case 'g': // &g\r
+                       {\r
+                               if (*++stre == 't' && *++stre == ';') // &gt;\r
+                               {\r
+                                       *s++ = '>';\r
+                                       ++stre;\r
+                                       \r
+                                       g.push(s, stre - s);\r
+                                       return stre;\r
+                               }\r
+                               break;\r
+                       }\r
+\r
+                       case 'l': // &l\r
+                       {\r
+                               if (*++stre == 't' && *++stre == ';') // &lt;\r
+                               {\r
+                                       *s++ = '<';\r
+                                       ++stre;\r
+                                               \r
+                                       g.push(s, stre - s);\r
+                                       return stre;\r
+                               }\r
+                               break;\r
+                       }\r
+\r
+                       case 'q': // &q\r
+                       {\r
+                               if (*++stre == 'u' && *++stre == 'o' && *++stre == 't' && *++stre == ';') // &quot;\r
+                               {\r
+                                       *s++ = '"';\r
+                                       ++stre;\r
+                                       \r
+                                       g.push(s, stre - s);\r
+                                       return stre;\r
+                               }\r
+                               break;\r
+                       }\r
+\r
+                       default:\r
+                               break;\r
+               }\r
+               \r
+               return stre;\r
+       }\r
+\r
+       // Utility macro for last character handling\r
+       #define ENDSWITH(c, e) ((c) == (e) || ((c) == 0 && endch == (e)))\r
+\r
+       PUGI__FN char_t* strconv_comment(char_t* s, char_t endch)\r
+       {\r
+               gap g;\r
+               \r
+               while (true)\r
+               {\r
+                       while (!PUGI__IS_CHARTYPE(*s, ct_parse_comment)) ++s;\r
+               \r
+                       if (*s == '\r') // Either a single 0x0d or 0x0d 0x0a pair\r
+                       {\r
+                               *s++ = '\n'; // replace first one with 0x0a\r
+                               \r
+                               if (*s == '\n') g.push(s, 1);\r
+                       }\r
+                       else if (s[0] == '-' && s[1] == '-' && ENDSWITH(s[2], '>')) // comment ends here\r
+                       {\r
+                               *g.flush(s) = 0;\r
+                               \r
+                               return s + (s[2] == '>' ? 3 : 2);\r
+                       }\r
+                       else if (*s == 0)\r
+                       {\r
+                               return 0;\r
+                       }\r
+                       else ++s;\r
+               }\r
+       }\r
+\r
+       PUGI__FN char_t* strconv_cdata(char_t* s, char_t endch)\r
+       {\r
+               gap g;\r
+                       \r
+               while (true)\r
+               {\r
+                       while (!PUGI__IS_CHARTYPE(*s, ct_parse_cdata)) ++s;\r
+                       \r
+                       if (*s == '\r') // Either a single 0x0d or 0x0d 0x0a pair\r
+                       {\r
+                               *s++ = '\n'; // replace first one with 0x0a\r
+                               \r
+                               if (*s == '\n') g.push(s, 1);\r
+                       }\r
+                       else if (s[0] == ']' && s[1] == ']' && ENDSWITH(s[2], '>')) // CDATA ends here\r
+                       {\r
+                               *g.flush(s) = 0;\r
+                               \r
+                               return s + 1;\r
+                       }\r
+                       else if (*s == 0)\r
+                       {\r
+                               return 0;\r
+                       }\r
+                       else ++s;\r
+               }\r
+       }\r
+       \r
+       typedef char_t* (*strconv_pcdata_t)(char_t*);\r
+               \r
+       template <typename opt_trim, typename opt_eol, typename opt_escape> struct strconv_pcdata_impl\r
+       {\r
+               static char_t* parse(char_t* s)\r
+               {\r
+                       gap g;\r
+\r
+                       char_t* begin = s;\r
+\r
+                       while (true)\r
+                       {\r
+                               while (!PUGI__IS_CHARTYPE(*s, ct_parse_pcdata)) ++s;\r
+                                       \r
+                               if (*s == '<') // PCDATA ends here\r
+                               {\r
+                                       char_t* end = g.flush(s);\r
+\r
+                                       if (opt_trim::value)\r
+                                               while (end > begin && PUGI__IS_CHARTYPE(end[-1], ct_space))\r
+                                                       --end;\r
+\r
+                                       *end = 0;\r
+                                       \r
+                                       return s + 1;\r
+                               }\r
+                               else if (opt_eol::value && *s == '\r') // Either a single 0x0d or 0x0d 0x0a pair\r
+                               {\r
+                                       *s++ = '\n'; // replace first one with 0x0a\r
+                                       \r
+                                       if (*s == '\n') g.push(s, 1);\r
+                               }\r
+                               else if (opt_escape::value && *s == '&')\r
+                               {\r
+                                       s = strconv_escape(s, g);\r
+                               }\r
+                               else if (*s == 0)\r
+                               {\r
+                                       char_t* end = g.flush(s);\r
+\r
+                                       if (opt_trim::value)\r
+                                               while (end > begin && PUGI__IS_CHARTYPE(end[-1], ct_space))\r
+                                                       --end;\r
+\r
+                                       *end = 0;\r
+\r
+                                       return s;\r
+                               }\r
+                               else ++s;\r
+                       }\r
+               }\r
+       };\r
+       \r
+       PUGI__FN strconv_pcdata_t get_strconv_pcdata(unsigned int optmask)\r
+       {\r
+               PUGI__STATIC_ASSERT(parse_escapes == 0x10 && parse_eol == 0x20 && parse_trim_pcdata == 0x0800);\r
+\r
+               switch (((optmask >> 4) & 3) | ((optmask >> 9) & 4)) // get bitmask for flags (eol escapes trim)\r
+               {\r
+               case 0: return strconv_pcdata_impl<opt_false, opt_false, opt_false>::parse;\r
+               case 1: return strconv_pcdata_impl<opt_false, opt_false, opt_true>::parse;\r
+               case 2: return strconv_pcdata_impl<opt_false, opt_true, opt_false>::parse;\r
+               case 3: return strconv_pcdata_impl<opt_false, opt_true, opt_true>::parse;\r
+               case 4: return strconv_pcdata_impl<opt_true, opt_false, opt_false>::parse;\r
+               case 5: return strconv_pcdata_impl<opt_true, opt_false, opt_true>::parse;\r
+               case 6: return strconv_pcdata_impl<opt_true, opt_true, opt_false>::parse;\r
+               case 7: return strconv_pcdata_impl<opt_true, opt_true, opt_true>::parse;\r
+               default: assert(false); return 0; // should not get here\r
+               }\r
+       }\r
+\r
+       typedef char_t* (*strconv_attribute_t)(char_t*, char_t);\r
+       \r
+       template <typename opt_escape> struct strconv_attribute_impl\r
+       {\r
+               static char_t* parse_wnorm(char_t* s, char_t end_quote)\r
+               {\r
+                       gap g;\r
+\r
+                       // trim leading whitespaces\r
+                       if (PUGI__IS_CHARTYPE(*s, ct_space))\r
+                       {\r
+                               char_t* str = s;\r
+                               \r
+                               do ++str;\r
+                               while (PUGI__IS_CHARTYPE(*str, ct_space));\r
+                               \r
+                               g.push(s, str - s);\r
+                       }\r
+\r
+                       while (true)\r
+                       {\r
+                               while (!PUGI__IS_CHARTYPE(*s, ct_parse_attr_ws | ct_space)) ++s;\r
+                               \r
+                               if (*s == end_quote)\r
+                               {\r
+                                       char_t* str = g.flush(s);\r
+                                       \r
+                                       do *str-- = 0;\r
+                                       while (PUGI__IS_CHARTYPE(*str, ct_space));\r
+                               \r
+                                       return s + 1;\r
+                               }\r
+                               else if (PUGI__IS_CHARTYPE(*s, ct_space))\r
+                               {\r
+                                       *s++ = ' ';\r
+               \r
+                                       if (PUGI__IS_CHARTYPE(*s, ct_space))\r
+                                       {\r
+                                               char_t* str = s + 1;\r
+                                               while (PUGI__IS_CHARTYPE(*str, ct_space)) ++str;\r
+                                               \r
+                                               g.push(s, str - s);\r
+                                       }\r
+                               }\r
+                               else if (opt_escape::value && *s == '&')\r
+                               {\r
+                                       s = strconv_escape(s, g);\r
+                               }\r
+                               else if (!*s)\r
+                               {\r
+                                       return 0;\r
+                               }\r
+                               else ++s;\r
+                       }\r
+               }\r
+\r
+               static char_t* parse_wconv(char_t* s, char_t end_quote)\r
+               {\r
+                       gap g;\r
+\r
+                       while (true)\r
+                       {\r
+                               while (!PUGI__IS_CHARTYPE(*s, ct_parse_attr_ws)) ++s;\r
+                               \r
+                               if (*s == end_quote)\r
+                               {\r
+                                       *g.flush(s) = 0;\r
+                               \r
+                                       return s + 1;\r
+                               }\r
+                               else if (PUGI__IS_CHARTYPE(*s, ct_space))\r
+                               {\r
+                                       if (*s == '\r')\r
+                                       {\r
+                                               *s++ = ' ';\r
+                               \r
+                                               if (*s == '\n') g.push(s, 1);\r
+                                       }\r
+                                       else *s++ = ' ';\r
+                               }\r
+                               else if (opt_escape::value && *s == '&')\r
+                               {\r
+                                       s = strconv_escape(s, g);\r
+                               }\r
+                               else if (!*s)\r
+                               {\r
+                                       return 0;\r
+                               }\r
+                               else ++s;\r
+                       }\r
+               }\r
+\r
+               static char_t* parse_eol(char_t* s, char_t end_quote)\r
+               {\r
+                       gap g;\r
+\r
+                       while (true)\r
+                       {\r
+                               while (!PUGI__IS_CHARTYPE(*s, ct_parse_attr)) ++s;\r
+                               \r
+                               if (*s == end_quote)\r
+                               {\r
+                                       *g.flush(s) = 0;\r
+                               \r
+                                       return s + 1;\r
+                               }\r
+                               else if (*s == '\r')\r
+                               {\r
+                                       *s++ = '\n';\r
+                                       \r
+                                       if (*s == '\n') g.push(s, 1);\r
+                               }\r
+                               else if (opt_escape::value && *s == '&')\r
+                               {\r
+                                       s = strconv_escape(s, g);\r
+                               }\r
+                               else if (!*s)\r
+                               {\r
+                                       return 0;\r
+                               }\r
+                               else ++s;\r
+                       }\r
+               }\r
+\r
+               static char_t* parse_simple(char_t* s, char_t end_quote)\r
+               {\r
+                       gap g;\r
+\r
+                       while (true)\r
+                       {\r
+                               while (!PUGI__IS_CHARTYPE(*s, ct_parse_attr)) ++s;\r
+                               \r
+                               if (*s == end_quote)\r
+                               {\r
+                                       *g.flush(s) = 0;\r
+                               \r
+                                       return s + 1;\r
+                               }\r
+                               else if (opt_escape::value && *s == '&')\r
+                               {\r
+                                       s = strconv_escape(s, g);\r
+                               }\r
+                               else if (!*s)\r
+                               {\r
+                                       return 0;\r
+                               }\r
+                               else ++s;\r
+                       }\r
+               }\r
+       };\r
+\r
+       PUGI__FN strconv_attribute_t get_strconv_attribute(unsigned int optmask)\r
+       {\r
+               PUGI__STATIC_ASSERT(parse_escapes == 0x10 && parse_eol == 0x20 && parse_wconv_attribute == 0x40 && parse_wnorm_attribute == 0x80);\r
+               \r
+               switch ((optmask >> 4) & 15) // get bitmask for flags (wconv wnorm eol escapes)\r
+               {\r
+               case 0:  return strconv_attribute_impl<opt_false>::parse_simple;\r
+               case 1:  return strconv_attribute_impl<opt_true>::parse_simple;\r
+               case 2:  return strconv_attribute_impl<opt_false>::parse_eol;\r
+               case 3:  return strconv_attribute_impl<opt_true>::parse_eol;\r
+               case 4:  return strconv_attribute_impl<opt_false>::parse_wconv;\r
+               case 5:  return strconv_attribute_impl<opt_true>::parse_wconv;\r
+               case 6:  return strconv_attribute_impl<opt_false>::parse_wconv;\r
+               case 7:  return strconv_attribute_impl<opt_true>::parse_wconv;\r
+               case 8:  return strconv_attribute_impl<opt_false>::parse_wnorm;\r
+               case 9:  return strconv_attribute_impl<opt_true>::parse_wnorm;\r
+               case 10: return strconv_attribute_impl<opt_false>::parse_wnorm;\r
+               case 11: return strconv_attribute_impl<opt_true>::parse_wnorm;\r
+               case 12: return strconv_attribute_impl<opt_false>::parse_wnorm;\r
+               case 13: return strconv_attribute_impl<opt_true>::parse_wnorm;\r
+               case 14: return strconv_attribute_impl<opt_false>::parse_wnorm;\r
+               case 15: return strconv_attribute_impl<opt_true>::parse_wnorm;\r
+               default: assert(false); return 0; // should not get here\r
+               }\r
+       }\r
+\r
+       inline xml_parse_result make_parse_result(xml_parse_status status, ptrdiff_t offset = 0)\r
+       {\r
+               xml_parse_result result;\r
+               result.status = status;\r
+               result.offset = offset;\r
+\r
+               return result;\r
+       }\r
+\r
+       struct xml_parser\r
+       {\r
+               xml_allocator alloc;\r
+               char_t* error_offset;\r
+               xml_parse_status error_status;\r
+               \r
+               // Parser utilities.\r
+               #define PUGI__SKIPWS()                  { while (PUGI__IS_CHARTYPE(*s, ct_space)) ++s; }\r
+               #define PUGI__OPTSET(OPT)                       ( optmsk & (OPT) )\r
+               #define PUGI__PUSHNODE(TYPE)            { cursor = append_node(cursor, alloc, TYPE); if (!cursor) PUGI__THROW_ERROR(status_out_of_memory, s); }\r
+               #define PUGI__POPNODE()                 { cursor = cursor->parent; }\r
+               #define PUGI__SCANFOR(X)                        { while (*s != 0 && !(X)) ++s; }\r
+               #define PUGI__SCANWHILE(X)              { while ((X)) ++s; }\r
+               #define PUGI__ENDSEG()                  { ch = *s; *s = 0; ++s; }\r
+               #define PUGI__THROW_ERROR(err, m)       return error_offset = m, error_status = err, static_cast<char_t*>(0)\r
+               #define PUGI__CHECK_ERROR(err, m)       { if (*s == 0) PUGI__THROW_ERROR(err, m); }\r
+               \r
+               xml_parser(const xml_allocator& alloc_): alloc(alloc_), error_offset(0), error_status(status_ok)\r
+               {\r
+               }\r
+\r
+               // DOCTYPE consists of nested sections of the following possible types:\r
+               // <!-- ... -->, <? ... ?>, "...", '...'\r
+               // <![...]]>\r
+               // <!...>\r
+               // First group can not contain nested groups\r
+               // Second group can contain nested groups of the same type\r
+               // Third group can contain all other groups\r
+               char_t* parse_doctype_primitive(char_t* s)\r
+               {\r
+                       if (*s == '"' || *s == '\'')\r
+                       {\r
+                               // quoted string\r
+                               char_t ch = *s++;\r
+                               PUGI__SCANFOR(*s == ch);\r
+                               if (!*s) PUGI__THROW_ERROR(status_bad_doctype, s);\r
+\r
+                               s++;\r
+                       }\r
+                       else if (s[0] == '<' && s[1] == '?')\r
+                       {\r
+                               // <? ... ?>\r
+                               s += 2;\r
+                               PUGI__SCANFOR(s[0] == '?' && s[1] == '>'); // no need for ENDSWITH because ?> can't terminate proper doctype\r
+                               if (!*s) PUGI__THROW_ERROR(status_bad_doctype, s);\r
+\r
+                               s += 2;\r
+                       }\r
+                       else if (s[0] == '<' && s[1] == '!' && s[2] == '-' && s[3] == '-')\r
+                       {\r
+                               s += 4;\r
+                               PUGI__SCANFOR(s[0] == '-' && s[1] == '-' && s[2] == '>'); // no need for ENDSWITH because --> can't terminate proper doctype\r
+                               if (!*s) PUGI__THROW_ERROR(status_bad_doctype, s);\r
+\r
+                               s += 4;\r
+                       }\r
+                       else PUGI__THROW_ERROR(status_bad_doctype, s);\r
+\r
+                       return s;\r
+               }\r
+\r
+               char_t* parse_doctype_ignore(char_t* s)\r
+               {\r
+                       assert(s[0] == '<' && s[1] == '!' && s[2] == '[');\r
+                       s++;\r
+\r
+                       while (*s)\r
+                       {\r
+                               if (s[0] == '<' && s[1] == '!' && s[2] == '[')\r
+                               {\r
+                                       // nested ignore section\r
+                                       s = parse_doctype_ignore(s);\r
+                                       if (!s) return s;\r
+                               }\r
+                               else if (s[0] == ']' && s[1] == ']' && s[2] == '>')\r
+                               {\r
+                                       // ignore section end\r
+                                       s += 3;\r
+\r
+                                       return s;\r
+                               }\r
+                               else s++;\r
+                       }\r
+\r
+                       PUGI__THROW_ERROR(status_bad_doctype, s);\r
+               }\r
+\r
+               char_t* parse_doctype_group(char_t* s, char_t endch, bool toplevel)\r
+               {\r
+                       assert((s[0] == '<' || s[0] == 0) && s[1] == '!');\r
+                       s++;\r
+\r
+                       while (*s)\r
+                       {\r
+                               if (s[0] == '<' && s[1] == '!' && s[2] != '-')\r
+                               {\r
+                                       if (s[2] == '[')\r
+                                       {\r
+                                               // ignore\r
+                                               s = parse_doctype_ignore(s);\r
+                                               if (!s) return s;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               // some control group\r
+                                               s = parse_doctype_group(s, endch, false);\r
+                                               if (!s) return s;\r
+\r
+                                               // skip >\r
+                                               assert(*s == '>');\r
+                                               s++;\r
+                                       }\r
+                               }\r
+                               else if (s[0] == '<' || s[0] == '"' || s[0] == '\'')\r
+                               {\r
+                                       // unknown tag (forbidden), or some primitive group\r
+                                       s = parse_doctype_primitive(s);\r
+                                       if (!s) return s;\r
+                               }\r
+                               else if (*s == '>')\r
+                               {\r
+                                       return s;\r
+                               }\r
+                               else s++;\r
+                       }\r
+\r
+                       if (!toplevel || endch != '>') PUGI__THROW_ERROR(status_bad_doctype, s);\r
+\r
+                       return s;\r
+               }\r
+\r
+               char_t* parse_exclamation(char_t* s, xml_node_struct* cursor, unsigned int optmsk, char_t endch)\r
+               {\r
+                       // parse node contents, starting with exclamation mark\r
+                       ++s;\r
+\r
+                       if (*s == '-') // '<!-...'\r
+                       {\r
+                               ++s;\r
+\r
+                               if (*s == '-') // '<!--...'\r
+                               {\r
+                                       ++s;\r
+\r
+                                       if (PUGI__OPTSET(parse_comments))\r
+                                       {\r
+                                               PUGI__PUSHNODE(node_comment); // Append a new node on the tree.\r
+                                               cursor->value = s; // Save the offset.\r
+                                       }\r
+\r
+                                       if (PUGI__OPTSET(parse_eol) && PUGI__OPTSET(parse_comments))\r
+                                       {\r
+                                               s = strconv_comment(s, endch);\r
+\r
+                                               if (!s) PUGI__THROW_ERROR(status_bad_comment, cursor->value);\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               // Scan for terminating '-->'.\r
+                                               PUGI__SCANFOR(s[0] == '-' && s[1] == '-' && ENDSWITH(s[2], '>'));\r
+                                               PUGI__CHECK_ERROR(status_bad_comment, s);\r
+\r
+                                               if (PUGI__OPTSET(parse_comments))\r
+                                                       *s = 0; // Zero-terminate this segment at the first terminating '-'.\r
+\r
+                                               s += (s[2] == '>' ? 3 : 2); // Step over the '\0->'.\r
+                                       }\r
+                               }\r
+                               else PUGI__THROW_ERROR(status_bad_comment, s);\r
+                       }\r
+                       else if (*s == '[')\r
+                       {\r
+                               // '<![CDATA[...'\r
+                               if (*++s=='C' && *++s=='D' && *++s=='A' && *++s=='T' && *++s=='A' && *++s == '[')\r
+                               {\r
+                                       ++s;\r
+\r
+                                       if (PUGI__OPTSET(parse_cdata))\r
+                                       {\r
+                                               PUGI__PUSHNODE(node_cdata); // Append a new node on the tree.\r
+                                               cursor->value = s; // Save the offset.\r
+\r
+                                               if (PUGI__OPTSET(parse_eol))\r
+                                               {\r
+                                                       s = strconv_cdata(s, endch);\r
+\r
+                                                       if (!s) PUGI__THROW_ERROR(status_bad_cdata, cursor->value);\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       // Scan for terminating ']]>'.\r
+                                                       PUGI__SCANFOR(s[0] == ']' && s[1] == ']' && ENDSWITH(s[2], '>'));\r
+                                                       PUGI__CHECK_ERROR(status_bad_cdata, s);\r
+\r
+                                                       *s++ = 0; // Zero-terminate this segment.\r
+                                               }\r
+                                       }\r
+                                       else // Flagged for discard, but we still have to scan for the terminator.\r
+                                       {\r
+                                               // Scan for terminating ']]>'.\r
+                                               PUGI__SCANFOR(s[0] == ']' && s[1] == ']' && ENDSWITH(s[2], '>'));\r
+                                               PUGI__CHECK_ERROR(status_bad_cdata, s);\r
+\r
+                                               ++s;\r
+                                       }\r
+\r
+                                       s += (s[1] == '>' ? 2 : 1); // Step over the last ']>'.\r
+                               }\r
+                               else PUGI__THROW_ERROR(status_bad_cdata, s);\r
+                       }\r
+                       else if (s[0] == 'D' && s[1] == 'O' && s[2] == 'C' && s[3] == 'T' && s[4] == 'Y' && s[5] == 'P' && ENDSWITH(s[6], 'E'))\r
+                       {\r
+                               s -= 2;\r
+\r
+                               if (cursor->parent) PUGI__THROW_ERROR(status_bad_doctype, s);\r
+\r
+                               char_t* mark = s + 9;\r
+\r
+                               s = parse_doctype_group(s, endch, true);\r
+                               if (!s) return s;\r
+\r
+                               assert((*s == 0 && endch == '>') || *s == '>');\r
+                               if (*s) *s++ = 0;\r
+\r
+                               if (PUGI__OPTSET(parse_doctype))\r
+                               {\r
+                                       while (PUGI__IS_CHARTYPE(*mark, ct_space)) ++mark;\r
+\r
+                                       PUGI__PUSHNODE(node_doctype);\r
+\r
+                                       cursor->value = mark;\r
+\r
+                                       PUGI__POPNODE();\r
+                               }\r
+                       }\r
+                       else if (*s == 0 && endch == '-') PUGI__THROW_ERROR(status_bad_comment, s);\r
+                       else if (*s == 0 && endch == '[') PUGI__THROW_ERROR(status_bad_cdata, s);\r
+                       else PUGI__THROW_ERROR(status_unrecognized_tag, s);\r
+\r
+                       return s;\r
+               }\r
+\r
+               char_t* parse_question(char_t* s, xml_node_struct*& ref_cursor, unsigned int optmsk, char_t endch)\r
+               {\r
+                       // load into registers\r
+                       xml_node_struct* cursor = ref_cursor;\r
+                       char_t ch = 0;\r
+\r
+                       // parse node contents, starting with question mark\r
+                       ++s;\r
+\r
+                       // read PI target\r
+                       char_t* target = s;\r
+\r
+                       if (!PUGI__IS_CHARTYPE(*s, ct_start_symbol)) PUGI__THROW_ERROR(status_bad_pi, s);\r
+\r
+                       PUGI__SCANWHILE(PUGI__IS_CHARTYPE(*s, ct_symbol));\r
+                       PUGI__CHECK_ERROR(status_bad_pi, s);\r
+\r
+                       // determine node type; stricmp / strcasecmp is not portable\r
+                       bool declaration = (target[0] | ' ') == 'x' && (target[1] | ' ') == 'm' && (target[2] | ' ') == 'l' && target + 3 == s;\r
+\r
+                       if (declaration ? PUGI__OPTSET(parse_declaration) : PUGI__OPTSET(parse_pi))\r
+                       {\r
+                               if (declaration)\r
+                               {\r
+                                       // disallow non top-level declarations\r
+                                       if (cursor->parent) PUGI__THROW_ERROR(status_bad_pi, s);\r
+\r
+                                       PUGI__PUSHNODE(node_declaration);\r
+                               }\r
+                               else\r
+                               {\r
+                                       PUGI__PUSHNODE(node_pi);\r
+                               }\r
+\r
+                               cursor->name = target;\r
+\r
+                               PUGI__ENDSEG();\r
+\r
+                               // parse value/attributes\r
+                               if (ch == '?')\r
+                               {\r
+                                       // empty node\r
+                                       if (!ENDSWITH(*s, '>')) PUGI__THROW_ERROR(status_bad_pi, s);\r
+                                       s += (*s == '>');\r
+\r
+                                       PUGI__POPNODE();\r
+                               }\r
+                               else if (PUGI__IS_CHARTYPE(ch, ct_space))\r
+                               {\r
+                                       PUGI__SKIPWS();\r
+\r
+                                       // scan for tag end\r
+                                       char_t* value = s;\r
+\r
+                                       PUGI__SCANFOR(s[0] == '?' && ENDSWITH(s[1], '>'));\r
+                                       PUGI__CHECK_ERROR(status_bad_pi, s);\r
+\r
+                                       if (declaration)\r
+                                       {\r
+                                               // replace ending ? with / so that 'element' terminates properly\r
+                                               *s = '/';\r
+\r
+                                               // we exit from this function with cursor at node_declaration, which is a signal to parse() to go to LOC_ATTRIBUTES\r
+                                               s = value;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               // store value and step over >\r
+                                               cursor->value = value;\r
+                                               PUGI__POPNODE();\r
+\r
+                                               PUGI__ENDSEG();\r
+\r
+                                               s += (*s == '>');\r
+                                       }\r
+                               }\r
+                               else PUGI__THROW_ERROR(status_bad_pi, s);\r
+                       }\r
+                       else\r
+                       {\r
+                               // scan for tag end\r
+                               PUGI__SCANFOR(s[0] == '?' && ENDSWITH(s[1], '>'));\r
+                               PUGI__CHECK_ERROR(status_bad_pi, s);\r
+\r
+                               s += (s[1] == '>' ? 2 : 1);\r
+                       }\r
+\r
+                       // store from registers\r
+                       ref_cursor = cursor;\r
+\r
+                       return s;\r
+               }\r
+\r
+               char_t* parse_tree(char_t* s, xml_node_struct* root, unsigned int optmsk, char_t endch)\r
+               {\r
+                       strconv_attribute_t strconv_attribute = get_strconv_attribute(optmsk);\r
+                       strconv_pcdata_t strconv_pcdata = get_strconv_pcdata(optmsk);\r
+                       \r
+                       char_t ch = 0;\r
+                       xml_node_struct* cursor = root;\r
+                       char_t* mark = s;\r
+\r
+                       while (*s != 0)\r
+                       {\r
+                               if (*s == '<')\r
+                               {\r
+                                       ++s;\r
+\r
+                               LOC_TAG:\r
+                                       if (PUGI__IS_CHARTYPE(*s, ct_start_symbol)) // '<#...'\r
+                                       {\r
+                                               PUGI__PUSHNODE(node_element); // Append a new node to the tree.\r
+\r
+                                               cursor->name = s;\r
+\r
+                                               PUGI__SCANWHILE(PUGI__IS_CHARTYPE(*s, ct_symbol)); // Scan for a terminator.\r
+                                               PUGI__ENDSEG(); // Save char in 'ch', terminate & step over.\r
+\r
+                                               if (ch == '>')\r
+                                               {\r
+                                                       // end of tag\r
+                                               }\r
+                                               else if (PUGI__IS_CHARTYPE(ch, ct_space))\r
+                                               {\r
+                                               LOC_ATTRIBUTES:\r
+                                                       while (true)\r
+                                                       {\r
+                                                               PUGI__SKIPWS(); // Eat any whitespace.\r
+                                               \r
+                                                               if (PUGI__IS_CHARTYPE(*s, ct_start_symbol)) // <... #...\r
+                                                               {\r
+                                                                       xml_attribute_struct* a = append_attribute_ll(cursor, alloc); // Make space for this attribute.\r
+                                                                       if (!a) PUGI__THROW_ERROR(status_out_of_memory, s);\r
+\r
+                                                                       a->name = s; // Save the offset.\r
+\r
+                                                                       PUGI__SCANWHILE(PUGI__IS_CHARTYPE(*s, ct_symbol)); // Scan for a terminator.\r
+                                                                       PUGI__CHECK_ERROR(status_bad_attribute, s); //$ redundant, left for performance\r
+\r
+                                                                       PUGI__ENDSEG(); // Save char in 'ch', terminate & step over.\r
+                                                                       PUGI__CHECK_ERROR(status_bad_attribute, s); //$ redundant, left for performance\r
+\r
+                                                                       if (PUGI__IS_CHARTYPE(ch, ct_space))\r
+                                                                       {\r
+                                                                               PUGI__SKIPWS(); // Eat any whitespace.\r
+                                                                               PUGI__CHECK_ERROR(status_bad_attribute, s); //$ redundant, left for performance\r
+\r
+                                                                               ch = *s;\r
+                                                                               ++s;\r
+                                                                       }\r
+                                                                       \r
+                                                                       if (ch == '=') // '<... #=...'\r
+                                                                       {\r
+                                                                               PUGI__SKIPWS(); // Eat any whitespace.\r
+\r
+                                                                               if (*s == '"' || *s == '\'') // '<... #="...'\r
+                                                                               {\r
+                                                                                       ch = *s; // Save quote char to avoid breaking on "''" -or- '""'.\r
+                                                                                       ++s; // Step over the quote.\r
+                                                                                       a->value = s; // Save the offset.\r
+\r
+                                                                                       s = strconv_attribute(s, ch);\r
+                                                                               \r
+                                                                                       if (!s) PUGI__THROW_ERROR(status_bad_attribute, a->value);\r
+\r
+                                                                                       // After this line the loop continues from the start;\r
+                                                                                       // Whitespaces, / and > are ok, symbols and EOF are wrong,\r
+                                                                                       // everything else will be detected\r
+                                                                                       if (PUGI__IS_CHARTYPE(*s, ct_start_symbol)) PUGI__THROW_ERROR(status_bad_attribute, s);\r
+                                                                               }\r
+                                                                               else PUGI__THROW_ERROR(status_bad_attribute, s);\r
+                                                                       }\r
+                                                                       else PUGI__THROW_ERROR(status_bad_attribute, s);\r
+                                                               }\r
+                                                               else if (*s == '/')\r
+                                                               {\r
+                                                                       ++s;\r
+                                                                       \r
+                                                                       if (*s == '>')\r
+                                                                       {\r
+                                                                               PUGI__POPNODE();\r
+                                                                               s++;\r
+                                                                               break;\r
+                                                                       }\r
+                                                                       else if (*s == 0 && endch == '>')\r
+                                                                       {\r
+                                                                               PUGI__POPNODE();\r
+                                                                               break;\r
+                                                                       }\r
+                                                                       else PUGI__THROW_ERROR(status_bad_start_element, s);\r
+                                                               }\r
+                                                               else if (*s == '>')\r
+                                                               {\r
+                                                                       ++s;\r
+\r
+                                                                       break;\r
+                                                               }\r
+                                                               else if (*s == 0 && endch == '>')\r
+                                                               {\r
+                                                                       break;\r
+                                                               }\r
+                                                               else PUGI__THROW_ERROR(status_bad_start_element, s);\r
+                                                       }\r
+\r
+                                                       // !!!\r
+                                               }\r
+                                               else if (ch == '/') // '<#.../'\r
+                                               {\r
+                                                       if (!ENDSWITH(*s, '>')) PUGI__THROW_ERROR(status_bad_start_element, s);\r
+\r
+                                                       PUGI__POPNODE(); // Pop.\r
+\r
+                                                       s += (*s == '>');\r
+                                               }\r
+                                               else if (ch == 0)\r
+                                               {\r
+                                                       // we stepped over null terminator, backtrack & handle closing tag\r
+                                                       --s;\r
+                                                       \r
+                                                       if (endch != '>') PUGI__THROW_ERROR(status_bad_start_element, s);\r
+                                               }\r
+                                               else PUGI__THROW_ERROR(status_bad_start_element, s);\r
+                                       }\r
+                                       else if (*s == '/')\r
+                                       {\r
+                                               ++s;\r
+\r
+                                               char_t* name = cursor->name;\r
+                                               if (!name) PUGI__THROW_ERROR(status_end_element_mismatch, s);\r
+                                               \r
+                                               while (PUGI__IS_CHARTYPE(*s, ct_symbol))\r
+                                               {\r
+                                                       if (*s++ != *name++) PUGI__THROW_ERROR(status_end_element_mismatch, s);\r
+                                               }\r
+\r
+                                               if (*name)\r
+                                               {\r
+                                                       if (*s == 0 && name[0] == endch && name[1] == 0) PUGI__THROW_ERROR(status_bad_end_element, s);\r
+                                                       else PUGI__THROW_ERROR(status_end_element_mismatch, s);\r
+                                               }\r
+                                                       \r
+                                               PUGI__POPNODE(); // Pop.\r
+\r
+                                               PUGI__SKIPWS();\r
+\r
+                                               if (*s == 0)\r
+                                               {\r
+                                                       if (endch != '>') PUGI__THROW_ERROR(status_bad_end_element, s);\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       if (*s != '>') PUGI__THROW_ERROR(status_bad_end_element, s);\r
+                                                       ++s;\r
+                                               }\r
+                                       }\r
+                                       else if (*s == '?') // '<?...'\r
+                                       {\r
+                                               s = parse_question(s, cursor, optmsk, endch);\r
+                                               if (!s) return s;\r
+\r
+                                               assert(cursor);\r
+                                               if ((cursor->header & xml_memory_page_type_mask) + 1 == node_declaration) goto LOC_ATTRIBUTES;\r
+                                       }\r
+                                       else if (*s == '!') // '<!...'\r
+                                       {\r
+                                               s = parse_exclamation(s, cursor, optmsk, endch);\r
+                                               if (!s) return s;\r
+                                       }\r
+                                       else if (*s == 0 && endch == '?') PUGI__THROW_ERROR(status_bad_pi, s);\r
+                                       else PUGI__THROW_ERROR(status_unrecognized_tag, s);\r
+                               }\r
+                               else\r
+                               {\r
+                                       mark = s; // Save this offset while searching for a terminator.\r
+\r
+                                       PUGI__SKIPWS(); // Eat whitespace if no genuine PCDATA here.\r
+\r
+                                       if (*s == '<' || !*s)\r
+                                       {\r
+                                               // We skipped some whitespace characters because otherwise we would take the tag branch instead of PCDATA one\r
+                                               assert(mark != s);\r
+\r
+                                               if (!PUGI__OPTSET(parse_ws_pcdata | parse_ws_pcdata_single) || PUGI__OPTSET(parse_trim_pcdata))\r
+                                               {\r
+                                                       continue;\r
+                                               }\r
+                                               else if (PUGI__OPTSET(parse_ws_pcdata_single))\r
+                                               {\r
+                                                       if (s[0] != '<' || s[1] != '/' || cursor->first_child) continue;\r
+                                               }\r
+                                       }\r
+\r
+                                       if (!PUGI__OPTSET(parse_trim_pcdata))\r
+                                               s = mark;\r
+                                                       \r
+                                       if (cursor->parent || PUGI__OPTSET(parse_fragment))\r
+                                       {\r
+                                               PUGI__PUSHNODE(node_pcdata); // Append a new node on the tree.\r
+                                               cursor->value = s; // Save the offset.\r
+\r
+                                               s = strconv_pcdata(s);\r
+                                                               \r
+                                               PUGI__POPNODE(); // Pop since this is a standalone.\r
+                                               \r
+                                               if (!*s) break;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               PUGI__SCANFOR(*s == '<'); // '...<'\r
+                                               if (!*s) break;\r
+                                               \r
+                                               ++s;\r
+                                       }\r
+\r
+                                       // We're after '<'\r
+                                       goto LOC_TAG;\r
+                               }\r
+                       }\r
+\r
+                       // check that last tag is closed\r
+                       if (cursor != root) PUGI__THROW_ERROR(status_end_element_mismatch, s);\r
+\r
+                       return s;\r
+               }\r
+\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               static char_t* parse_skip_bom(char_t* s)\r
+               {\r
+                       unsigned int bom = 0xfeff;\r
+                       return (s[0] == static_cast<wchar_t>(bom)) ? s + 1 : s;\r
+               }\r
+       #else\r
+               static char_t* parse_skip_bom(char_t* s)\r
+               {\r
+                       return (s[0] == '\xef' && s[1] == '\xbb' && s[2] == '\xbf') ? s + 3 : s;\r
+               }\r
+       #endif\r
+\r
+               static bool has_element_node_siblings(xml_node_struct* node)\r
+               {\r
+                       while (node)\r
+                       {\r
+                               xml_node_type type = static_cast<xml_node_type>((node->header & impl::xml_memory_page_type_mask) + 1);\r
+                               if (type == node_element) return true;\r
+\r
+                               node = node->next_sibling;\r
+                       }\r
+\r
+                       return false;\r
+               }\r
+\r
+               static xml_parse_result parse(char_t* buffer, size_t length, xml_document_struct* xmldoc, xml_node_struct* root, unsigned int optmsk)\r
+               {\r
+                       // allocator object is a part of document object\r
+                       xml_allocator& alloc = *static_cast<xml_allocator*>(xmldoc);\r
+\r
+                       // early-out for empty documents\r
+                       if (length == 0)\r
+                               return make_parse_result(PUGI__OPTSET(parse_fragment) ? status_ok : status_no_document_element);\r
+\r
+                       // get last child of the root before parsing\r
+                       xml_node_struct* last_root_child = root->first_child ? root->first_child->prev_sibling_c : 0;\r
+       \r
+                       // create parser on stack\r
+                       xml_parser parser(alloc);\r
+\r
+                       // save last character and make buffer zero-terminated (speeds up parsing)\r
+                       char_t endch = buffer[length - 1];\r
+                       buffer[length - 1] = 0;\r
+                       \r
+                       // skip BOM to make sure it does not end up as part of parse output\r
+                       char_t* buffer_data = parse_skip_bom(buffer);\r
+\r
+                       // perform actual parsing\r
+                       parser.parse_tree(buffer_data, root, optmsk, endch);\r
+\r
+                       // update allocator state\r
+                       alloc = parser.alloc;\r
+\r
+                       xml_parse_result result = make_parse_result(parser.error_status, parser.error_offset ? parser.error_offset - buffer : 0);\r
+                       assert(result.offset >= 0 && static_cast<size_t>(result.offset) <= length);\r
+\r
+                       if (result)\r
+                       {\r
+                               // since we removed last character, we have to handle the only possible false positive (stray <)\r
+                               if (endch == '<')\r
+                                       return make_parse_result(status_unrecognized_tag, length - 1);\r
+\r
+                               // check if there are any element nodes parsed\r
+                               xml_node_struct* first_root_child_parsed = last_root_child ? last_root_child->next_sibling : root->first_child;\r
+\r
+                               if (!PUGI__OPTSET(parse_fragment) && !has_element_node_siblings(first_root_child_parsed))\r
+                                       return make_parse_result(status_no_document_element, length - 1);\r
+                       }\r
+                       else\r
+                       {\r
+                               // roll back offset if it occurs on a null terminator in the source buffer\r
+                               if (result.offset > 0 && static_cast<size_t>(result.offset) == length - 1 && endch == 0)\r
+                                       result.offset--;\r
+                       }\r
+\r
+                       return result;\r
+               }\r
+       };\r
+\r
+       // Output facilities\r
+       PUGI__FN xml_encoding get_write_native_encoding()\r
+       {\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               return get_wchar_encoding();\r
+       #else\r
+               return encoding_utf8;\r
+       #endif\r
+       }\r
+\r
+       PUGI__FN xml_encoding get_write_encoding(xml_encoding encoding)\r
+       {\r
+               // replace wchar encoding with utf implementation\r
+               if (encoding == encoding_wchar) return get_wchar_encoding();\r
+\r
+               // replace utf16 encoding with utf16 with specific endianness\r
+               if (encoding == encoding_utf16) return is_little_endian() ? encoding_utf16_le : encoding_utf16_be;\r
+\r
+               // replace utf32 encoding with utf32 with specific endianness\r
+               if (encoding == encoding_utf32) return is_little_endian() ? encoding_utf32_le : encoding_utf32_be;\r
+\r
+               // only do autodetection if no explicit encoding is requested\r
+               if (encoding != encoding_auto) return encoding;\r
+\r
+               // assume utf8 encoding\r
+               return encoding_utf8;\r
+       }\r
+\r
+#ifdef PUGIXML_WCHAR_MODE\r
+       PUGI__FN size_t get_valid_length(const char_t* data, size_t length)\r
+       {\r
+               assert(length > 0);\r
+\r
+               // discard last character if it's the lead of a surrogate pair \r
+               return (sizeof(wchar_t) == 2 && static_cast<unsigned int>(static_cast<uint16_t>(data[length - 1]) - 0xD800) < 0x400) ? length - 1 : length;\r
+       }\r
+\r
+       PUGI__FN size_t convert_buffer_output(char_t* r_char, uint8_t* r_u8, uint16_t* r_u16, uint32_t* r_u32, const char_t* data, size_t length, xml_encoding encoding)\r
+       {\r
+               // only endian-swapping is required\r
+               if (need_endian_swap_utf(encoding, get_wchar_encoding()))\r
+               {\r
+                       convert_wchar_endian_swap(r_char, data, length);\r
+\r
+                       return length * sizeof(char_t);\r
+               }\r
+       \r
+               // convert to utf8\r
+               if (encoding == encoding_utf8)\r
+               {\r
+                       uint8_t* dest = r_u8;\r
+                       uint8_t* end = utf_decoder<utf8_writer>::decode_wchar_block(data, length, dest);\r
+\r
+                       return static_cast<size_t>(end - dest);\r
+               }\r
+\r
+               // convert to utf16\r
+               if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)\r
+               {\r
+                       uint16_t* dest = r_u16;\r
+\r
+                       // convert to native utf16\r
+                       uint16_t* end = utf_decoder<utf16_writer>::decode_wchar_block(data, length, dest);\r
+\r
+                       // swap if necessary\r
+                       xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be;\r
+\r
+                       if (native_encoding != encoding) convert_utf_endian_swap(dest, dest, static_cast<size_t>(end - dest));\r
+\r
+                       return static_cast<size_t>(end - dest) * sizeof(uint16_t);\r
+               }\r
+\r
+               // convert to utf32\r
+               if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)\r
+               {\r
+                       uint32_t* dest = r_u32;\r
+\r
+                       // convert to native utf32\r
+                       uint32_t* end = utf_decoder<utf32_writer>::decode_wchar_block(data, length, dest);\r
+\r
+                       // swap if necessary\r
+                       xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be;\r
+\r
+                       if (native_encoding != encoding) convert_utf_endian_swap(dest, dest, static_cast<size_t>(end - dest));\r
+\r
+                       return static_cast<size_t>(end - dest) * sizeof(uint32_t);\r
+               }\r
+\r
+               // convert to latin1\r
+               if (encoding == encoding_latin1)\r
+               {\r
+                       uint8_t* dest = r_u8;\r
+                       uint8_t* end = utf_decoder<latin1_writer>::decode_wchar_block(data, length, dest);\r
+\r
+                       return static_cast<size_t>(end - dest);\r
+               }\r
+\r
+               assert(!"Invalid encoding");\r
+               return 0;\r
+       }\r
+#else\r
+       PUGI__FN size_t get_valid_length(const char_t* data, size_t length)\r
+       {\r
+               assert(length > 4);\r
+\r
+               for (size_t i = 1; i <= 4; ++i)\r
+               {\r
+                       uint8_t ch = static_cast<uint8_t>(data[length - i]);\r
+\r
+                       // either a standalone character or a leading one\r
+                       if ((ch & 0xc0) != 0x80) return length - i;\r
+               }\r
+\r
+               // there are four non-leading characters at the end, sequence tail is broken so might as well process the whole chunk\r
+               return length;\r
+       }\r
+\r
+       PUGI__FN size_t convert_buffer_output(char_t* /* r_char */, uint8_t* r_u8, uint16_t* r_u16, uint32_t* r_u32, const char_t* data, size_t length, xml_encoding encoding)\r
+       {\r
+               if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)\r
+               {\r
+                       uint16_t* dest = r_u16;\r
+\r
+                       // convert to native utf16\r
+                       uint16_t* end = utf_decoder<utf16_writer>::decode_utf8_block(reinterpret_cast<const uint8_t*>(data), length, dest);\r
+\r
+                       // swap if necessary\r
+                       xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be;\r
+\r
+                       if (native_encoding != encoding) convert_utf_endian_swap(dest, dest, static_cast<size_t>(end - dest));\r
+\r
+                       return static_cast<size_t>(end - dest) * sizeof(uint16_t);\r
+               }\r
+\r
+               if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)\r
+               {\r
+                       uint32_t* dest = r_u32;\r
+\r
+                       // convert to native utf32\r
+                       uint32_t* end = utf_decoder<utf32_writer>::decode_utf8_block(reinterpret_cast<const uint8_t*>(data), length, dest);\r
+\r
+                       // swap if necessary\r
+                       xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be;\r
+\r
+                       if (native_encoding != encoding) convert_utf_endian_swap(dest, dest, static_cast<size_t>(end - dest));\r
+\r
+                       return static_cast<size_t>(end - dest) * sizeof(uint32_t);\r
+               }\r
+\r
+               if (encoding == encoding_latin1)\r
+               {\r
+                       uint8_t* dest = r_u8;\r
+                       uint8_t* end = utf_decoder<latin1_writer>::decode_utf8_block(reinterpret_cast<const uint8_t*>(data), length, dest);\r
+\r
+                       return static_cast<size_t>(end - dest);\r
+               }\r
+\r
+               assert(!"Invalid encoding");\r
+               return 0;\r
+       }\r
+#endif\r
+\r
+       class xml_buffered_writer\r
+       {\r
+               xml_buffered_writer(const xml_buffered_writer&);\r
+               xml_buffered_writer& operator=(const xml_buffered_writer&);\r
+\r
+       public:\r
+               xml_buffered_writer(xml_writer& writer_, xml_encoding user_encoding): writer(writer_), bufsize(0), encoding(get_write_encoding(user_encoding))\r
+               {\r
+                       PUGI__STATIC_ASSERT(bufcapacity >= 8);\r
+               }\r
+\r
+               ~xml_buffered_writer()\r
+               {\r
+                       flush();\r
+               }\r
+\r
+               void flush()\r
+               {\r
+                       flush(buffer, bufsize);\r
+                       bufsize = 0;\r
+               }\r
+\r
+               void flush(const char_t* data, size_t size)\r
+               {\r
+                       if (size == 0) return;\r
+\r
+                       // fast path, just write data\r
+                       if (encoding == get_write_native_encoding())\r
+                               writer.write(data, size * sizeof(char_t));\r
+                       else\r
+                       {\r
+                               // convert chunk\r
+                               size_t result = convert_buffer_output(scratch.data_char, scratch.data_u8, scratch.data_u16, scratch.data_u32, data, size, encoding);\r
+                               assert(result <= sizeof(scratch));\r
+\r
+                               // write data\r
+                               writer.write(scratch.data_u8, result);\r
+                       }\r
+               }\r
+\r
+               void write(const char_t* data, size_t length)\r
+               {\r
+                       if (bufsize + length > bufcapacity)\r
+                       {\r
+                               // flush the remaining buffer contents\r
+                               flush();\r
+\r
+                               // handle large chunks\r
+                               if (length > bufcapacity)\r
+                               {\r
+                                       if (encoding == get_write_native_encoding())\r
+                                       {\r
+                                               // fast path, can just write data chunk\r
+                                               writer.write(data, length * sizeof(char_t));\r
+                                               return;\r
+                                       }\r
+\r
+                                       // need to convert in suitable chunks\r
+                                       while (length > bufcapacity)\r
+                                       {\r
+                                               // get chunk size by selecting such number of characters that are guaranteed to fit into scratch buffer\r
+                                               // and form a complete codepoint sequence (i.e. discard start of last codepoint if necessary)\r
+                                               size_t chunk_size = get_valid_length(data, bufcapacity);\r
+\r
+                                               // convert chunk and write\r
+                                               flush(data, chunk_size);\r
+\r
+                                               // iterate\r
+                                               data += chunk_size;\r
+                                               length -= chunk_size;\r
+                                       }\r
+\r
+                                       // small tail is copied below\r
+                                       bufsize = 0;\r
+                               }\r
+                       }\r
+\r
+                       memcpy(buffer + bufsize, data, length * sizeof(char_t));\r
+                       bufsize += length;\r
+               }\r
+\r
+               void write(const char_t* data)\r
+               {\r
+                       write(data, strlength(data));\r
+               }\r
+\r
+               void write(char_t d0)\r
+               {\r
+                       if (bufsize + 1 > bufcapacity) flush();\r
+\r
+                       buffer[bufsize + 0] = d0;\r
+                       bufsize += 1;\r
+               }\r
+\r
+               void write(char_t d0, char_t d1)\r
+               {\r
+                       if (bufsize + 2 > bufcapacity) flush();\r
+\r
+                       buffer[bufsize + 0] = d0;\r
+                       buffer[bufsize + 1] = d1;\r
+                       bufsize += 2;\r
+               }\r
+\r
+               void write(char_t d0, char_t d1, char_t d2)\r
+               {\r
+                       if (bufsize + 3 > bufcapacity) flush();\r
+\r
+                       buffer[bufsize + 0] = d0;\r
+                       buffer[bufsize + 1] = d1;\r
+                       buffer[bufsize + 2] = d2;\r
+                       bufsize += 3;\r
+               }\r
+\r
+               void write(char_t d0, char_t d1, char_t d2, char_t d3)\r
+               {\r
+                       if (bufsize + 4 > bufcapacity) flush();\r
+\r
+                       buffer[bufsize + 0] = d0;\r
+                       buffer[bufsize + 1] = d1;\r
+                       buffer[bufsize + 2] = d2;\r
+                       buffer[bufsize + 3] = d3;\r
+                       bufsize += 4;\r
+               }\r
+\r
+               void write(char_t d0, char_t d1, char_t d2, char_t d3, char_t d4)\r
+               {\r
+                       if (bufsize + 5 > bufcapacity) flush();\r
+\r
+                       buffer[bufsize + 0] = d0;\r
+                       buffer[bufsize + 1] = d1;\r
+                       buffer[bufsize + 2] = d2;\r
+                       buffer[bufsize + 3] = d3;\r
+                       buffer[bufsize + 4] = d4;\r
+                       bufsize += 5;\r
+               }\r
+\r
+               void write(char_t d0, char_t d1, char_t d2, char_t d3, char_t d4, char_t d5)\r
+               {\r
+                       if (bufsize + 6 > bufcapacity) flush();\r
+\r
+                       buffer[bufsize + 0] = d0;\r
+                       buffer[bufsize + 1] = d1;\r
+                       buffer[bufsize + 2] = d2;\r
+                       buffer[bufsize + 3] = d3;\r
+                       buffer[bufsize + 4] = d4;\r
+                       buffer[bufsize + 5] = d5;\r
+                       bufsize += 6;\r
+               }\r
+\r
+               // utf8 maximum expansion: x4 (-> utf32)\r
+               // utf16 maximum expansion: x2 (-> utf32)\r
+               // utf32 maximum expansion: x1\r
+               enum\r
+               {\r
+                       bufcapacitybytes =\r
+                       #ifdef PUGIXML_MEMORY_OUTPUT_STACK\r
+                               PUGIXML_MEMORY_OUTPUT_STACK\r
+                       #else\r
+                               10240\r
+                       #endif\r
+                       ,\r
+                       bufcapacity = bufcapacitybytes / (sizeof(char_t) + 4)\r
+               };\r
+\r
+               char_t buffer[bufcapacity];\r
+\r
+               union\r
+               {\r
+                       uint8_t data_u8[4 * bufcapacity];\r
+                       uint16_t data_u16[2 * bufcapacity];\r
+                       uint32_t data_u32[bufcapacity];\r
+                       char_t data_char[bufcapacity];\r
+               } scratch;\r
+\r
+               xml_writer& writer;\r
+               size_t bufsize;\r
+               xml_encoding encoding;\r
+       };\r
+\r
+       PUGI__FN void text_output_escaped(xml_buffered_writer& writer, const char_t* s, chartypex_t type)\r
+       {\r
+               while (*s)\r
+               {\r
+                       const char_t* prev = s;\r
+                       \r
+                       // While *s is a usual symbol\r
+                       while (!PUGI__IS_CHARTYPEX(*s, type)) ++s;\r
+               \r
+                       writer.write(prev, static_cast<size_t>(s - prev));\r
+\r
+                       switch (*s)\r
+                       {\r
+                               case 0: break;\r
+                               case '&':\r
+                                       writer.write('&', 'a', 'm', 'p', ';');\r
+                                       ++s;\r
+                                       break;\r
+                               case '<':\r
+                                       writer.write('&', 'l', 't', ';');\r
+                                       ++s;\r
+                                       break;\r
+                               case '>':\r
+                                       writer.write('&', 'g', 't', ';');\r
+                                       ++s;\r
+                                       break;\r
+                               case '"':\r
+                                       writer.write('&', 'q', 'u', 'o', 't', ';');\r
+                                       ++s;\r
+                                       break;\r
+                               default: // s is not a usual symbol\r
+                               {\r
+                                       unsigned int ch = static_cast<unsigned int>(*s++);\r
+                                       assert(ch < 32);\r
+\r
+                                       writer.write('&', '#', static_cast<char_t>((ch / 10) + '0'), static_cast<char_t>((ch % 10) + '0'), ';');\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       PUGI__FN void text_output(xml_buffered_writer& writer, const char_t* s, chartypex_t type, unsigned int flags)\r
+       {\r
+               if (flags & format_no_escapes)\r
+                       writer.write(s);\r
+               else\r
+                       text_output_escaped(writer, s, type);\r
+       }\r
+\r
+       PUGI__FN void text_output_cdata(xml_buffered_writer& writer, const char_t* s)\r
+       {\r
+               do\r
+               {\r
+                       writer.write('<', '!', '[', 'C', 'D');\r
+                       writer.write('A', 'T', 'A', '[');\r
+\r
+                       const char_t* prev = s;\r
+\r
+                       // look for ]]> sequence - we can't output it as is since it terminates CDATA\r
+                       while (*s && !(s[0] == ']' && s[1] == ']' && s[2] == '>')) ++s;\r
+\r
+                       // skip ]] if we stopped at ]]>, > will go to the next CDATA section\r
+                       if (*s) s += 2;\r
+\r
+                       writer.write(prev, static_cast<size_t>(s - prev));\r
+\r
+                       writer.write(']', ']', '>');\r
+               }\r
+               while (*s);\r
+       }\r
+\r
+       PUGI__FN void node_output_attributes(xml_buffered_writer& writer, const xml_node& node, unsigned int flags)\r
+       {\r
+               const char_t* default_name = PUGIXML_TEXT(":anonymous");\r
+\r
+               for (xml_attribute a = node.first_attribute(); a; a = a.next_attribute())\r
+               {\r
+                       writer.write(' ');\r
+                       writer.write(a.name()[0] ? a.name() : default_name);\r
+                       writer.write('=', '"');\r
+\r
+                       text_output(writer, a.value(), ctx_special_attr, flags);\r
+\r
+                       writer.write('"');\r
+               }\r
+       }\r
+\r
+       PUGI__FN void node_output(xml_buffered_writer& writer, const xml_node& node, const char_t* indent, unsigned int flags, unsigned int depth)\r
+       {\r
+               const char_t* default_name = PUGIXML_TEXT(":anonymous");\r
+\r
+               if ((flags & format_indent) != 0 && (flags & format_raw) == 0)\r
+                       for (unsigned int i = 0; i < depth; ++i) writer.write(indent);\r
+\r
+               switch (node.type())\r
+               {\r
+               case node_document:\r
+               {\r
+                       for (xml_node n = node.first_child(); n; n = n.next_sibling())\r
+                               node_output(writer, n, indent, flags, depth);\r
+                       break;\r
+               }\r
+                       \r
+               case node_element:\r
+               {\r
+                       const char_t* name = node.name()[0] ? node.name() : default_name;\r
+\r
+                       writer.write('<');\r
+                       writer.write(name);\r
+\r
+                       node_output_attributes(writer, node, flags);\r
+\r
+                       if (flags & format_raw)\r
+                       {\r
+                               if (!node.first_child())\r
+                                       writer.write(' ', '/', '>');\r
+                               else\r
+                               {\r
+                                       writer.write('>');\r
+\r
+                                       for (xml_node n = node.first_child(); n; n = n.next_sibling())\r
+                                               node_output(writer, n, indent, flags, depth + 1);\r
+\r
+                                       writer.write('<', '/');\r
+                                       writer.write(name);\r
+                                       writer.write('>');\r
+                               }\r
+                       }\r
+                       else if (!node.first_child())\r
+                               writer.write(' ', '/', '>', '\n');\r
+                       else if (node.first_child() == node.last_child() && (node.first_child().type() == node_pcdata || node.first_child().type() == node_cdata))\r
+                       {\r
+                               writer.write('>');\r
+\r
+                               if (node.first_child().type() == node_pcdata)\r
+                                       text_output(writer, node.first_child().value(), ctx_special_pcdata, flags);\r
+                               else\r
+                                       text_output_cdata(writer, node.first_child().value());\r
+\r
+                               writer.write('<', '/');\r
+                               writer.write(name);\r
+                               writer.write('>', '\n');\r
+                       }\r
+                       else\r
+                       {\r
+                               writer.write('>', '\n');\r
+                               \r
+                               for (xml_node n = node.first_child(); n; n = n.next_sibling())\r
+                                       node_output(writer, n, indent, flags, depth + 1);\r
+\r
+                               if ((flags & format_indent) != 0 && (flags & format_raw) == 0)\r
+                                       for (unsigned int i = 0; i < depth; ++i) writer.write(indent);\r
+                               \r
+                               writer.write('<', '/');\r
+                               writer.write(name);\r
+                               writer.write('>', '\n');\r
+                       }\r
+\r
+                       break;\r
+               }\r
+               \r
+               case node_pcdata:\r
+                       text_output(writer, node.value(), ctx_special_pcdata, flags);\r
+                       if ((flags & format_raw) == 0) writer.write('\n');\r
+                       break;\r
+\r
+               case node_cdata:\r
+                       text_output_cdata(writer, node.value());\r
+                       if ((flags & format_raw) == 0) writer.write('\n');\r
+                       break;\r
+\r
+               case node_comment:\r
+                       writer.write('<', '!', '-', '-');\r
+                       writer.write(node.value());\r
+                       writer.write('-', '-', '>');\r
+                       if ((flags & format_raw) == 0) writer.write('\n');\r
+                       break;\r
+\r
+               case node_pi:\r
+               case node_declaration:\r
+                       writer.write('<', '?');\r
+                       writer.write(node.name()[0] ? node.name() : default_name);\r
+\r
+                       if (node.type() == node_declaration)\r
+                       {\r
+                               node_output_attributes(writer, node, flags);\r
+                       }\r
+                       else if (node.value()[0])\r
+                       {\r
+                               writer.write(' ');\r
+                               writer.write(node.value());\r
+                       }\r
+\r
+                       writer.write('?', '>');\r
+                       if ((flags & format_raw) == 0) writer.write('\n');\r
+                       break;\r
+\r
+               case node_doctype:\r
+                       writer.write('<', '!', 'D', 'O', 'C');\r
+                       writer.write('T', 'Y', 'P', 'E');\r
+\r
+                       if (node.value()[0])\r
+                       {\r
+                               writer.write(' ');\r
+                               writer.write(node.value());\r
+                       }\r
+\r
+                       writer.write('>');\r
+                       if ((flags & format_raw) == 0) writer.write('\n');\r
+                       break;\r
+\r
+               default:\r
+                       assert(!"Invalid node type");\r
+               }\r
+       }\r
+\r
+       inline bool has_declaration(const xml_node& node)\r
+       {\r
+               for (xml_node child = node.first_child(); child; child = child.next_sibling())\r
+               {\r
+                       xml_node_type type = child.type();\r
+\r
+                       if (type == node_declaration) return true;\r
+                       if (type == node_element) return false;\r
+               }\r
+\r
+               return false;\r
+       }\r
+\r
+       inline bool allow_insert_child(xml_node_type parent, xml_node_type child)\r
+       {\r
+               if (parent != node_document && parent != node_element) return false;\r
+               if (child == node_document || child == node_null) return false;\r
+               if (parent != node_document && (child == node_declaration || child == node_doctype)) return false;\r
+\r
+               return true;\r
+       }\r
+\r
+       PUGI__FN void recursive_copy_skip(xml_node& dest, const xml_node& source, const xml_node& skip)\r
+       {\r
+               assert(dest.type() == source.type());\r
+\r
+               switch (source.type())\r
+               {\r
+               case node_element:\r
+               {\r
+                       dest.set_name(source.name());\r
+\r
+                       for (xml_attribute a = source.first_attribute(); a; a = a.next_attribute())\r
+                               dest.append_attribute(a.name()).set_value(a.value());\r
+\r
+                       for (xml_node c = source.first_child(); c; c = c.next_sibling())\r
+                       {\r
+                               if (c == skip) continue;\r
+\r
+                               xml_node cc = dest.append_child(c.type());\r
+                               assert(cc);\r
+\r
+                               recursive_copy_skip(cc, c, skip);\r
+                       }\r
+\r
+                       break;\r
+               }\r
+\r
+               case node_pcdata:\r
+               case node_cdata:\r
+               case node_comment:\r
+               case node_doctype:\r
+                       dest.set_value(source.value());\r
+                       break;\r
+\r
+               case node_pi:\r
+                       dest.set_name(source.name());\r
+                       dest.set_value(source.value());\r
+                       break;\r
+\r
+               case node_declaration:\r
+               {\r
+                       dest.set_name(source.name());\r
+\r
+                       for (xml_attribute a = source.first_attribute(); a; a = a.next_attribute())\r
+                               dest.append_attribute(a.name()).set_value(a.value());\r
+\r
+                       break;\r
+               }\r
+\r
+               default:\r
+                       assert(!"Invalid node type");\r
+               }\r
+       }\r
+\r
+       inline bool is_text_node(xml_node_struct* node)\r
+       {\r
+               xml_node_type type = static_cast<xml_node_type>((node->header & impl::xml_memory_page_type_mask) + 1);\r
+\r
+               return type == node_pcdata || type == node_cdata;\r
+       }\r
+\r
+       // get value with conversion functions\r
+       PUGI__FN int get_integer_base(const char_t* value)\r
+       {\r
+               const char_t* s = value;\r
+\r
+               while (PUGI__IS_CHARTYPE(*s, ct_space))\r
+                       s++;\r
+\r
+               if (*s == '-')\r
+                       s++;\r
+\r
+               return (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10;\r
+       }\r
+\r
+       PUGI__FN int get_value_int(const char_t* value, int def)\r
+       {\r
+               if (!value) return def;\r
+\r
+               int base = get_integer_base(value);\r
+\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               return static_cast<int>(wcstol(value, 0, base));\r
+       #else\r
+               return static_cast<int>(strtol(value, 0, base));\r
+       #endif\r
+       }\r
+\r
+       PUGI__FN unsigned int get_value_uint(const char_t* value, unsigned int def)\r
+       {\r
+               if (!value) return def;\r
+\r
+               int base = get_integer_base(value);\r
+\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               return static_cast<unsigned int>(wcstoul(value, 0, base));\r
+       #else\r
+               return static_cast<unsigned int>(strtoul(value, 0, base));\r
+       #endif\r
+       }\r
+\r
+       PUGI__FN double get_value_double(const char_t* value, double def)\r
+       {\r
+               if (!value) return def;\r
+\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               return wcstod(value, 0);\r
+       #else\r
+               return strtod(value, 0);\r
+       #endif\r
+       }\r
+\r
+       PUGI__FN float get_value_float(const char_t* value, float def)\r
+       {\r
+               if (!value) return def;\r
+\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               return static_cast<float>(wcstod(value, 0));\r
+       #else\r
+               return static_cast<float>(strtod(value, 0));\r
+       #endif\r
+       }\r
+\r
+       PUGI__FN bool get_value_bool(const char_t* value, bool def)\r
+       {\r
+               if (!value) return def;\r
+\r
+               // only look at first char\r
+               char_t first = *value;\r
+\r
+               // 1*, t* (true), T* (True), y* (yes), Y* (YES)\r
+               return (first == '1' || first == 't' || first == 'T' || first == 'y' || first == 'Y');\r
+       }\r
+\r
+#ifdef PUGIXML_HAS_LONG_LONG\r
+       PUGI__FN long long get_value_llong(const char_t* value, long long def)\r
+       {\r
+               if (!value) return def;\r
+\r
+               int base = get_integer_base(value);\r
+\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               #ifdef PUGI__MSVC_CRT_VERSION\r
+                       return _wcstoi64(value, 0, base);\r
+               #else\r
+                       return wcstoll(value, 0, base);\r
+               #endif\r
+       #else\r
+               #ifdef PUGI__MSVC_CRT_VERSION\r
+                       return _strtoi64(value, 0, base);\r
+               #else\r
+                       return strtoll(value, 0, base);\r
+               #endif\r
+       #endif\r
+       }\r
+\r
+       PUGI__FN unsigned long long get_value_ullong(const char_t* value, unsigned long long def)\r
+       {\r
+               if (!value) return def;\r
+\r
+               int base = get_integer_base(value);\r
+\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               #ifdef PUGI__MSVC_CRT_VERSION\r
+                       return _wcstoui64(value, 0, base);\r
+               #else\r
+                       return wcstoull(value, 0, base);\r
+               #endif\r
+       #else\r
+               #ifdef PUGI__MSVC_CRT_VERSION\r
+                       return _strtoui64(value, 0, base);\r
+               #else\r
+                       return strtoull(value, 0, base);\r
+               #endif\r
+       #endif\r
+       }\r
+#endif\r
+\r
+       // set value with conversion functions\r
+       PUGI__FN bool set_value_buffer(char_t*& dest, uintptr_t& header, uintptr_t header_mask, char (&buf)[128])\r
+       {\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               char_t wbuf[128];\r
+               impl::widen_ascii(wbuf, buf);\r
+\r
+               return strcpy_insitu(dest, header, header_mask, wbuf);\r
+       #else\r
+               return strcpy_insitu(dest, header, header_mask, buf);\r
+       #endif\r
+       }\r
+\r
+       PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, int value)\r
+       {\r
+               char buf[128];\r
+               sprintf(buf, "%d", value);\r
+       \r
+               return set_value_buffer(dest, header, header_mask, buf);\r
+       }\r
+\r
+       PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, unsigned int value)\r
+       {\r
+               char buf[128];\r
+               sprintf(buf, "%u", value);\r
+\r
+               return set_value_buffer(dest, header, header_mask, buf);\r
+       }\r
+\r
+       PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, double value)\r
+       {\r
+               char buf[128];\r
+               sprintf(buf, "%g", value);\r
+\r
+               return set_value_buffer(dest, header, header_mask, buf);\r
+       }\r
+       \r
+       PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, bool value)\r
+       {\r
+               return strcpy_insitu(dest, header, header_mask, value ? PUGIXML_TEXT("true") : PUGIXML_TEXT("false"));\r
+       }\r
+\r
+#ifdef PUGIXML_HAS_LONG_LONG\r
+       PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, long long value)\r
+       {\r
+               char buf[128];\r
+               sprintf(buf, "%lld", value);\r
+       \r
+               return set_value_buffer(dest, header, header_mask, buf);\r
+       }\r
+\r
+       PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, unsigned long long value)\r
+       {\r
+               char buf[128];\r
+               sprintf(buf, "%llu", value);\r
+       \r
+               return set_value_buffer(dest, header, header_mask, buf);\r
+       }\r
+#endif\r
+\r
+       // we need to get length of entire file to load it in memory; the only (relatively) sane way to do it is via seek/tell trick\r
+       PUGI__FN xml_parse_status get_file_size(FILE* file, size_t& out_result)\r
+       {\r
+       #if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 && !defined(_WIN32_WCE)\r
+               // there are 64-bit versions of fseek/ftell, let's use them\r
+               typedef __int64 length_type;\r
+\r
+               _fseeki64(file, 0, SEEK_END);\r
+               length_type length = _ftelli64(file);\r
+               _fseeki64(file, 0, SEEK_SET);\r
+       #elif defined(__MINGW32__) && !defined(__NO_MINGW_LFS) && !defined(__STRICT_ANSI__)\r
+               // there are 64-bit versions of fseek/ftell, let's use them\r
+               typedef off64_t length_type;\r
+\r
+               fseeko64(file, 0, SEEK_END);\r
+               length_type length = ftello64(file);\r
+               fseeko64(file, 0, SEEK_SET);\r
+       #else\r
+               // if this is a 32-bit OS, long is enough; if this is a unix system, long is 64-bit, which is enough; otherwise we can't do anything anyway.\r
+               typedef long length_type;\r
+\r
+               fseek(file, 0, SEEK_END);\r
+               length_type length = ftell(file);\r
+               fseek(file, 0, SEEK_SET);\r
+       #endif\r
+\r
+               // check for I/O errors\r
+               if (length < 0) return status_io_error;\r
+               \r
+               // check for overflow\r
+               size_t result = static_cast<size_t>(length);\r
+\r
+               if (static_cast<length_type>(result) != length) return status_out_of_memory;\r
+\r
+               // finalize\r
+               out_result = result;\r
+\r
+               return status_ok;\r
+       }\r
+\r
+       PUGI__FN size_t zero_terminate_buffer(void* buffer, size_t size, xml_encoding encoding) \r
+       {\r
+               // We only need to zero-terminate if encoding conversion does not do it for us\r
+       #ifdef PUGIXML_WCHAR_MODE\r
+               xml_encoding wchar_encoding = get_wchar_encoding();\r
+\r
+               if (encoding == wchar_encoding || need_endian_swap_utf(encoding, wchar_encoding))\r
+               {\r
+                       size_t length = size / sizeof(char_t);\r
+\r
+                       static_cast<char_t*>(buffer)[length] = 0;\r
+                       return (length + 1) * sizeof(char_t);\r
+               }\r
+       #else\r
+               if (encoding == encoding_utf8)\r
+               {\r
+                       static_cast<char*>(buffer)[size] = 0;\r
+                       return size + 1;\r
+               }\r
+       #endif\r
+\r
+               return size;\r
+       }\r
+\r
+       PUGI__FN xml_parse_result load_file_impl(xml_document& doc, FILE* file, unsigned int options, xml_encoding encoding)\r
+       {\r
+               if (!file) return make_parse_result(status_file_not_found);\r
+\r
+               // get file size (can result in I/O errors)\r
+               size_t size = 0;\r
+               xml_parse_status size_status = get_file_size(file, size);\r
+\r
+               if (size_status != status_ok)\r
+               {\r
+                       fclose(file);\r
+                       return make_parse_result(size_status);\r
+               }\r
+               \r
+               size_t max_suffix_size = sizeof(char_t);\r
+\r
+               // allocate buffer for the whole file\r
+               char* contents = static_cast<char*>(xml_memory::allocate(size + max_suffix_size));\r
+\r
+               if (!contents)\r
+               {\r
+                       fclose(file);\r
+                       return make_parse_result(status_out_of_memory);\r
+               }\r
+\r
+               // read file in memory\r
+               size_t read_size = fread(contents, 1, size, file);\r
+               fclose(file);\r
+\r
+               if (read_size != size)\r
+               {\r
+                       xml_memory::deallocate(contents);\r
+                       return make_parse_result(status_io_error);\r
+               }\r
+\r
+               xml_encoding real_encoding = get_buffer_encoding(encoding, contents, size);\r
+               \r
+               return doc.load_buffer_inplace_own(contents, zero_terminate_buffer(contents, size, real_encoding), options, real_encoding);\r
+       }\r
+\r
+#ifndef PUGIXML_NO_STL\r
+       template <typename T> struct xml_stream_chunk\r
+       {\r
+               static xml_stream_chunk* create()\r
+               {\r
+                       void* memory = xml_memory::allocate(sizeof(xml_stream_chunk));\r
+                       \r
+                       return new (memory) xml_stream_chunk();\r
+               }\r
+\r
+               static void destroy(void* ptr)\r
+               {\r
+                       xml_stream_chunk* chunk = static_cast<xml_stream_chunk*>(ptr);\r
+\r
+                       // free chunk chain\r
+                       while (chunk)\r
+                       {\r
+                               xml_stream_chunk* next = chunk->next;\r
+                               xml_memory::deallocate(chunk);\r
+                               chunk = next;\r
+                       }\r
+               }\r
+\r
+               xml_stream_chunk(): next(0), size(0)\r
+               {\r
+               }\r
+\r
+               xml_stream_chunk* next;\r
+               size_t size;\r
+\r
+               T data[xml_memory_page_size / sizeof(T)];\r
+       };\r
+\r
+       template <typename T> PUGI__FN xml_parse_status load_stream_data_noseek(std::basic_istream<T>& stream, void** out_buffer, size_t* out_size)\r
+       {\r
+               buffer_holder chunks(0, xml_stream_chunk<T>::destroy);\r
+\r
+               // read file to a chunk list\r
+               size_t total = 0;\r
+               xml_stream_chunk<T>* last = 0;\r
+\r
+               while (!stream.eof())\r
+               {\r
+                       // allocate new chunk\r
+                       xml_stream_chunk<T>* chunk = xml_stream_chunk<T>::create();\r
+                       if (!chunk) return status_out_of_memory;\r
+\r
+                       // append chunk to list\r
+                       if (last) last = last->next = chunk;\r
+                       else chunks.data = last = chunk;\r
+\r
+                       // read data to chunk\r
+                       stream.read(chunk->data, static_cast<std::streamsize>(sizeof(chunk->data) / sizeof(T)));\r
+                       chunk->size = static_cast<size_t>(stream.gcount()) * sizeof(T);\r
+\r
+                       // read may set failbit | eofbit in case gcount() is less than read length, so check for other I/O errors\r
+                       if (stream.bad() || (!stream.eof() && stream.fail())) return status_io_error;\r
+\r
+                       // guard against huge files (chunk size is small enough to make this overflow check work)\r
+                       if (total + chunk->size < total) return status_out_of_memory;\r
+                       total += chunk->size;\r
+               }\r
+\r
+               size_t max_suffix_size = sizeof(char_t);\r
+\r
+               // copy chunk list to a contiguous buffer\r
+               char* buffer = static_cast<char*>(xml_memory::allocate(total + max_suffix_size));\r
+               if (!buffer) return status_out_of_memory;\r
+\r
+               char* write = buffer;\r
+\r
+               for (xml_stream_chunk<T>* chunk = static_cast<xml_stream_chunk<T>*>(chunks.data); chunk; chunk = chunk->next)\r
+               {\r
+                       assert(write + chunk->size <= buffer + total);\r
+                       memcpy(write, chunk->data, chunk->size);\r
+                       write += chunk->size;\r
+               }\r
+\r
+               assert(write == buffer + total);\r
+\r
+               // return buffer\r
+               *out_buffer = buffer;\r
+               *out_size = total;\r
+\r
+               return status_ok;\r
+       }\r
+\r
+       template <typename T> PUGI__FN xml_parse_status load_stream_data_seek(std::basic_istream<T>& stream, void** out_buffer, size_t* out_size)\r
+       {\r
+               // get length of remaining data in stream\r
+               typename std::basic_istream<T>::pos_type pos = stream.tellg();\r
+               stream.seekg(0, std::ios::end);\r
+               std::streamoff length = stream.tellg() - pos;\r
+               stream.seekg(pos);\r
+\r
+               if (stream.fail() || pos < 0) return status_io_error;\r
+\r
+               // guard against huge files\r
+               size_t read_length = static_cast<size_t>(length);\r
+\r
+               if (static_cast<std::streamsize>(read_length) != length || length < 0) return status_out_of_memory;\r
+\r
+               size_t max_suffix_size = sizeof(char_t);\r
+\r
+               // read stream data into memory (guard against stream exceptions with buffer holder)\r
+               buffer_holder buffer(xml_memory::allocate(read_length * sizeof(T) + max_suffix_size), xml_memory::deallocate);\r
+               if (!buffer.data) return status_out_of_memory;\r
+\r
+               stream.read(static_cast<T*>(buffer.data), static_cast<std::streamsize>(read_length));\r
+\r
+               // read may set failbit | eofbit in case gcount() is less than read_length (i.e. line ending conversion), so check for other I/O errors\r
+               if (stream.bad() || (!stream.eof() && stream.fail())) return status_io_error;\r
+\r
+               // return buffer\r
+               size_t actual_length = static_cast<size_t>(stream.gcount());\r
+               assert(actual_length <= read_length);\r
+               \r
+               *out_buffer = buffer.release();\r
+               *out_size = actual_length * sizeof(T);\r
+\r
+               return status_ok;\r
+       }\r
+\r
+       template <typename T> PUGI__FN xml_parse_result load_stream_impl(xml_document& doc, std::basic_istream<T>& stream, unsigned int options, xml_encoding encoding)\r
+       {\r
+               void* buffer = 0;\r
+               size_t size = 0;\r
+               xml_parse_status status = status_ok;\r
+\r
+               // if stream has an error bit set, bail out (otherwise tellg() can fail and we'll clear error bits)\r
+               if (stream.fail()) return make_parse_result(status_io_error);\r
+\r
+               // load stream to memory (using seek-based implementation if possible, since it's faster and takes less memory)\r
+               if (stream.tellg() < 0)\r
+               {\r
+                       stream.clear(); // clear error flags that could be set by a failing tellg\r
+                       status = load_stream_data_noseek(stream, &buffer, &size);\r
+               }\r
+               else\r
+                       status = load_stream_data_seek(stream, &buffer, &size);\r
+\r
+               if (status != status_ok) return make_parse_result(status);\r
+\r
+               xml_encoding real_encoding = get_buffer_encoding(encoding, buffer, size);\r
+               \r
+               return doc.load_buffer_inplace_own(buffer, zero_terminate_buffer(buffer, size, real_encoding), options, real_encoding);\r
+       }\r
+#endif\r
+\r
+#if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__) || (defined(__MINGW32__) && !defined(__STRICT_ANSI__))\r
+       PUGI__FN FILE* open_file_wide(const wchar_t* path, const wchar_t* mode)\r
+       {\r
+               return _wfopen(path, mode);\r
+       }\r
+#else\r
+       PUGI__FN char* convert_path_heap(const wchar_t* str)\r
+       {\r
+               assert(str);\r
+\r
+               // first pass: get length in utf8 characters\r
+               size_t length = strlength_wide(str);\r
+               size_t size = as_utf8_begin(str, length);\r
+\r
+               // allocate resulting string\r
+               char* result = static_cast<char*>(xml_memory::allocate(size + 1));\r
+               if (!result) return 0;\r
+\r
+               // second pass: convert to utf8\r
+               as_utf8_end(result, size, str, length);\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN FILE* open_file_wide(const wchar_t* path, const wchar_t* mode)\r
+       {\r
+               // there is no standard function to open wide paths, so our best bet is to try utf8 path\r
+               char* path_utf8 = convert_path_heap(path);\r
+               if (!path_utf8) return 0;\r
+\r
+               // convert mode to ASCII (we mirror _wfopen interface)\r
+               char mode_ascii[4] = {0};\r
+               for (size_t i = 0; mode[i]; ++i) mode_ascii[i] = static_cast<char>(mode[i]);\r
+\r
+               // try to open the utf8 path\r
+               FILE* result = fopen(path_utf8, mode_ascii);\r
+\r
+               // free dummy buffer\r
+               xml_memory::deallocate(path_utf8);\r
+\r
+               return result;\r
+       }\r
+#endif\r
+\r
+       PUGI__FN bool save_file_impl(const xml_document& doc, FILE* file, const char_t* indent, unsigned int flags, xml_encoding encoding)\r
+       {\r
+               if (!file) return false;\r
+\r
+               xml_writer_file writer(file);\r
+               doc.save(writer, indent, flags, encoding);\r
+\r
+               int result = ferror(file);\r
+\r
+               fclose(file);\r
+\r
+               return result == 0;\r
+       }\r
+\r
+       PUGI__FN xml_parse_result load_buffer_impl(xml_document_struct* doc, xml_node_struct* root, void* contents, size_t size, unsigned int options, xml_encoding encoding, bool is_mutable, bool own, char_t** out_buffer)\r
+       {\r
+               // check input buffer\r
+               assert(contents || size == 0);\r
+\r
+               // get actual encoding\r
+               xml_encoding buffer_encoding = impl::get_buffer_encoding(encoding, contents, size);\r
+\r
+               // get private buffer\r
+               char_t* buffer = 0;\r
+               size_t length = 0;\r
+\r
+               if (!impl::convert_buffer(buffer, length, buffer_encoding, contents, size, is_mutable)) return impl::make_parse_result(status_out_of_memory);\r
+               \r
+               // delete original buffer if we performed a conversion\r
+               if (own && buffer != contents && contents) impl::xml_memory::deallocate(contents);\r
+\r
+               // store buffer for offset_debug\r
+               doc->buffer = buffer;\r
+\r
+               // parse\r
+               xml_parse_result res = impl::xml_parser::parse(buffer, length, doc, root, options);\r
+\r
+               // remember encoding\r
+               res.encoding = buffer_encoding;\r
+\r
+               // grab onto buffer if it's our buffer, user is responsible for deallocating contents himself\r
+               if (own || buffer != contents) *out_buffer = buffer;\r
+\r
+               return res;\r
+       }\r
+PUGI__NS_END\r
+\r
+namespace pugi\r
+{\r
+       PUGI__FN xml_writer_file::xml_writer_file(void* file_): file(file_)\r
+       {\r
+       }\r
+\r
+       PUGI__FN void xml_writer_file::write(const void* data, size_t size)\r
+       {\r
+               size_t result = fwrite(data, 1, size, static_cast<FILE*>(file));\r
+               (void)!result; // unfortunately we can't do proper error handling here\r
+       }\r
+\r
+#ifndef PUGIXML_NO_STL\r
+       PUGI__FN xml_writer_stream::xml_writer_stream(std::basic_ostream<char, std::char_traits<char> >& stream): narrow_stream(&stream), wide_stream(0)\r
+       {\r
+       }\r
+\r
+       PUGI__FN xml_writer_stream::xml_writer_stream(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream): narrow_stream(0), wide_stream(&stream)\r
+       {\r
+       }\r
+\r
+       PUGI__FN void xml_writer_stream::write(const void* data, size_t size)\r
+       {\r
+               if (narrow_stream)\r
+               {\r
+                       assert(!wide_stream);\r
+                       narrow_stream->write(reinterpret_cast<const char*>(data), static_cast<std::streamsize>(size));\r
+               }\r
+               else\r
+               {\r
+                       assert(wide_stream);\r
+                       assert(size % sizeof(wchar_t) == 0);\r
+\r
+                       wide_stream->write(reinterpret_cast<const wchar_t*>(data), static_cast<std::streamsize>(size / sizeof(wchar_t)));\r
+               }\r
+       }\r
+#endif\r
+\r
+       PUGI__FN xml_tree_walker::xml_tree_walker(): _depth(0)\r
+       {\r
+       }\r
+       \r
+       PUGI__FN xml_tree_walker::~xml_tree_walker()\r
+       {\r
+       }\r
+\r
+       PUGI__FN int xml_tree_walker::depth() const\r
+       {\r
+               return _depth;\r
+       }\r
+\r
+       PUGI__FN bool xml_tree_walker::begin(xml_node&)\r
+       {\r
+               return true;\r
+       }\r
+\r
+       PUGI__FN bool xml_tree_walker::end(xml_node&)\r
+       {\r
+               return true;\r
+       }\r
+\r
+       PUGI__FN xml_attribute::xml_attribute(): _attr(0)\r
+       {\r
+       }\r
+\r
+       PUGI__FN xml_attribute::xml_attribute(xml_attribute_struct* attr): _attr(attr)\r
+       {\r
+       }\r
+\r
+       PUGI__FN static void unspecified_bool_xml_attribute(xml_attribute***)\r
+       {\r
+       }\r
+\r
+       PUGI__FN xml_attribute::operator xml_attribute::unspecified_bool_type() const\r
+       {\r
+               return _attr ? unspecified_bool_xml_attribute : 0;\r
+       }\r
+\r
+       PUGI__FN bool xml_attribute::operator!() const\r
+       {\r
+               return !_attr;\r
+       }\r
+\r
+       PUGI__FN bool xml_attribute::operator==(const xml_attribute& r) const\r
+       {\r
+               return (_attr == r._attr);\r
+       }\r
+       \r
+       PUGI__FN bool xml_attribute::operator!=(const xml_attribute& r) const\r
+       {\r
+               return (_attr != r._attr);\r
+       }\r
+\r
+       PUGI__FN bool xml_attribute::operator<(const xml_attribute& r) const\r
+       {\r
+               return (_attr < r._attr);\r
+       }\r
+       \r
+       PUGI__FN bool xml_attribute::operator>(const xml_attribute& r) const\r
+       {\r
+               return (_attr > r._attr);\r
+       }\r
+       \r
+       PUGI__FN bool xml_attribute::operator<=(const xml_attribute& r) const\r
+       {\r
+               return (_attr <= r._attr);\r
+       }\r
+       \r
+       PUGI__FN bool xml_attribute::operator>=(const xml_attribute& r) const\r
+       {\r
+               return (_attr >= r._attr);\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_attribute::next_attribute() const\r
+       {\r
+               return _attr ? xml_attribute(_attr->next_attribute) : xml_attribute();\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_attribute::previous_attribute() const\r
+       {\r
+               return _attr && _attr->prev_attribute_c->next_attribute ? xml_attribute(_attr->prev_attribute_c) : xml_attribute();\r
+       }\r
+\r
+       PUGI__FN const char_t* xml_attribute::as_string(const char_t* def) const\r
+       {\r
+               return (_attr && _attr->value) ? _attr->value : def;\r
+       }\r
+\r
+       PUGI__FN int xml_attribute::as_int(int def) const\r
+       {\r
+               return impl::get_value_int(_attr ? _attr->value : 0, def);\r
+       }\r
+\r
+       PUGI__FN unsigned int xml_attribute::as_uint(unsigned int def) const\r
+       {\r
+               return impl::get_value_uint(_attr ? _attr->value : 0, def);\r
+       }\r
+\r
+       PUGI__FN double xml_attribute::as_double(double def) const\r
+       {\r
+               return impl::get_value_double(_attr ? _attr->value : 0, def);\r
+       }\r
+\r
+       PUGI__FN float xml_attribute::as_float(float def) const\r
+       {\r
+               return impl::get_value_float(_attr ? _attr->value : 0, def);\r
+       }\r
+\r
+       PUGI__FN bool xml_attribute::as_bool(bool def) const\r
+       {\r
+               return impl::get_value_bool(_attr ? _attr->value : 0, def);\r
+       }\r
+\r
+#ifdef PUGIXML_HAS_LONG_LONG\r
+       PUGI__FN long long xml_attribute::as_llong(long long def) const\r
+       {\r
+               return impl::get_value_llong(_attr ? _attr->value : 0, def);\r
+       }\r
+\r
+       PUGI__FN unsigned long long xml_attribute::as_ullong(unsigned long long def) const\r
+       {\r
+               return impl::get_value_ullong(_attr ? _attr->value : 0, def);\r
+       }\r
+#endif\r
+\r
+       PUGI__FN bool xml_attribute::empty() const\r
+       {\r
+               return !_attr;\r
+       }\r
+\r
+       PUGI__FN const char_t* xml_attribute::name() const\r
+       {\r
+               return (_attr && _attr->name) ? _attr->name : PUGIXML_TEXT("");\r
+       }\r
+\r
+       PUGI__FN const char_t* xml_attribute::value() const\r
+       {\r
+               return (_attr && _attr->value) ? _attr->value : PUGIXML_TEXT("");\r
+       }\r
+\r
+       PUGI__FN size_t xml_attribute::hash_value() const\r
+       {\r
+               return static_cast<size_t>(reinterpret_cast<uintptr_t>(_attr) / sizeof(xml_attribute_struct));\r
+       }\r
+\r
+       PUGI__FN xml_attribute_struct* xml_attribute::internal_object() const\r
+       {\r
+               return _attr;\r
+       }\r
+\r
+       PUGI__FN xml_attribute& xml_attribute::operator=(const char_t* rhs)\r
+       {\r
+               set_value(rhs);\r
+               return *this;\r
+       }\r
+       \r
+       PUGI__FN xml_attribute& xml_attribute::operator=(int rhs)\r
+       {\r
+               set_value(rhs);\r
+               return *this;\r
+       }\r
+\r
+       PUGI__FN xml_attribute& xml_attribute::operator=(unsigned int rhs)\r
+       {\r
+               set_value(rhs);\r
+               return *this;\r
+       }\r
+\r
+       PUGI__FN xml_attribute& xml_attribute::operator=(double rhs)\r
+       {\r
+               set_value(rhs);\r
+               return *this;\r
+       }\r
+       \r
+       PUGI__FN xml_attribute& xml_attribute::operator=(bool rhs)\r
+       {\r
+               set_value(rhs);\r
+               return *this;\r
+       }\r
+\r
+#ifdef PUGIXML_HAS_LONG_LONG\r
+       PUGI__FN xml_attribute& xml_attribute::operator=(long long rhs)\r
+       {\r
+               set_value(rhs);\r
+               return *this;\r
+       }\r
+\r
+       PUGI__FN xml_attribute& xml_attribute::operator=(unsigned long long rhs)\r
+       {\r
+               set_value(rhs);\r
+               return *this;\r
+       }\r
+#endif\r
+\r
+       PUGI__FN bool xml_attribute::set_name(const char_t* rhs)\r
+       {\r
+               if (!_attr) return false;\r
+               \r
+               return impl::strcpy_insitu(_attr->name, _attr->header, impl::xml_memory_page_name_allocated_mask, rhs);\r
+       }\r
+               \r
+       PUGI__FN bool xml_attribute::set_value(const char_t* rhs)\r
+       {\r
+               if (!_attr) return false;\r
+\r
+               return impl::strcpy_insitu(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);\r
+       }\r
+\r
+       PUGI__FN bool xml_attribute::set_value(int rhs)\r
+       {\r
+               if (!_attr) return false;\r
+\r
+               return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);\r
+       }\r
+\r
+       PUGI__FN bool xml_attribute::set_value(unsigned int rhs)\r
+       {\r
+               if (!_attr) return false;\r
+\r
+               return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);\r
+       }\r
+\r
+       PUGI__FN bool xml_attribute::set_value(double rhs)\r
+       {\r
+               if (!_attr) return false;\r
+\r
+               return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);\r
+       }\r
+       \r
+       PUGI__FN bool xml_attribute::set_value(bool rhs)\r
+       {\r
+               if (!_attr) return false;\r
+\r
+               return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);\r
+       }\r
+\r
+#ifdef PUGIXML_HAS_LONG_LONG\r
+       PUGI__FN bool xml_attribute::set_value(long long rhs)\r
+       {\r
+               if (!_attr) return false;\r
+\r
+               return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);\r
+       }\r
+\r
+       PUGI__FN bool xml_attribute::set_value(unsigned long long rhs)\r
+       {\r
+               if (!_attr) return false;\r
+\r
+               return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);\r
+       }\r
+#endif\r
+\r
+#ifdef __BORLANDC__\r
+       PUGI__FN bool operator&&(const xml_attribute& lhs, bool rhs)\r
+       {\r
+               return (bool)lhs && rhs;\r
+       }\r
+\r
+       PUGI__FN bool operator||(const xml_attribute& lhs, bool rhs)\r
+       {\r
+               return (bool)lhs || rhs;\r
+       }\r
+#endif\r
+\r
+       PUGI__FN xml_node::xml_node(): _root(0)\r
+       {\r
+       }\r
+\r
+       PUGI__FN xml_node::xml_node(xml_node_struct* p): _root(p)\r
+       {\r
+       }\r
+       \r
+       PUGI__FN static void unspecified_bool_xml_node(xml_node***)\r
+       {\r
+       }\r
+\r
+       PUGI__FN xml_node::operator xml_node::unspecified_bool_type() const\r
+       {\r
+               return _root ? unspecified_bool_xml_node : 0;\r
+       }\r
+\r
+       PUGI__FN bool xml_node::operator!() const\r
+       {\r
+               return !_root;\r
+       }\r
+\r
+       PUGI__FN xml_node::iterator xml_node::begin() const\r
+       {\r
+               return iterator(_root ? _root->first_child : 0, _root);\r
+       }\r
+\r
+       PUGI__FN xml_node::iterator xml_node::end() const\r
+       {\r
+               return iterator(0, _root);\r
+       }\r
+       \r
+       PUGI__FN xml_node::attribute_iterator xml_node::attributes_begin() const\r
+       {\r
+               return attribute_iterator(_root ? _root->first_attribute : 0, _root);\r
+       }\r
+\r
+       PUGI__FN xml_node::attribute_iterator xml_node::attributes_end() const\r
+       {\r
+               return attribute_iterator(0, _root);\r
+       }\r
+       \r
+       PUGI__FN xml_object_range<xml_node_iterator> xml_node::children() const\r
+       {\r
+               return xml_object_range<xml_node_iterator>(begin(), end());\r
+       }\r
+\r
+       PUGI__FN xml_object_range<xml_named_node_iterator> xml_node::children(const char_t* name_) const\r
+       {\r
+               return xml_object_range<xml_named_node_iterator>(xml_named_node_iterator(child(name_)._root, _root, name_), xml_named_node_iterator(0, _root, name_));\r
+       }\r
+\r
+       PUGI__FN xml_object_range<xml_attribute_iterator> xml_node::attributes() const\r
+       {\r
+               return xml_object_range<xml_attribute_iterator>(attributes_begin(), attributes_end());\r
+       }\r
+\r
+       PUGI__FN bool xml_node::operator==(const xml_node& r) const\r
+       {\r
+               return (_root == r._root);\r
+       }\r
+\r
+       PUGI__FN bool xml_node::operator!=(const xml_node& r) const\r
+       {\r
+               return (_root != r._root);\r
+       }\r
+\r
+       PUGI__FN bool xml_node::operator<(const xml_node& r) const\r
+       {\r
+               return (_root < r._root);\r
+       }\r
+       \r
+       PUGI__FN bool xml_node::operator>(const xml_node& r) const\r
+       {\r
+               return (_root > r._root);\r
+       }\r
+       \r
+       PUGI__FN bool xml_node::operator<=(const xml_node& r) const\r
+       {\r
+               return (_root <= r._root);\r
+       }\r
+       \r
+       PUGI__FN bool xml_node::operator>=(const xml_node& r) const\r
+       {\r
+               return (_root >= r._root);\r
+       }\r
+\r
+       PUGI__FN bool xml_node::empty() const\r
+       {\r
+               return !_root;\r
+       }\r
+       \r
+       PUGI__FN const char_t* xml_node::name() const\r
+       {\r
+               return (_root && _root->name) ? _root->name : PUGIXML_TEXT("");\r
+       }\r
+\r
+       PUGI__FN xml_node_type xml_node::type() const\r
+       {\r
+               return _root ? static_cast<xml_node_type>((_root->header & impl::xml_memory_page_type_mask) + 1) : node_null;\r
+       }\r
+       \r
+       PUGI__FN const char_t* xml_node::value() const\r
+       {\r
+               return (_root && _root->value) ? _root->value : PUGIXML_TEXT("");\r
+       }\r
+       \r
+       PUGI__FN xml_node xml_node::child(const char_t* name_) const\r
+       {\r
+               if (!_root) return xml_node();\r
+\r
+               for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)\r
+                       if (i->name && impl::strequal(name_, i->name)) return xml_node(i);\r
+\r
+               return xml_node();\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::attribute(const char_t* name_) const\r
+       {\r
+               if (!_root) return xml_attribute();\r
+\r
+               for (xml_attribute_struct* i = _root->first_attribute; i; i = i->next_attribute)\r
+                       if (i->name && impl::strequal(name_, i->name))\r
+                               return xml_attribute(i);\r
+               \r
+               return xml_attribute();\r
+       }\r
+       \r
+       PUGI__FN xml_node xml_node::next_sibling(const char_t* name_) const\r
+       {\r
+               if (!_root) return xml_node();\r
+               \r
+               for (xml_node_struct* i = _root->next_sibling; i; i = i->next_sibling)\r
+                       if (i->name && impl::strequal(name_, i->name)) return xml_node(i);\r
+\r
+               return xml_node();\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::next_sibling() const\r
+       {\r
+               if (!_root) return xml_node();\r
+               \r
+               if (_root->next_sibling) return xml_node(_root->next_sibling);\r
+               else return xml_node();\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::previous_sibling(const char_t* name_) const\r
+       {\r
+               if (!_root) return xml_node();\r
+               \r
+               for (xml_node_struct* i = _root->prev_sibling_c; i->next_sibling; i = i->prev_sibling_c)\r
+                       if (i->name && impl::strequal(name_, i->name)) return xml_node(i);\r
+\r
+               return xml_node();\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::previous_sibling() const\r
+       {\r
+               if (!_root) return xml_node();\r
+               \r
+               if (_root->prev_sibling_c->next_sibling) return xml_node(_root->prev_sibling_c);\r
+               else return xml_node();\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::parent() const\r
+       {\r
+               return _root ? xml_node(_root->parent) : xml_node();\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::root() const\r
+       {\r
+               if (!_root) return xml_node();\r
+\r
+               impl::xml_memory_page* page = reinterpret_cast<impl::xml_memory_page*>(_root->header & impl::xml_memory_page_pointer_mask);\r
+\r
+               return xml_node(static_cast<impl::xml_document_struct*>(page->allocator));\r
+       }\r
+\r
+       PUGI__FN xml_text xml_node::text() const\r
+       {\r
+               return xml_text(_root);\r
+       }\r
+\r
+       PUGI__FN const char_t* xml_node::child_value() const\r
+       {\r
+               if (!_root) return PUGIXML_TEXT("");\r
+               \r
+               for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)\r
+                       if (i->value && impl::is_text_node(i))\r
+                               return i->value;\r
+\r
+               return PUGIXML_TEXT("");\r
+       }\r
+\r
+       PUGI__FN const char_t* xml_node::child_value(const char_t* name_) const\r
+       {\r
+               return child(name_).child_value();\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::first_attribute() const\r
+       {\r
+               return _root ? xml_attribute(_root->first_attribute) : xml_attribute();\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::last_attribute() const\r
+       {\r
+               return _root && _root->first_attribute ? xml_attribute(_root->first_attribute->prev_attribute_c) : xml_attribute();\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::first_child() const\r
+       {\r
+               return _root ? xml_node(_root->first_child) : xml_node();\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::last_child() const\r
+       {\r
+               return _root && _root->first_child ? xml_node(_root->first_child->prev_sibling_c) : xml_node();\r
+       }\r
+\r
+       PUGI__FN bool xml_node::set_name(const char_t* rhs)\r
+       {\r
+               switch (type())\r
+               {\r
+               case node_pi:\r
+               case node_declaration:\r
+               case node_element:\r
+                       return impl::strcpy_insitu(_root->name, _root->header, impl::xml_memory_page_name_allocated_mask, rhs);\r
+\r
+               default:\r
+                       return false;\r
+               }\r
+       }\r
+               \r
+       PUGI__FN bool xml_node::set_value(const char_t* rhs)\r
+       {\r
+               switch (type())\r
+               {\r
+               case node_pi:\r
+               case node_cdata:\r
+               case node_pcdata:\r
+               case node_comment:\r
+               case node_doctype:\r
+                       return impl::strcpy_insitu(_root->value, _root->header, impl::xml_memory_page_value_allocated_mask, rhs);\r
+\r
+               default:\r
+                       return false;\r
+               }\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::append_attribute(const char_t* name_)\r
+       {\r
+               if (type() != node_element && type() != node_declaration) return xml_attribute();\r
+               \r
+               xml_attribute a(impl::append_attribute_ll(_root, impl::get_allocator(_root)));\r
+               a.set_name(name_);\r
+               \r
+               return a;\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::prepend_attribute(const char_t* name_)\r
+       {\r
+               if (type() != node_element && type() != node_declaration) return xml_attribute();\r
+               \r
+               xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root)));\r
+               if (!a) return xml_attribute();\r
+\r
+               a.set_name(name_);\r
+               \r
+               xml_attribute_struct* head = _root->first_attribute;\r
+\r
+               if (head)\r
+               {\r
+                       a._attr->prev_attribute_c = head->prev_attribute_c;\r
+                       head->prev_attribute_c = a._attr;\r
+               }\r
+               else\r
+                       a._attr->prev_attribute_c = a._attr;\r
+               \r
+               a._attr->next_attribute = head;\r
+               _root->first_attribute = a._attr;\r
+                               \r
+               return a;\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::insert_attribute_before(const char_t* name_, const xml_attribute& attr)\r
+       {\r
+               if ((type() != node_element && type() != node_declaration) || attr.empty()) return xml_attribute();\r
+               \r
+               // check that attribute belongs to *this\r
+               xml_attribute_struct* cur = attr._attr;\r
+\r
+               while (cur->prev_attribute_c->next_attribute) cur = cur->prev_attribute_c;\r
+\r
+               if (cur != _root->first_attribute) return xml_attribute();\r
+\r
+               xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root)));\r
+               if (!a) return xml_attribute();\r
+\r
+               a.set_name(name_);\r
+\r
+               if (attr._attr->prev_attribute_c->next_attribute)\r
+                       attr._attr->prev_attribute_c->next_attribute = a._attr;\r
+               else\r
+                       _root->first_attribute = a._attr;\r
+               \r
+               a._attr->prev_attribute_c = attr._attr->prev_attribute_c;\r
+               a._attr->next_attribute = attr._attr;\r
+               attr._attr->prev_attribute_c = a._attr;\r
+                               \r
+               return a;\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::insert_attribute_after(const char_t* name_, const xml_attribute& attr)\r
+       {\r
+               if ((type() != node_element && type() != node_declaration) || attr.empty()) return xml_attribute();\r
+               \r
+               // check that attribute belongs to *this\r
+               xml_attribute_struct* cur = attr._attr;\r
+\r
+               while (cur->prev_attribute_c->next_attribute) cur = cur->prev_attribute_c;\r
+\r
+               if (cur != _root->first_attribute) return xml_attribute();\r
+\r
+               xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root)));\r
+               if (!a) return xml_attribute();\r
+\r
+               a.set_name(name_);\r
+\r
+               if (attr._attr->next_attribute)\r
+                       attr._attr->next_attribute->prev_attribute_c = a._attr;\r
+               else\r
+                       _root->first_attribute->prev_attribute_c = a._attr;\r
+               \r
+               a._attr->next_attribute = attr._attr->next_attribute;\r
+               a._attr->prev_attribute_c = attr._attr;\r
+               attr._attr->next_attribute = a._attr;\r
+\r
+               return a;\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::append_copy(const xml_attribute& proto)\r
+       {\r
+               if (!proto) return xml_attribute();\r
+\r
+               xml_attribute result = append_attribute(proto.name());\r
+               result.set_value(proto.value());\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::prepend_copy(const xml_attribute& proto)\r
+       {\r
+               if (!proto) return xml_attribute();\r
+\r
+               xml_attribute result = prepend_attribute(proto.name());\r
+               result.set_value(proto.value());\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::insert_copy_after(const xml_attribute& proto, const xml_attribute& attr)\r
+       {\r
+               if (!proto) return xml_attribute();\r
+\r
+               xml_attribute result = insert_attribute_after(proto.name(), attr);\r
+               result.set_value(proto.value());\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_attribute xml_node::insert_copy_before(const xml_attribute& proto, const xml_attribute& attr)\r
+       {\r
+               if (!proto) return xml_attribute();\r
+\r
+               xml_attribute result = insert_attribute_before(proto.name(), attr);\r
+               result.set_value(proto.value());\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::append_child(xml_node_type type_)\r
+       {\r
+               if (!impl::allow_insert_child(this->type(), type_)) return xml_node();\r
+               \r
+               xml_node n(impl::append_node(_root, impl::get_allocator(_root), type_));\r
+\r
+               if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));\r
+\r
+               return n;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::prepend_child(xml_node_type type_)\r
+       {\r
+               if (!impl::allow_insert_child(this->type(), type_)) return xml_node();\r
+               \r
+               xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));\r
+               if (!n) return xml_node();\r
+\r
+               n._root->parent = _root;\r
+\r
+               xml_node_struct* head = _root->first_child;\r
+\r
+               if (head)\r
+               {\r
+                       n._root->prev_sibling_c = head->prev_sibling_c;\r
+                       head->prev_sibling_c = n._root;\r
+               }\r
+               else\r
+                       n._root->prev_sibling_c = n._root;\r
+               \r
+               n._root->next_sibling = head;\r
+               _root->first_child = n._root;\r
+                               \r
+               if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));\r
+\r
+               return n;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::insert_child_before(xml_node_type type_, const xml_node& node)\r
+       {\r
+               if (!impl::allow_insert_child(this->type(), type_)) return xml_node();\r
+               if (!node._root || node._root->parent != _root) return xml_node();\r
+       \r
+               xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));\r
+               if (!n) return xml_node();\r
+\r
+               n._root->parent = _root;\r
+               \r
+               if (node._root->prev_sibling_c->next_sibling)\r
+                       node._root->prev_sibling_c->next_sibling = n._root;\r
+               else\r
+                       _root->first_child = n._root;\r
+               \r
+               n._root->prev_sibling_c = node._root->prev_sibling_c;\r
+               n._root->next_sibling = node._root;\r
+               node._root->prev_sibling_c = n._root;\r
+\r
+               if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));\r
+\r
+               return n;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::insert_child_after(xml_node_type type_, const xml_node& node)\r
+       {\r
+               if (!impl::allow_insert_child(this->type(), type_)) return xml_node();\r
+               if (!node._root || node._root->parent != _root) return xml_node();\r
+       \r
+               xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));\r
+               if (!n) return xml_node();\r
+\r
+               n._root->parent = _root;\r
+       \r
+               if (node._root->next_sibling)\r
+                       node._root->next_sibling->prev_sibling_c = n._root;\r
+               else\r
+                       _root->first_child->prev_sibling_c = n._root;\r
+               \r
+               n._root->next_sibling = node._root->next_sibling;\r
+               n._root->prev_sibling_c = node._root;\r
+               node._root->next_sibling = n._root;\r
+\r
+               if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));\r
+\r
+               return n;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::append_child(const char_t* name_)\r
+       {\r
+               xml_node result = append_child(node_element);\r
+\r
+               result.set_name(name_);\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::prepend_child(const char_t* name_)\r
+       {\r
+               xml_node result = prepend_child(node_element);\r
+\r
+               result.set_name(name_);\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::insert_child_after(const char_t* name_, const xml_node& node)\r
+       {\r
+               xml_node result = insert_child_after(node_element, node);\r
+\r
+               result.set_name(name_);\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::insert_child_before(const char_t* name_, const xml_node& node)\r
+       {\r
+               xml_node result = insert_child_before(node_element, node);\r
+\r
+               result.set_name(name_);\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::append_copy(const xml_node& proto)\r
+       {\r
+               xml_node result = append_child(proto.type());\r
+\r
+               if (result) impl::recursive_copy_skip(result, proto, result);\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::prepend_copy(const xml_node& proto)\r
+       {\r
+               xml_node result = prepend_child(proto.type());\r
+\r
+               if (result) impl::recursive_copy_skip(result, proto, result);\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::insert_copy_after(const xml_node& proto, const xml_node& node)\r
+       {\r
+               xml_node result = insert_child_after(proto.type(), node);\r
+\r
+               if (result) impl::recursive_copy_skip(result, proto, result);\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::insert_copy_before(const xml_node& proto, const xml_node& node)\r
+       {\r
+               xml_node result = insert_child_before(proto.type(), node);\r
+\r
+               if (result) impl::recursive_copy_skip(result, proto, result);\r
+\r
+               return result;\r
+       }\r
+\r
+       PUGI__FN bool xml_node::remove_attribute(const char_t* name_)\r
+       {\r
+               return remove_attribute(attribute(name_));\r
+       }\r
+\r
+       PUGI__FN bool xml_node::remove_attribute(const xml_attribute& a)\r
+       {\r
+               if (!_root || !a._attr) return false;\r
+\r
+               // check that attribute belongs to *this\r
+               xml_attribute_struct* attr = a._attr;\r
+\r
+               while (attr->prev_attribute_c->next_attribute) attr = attr->prev_attribute_c;\r
+\r
+               if (attr != _root->first_attribute) return false;\r
+\r
+               if (a._attr->next_attribute) a._attr->next_attribute->prev_attribute_c = a._attr->prev_attribute_c;\r
+               else if (_root->first_attribute) _root->first_attribute->prev_attribute_c = a._attr->prev_attribute_c;\r
+               \r
+               if (a._attr->prev_attribute_c->next_attribute) a._attr->prev_attribute_c->next_attribute = a._attr->next_attribute;\r
+               else _root->first_attribute = a._attr->next_attribute;\r
+\r
+               impl::destroy_attribute(a._attr, impl::get_allocator(_root));\r
+\r
+               return true;\r
+       }\r
+\r
+       PUGI__FN bool xml_node::remove_child(const char_t* name_)\r
+       {\r
+               return remove_child(child(name_));\r
+       }\r
+\r
+       PUGI__FN bool xml_node::remove_child(const xml_node& n)\r
+       {\r
+               if (!_root || !n._root || n._root->parent != _root) return false;\r
+\r
+               if (n._root->next_sibling) n._root->next_sibling->prev_sibling_c = n._root->prev_sibling_c;\r
+               else if (_root->first_child) _root->first_child->prev_sibling_c = n._root->prev_sibling_c;\r
+               \r
+               if (n._root->prev_sibling_c->next_sibling) n._root->prev_sibling_c->next_sibling = n._root->next_sibling;\r
+               else _root->first_child = n._root->next_sibling;\r
+               \r
+               impl::destroy_node(n._root, impl::get_allocator(_root));\r
+\r
+               return true;\r
+       }\r
+\r
+       PUGI__FN xml_parse_result xml_node::append_buffer(const void* contents, size_t size, unsigned int options, xml_encoding encoding)\r
+       {\r
+               // append_buffer is only valid for elements/documents\r
+               if (!impl::allow_insert_child(type(), node_element)) return impl::make_parse_result(status_append_invalid_root);\r
+\r
+               // get document node\r
+               impl::xml_document_struct* doc = static_cast<impl::xml_document_struct*>(root()._root);\r
+               assert(doc);\r
+               \r
+               // get extra buffer element (we'll store the document fragment buffer there so that we can deallocate it later)\r
+               impl::xml_memory_page* page = 0;\r
+               impl::xml_extra_buffer* extra = static_cast<impl::xml_extra_buffer*>(doc->allocate_memory(sizeof(impl::xml_extra_buffer), page));\r
+               (void)page;\r
+\r
+               if (!extra) return impl::make_parse_result(status_out_of_memory);\r
+\r
+               // save name; name of the root has to be NULL before parsing - otherwise closing node mismatches will not be detected at the top level\r
+               char_t* rootname = _root->name;\r
+               _root->name = 0;\r
+\r
+               // parse\r
+               char_t* buffer = 0;\r
+               xml_parse_result res = impl::load_buffer_impl(doc, _root, const_cast<void*>(contents), size, options, encoding, false, false, &buffer);\r
+\r
+               // restore name\r
+               _root->name = rootname;\r
+\r
+               // add extra buffer to the list\r
+               extra->buffer = buffer;\r
+               extra->next = doc->extra_buffers;\r
+               doc->extra_buffers = extra;\r
+\r
+               return res;\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::find_child_by_attribute(const char_t* name_, const char_t* attr_name, const char_t* attr_value) const\r
+       {\r
+               if (!_root) return xml_node();\r
+               \r
+               for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)\r
+                       if (i->name && impl::strequal(name_, i->name))\r
+                       {\r
+                               for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)\r
+                                       if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT("")))\r
+                                               return xml_node(i);\r
+                       }\r
+\r
+               return xml_node();\r
+       }\r
+\r
+       PUGI__FN xml_node xml_node::find_child_by_attribute(const char_t* attr_name, const char_t* attr_value) const\r
+       {\r
+               if (!_root) return xml_node();\r
+               \r
+               for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)\r
+                       for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)\r
+                               if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT("")))\r
+                                       return xml_node(i);\r
+\r
+               return xml_node();\r
+       }\r
+\r
+#ifndef PUGIXML_NO_STL\r
+       PUGI__FN string_t xml_node::path(char_t delimiter) const\r
+       {\r
+               xml_node cursor = *this; // Make a copy.\r
+               \r
+               string_t result = cursor.name();\r
+\r
+               while (cursor.parent())\r
+               {\r
+                       cursor = cursor.parent();\r
+                       \r
+                       string_t temp = cursor.name();\r
+                       temp += delimiter;\r
+                       temp += result;\r
+                       result.swap(temp);\r
+               }\r
+\r
+               return result;\r
+       }\r
+#endif\r
+\r
+       PUGI__FN xml_node xml_node::first_element_by_path(const char_t* path_, char_t delimiter) const\r
+       {\r
+               xml_node found = *this; // Current search context.\r
+\r
+               if (!_root || !path_ || !path_[0]) return found;\r
+\r
+               if (path_[0] == delimiter)\r
+               {\r
+                       // Absolute path; e.g. '/foo/bar'\r
+                       found = found.root();\r
+                       ++path_;\r
+               }\r
+\r
+               const char_t* path_segment = path_;\r
+\r
+               while (*path_segment == delimiter) ++path_segment;\r
+\r
+               const char_t* path_segment_end = path_segment;\r
+\r
+               while (*path_segment_end && *path_segment_end != delimiter) ++path_segment_end;\r
+\r
+               if (path_segment == path_segment_end) return found;\r
+\r
+               const char_t* next_segment = path_segment_end;\r
+\r
+               while (*next_segment == delimiter) ++next_segment;\r
+\r
+               if (*path_segment == '.' && path_segment + 1 == path_segment_end)\r
+                       return found.first_element_by_path(next_segment, delimiter);\r
+               else if (*path_segment == '.' && *(path_segment+1) == '.' && path_segment + 2 == path_segment_end)\r
+                       return found.parent().first_element_by_path(next_segment, delimiter);\r
+               else\r
+               {\r
+                       for (xml_node_struct* j = found._root->first_child; j; j = j->next_sibling)\r
+                       {\r
+                               if (j->name && impl::strequalrange(j->name, path_segment, static_cast<size_t>(path_segment_end - path_segment)))\r
+                               {\r
+                                       xml_node subsearch = xml_node(j).first_element_by_path(next_segment, delimiter);\r
+\r
+                                       if (subsearch) return subsearch;\r
+                               }\r
+                       }\r
+\r
+                       return xml_node();\r
+               }\r
+       }\r
+\r
+       PUGI__FN bool xml_node::traverse(xml_tree_walker& walker)\r
+       {\r
+               walker._depth = -1;\r
+               \r
+               xml_node arg_begin = *this;\r
+               if (!walker.begin(arg_begin)) return false;\r
+\r
+               xml_node cur = first_child();\r
+                               \r
+               if (cur)\r
+               {\r
+                       ++walker._depth;\r
+\r
+                       do \r
+                       {\r
+                               xml_node arg_for_each = cur;\r
+                               if (!walker.for_each(arg_for_each))\r
+                                       return false;\r
+                                               \r
+                               if (cur.first_child())\r
+                               {\r
+                                       ++walker._depth;\r
+                                       cur = cur.first_child();\r
+                               }\r
+                               else if (cur.next_sibling())\r
+                                       cur = cur.next_sibling();\r
+                               else\r
+                               {\r
+                                       // Borland C++ workaround\r
+                                       while (!cur.next_sibling() && cur != *this && !cur.parent().empty())\r
+                                       {\r
+                                               --walker._depth;\r
+                                               cur = cur.parent();\r
+                                       }\r
+                                               \r
+                                       if (cur != *this)\r
+                                               cur = cur.next_sibling();\r
+                               }\r
+                       }\r
+                       while (cur && cur != *this);\r
+               }\r
+\r
+               assert(walker._depth == -1);\r
+\r
+               xml_node arg_end = *this;\r
+               return walker.end(arg_end);\r
+       }\r
+\r
+       PUGI__FN size_t xml_node::hash_value() const\r
+       {\r
+               return static_cast<size_t>(reinterpret_cast<uintptr_t>(_root) / sizeof(xml_node_struct));\r
+       }\r
+\r
+       PUGI__FN xml_node_struct* xml_node::internal_object() const\r
+       {\r
+               return _root;\r
+       }\r
+\r
+       PUGI__FN void xml_node::print(xml_writer& writer, const char_t* indent, unsigned int flags, xml_encoding encoding, unsigned int depth) const\r
+       {\r
+               if (!_root) return;\r
+\r
+               impl::xml_buffered_writer buffered_writer(writer, encoding);\r
+\r
+               impl::node_output(buffered_writer, *this, indent, flags, depth);\r
+       }\r
+\r
+#ifndef PUGIXML_NO_STL\r
+       PUGI__FN void xml_node::print(std::basic_ostream<char, std::char_traits<char> >& stream, const char_t* indent, unsigned int flags, xml_encoding encoding, unsigned int depth) const\r
+       {\r
+               xml_writer_stream writer(stream);\r
+\r
+               print(writer, indent, flags, encoding, depth);\r
+       }\r
+\r
+       PUGI__FN void xml_node::print(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream, const char_t* indent, unsigned int flags, unsigned int depth) const\r
+       {\r
+               xml_writer_stream writer(stream);\r
+\r
+               print(writer, indent, flags, encoding_wchar, depth);\r
+       }\r
+#endif\r
+\r
+       PUGI__FN ptrdiff_t xml_node::offset_debug() const\r
+       {\r
+               xml_node_struct* r = root()._root;\r
+\r
+               if (!r) return -1;\r
+\r
+               const char_t* buffer = static_cast<impl::xml_document_struct*>(r)->buffer;\r
+\r
+               if (!buffer) return -1;\r
+\r
+               switch (type())\r
+               {\r
+               case node_document:\r
+                       return 0;\r
+\r
+               case node_element:\r
+               case node_declaration:\r
+               case node_pi:\r
+                       return (_root->header & impl::xml_memory_page_name_allocated_mask) ? -1 : _root->name - buffer;\r
+\r
+               case node_pcdata:\r
+               case node_cdata:\r
+               case node_comment:\r
+               case node_doctype:\r
+                       return (_root->header & impl::xml_memory_page_value_allocated_mask) ? -1 : _root->value - buffer;\r
+\r
+               default:\r
+                       return -1;\r
+               }\r
+       }\r
+\r
+#ifdef __BORLANDC__\r
+       PUGI__FN bool operator&&(const xml_node& lhs, bool rhs)\r
+       {\r
+               return (bool)lhs && rhs;\r
+       }\r
+\r
+       PUGI__FN bool operator||(const xml_node& lhs, bool rhs)\r
+       {\r
+               return (bool)lhs || rhs;\r
+       }\r
+#endif\r
+\r
+       PUGI__FN xml_text::xml_text(xml_node_struct* root): _root(root)\r
+       {\r
+       }\r
+\r
+       PUGI__FN xml_node_struct* xml_text::_data() const\r
+       {\r
+               if (!_root || impl::is_text_node(_root)) return _root;\r
+\r
+               for (xml_node_struct* node = _root->first_child; node; node = node->next_sibling)\r
+                       if (impl::is_text_node(node))\r
+                               return node;\r
+\r
+               return 0;\r
+       }\r
+\r
+       PUGI__FN xml_node_struct* xml_text::_data_new()\r
+       {\r
+               xml_node_struct* d = _data();\r
+               if (d) return d;\r
+\r
+               return xml_node(_root).append_child(node_pcdata).internal_object();\r
+       }\r
+\r
+       PUGI__FN xml_text::xml_text(): _root(0)\r
+       {\r
+       }\r
+\r
+       PUGI__FN static void unspecified_bool_xml_text(xml_text***)\r
+       {\r
+       }\r
+\r
+       PUGI__FN xml_text::operator xml_text::unspecified_bool_type() const\r
+       {\r
+               return _data() ? unspecified_bool_xml_text : 0;\r
+       }\r
+\r
+       PUGI__FN bool xml_text::operator!() const\r
+       {\r
+               return !_data();\r
+       }\r
+\r
+       PUGI__FN bool xml_text::empty() const\r
+       {\r
+               return _data() == 0;\r
+       }\r
+\r
+       PUGI__FN const char_t* xml_text::get() const\r
+       {\r
+               xml_node_struct* d = _data();\r
+\r
+               return (d && d->value) ? d->value : PUGIXML_TEXT("");\r
+       }\r
+\r
+       PUGI__FN const char_t* xml_text::as_string(const char_t* def) const\r
+       {\r
+               xml_node_struct* d = _data();\r
+\r
+               return (d && d->value) ? d->value : def;\r
+       }\r
+\r
+       PUGI__FN int xml_text::as_int(int def) const\r
+       {\r
+               xml_node_struct* d = _data();\r
+\r
+               return impl::get_value_int(d ? d->value : 0, def);\r
+       }\r
+\r
+       PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const\r
+       {\r
+               xml_node_struct* d = _data();\r
+\r
+               return impl::get_value_uint(d ? d->value : 0, def);\r
+       }\r
+\r
+       PUGI__FN double xml_text::as_double(double def) const\r
+       {\r
+               xml_node_struct* d = _data();\r
+\r
+               return impl::get_value_double(d ? d->value : 0, def);\r
+       }\r
+\r
+       PUGI__FN float xml_text::as_float(float def) const\r
+       {\r
+               xml_node_struct* d = _data();\r
+\r
+               return impl::get_value_float(d ? d->value : 0, def);\r
+       }\r
+\r
+       PUGI__FN bool xml_text::as_bool(bool def) const\r
+       {\r
+               xml_node_struct* d = _data();\r
+\r
+               return impl::get_value_bool(d ? d->value : 0, def);\r
+       }\r
+\r
+#ifdef PUGIXML_HAS_LONG_LONG\r
+       PUGI__FN long long xml_text::as_llong(long long def) const\r
+       {\r
+               xml_node_struct* d = _data();\r
+\r
+               return impl::get_value_llong(d ? d->value : 0, def);\r
+       }\r
+\r
+       PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def) const\r
+       {\r
+               xml_node_struct* d = _data();\r
+\r
+               return impl::get_value_ullong(d ? d->value : 0, def);\r
+       }\r
+#endif\r
+\r
+       PUGI__FN bool xml_text::set(const char_t* rhs)\r
+       {\r
+               xml_node_struct* dn = _data_new();\r
+\r
+               return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;\r
+       }\r
+\r
+       PUGI__FN bool xml_text::set(int rhs)\r
+       {\r
+               xml_node_struct* dn = _data_new();\r
+\r
+               return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;\r
+       }\r
+\r
+       PUGI__FN bool xml_text::set(unsigned int rhs)\r
+       {\r
+               xml_node_struct* dn = _data_new();\r
+\r
+               return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;\r
+       }\r
+\r
+       PUGI__FN bool xml_text::set(double rhs)\r
+       {\r
+               xml_node_struct* dn = _data_new();\r
+\r
+               return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;\r
+       }\r
+\r
+       PUGI__FN bool xml_text::set(bool rhs)\r
+       {\r
+               xml_node_struct* dn = _data_new();\r
+\r
+               return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;\r
+       }\r
+\r
+#ifdef PUGIXML_HAS_LONG_LONG\r
+       PUGI__FN bool xml_text::set(long long rhs)\r
+       {\r
+               xml_node_struct* dn = _data_new();\r
+\r
+               return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;\r
+       }\r
+\r
+       PUGI__FN bool xml_text::set(unsigned long long rhs)\r
+       {\r
+               xml_node_struct* dn = _data_new();\r
+\r
+               return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;\r
+       }\r
+#endif\r
+\r
+       PUGI__FN xml_text& xml_text::operator=(const char_t* rhs)\r
+       {\r
+               set(rhs);\r
+               return *this;\r
+       }\r
+\r
+       PUGI__FN xml_text& xml_text::operator=(int rhs)\r
+       {\r
+               set(rhs);\r
+               return *this;\r
+       }\r
+\r
+       PUGI__FN xml_text& xml_text::operator=(unsigned int rhs)\r
+       {\r
+               set(rhs);\r
+               return *this;\r
+       }\r
+\r
+       PUGI__FN xml_text& xml_text::operator=(double rhs)\r
+       {\r
+               set(rhs);\r
+               return *this;\r
+       }\r
+\r
+       PUGI__FN xml_text& xml_text::operator=(bool rhs)\r
+       {\r
+               set(rhs);\r
+               return *this;\r
+       }\r
+\r
+#ifdef PUGIXML_HAS_LONG_LONG\r
+       PUGI__FN xml_text& xml_text::operator=(long long rhs)\r
+       {\r
+               set(rhs);\r
+               return *this;\r
+       }\r
+\r
+       PUGI__FN xml_text& xml_text::operator=(unsigned long long rhs)\r
+       {\r
+               set(rhs);\r
+               return *this;\r
+       }\r
+#endif\r
+\r
+       PUGI__FN xml_node xml_text::data() const\r
+       {\r
+               return xml_node(_data());\r
+       }\r
+\r
+#ifdef __BORLANDC__\r
+       PUGI__FN bool operator&&(const xml_text& lhs, bool rhs)\r
+       {\r
+               return (bool)lhs && rhs;\r
+       }\r
+\r
+       PUGI__FN bool operator||(const xml_text& lhs, bool rhs)\r
+       {\r
+               return (bool)lhs || rhs;\r
+       }\r
+#endif\r
+\r
+       PUGI__FN xml_node_iterator::xml_node_iterator()\r
+       {\r
+       }\r
+\r
+       PUGI__FN xml_node_iterator::xml_node_iterator(const xml_node& node): _wrap(node), _parent(node.parent())