14#ifndef SOURCE_PUGIXML_CPP
15#define SOURCE_PUGIXML_CPP
25#ifdef PUGIXML_WCHAR_MODE
29#ifndef PUGIXML_NO_XPATH
45# pragma warning(disable: 4127)
46# pragma warning(disable: 4324)
47# pragma warning(disable: 4702)
48# pragma warning(disable: 4996)
51#if defined(_MSC_VER) && defined(__c2__)
52# pragma clang diagnostic push
53# pragma clang diagnostic ignored "-Wdeprecated"
56#ifdef __INTEL_COMPILER
57# pragma warning(disable: 177)
58# pragma warning(disable: 279)
59# pragma warning(disable: 1478 1786)
60# pragma warning(disable: 1684)
63#if defined(__BORLANDC__) && defined(PUGIXML_HEADER_ONLY)
75# pragma diag_suppress=178
76# pragma diag_suppress=237
79#ifdef __TI_COMPILER_VERSION__
80# pragma diag_suppress 179
84#if defined(_MSC_VER) && _MSC_VER >= 1300
85# define PUGI__NO_INLINE __declspec(noinline)
86#elif defined(__GNUC__)
87# define PUGI__NO_INLINE __attribute__((noinline))
89# define PUGI__NO_INLINE
93#if defined(__GNUC__) && !defined(__c2__)
94# define PUGI__UNLIKELY(cond) __builtin_expect(cond, 0)
96# define PUGI__UNLIKELY(cond) (cond)
100#define PUGI__STATIC_ASSERT(cond) { static const char condition_failed[(cond) ? 1 : -1] = {0}; (void)condition_failed[0]; }
104# define PUGI__DMC_VOLATILE volatile
106# define PUGI__DMC_VOLATILE
110#if defined(__clang__) && defined(__has_attribute)
111# if __has_attribute(no_sanitize)
112# define PUGI__UNSIGNED_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
114# define PUGI__UNSIGNED_OVERFLOW
117# define PUGI__UNSIGNED_OVERFLOW
121#if defined(__BORLANDC__) && !defined(__MEM_H_USING_LIST)
128#if defined(PUGIXML_HAS_LONG_LONG) && defined(__GNUC__) && !defined(LLONG_MAX) && !defined(LLONG_MIN) && !defined(ULLONG_MAX)
129# define LLONG_MIN (-LLONG_MAX - 1LL)
130# define LLONG_MAX __LONG_LONG_MAX__
131# define ULLONG_MAX (LLONG_MAX * 2ULL + 1ULL)
135#if defined(_MSC_VER) && !defined(__S3E__)
136# define PUGI__MSVC_CRT_VERSION _MSC_VER
140#if __cplusplus >= 201103
141# define PUGI__SNPRINTF(buf, ...) snprintf(buf, sizeof(buf), __VA_ARGS__)
142#elif defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400
143# define PUGI__SNPRINTF(buf, ...) _snprintf_s(buf, _countof(buf), _TRUNCATE, __VA_ARGS__)
145# define PUGI__SNPRINTF sprintf
149#ifdef PUGIXML_HEADER_ONLY
150# define PUGI__NS_BEGIN namespace pugi { namespace impl {
151# define PUGI__NS_END } }
152# define PUGI__FN inline
153# define PUGI__FN_NO_INLINE inline
155# if defined(_MSC_VER) && _MSC_VER < 1300
156# define PUGI__NS_BEGIN namespace pugi { namespace impl {
157# define PUGI__NS_END } }
159# define PUGI__NS_BEGIN namespace pugi { namespace impl { namespace {
160# define PUGI__NS_END } } }
163# define PUGI__FN_NO_INLINE PUGI__NO_INLINE
167#if (defined(_MSC_VER) && _MSC_VER < 1600) || (defined(__BORLANDC__) && __BORLANDC__ < 0x561)
170# ifndef _UINTPTR_T_DEFINED
171 typedef size_t uintptr_t;
174 typedef unsigned __int8 uint8_t;
175 typedef unsigned __int16 uint16_t;
176 typedef unsigned __int32 uint32_t;
194 template <
typename T>
216 #ifdef PUGIXML_WCHAR_MODE
228 #ifdef PUGIXML_WCHAR_MODE
229 return wcscmp(src, dst) == 0;
231 return strcmp(src, dst) == 0;
238 for (
size_t i = 0; i < count; ++i)
239 if (lhs[i] != rhs[i])
242 return lhs[count] == 0;
250 #ifdef PUGIXML_WCHAR_MODE
253 const wchar_t* end = s;
255 return static_cast<size_t>(end - s);
264 typedef void (*
D)(T*);
287#ifdef PUGIXML_COMPACT
289 class compact_hash_table
292 compact_hash_table(): _items(0), _capacity(0), _count(0)
307 void* find(
const void* key)
309 if (_capacity == 0)
return 0;
311 item_t* item = get_item(key);
313 assert(item->key == key || (item->key == 0 && item->value == 0));
318 void insert(
const void* key,
void* value)
320 assert(_capacity != 0 && _count < _capacity - _capacity / 4);
322 item_t* item = get_item(key);
334 bool reserve(
size_t extra = 16)
336 if (_count + extra >= _capacity - _capacity / 4)
337 return rehash(_count + extra);
354 bool rehash(
size_t count);
356 item_t* get_item(
const void* key)
359 assert(_capacity > 0);
361 size_t hashmod = _capacity - 1;
362 size_t bucket = hash(key) & hashmod;
364 for (
size_t probe = 0; probe <= hashmod; ++probe)
366 item_t& probe_item = _items[bucket];
368 if (probe_item.key == key || probe_item.key == 0)
372 bucket = (bucket + probe + 1) & hashmod;
375 assert(
false &&
"Hash table is full");
381 unsigned int h =
static_cast<unsigned int>(
reinterpret_cast<uintptr_t
>(key) & 0xffffffff);
396 size_t capacity = 32;
397 while (count >= capacity - capacity / 4)
400 compact_hash_table rt;
401 rt._capacity = capacity;
407 memset(rt._items, 0,
sizeof(item_t) * capacity);
409 for (
size_t i = 0; i < _capacity; ++i)
411 rt.insert(_items[i].key, _items[i].value);
416 _capacity = capacity;
419 assert(_count == rt._count);
428#ifdef PUGIXML_COMPACT
429 static const uintptr_t xml_memory_block_alignment = 4;
431 static const uintptr_t xml_memory_block_alignment =
sizeof(
void*);
435 static const uintptr_t xml_memory_page_contents_shared_mask = 64;
436 static const uintptr_t xml_memory_page_name_allocated_mask = 32;
437 static const uintptr_t xml_memory_page_value_allocated_mask = 16;
438 static const uintptr_t xml_memory_page_type_mask = 15;
441 static const uintptr_t xml_memory_page_name_allocated_or_shared_mask = xml_memory_page_name_allocated_mask | xml_memory_page_contents_shared_mask;
442 static const uintptr_t xml_memory_page_value_allocated_or_shared_mask = xml_memory_page_value_allocated_mask | xml_memory_page_contents_shared_mask;
444#ifdef PUGIXML_COMPACT
445 #define PUGI__GETHEADER_IMPL(object, page, flags)
446 #define PUGI__GETPAGE_IMPL(header) (header).get_page()
448 #define PUGI__GETHEADER_IMPL(object, page, flags) (((reinterpret_cast<char*>(object) - reinterpret_cast<char*>(page)) << 8) | (flags))
450 #define PUGI__GETPAGE_IMPL(header) static_cast<impl::xml_memory_page*>(const_cast<void*>(static_cast<const void*>(reinterpret_cast<const char*>(&header) - (header >> 8))))
453 #define PUGI__GETPAGE(n) PUGI__GETPAGE_IMPL((n)->header)
454 #define PUGI__NODETYPE(n) static_cast<xml_node_type>((n)->header & impl::xml_memory_page_type_mask)
470 #ifdef PUGIXML_COMPACT
471 result->compact_string_base = 0;
472 result->compact_shared_parent = 0;
473 result->compact_page_marker = 0;
487 #ifdef PUGIXML_COMPACT
488 char_t* compact_string_base;
489 void* compact_shared_parent;
490 uint32_t* compact_page_marker;
494 static const size_t xml_memory_page_size =
495 #ifdef PUGIXML_MEMORY_PAGE_SIZE
496 (PUGIXML_MEMORY_PAGE_SIZE)
512 #ifdef PUGIXML_COMPACT
523 if (!memory)
return 0;
555 #ifdef PUGIXML_COMPACT
559 if (!result)
return 0;
562 ptrdiff_t offset =
static_cast<char*
>(result) -
reinterpret_cast<char*
>(out_page->compact_page_marker);
564 if (
PUGI__UNLIKELY(
static_cast<uintptr_t
>(offset) >= 256 * xml_memory_block_alignment))
567 uint32_t* marker =
static_cast<uint32_t*
>(result);
569 *marker =
static_cast<uint32_t
>(
reinterpret_cast<char*
>(marker) -
reinterpret_cast<char*
>(out_page));
570 out_page->compact_page_marker = marker;
607 assert(
_root == page);
613 #ifdef PUGIXML_COMPACT
615 page->compact_string_base = 0;
616 page->compact_shared_parent = 0;
617 page->compact_page_marker = 0;
624 assert(
_root != page);
639 static const size_t max_encoded_offset = (1 << 16) * xml_memory_block_alignment;
647 size_t full_size = (size + (xml_memory_block_alignment - 1)) & ~(xml_memory_block_alignment - 1);
652 if (!header)
return 0;
655 ptrdiff_t page_offset =
reinterpret_cast<char*
>(header) -
reinterpret_cast<char*
>(page) -
sizeof(
xml_memory_page);
657 assert(page_offset % xml_memory_block_alignment == 0);
658 assert(page_offset >= 0 &&
static_cast<size_t>(page_offset) < max_encoded_offset);
659 header->
page_offset =
static_cast<uint16_t
>(
static_cast<size_t>(page_offset) / xml_memory_block_alignment);
662 assert(full_size % xml_memory_block_alignment == 0);
663 assert(full_size < max_encoded_offset || (page->
busy_size == full_size && page_offset == 0));
664 header->
full_size =
static_cast<uint16_t
>(full_size < max_encoded_offset ? full_size / xml_memory_block_alignment : 0);
668 return static_cast<char_t*
>(
static_cast<void*
>(header + 1));
692 #ifdef PUGIXML_COMPACT
693 return _hash->reserve();
702 #ifdef PUGIXML_COMPACT
703 compact_hash_table* _hash;
709 const size_t large_allocation_threshold = xml_memory_page_size / 4;
716 if (size <= large_allocation_threshold)
736 _root->prev->next = page;
746#ifdef PUGIXML_COMPACT
748 static const uintptr_t compact_alignment_log2 = 2;
749 static const uintptr_t compact_alignment = 1 << compact_alignment_log2;
758 ptrdiff_t offset = (
reinterpret_cast<char*
>(
this) -
reinterpret_cast<char*
>(page->compact_page_marker));
759 assert(offset % compact_alignment == 0 &&
static_cast<uintptr_t
>(offset) < 256 * compact_alignment);
761 _page =
static_cast<unsigned char>(offset >> compact_alignment_log2);
762 _flags =
static_cast<unsigned char>(flags);
765 void operator&=(uintptr_t mod)
767 _flags &=
static_cast<unsigned char>(mod);
770 void operator|=(uintptr_t mod)
772 _flags |=
static_cast<unsigned char>(mod);
775 uintptr_t operator&(uintptr_t mod)
const
780 xml_memory_page* get_page()
const
783 const char* page_marker =
reinterpret_cast<const char*
>(
this) - (_page << compact_alignment_log2);
784 const char* page = page_marker - *
reinterpret_cast<const uint32_t*
>(
static_cast<const void*
>(page_marker));
786 return const_cast<xml_memory_page*
>(
reinterpret_cast<const xml_memory_page*
>(
static_cast<const void*
>(page)));
791 unsigned char _flags;
796 const compact_header* header =
reinterpret_cast<const compact_header*
>(
static_cast<const char*
>(object) - header_offset);
798 return header->get_page();
801 template <
int header_offset,
typename T>
PUGI__FN_NO_INLINE T* compact_get_value(
const void*
object)
803 return static_cast<T*
>(compact_get_page(
object, header_offset)->allocator->_hash->find(
object));
806 template <
int header_offset,
typename T>
PUGI__FN_NO_INLINE void compact_set_value(
const void*
object, T* value)
808 compact_get_page(
object, header_offset)->allocator->_hash->insert(
object, value);
811 template <
typename T,
int header_offset,
int start = -126>
class compact_pointer
814 compact_pointer(): _data(0)
818 void operator=(
const compact_pointer& rhs)
823 void operator=(T* value)
831 ptrdiff_t diff =
reinterpret_cast<char*
>(value) -
reinterpret_cast<char*
>(
this);
832 ptrdiff_t offset = ((diff + int(compact_alignment - 1)) >> compact_alignment_log2) - start;
834 if (
static_cast<uintptr_t
>(offset) <= 253)
835 _data =
static_cast<unsigned char>(offset + 1);
838 compact_set_value<header_offset>(
this, value);
853 uintptr_t base =
reinterpret_cast<uintptr_t
>(
this) & ~(compact_alignment - 1);
855 return reinterpret_cast<T*
>(base + (_data - 1 + start) * compact_alignment);
858 return compact_get_value<header_offset, T>(
this);
864 T* operator->()
const
873 template <
typename T,
int header_offset>
class compact_pointer_parent
876 compact_pointer_parent(): _data(0)
880 void operator=(
const compact_pointer_parent& rhs)
885 void operator=(T* value)
893 ptrdiff_t diff =
reinterpret_cast<char*
>(value) -
reinterpret_cast<char*
>(
this);
894 ptrdiff_t offset = ((diff + int(compact_alignment - 1)) >> compact_alignment_log2) + 65533;
896 if (
static_cast<uintptr_t
>(offset) <= 65533)
898 _data =
static_cast<unsigned short>(offset + 1);
902 xml_memory_page* page = compact_get_page(
this, header_offset);
905 page->compact_shared_parent = value;
907 if (page->compact_shared_parent == value)
913 compact_set_value<header_offset>(
this, value);
931 uintptr_t base =
reinterpret_cast<uintptr_t
>(
this) & ~(compact_alignment - 1);
933 return reinterpret_cast<T*
>(base + (_data - 1 - 65533) * compact_alignment);
935 else if (_data == 65534)
936 return static_cast<T*
>(compact_get_page(
this, header_offset)->compact_shared_parent);
938 return compact_get_value<header_offset, T>(
this);
944 T* operator->()
const
953 template <
int header_offset,
int base_offset>
class compact_string
956 compact_string(): _data(0)
960 void operator=(
const compact_string& rhs)
965 void operator=(char_t* value)
969 xml_memory_page* page = compact_get_page(
this, header_offset);
972 page->compact_string_base = value;
974 ptrdiff_t offset = value - page->compact_string_base;
976 if (
static_cast<uintptr_t
>(offset) < (65535 << 7))
979 uint16_t* base =
reinterpret_cast<uint16_t*
>(
static_cast<void*
>(
reinterpret_cast<char*
>(
this) - base_offset));
983 *base =
static_cast<uint16_t
>((offset >> 7) + 1);
984 _data =
static_cast<unsigned char>((offset & 127) + 1);
988 ptrdiff_t remainder = offset - ((*base - 1) << 7);
990 if (
static_cast<uintptr_t
>(remainder) <= 253)
992 _data =
static_cast<unsigned char>(remainder + 1);
996 compact_set_value<header_offset>(
this, value);
1004 compact_set_value<header_offset>(
this, value);
1021 xml_memory_page* page = compact_get_page(
this, header_offset);
1024 const uint16_t* base =
reinterpret_cast<const uint16_t*
>(
static_cast<const void*
>(
reinterpret_cast<const char*
>(
this) - base_offset));
1027 ptrdiff_t offset = ((*base - 1) << 7) + (_data - 1);
1029 return page->compact_string_base + offset;
1033 return compact_get_value<header_offset, char_t>(
this);
1041 unsigned char _data;
1046#ifdef PUGIXML_COMPACT
1056 impl::compact_header
header;
1058 uint16_t namevalue_base;
1060 impl::compact_string<4, 2>
name;
1061 impl::compact_string<5, 3>
value;
1064 impl::compact_pointer<xml_attribute_struct, 7, 0>
next_attribute;
1074 impl::compact_header
header;
1076 uint16_t namevalue_base;
1078 impl::compact_string<4, 2>
name;
1079 impl::compact_string<5, 3>
value;
1081 impl::compact_pointer_parent<xml_node_struct, 6>
parent;
1083 impl::compact_pointer<xml_node_struct, 8, 0>
first_child;
1086 impl::compact_pointer<xml_node_struct, 10, 0>
next_sibling;
1151 #ifdef PUGIXML_COMPACT
1152 compact_hash_table hash;
1176 void* memory = alloc.
allocate_object(
sizeof(xml_attribute_struct), page);
1177 if (!memory)
return 0;
1179 return new (memory) xml_attribute_struct(page);
1186 if (!memory)
return 0;
1188 return new (memory) xml_node_struct(page, type);
1193 if (a->header & impl::xml_memory_page_name_allocated_mask)
1196 if (a->header & impl::xml_memory_page_value_allocated_mask)
1204 if (n->header & impl::xml_memory_page_name_allocated_mask)
1207 if (n->header & impl::xml_memory_page_value_allocated_mask)
1210 for (xml_attribute_struct* attr = n->first_attribute; attr; )
1212 xml_attribute_struct* next = attr->next_attribute;
1219 for (xml_node_struct* child = n->first_child; child; )
1221 xml_node_struct* next = child->next_sibling;
1231 inline void append_node(xml_node_struct* child, xml_node_struct* node)
1233 child->parent = node;
1235 xml_node_struct* head = node->first_child;
1239 xml_node_struct* tail = head->prev_sibling_c;
1241 tail->next_sibling = child;
1242 child->prev_sibling_c = tail;
1243 head->prev_sibling_c = child;
1247 node->first_child = child;
1248 child->prev_sibling_c = child;
1254 child->parent = node;
1256 xml_node_struct* head = node->first_child;
1260 child->prev_sibling_c = head->prev_sibling_c;
1261 head->prev_sibling_c = child;
1264 child->prev_sibling_c = child;
1266 child->next_sibling = head;
1267 node->first_child = child;
1272 xml_node_struct* parent = node->parent;
1274 child->parent = parent;
1276 if (node->next_sibling)
1277 node->next_sibling->prev_sibling_c = child;
1279 parent->first_child->prev_sibling_c = child;
1281 child->next_sibling = node->next_sibling;
1282 child->prev_sibling_c = node;
1284 node->next_sibling = child;
1289 xml_node_struct* parent = node->parent;
1291 child->parent = parent;
1293 if (node->prev_sibling_c->next_sibling)
1294 node->prev_sibling_c->next_sibling = child;
1296 parent->first_child = child;
1298 child->prev_sibling_c = node->prev_sibling_c;
1299 child->next_sibling = node;
1301 node->prev_sibling_c = child;
1306 xml_node_struct* parent = node->parent;
1308 if (node->next_sibling)
1309 node->next_sibling->prev_sibling_c = node->prev_sibling_c;
1311 parent->first_child->prev_sibling_c = node->prev_sibling_c;
1313 if (node->prev_sibling_c->next_sibling)
1314 node->prev_sibling_c->next_sibling = node->next_sibling;
1316 parent->first_child = node->next_sibling;
1319 node->prev_sibling_c = 0;
1320 node->next_sibling = 0;
1325 xml_attribute_struct* head = node->first_attribute;
1329 xml_attribute_struct* tail = head->prev_attribute_c;
1331 tail->next_attribute = attr;
1332 attr->prev_attribute_c = tail;
1333 head->prev_attribute_c = attr;
1337 node->first_attribute = attr;
1338 attr->prev_attribute_c = attr;
1344 xml_attribute_struct* head = node->first_attribute;
1348 attr->prev_attribute_c = head->prev_attribute_c;
1349 head->prev_attribute_c = attr;
1352 attr->prev_attribute_c = attr;
1354 attr->next_attribute = head;
1355 node->first_attribute = attr;
1360 if (place->next_attribute)
1361 place->next_attribute->prev_attribute_c = attr;
1363 node->first_attribute->prev_attribute_c = attr;
1365 attr->next_attribute = place->next_attribute;
1366 attr->prev_attribute_c = place;
1367 place->next_attribute = attr;
1372 if (place->prev_attribute_c->next_attribute)
1373 place->prev_attribute_c->next_attribute = attr;
1375 node->first_attribute = attr;
1377 attr->prev_attribute_c = place->prev_attribute_c;
1378 attr->next_attribute = place;
1379 place->prev_attribute_c = attr;
1384 if (attr->next_attribute)
1385 attr->next_attribute->prev_attribute_c = attr->prev_attribute_c;
1387 node->first_attribute->prev_attribute_c = attr->prev_attribute_c;
1389 if (attr->prev_attribute_c->next_attribute)
1390 attr->prev_attribute_c->next_attribute = attr->next_attribute;
1392 node->first_attribute = attr->next_attribute;
1394 attr->prev_attribute_c = 0;
1395 attr->next_attribute = 0;
1400 if (!alloc.
reserve())
return 0;
1403 if (!child)
return 0;
1412 if (!alloc.
reserve())
return 0;
1415 if (!attr)
return 0;
1440 return static_cast<uint16_t
>(((value & 0xff) << 8) | (value >> 8));
1445 return ((value & 0xff) << 24) | ((value & 0xff00) << 8) | ((value & 0xff0000) >> 8) | (value >> 24);
1455 if (ch < 0x80)
return result + 1;
1457 else if (ch < 0x800)
return result + 2;
1459 else return result + 3;
1478 *result =
static_cast<uint8_t
>(ch);
1482 else if (ch < 0x800)
1484 result[0] =
static_cast<uint8_t
>(0xC0 | (ch >> 6));
1485 result[1] =
static_cast<uint8_t
>(0x80 | (ch & 0x3F));
1491 result[0] =
static_cast<uint8_t
>(0xE0 | (ch >> 12));
1492 result[1] =
static_cast<uint8_t
>(0x80 | ((ch >> 6) & 0x3F));
1493 result[2] =
static_cast<uint8_t
>(0x80 | (ch & 0x3F));
1501 result[0] =
static_cast<uint8_t
>(0xF0 | (ch >> 18));
1502 result[1] =
static_cast<uint8_t
>(0x80 | ((ch >> 12) & 0x3F));
1503 result[2] =
static_cast<uint8_t
>(0x80 | ((ch >> 6) & 0x3F));
1504 result[3] =
static_cast<uint8_t
>(0x80 | (ch & 0x3F));
1510 return (ch < 0x10000) ?
low(result, ch) :
high(result, ch);
1535 *result =
static_cast<uint16_t
>(ch);
1542 uint32_t msh =
static_cast<uint32_t
>(ch - 0x10000) >> 10;
1543 uint32_t lsh =
static_cast<uint32_t
>(ch - 0x10000) & 0x3ff;
1545 result[0] =
static_cast<uint16_t
>(0xD800 + msh);
1546 result[1] =
static_cast<uint16_t
>(0xDC00 + lsh);
1553 return (ch < 0x10000) ?
low(result, ch) :
high(result, ch);
1604 *result =
static_cast<uint8_t
>(ch > 255 ?
'?' : ch);
1623 template <
typename Traits>
static inline typename Traits::value_type
process(
const uint8_t* data,
size_t size,
typename Traits::value_type result, Traits)
1625 const uint8_t utf8_byte_mask = 0x3f;
1629 uint8_t lead = *data;
1634 result = Traits::low(result, lead);
1639 if ((
reinterpret_cast<uintptr_t
>(data) & 3) == 0)
1642 while (size >= 4 && (*
static_cast<const uint32_t*
>(
static_cast<const void*
>(data)) & 0x80808080) == 0)
1644 result = Traits::low(result, data[0]);
1645 result = Traits::low(result, data[1]);
1646 result = Traits::low(result, data[2]);
1647 result = Traits::low(result, data[3]);
1654 else if (
static_cast<unsigned int>(lead - 0xC0) < 0x20 && size >= 2 && (data[1] & 0xc0) == 0x80)
1656 result = Traits::low(result, ((lead & ~0xC0) << 6) | (data[1] & utf8_byte_mask));
1661 else if (
static_cast<unsigned int>(lead - 0xE0) < 0x10 && size >= 3 && (data[1] & 0xc0) == 0x80 && (data[2] & 0xc0) == 0x80)
1663 result = Traits::low(result, ((lead & ~0xE0) << 12) | ((data[1] & utf8_byte_mask) << 6) | (data[2] & utf8_byte_mask));
1668 else if (
static_cast<unsigned int>(lead - 0xF0) < 0x08 && size >= 4 && (data[1] & 0xc0) == 0x80 && (data[2] & 0xc0) == 0x80 && (data[3] & 0xc0) == 0x80)
1670 result = Traits::high(result, ((lead & ~0xF0) << 18) | ((data[1] & utf8_byte_mask) << 12) | ((data[2] & utf8_byte_mask) << 6) | (data[3] & utf8_byte_mask));
1690 template <
typename Traits>
static inline typename Traits::value_type
process(
const uint16_t* data,
size_t size,
typename Traits::value_type result, Traits)
1694 uint16_t lead = opt_swap::value ?
endian_swap(*data) : *data;
1699 result = Traits::low(result, lead);
1704 else if (
static_cast<unsigned int>(lead - 0xE000) < 0x2000)
1706 result = Traits::low(result, lead);
1711 else if (
static_cast<unsigned int>(lead - 0xD800) < 0x400 && size >= 2)
1713 uint16_t next = opt_swap::value ?
endian_swap(data[1]) : data[1];
1715 if (
static_cast<unsigned int>(next - 0xDC00) < 0x400)
1717 result = Traits::high(result, 0x10000 + ((lead & 0x3ff) << 10) + (next & 0x3ff));
1742 template <
typename Traits>
static inline typename Traits::value_type
process(
const uint32_t* data,
size_t size,
typename Traits::value_type result, Traits)
1746 uint32_t lead = opt_swap::value ?
endian_swap(*data) : *data;
1751 result = Traits::low(result, lead);
1758 result = Traits::high(result, lead);
1772 template <
typename Traits>
static inline typename Traits::value_type
process(
const uint8_t* data,
size_t size,
typename Traits::value_type result, Traits)
1776 result = Traits::low(result, *data);
1810 template <
typename Traits>
static inline typename Traits::value_type
process(
const wchar_t* data,
size_t size,
typename Traits::value_type result, Traits traits)
1814 return decoder::process(
reinterpret_cast<const typename decoder::type*
>(data), size, result, traits);
1818#ifdef PUGIXML_WCHAR_MODE
1819 PUGI__FN void convert_wchar_endian_swap(
wchar_t* result,
const wchar_t* data,
size_t length)
1821 for (
size_t i = 0; i < length; ++i)
1840 static const unsigned char chartype_table[256] =
1842 55, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 0, 0, 63, 0, 0,
1843 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1844 8, 0, 6, 0, 0, 0, 7, 6, 0, 0, 0, 0, 0, 96, 64, 0,
1845 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 192, 0, 1, 0, 48, 0,
1846 0, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
1847 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 0, 0, 16, 0, 192,
1848 0, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
1849 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 0, 0, 0, 0, 0,
1851 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
1852 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
1853 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
1854 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
1855 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
1856 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
1857 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
1858 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192
1870 static const unsigned char chartypex_table[256] =
1872 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3,
1873 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1874 0, 0, 2, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 16, 16, 0,
1875 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 3, 0, 1, 0,
1877 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1878 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 0, 0, 0, 0, 20,
1879 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1880 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 0, 0, 0, 0, 0,
1882 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1883 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1884 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1885 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1886 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1887 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1888 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1889 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20
1892#ifdef PUGIXML_WCHAR_MODE
1893 #define PUGI__IS_CHARTYPE_IMPL(c, ct, table) ((static_cast<unsigned int>(c) < 128 ? table[static_cast<unsigned int>(c)] : table[128]) & (ct))
1895 #define PUGI__IS_CHARTYPE_IMPL(c, ct, table) (table[static_cast<unsigned char>(c)] & (ct))
1898 #define PUGI__IS_CHARTYPE(c, ct) PUGI__IS_CHARTYPE_IMPL(c, ct, chartype_table)
1899 #define PUGI__IS_CHARTYPEX(c, ct) PUGI__IS_CHARTYPE_IMPL(c, ct, chartypex_table)
1903 unsigned int ui = 1;
1905 return *
reinterpret_cast<unsigned char*
>(&ui) == 1;
1912 if (
sizeof(
wchar_t) == 2)
1920 #define PUGI__SCANCHAR(ch) { if (offset >= size || data[offset] != ch) return false; offset++; }
1921 #define PUGI__SCANCHARTYPE(ct) { while (offset < size && PUGI__IS_CHARTYPE(data[offset], ct)) offset++; }
1924 if (size < 6 || !((data[0] ==
'<') & (data[1] ==
'?') & (data[2] ==
'x') & (data[3] ==
'm') & (data[4] ==
'l') &&
PUGI__IS_CHARTYPE(data[5],
ct_space)))
1928 for (
size_t i = 6; i + 1 < size; ++i)
1934 if (data[i] ==
'e' && data[i + 1] ==
'n')
1948 uint8_t delimiter = (offset < size && data[offset] ==
'"') ?
'"' :
'\'';
1952 size_t start = offset;
1954 out_encoding = data + offset;
1958 out_length = offset - start;
1968 #undef PUGI__SCANCHAR
1969 #undef PUGI__SCANCHARTYPE
1975 if (size < 4)
return encoding_utf8;
1977 uint8_t d0 = data[0], d1 = data[1], d2 = data[2], d3 = data[3];
1980 if (d0 == 0 && d1 == 0 && d2 == 0xfe && d3 == 0xff)
return encoding_utf32_be;
1981 if (d0 == 0xff && d1 == 0xfe && d2 == 0 && d3 == 0)
return encoding_utf32_le;
1982 if (d0 == 0xfe && d1 == 0xff)
return encoding_utf16_be;
1983 if (d0 == 0xff && d1 == 0xfe)
return encoding_utf16_le;
1984 if (d0 == 0xef && d1 == 0xbb && d2 == 0xbf)
return encoding_utf8;
1987 if (d0 == 0 && d1 == 0 && d2 == 0 && d3 == 0x3c)
return encoding_utf32_be;
1988 if (d0 == 0x3c && d1 == 0 && d2 == 0 && d3 == 0)
return encoding_utf32_le;
1989 if (d0 == 0 && d1 == 0x3c && d2 == 0 && d3 == 0x3f)
return encoding_utf16_be;
1990 if (d0 == 0x3c && d1 == 0 && d2 == 0x3f && d3 == 0)
return encoding_utf16_le;
1993 if (d0 == 0 && d1 == 0x3c)
return encoding_utf16_be;
1994 if (d0 == 0x3c && d1 == 0)
return encoding_utf16_le;
1997 const uint8_t* enc = 0;
1998 size_t enc_length = 0;
2003 if (enc_length == 10
2004 && (enc[0] |
' ') ==
'i' && (enc[1] |
' ') ==
's' && (enc[2] |
' ') ==
'o'
2005 && enc[3] ==
'-' && enc[4] ==
'8' && enc[5] ==
'8' && enc[6] ==
'5' && enc[7] ==
'9'
2006 && enc[8] ==
'-' && enc[9] ==
'1')
2007 return encoding_latin1;
2011 && (enc[0] |
' ') ==
'l' && (enc[1] |
' ') ==
'a' && (enc[2] |
' ') ==
't'
2012 && (enc[3] |
' ') ==
'i' && (enc[4] |
' ') ==
'n'
2014 return encoding_latin1;
2017 return encoding_utf8;
2026 if (encoding == encoding_utf16)
return is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
2029 if (encoding == encoding_utf32)
return is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
2032 if (encoding != encoding_auto)
return encoding;
2035 const uint8_t* data =
static_cast<const uint8_t*
>(contents);
2042 size_t length = size /
sizeof(char_t);
2046 out_buffer =
static_cast<char_t*
>(
const_cast<void*
>(contents));
2047 out_length = length;
2052 if (!buffer)
return false;
2055 memcpy(buffer, contents, length *
sizeof(char_t));
2057 assert(length == 0);
2061 out_buffer = buffer;
2062 out_length = length + 1;
2068#ifdef PUGIXML_WCHAR_MODE
2069 PUGI__FN bool need_endian_swap_utf(xml_encoding le, xml_encoding re)
2075 PUGI__FN bool convert_buffer_endian_swap(char_t*& out_buffer,
size_t& out_length,
const void* contents,
size_t size,
bool is_mutable)
2077 const char_t* data =
static_cast<const char_t*
>(contents);
2078 size_t length = size /
sizeof(
char_t);
2084 convert_wchar_endian_swap(buffer, data, length);
2086 out_buffer = buffer;
2087 out_length = length;
2092 if (!buffer)
return false;
2094 convert_wchar_endian_swap(buffer, data, length);
2097 out_buffer = buffer;
2098 out_length = length + 1;
2106 const typename D::type* data =
static_cast<const typename D::type*
>(contents);
2107 size_t data_length = size /
sizeof(
typename D::type);
2110 size_t length = D::process(data, data_length, 0,
wchar_counter());
2114 if (!buffer)
return false;
2117 wchar_writer::value_type obegin =
reinterpret_cast<wchar_writer::value_type
>(buffer);
2118 wchar_writer::value_type oend = D::process(data, data_length, obegin,
wchar_writer());
2120 assert(oend == obegin + length);
2123 out_buffer = buffer;
2124 out_length = length + 1;
2129 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)
2135 if (encoding == wchar_encoding)
2139 if (need_endian_swap_utf(encoding, wchar_encoding))
2140 return convert_buffer_endian_swap(out_buffer, out_length, contents, size, is_mutable);
2143 if (encoding == encoding_utf8)
2147 if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)
2151 return (native_encoding == encoding) ?
2157 if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)
2161 return (native_encoding == encoding) ?
2167 if (encoding == encoding_latin1)
2170 assert(
false &&
"Invalid encoding");
2176 const typename D::type* data =
static_cast<const typename D::type*
>(contents);
2177 size_t data_length = size /
sizeof(
typename D::type);
2180 size_t length = D::process(data, data_length, 0,
utf8_counter());
2184 if (!buffer)
return false;
2187 uint8_t* obegin =
reinterpret_cast<uint8_t*
>(buffer);
2188 uint8_t* oend = D::process(data, data_length, obegin,
utf8_writer());
2190 assert(oend == obegin + length);
2193 out_buffer = buffer;
2194 out_length = length + 1;
2201 for (
size_t i = 0; i < size; ++i)
2210 const uint8_t* data =
static_cast<const uint8_t*
>(contents);
2211 size_t data_length = size;
2215 assert(prefix_length <= data_length);
2217 const uint8_t* postfix = data + prefix_length;
2218 size_t postfix_length = data_length - prefix_length;
2221 if (postfix_length == 0)
return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable);
2228 if (!buffer)
return false;
2231 memcpy(buffer, data, prefix_length);
2233 uint8_t* obegin =
reinterpret_cast<uint8_t*
>(buffer);
2236 assert(oend == obegin + length);
2239 out_buffer = buffer;
2240 out_length = length + 1;
2245 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)
2248 if (encoding == encoding_utf8)
2252 if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)
2254 xml_encoding native_encoding =
is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
2256 return (native_encoding == encoding) ?
2262 if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)
2264 xml_encoding native_encoding =
is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
2266 return (native_encoding == encoding) ?
2272 if (encoding == encoding_latin1)
2275 assert(
false &&
"Invalid encoding");
2289 uint8_t* begin =
reinterpret_cast<uint8_t*
>(buffer);
2292 assert(begin + size == end);
2297#ifndef PUGIXML_NO_STL
2305 result.resize(size);
2308 if (size > 0)
as_utf8_end(&result[0], size, str, length);
2315 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(str);
2321 std::basic_string<wchar_t> result;
2322 result.resize(length);
2327 wchar_writer::value_type begin =
reinterpret_cast<wchar_writer::value_type
>(&result[0]);
2330 assert(begin + length == end);
2338 template <
typename Header>
2342 if (header & xml_memory_page_contents_shared_mask)
return false;
2344 size_t target_length =
strlength(target);
2347 if ((header & header_mask) == 0)
return target_length >= length;
2350 const size_t reuse_threshold = 32;
2352 return target_length >= length && (target_length < reuse_threshold || target_length - length < target_length / 2);
2355 template <
typename String,
typename Header>
2356 PUGI__FN bool strcpy_insitu(String& dest, Header& header, uintptr_t header_mask,
const char_t* source,
size_t source_length)
2358 if (source_length == 0)
2367 header &= ~header_mask;
2374 memcpy(dest, source, source_length *
sizeof(char_t));
2375 dest[source_length] = 0;
2383 if (!alloc->
reserve())
return false;
2387 if (!buf)
return false;
2390 memcpy(buf, source, source_length *
sizeof(char_t));
2391 buf[source_length] = 0;
2398 header |= header_mask;
2415 void push(char_t*& s,
size_t count)
2421 memmove(
end -
size,
end,
reinterpret_cast<char*
>(s) -
reinterpret_cast<char*
>(
end));
2438 memmove(
end -
size,
end,
reinterpret_cast<char*
>(s) -
reinterpret_cast<char*
>(
end));
2448 char_t* stre = s + 1;
2454 unsigned int ucsc = 0;
2462 if (ch ==
';')
return stre;
2466 if (
static_cast<unsigned int>(ch -
'0') <= 9)
2467 ucsc = 16 * ucsc + (ch -
'0');
2468 else if (
static_cast<unsigned int>((ch |
' ') -
'a') <= 5)
2469 ucsc = 16 * ucsc + ((ch |
' ') -
'a' + 10);
2482 char_t ch = *++stre;
2484 if (ch ==
';')
return stre;
2488 if (
static_cast<unsigned int>(ch -
'0') <= 9)
2489 ucsc = 10 * ucsc + (ch -
'0');
2501 #ifdef PUGIXML_WCHAR_MODE
2502 s =
reinterpret_cast<char_t*
>(wchar_writer::any(
reinterpret_cast<wchar_writer::value_type
>(s), ucsc));
2504 s =
reinterpret_cast<char_t*
>(
utf8_writer::any(
reinterpret_cast<uint8_t*
>(s), ucsc));
2507 g.
push(s, stre - s);
2517 if (*++stre ==
'p' && *++stre ==
';')
2522 g.
push(s, stre - s);
2526 else if (*stre ==
'p')
2528 if (*++stre ==
'o' && *++stre ==
's' && *++stre ==
';')
2533 g.
push(s, stre - s);
2542 if (*++stre ==
't' && *++stre ==
';')
2547 g.
push(s, stre - s);
2555 if (*++stre ==
't' && *++stre ==
';')
2560 g.
push(s, stre - s);
2568 if (*++stre ==
'u' && *++stre ==
'o' && *++stre ==
't' && *++stre ==
';')
2573 g.
push(s, stre - s);
2587 #define PUGI__ENDSWITH(c, e) ((c) == (e) || ((c) == 0 && endch == (e)))
2588 #define PUGI__SKIPWS() { while (PUGI__IS_CHARTYPE(*s, ct_space)) ++s; }
2589 #define PUGI__OPTSET(OPT) ( optmsk & (OPT) )
2590 #define PUGI__PUSHNODE(TYPE) { cursor = append_new_node(cursor, *alloc, TYPE); if (!cursor) PUGI__THROW_ERROR(status_out_of_memory, s); }
2591 #define PUGI__POPNODE() { cursor = cursor->parent; }
2592 #define PUGI__SCANFOR(X) { while (*s != 0 && !(X)) ++s; }
2593 #define PUGI__SCANWHILE(X) { while (X) ++s; }
2594 #define PUGI__SCANWHILE_UNROLL(X) { for (;;) { char_t ss = s[0]; if (PUGI__UNLIKELY(!(X))) { break; } ss = s[1]; if (PUGI__UNLIKELY(!(X))) { s += 1; break; } ss = s[2]; if (PUGI__UNLIKELY(!(X))) { s += 2; break; } ss = s[3]; if (PUGI__UNLIKELY(!(X))) { s += 3; break; } s += 4; } }
2595 #define PUGI__ENDSEG() { ch = *s; *s = 0; ++s; }
2596 #define PUGI__THROW_ERROR(err, m) return error_offset = m, error_status = err, static_cast<char_t*>(0)
2597 #define PUGI__CHECK_ERROR(err, m) { if (*s == 0) PUGI__THROW_ERROR(err, m); }
2611 if (*s ==
'\n') g.
push(s, 1);
2613 else if (s[0] ==
'-' && s[1] ==
'-' &&
PUGI__ENDSWITH(s[2],
'>'))
2617 return s + (s[2] ==
'>' ? 3 : 2);
2639 if (*s ==
'\n') g.
push(s, 1);
2641 else if (s[0] ==
']' && s[1] ==
']' &&
PUGI__ENDSWITH(s[2],
'>'))
2655 typedef char_t* (*strconv_pcdata_t)(char_t*);
2671 char_t* end = g.
flush(s);
2673 if (opt_trim::value)
2681 else if (opt_eol::value && *s ==
'\r')
2685 if (*s ==
'\n') g.
push(s, 1);
2687 else if (opt_escape::value && *s ==
'&')
2693 char_t* end = g.
flush(s);
2695 if (opt_trim::value)
2710 PUGI__STATIC_ASSERT(parse_escapes == 0x10 && parse_eol == 0x20 && parse_trim_pcdata == 0x0800);
2712 switch (((optmask >> 4) & 3) | ((optmask >> 9) & 4))
2722 default: assert(
false);
return 0;
2726 typedef char_t* (*strconv_attribute_t)(char_t*, char_t);
2749 if (*s == end_quote)
2751 char_t* str = g.
flush(s);
2764 char_t* str = s + 1;
2770 else if (opt_escape::value && *s ==
'&')
2790 if (*s == end_quote)
2802 if (*s ==
'\n') g.
push(s, 1);
2806 else if (opt_escape::value && *s ==
'&')
2826 if (*s == end_quote)
2832 else if (*s ==
'\r')
2836 if (*s ==
'\n') g.
push(s, 1);
2838 else if (opt_escape::value && *s ==
'&')
2858 if (*s == end_quote)
2864 else if (opt_escape::value && *s ==
'&')
2879 PUGI__STATIC_ASSERT(parse_escapes == 0x10 && parse_eol == 0x20 && parse_wconv_attribute == 0x40 && parse_wnorm_attribute == 0x80);
2881 switch ((optmask >> 4) & 15)
2899 default: assert(
false);
return 0;
2905 xml_parse_result result;
2906 result.status = status;
2907 result.offset = offset;
2931 if (*s ==
'"' || *s ==
'\'')
2940 else if (s[0] ==
'<' && s[1] ==
'?')
2949 else if (s[0] ==
'<' && s[1] ==
'!' && s[2] ==
'-' && s[3] ==
'-')
2966 assert(s[0] ==
'<' && s[1] ==
'!' && s[2] ==
'[');
2971 if (s[0] ==
'<' && s[1] ==
'!' && s[2] ==
'[')
2977 else if (s[0] ==
']' && s[1] ==
']' && s[2] ==
'>')
2997 assert((s[0] ==
'<' || s[0] == 0) && s[1] ==
'!');
3002 if (s[0] ==
'<' && s[1] ==
'!' && s[2] !=
'-')
3017 else if (s[0] ==
'<' || s[0] ==
'"' || s[0] ==
'\'')
3073 s += (s[2] ==
'>' ? 3 : 2);
3081 if (*++s==
'C' && *++s==
'D' && *++s==
'A' && *++s==
'T' && *++s==
'A' && *++s ==
'[')
3114 s += (s[1] ==
'>' ? 2 : 1);
3118 else if (s[0] ==
'D' && s[1] ==
'O' && s[2] ==
'C' && s[3] ==
'T' && s[4] ==
'Y' && s[5] ==
'P' &&
PUGI__ENDSWITH(s[6],
'E'))
3124 char_t* mark = s + 9;
3129 assert((*s == 0 && endch ==
'>') || *s ==
'>');
3138 cursor->value = mark;
3148 char_t*
parse_question(char_t* s, xml_node_struct*& ref_cursor,
unsigned int optmsk, char_t endch)
3151 xml_node_struct* cursor = ref_cursor;
3166 bool declaration = (target[0] |
' ') ==
'x' && (target[1] |
' ') ==
'm' && (target[2] |
' ') ==
'l' && target + 3 == s;
3182 cursor->name = target;
3216 cursor->value = value;
3233 s += (s[1] ==
'>' ? 2 : 1);
3237 ref_cursor = cursor;
3242 char_t*
parse_tree(char_t* s, xml_node_struct* root,
unsigned int optmsk, char_t endch)
3248 xml_node_struct* cursor = root;
3300 if (*s ==
'"' || *s ==
'\'')
3306 s = strconv_attribute(s, ch);
3329 else if (*s == 0 && endch ==
'>')
3342 else if (*s == 0 && endch ==
'>')
3374 char_t* name = cursor->name;
3384 if (*s == 0 && name[0] == endch && name[1] == 0)
PUGI__THROW_ERROR(status_bad_end_element, s);
3408 if (
PUGI__NODETYPE(cursor) == node_declaration)
goto LOC_ATTRIBUTES;
3424 if (*s ==
'<' || !*s)
3435 if (s[0] !=
'<' || s[1] !=
'/' || cursor->first_child)
continue;
3444 if (
PUGI__OPTSET(parse_embed_pcdata) && cursor->parent && !cursor->first_child && !cursor->value)
3457 s = strconv_pcdata(s);
3480 #ifdef PUGIXML_WCHAR_MODE
3483 unsigned int bom = 0xfeff;
3484 return (s[0] ==
static_cast<wchar_t>(bom)) ? s + 1 : s;
3489 return (s[0] ==
'\xef' && s[1] ==
'\xbb' && s[2] ==
'\xbf') ? s + 3 : s;
3499 node = node->next_sibling;
3512 xml_node_struct* last_root_child = root->first_child ? root->first_child->prev_sibling_c + 0 : 0;
3518 char_t endch = buffer[length - 1];
3519 buffer[length - 1] = 0;
3525 parser.
parse_tree(buffer_data, root, optmsk, endch);
3528 assert(result.offset >= 0 &&
static_cast<size_t>(result.offset) <= length);
3537 xml_node_struct* first_root_child_parsed = last_root_child ? last_root_child->next_sibling + 0 : root->first_child+ 0;
3545 if (result.offset > 0 &&
static_cast<size_t>(result.offset) == length - 1 && endch == 0)
3556 #ifdef PUGIXML_WCHAR_MODE
3559 return encoding_utf8;
3569 if (encoding == encoding_utf16)
return is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
3572 if (encoding == encoding_utf32)
return is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
3575 if (encoding != encoding_auto)
return encoding;
3578 return encoding_utf8;
3585 typename T::value_type end = D::process(
reinterpret_cast<const typename D::type*
>(data), length, dest, T());
3587 return static_cast<size_t>(end - dest) *
sizeof(*dest);
3594 typename T::value_type end = D::process(
reinterpret_cast<const typename D::type*
>(data), length, dest, T());
3598 for (
typename T::value_type i = dest; i != end; ++i)
3602 return static_cast<size_t>(end - dest) *
sizeof(*dest);
3605#ifdef PUGIXML_WCHAR_MODE
3608 if (length < 1)
return 0;
3611 return (
sizeof(
wchar_t) == 2 &&
static_cast<unsigned int>(
static_cast<uint16_t
>(data[length - 1]) - 0xD800) < 0x400) ? length - 1 : length;
3614 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)
3619 convert_wchar_endian_swap(r_char, data, length);
3621 return length *
sizeof(
char_t);
3625 if (encoding == encoding_utf8)
3629 if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)
3637 if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)
3645 if (encoding == encoding_latin1)
3648 assert(
false &&
"Invalid encoding");
3654 if (length < 5)
return 0;
3656 for (
size_t i = 1; i <= 4; ++i)
3658 uint8_t ch =
static_cast<uint8_t
>(data[length - i]);
3661 if ((ch & 0xc0) != 0x80)
return length - i;
3670 if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)
3672 xml_encoding native_encoding =
is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
3677 if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)
3679 xml_encoding native_encoding =
is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
3684 if (encoding == encoding_latin1)
3687 assert(
false &&
"Invalid encoding");
3692 class xml_buffered_writer
3694 xml_buffered_writer(
const xml_buffered_writer&);
3695 xml_buffered_writer& operator=(
const xml_buffered_writer&);
3710 void flush(
const char_t* data,
size_t size)
3712 if (size == 0)
return;
3716 writer.write(data, size *
sizeof(char_t));
3721 assert(result <=
sizeof(
scratch));
3739 writer.write(data, length *
sizeof(char_t));
3752 flush(data, chunk_size);
3756 length -= chunk_size;
3773 memcpy(
buffer + offset, data, length *
sizeof(char_t));
3788 buffer[offset++] = *data++;
3798 size_t length = offset -
bufsize;
3826 void write(char_t d0, char_t d1, char_t d2)
3837 void write(char_t d0, char_t d1, char_t d2, char_t d3)
3849 void write(char_t d0, char_t d1, char_t d2, char_t d3, char_t d4)
3862 void write(char_t d0, char_t d1, char_t d2, char_t d3, char_t d4, char_t d5)
3882 #ifdef PUGIXML_MEMORY_OUTPUT_STACK
3883 PUGIXML_MEMORY_OUTPUT_STACK
3910 const char_t* prev = s;
3915 writer.write_buffer(prev,
static_cast<size_t>(s - prev));
3921 writer.write(
'&',
'a',
'm',
'p',
';');
3925 writer.write(
'&',
'l',
't',
';');
3929 writer.write(
'&',
'g',
't',
';');
3933 if (flags & format_attribute_single_quote)
3936 writer.write(
'&',
'q',
'u',
'o',
't',
';');
3940 if (flags & format_attribute_single_quote)
3941 writer.write(
'&',
'a',
'p',
'o',
's',
';');
3948 unsigned int ch =
static_cast<unsigned int>(*s++);
3951 if (!(flags & format_skip_control_chars))
3952 writer.write(
'&',
'#',
static_cast<char_t
>((ch / 10) +
'0'),
static_cast<char_t
>((ch % 10) +
'0'),
';');
3960 if (flags & format_no_escapes)
3970 writer.write(
'<',
'!',
'[',
'C',
'D');
3971 writer.write(
'A',
'T',
'A',
'[');
3973 const char_t* prev = s;
3976 while (*s && !(s[0] ==
']' && s[1] ==
']' && s[2] ==
'>')) ++s;
3981 writer.write_buffer(prev,
static_cast<size_t>(s - prev));
3983 writer.write(
']',
']',
'>');
3990 switch (indent_length)
3994 for (
unsigned int i = 0; i < depth; ++i)
4001 for (
unsigned int i = 0; i < depth; ++i)
4002 writer.write(indent[0], indent[1]);
4008 for (
unsigned int i = 0; i < depth; ++i)
4009 writer.write(indent[0], indent[1], indent[2]);
4015 for (
unsigned int i = 0; i < depth; ++i)
4016 writer.write(indent[0], indent[1], indent[2], indent[3]);
4022 for (
unsigned int i = 0; i < depth; ++i)
4023 writer.write_buffer(indent, indent_length);
4030 writer.write(
'<',
'!',
'-',
'-');
4034 const char_t* prev = s;
4037 while (*s && !(s[0] ==
'-' && (s[1] ==
'-' || s[1] == 0))) ++s;
4039 writer.write_buffer(prev,
static_cast<size_t>(s - prev));
4050 writer.write(
'-',
'-',
'>');
4057 const char_t* prev = s;
4060 while (*s && !(s[0] ==
'?' && s[1] ==
'>')) ++s;
4062 writer.write_buffer(prev,
static_cast<size_t>(s - prev));
4066 assert(s[0] ==
'?' && s[1] ==
'>');
4068 writer.write(
'?',
' ',
'>');
4076 const char_t* default_name =
PUGIXML_TEXT(
":anonymous");
4077 const char_t enquotation_char = (flags & format_attribute_single_quote) ?
'\'' :
'"';
4079 for (xml_attribute_struct* a = node->first_attribute; a; a = a->next_attribute)
4081 if ((flags & (format_indent_attributes | format_raw)) == format_indent_attributes)
4092 writer.write_string(a->name ? a->name + 0 : default_name);
4093 writer.write(
'=', enquotation_char);
4098 writer.write(enquotation_char);
4104 const char_t* default_name =
PUGIXML_TEXT(
":anonymous");
4105 const char_t* name = node->name ? node->name + 0 : default_name;
4108 writer.write_string(name);
4110 if (node->first_attribute)
4116 if (!node->first_child)
4118 if (flags & format_no_empty_element_tags)
4120 writer.write(
'>',
'<',
'/');
4121 writer.write_string(name);
4128 if ((flags & format_raw) == 0)
4149 if (!node->first_child)
4152 writer.write_string(name);
4166 const char_t* default_name =
PUGIXML_TEXT(
":anonymous");
4167 const char_t* name = node->name ? node->name + 0 : default_name;
4170 writer.write_string(name);
4176 const char_t* default_name =
PUGIXML_TEXT(
":anonymous");
4194 writer.write_string(node->name ? node->name + 0 : default_name);
4205 case node_declaration:
4207 writer.write_string(node->name ? node->name + 0 : default_name);
4213 writer.write(
'<',
'!',
'D',
'O',
'C');
4214 writer.write(
'T',
'Y',
'P',
'E');
4219 writer.write_string(node->value);
4226 assert(
false &&
"Invalid node type");
4238 size_t indent_length = ((flags & (format_indent | format_indent_attributes)) && (flags & format_raw) == 0) ?
strlength(indent) : 0;
4241 xml_node_struct* node = root;
4256 if ((indent_flags &
indent_newline) && (flags & format_raw) == 0)
4272 node = node->first_child;
4281 if (node->first_child)
4283 node = node->first_child;
4296 while (node != root)
4298 if (node->next_sibling)
4300 node = node->next_sibling;
4304 node = node->parent;
4311 if ((indent_flags &
indent_newline) && (flags & format_raw) == 0)
4323 while (node != root);
4325 if ((indent_flags &
indent_newline) && (flags & format_raw) == 0)
4331 for (xml_node_struct* child = node->first_child; child; child = child->next_sibling)
4335 if (type == node_declaration)
return true;
4336 if (type == node_element)
return false;
4344 for (xml_attribute_struct* a = node->first_attribute; a; a = a->next_attribute)
4353 return parent == node_element || parent == node_declaration;
4358 if (parent != node_document && parent != node_element)
return false;
4359 if (child == node_document || child == node_null)
return false;
4360 if (parent != node_document && (child == node_declaration || child == node_doctype))
return false;
4372 if (parent.root() != child.root())
4376 xml_node cur = parent;
4389 template <
typename String,
typename Header>
4392 assert(!dest && (header & header_mask) == 0);
4396 if (alloc && (source_header & header_mask) == 0)
4401 header |= xml_memory_page_contents_shared_mask;
4402 source_header |= xml_memory_page_contents_shared_mask;
4411 node_copy_string(dn->name, dn->header, xml_memory_page_name_allocated_mask, sn->name, sn->header, shared_alloc);
4412 node_copy_string(dn->value, dn->header, xml_memory_page_value_allocated_mask, sn->value, sn->header, shared_alloc);
4414 for (xml_attribute_struct* sa = sn->first_attribute; sa; sa = sa->next_attribute)
4420 node_copy_string(da->name, da->header, xml_memory_page_name_allocated_mask, sa->name, sa->header, shared_alloc);
4421 node_copy_string(da->value, da->header, xml_memory_page_value_allocated_mask, sa->value, sa->header, shared_alloc);
4433 xml_node_struct* dit = dn;
4434 xml_node_struct* sit = sn->first_child;
4436 while (sit && sit != sn)
4450 if (sit->first_child)
4453 sit = sit->first_child;
4462 if (sit->next_sibling)
4464 sit = sit->next_sibling;
4472 assert(sit == sn || dit);
4477 assert(!sit || dit == dn->parent);
4485 node_copy_string(da->name, da->header, xml_memory_page_name_allocated_mask, sa->name, sa->header, shared_alloc);
4486 node_copy_string(da->value, da->header, xml_memory_page_value_allocated_mask, sa->value, sa->header, shared_alloc);
4493 return type == node_pcdata || type == node_cdata;
4500 const char_t* s = value;
4505 bool negative = (*s ==
'-');
4507 s += (*s ==
'+' || *s ==
'-');
4509 bool overflow =
false;
4511 if (s[0] ==
'0' && (s[1] |
' ') ==
'x')
4519 const char_t* start = s;
4523 if (
static_cast<unsigned>(*s -
'0') < 10)
4524 result = result * 16 + (*s -
'0');
4525 else if (
static_cast<unsigned>((*s |
' ') -
'a') < 6)
4526 result = result * 16 + ((*s |
' ') -
'a' + 10);
4533 size_t digits =
static_cast<size_t>(s - start);
4535 overflow = digits >
sizeof(U) * 2;
4543 const char_t* start = s;
4547 if (
static_cast<unsigned>(*s -
'0') < 10)
4548 result = result * 10 + (*s -
'0');
4555 size_t digits =
static_cast<size_t>(s - start);
4559 const size_t max_digits10 =
sizeof(U) == 8 ? 20 :
sizeof(U) == 4 ? 10 : 5;
4560 const char_t max_lead =
sizeof(U) == 8 ?
'1' :
sizeof(U) == 4 ?
'4' :
'6';
4561 const size_t high_bit =
sizeof(U) * 8 - 1;
4563 overflow = digits >= max_digits10 && !(digits == max_digits10 && (*start < max_lead || (*start == max_lead && result >> high_bit)));
4570 return (overflow || result > ~minv + 1) ? minv : ~result + 1;
4572 return (overflow || result > 0 - minv) ? minv : 0 - result;
4576 return (overflow || result > maxv) ? maxv : result;
4591 #ifdef PUGIXML_WCHAR_MODE
4592 return wcstod(value, 0);
4594 return strtod(value, 0);
4600 #ifdef PUGIXML_WCHAR_MODE
4601 return static_cast<float>(wcstod(value, 0));
4603 return static_cast<float>(strtod(value, 0));
4610 char_t first = *value;
4613 return (first ==
'1' || first ==
't' || first ==
'T' || first ==
'y' || first ==
'Y');
4616#ifdef PUGIXML_HAS_LONG_LONG
4617 PUGI__FN long long get_value_llong(
const char_t* value)
4622 PUGI__FN unsigned long long get_value_ullong(
const char_t* value)
4630 char_t* result = end - 1;
4631 U rest = negative ? 0 - value : value;
4635 *result-- =
static_cast<char_t
>(
'0' + (rest % 10));
4640 assert(result >= begin);
4645 return result + !negative;
4649 template <
typename String,
typename Header>
4652 #ifdef PUGIXML_WCHAR_MODE
4654 assert(strlen(buf) <
sizeof(wbuf) /
sizeof(wbuf[0]));
4657 for (; buf[offset]; ++offset) wbuf[offset] = buf[offset];
4659 return strcpy_insitu(dest, header, header_mask, wbuf, offset);
4661 return strcpy_insitu(dest, header, header_mask, buf, strlen(buf));
4665 template <
typename U,
typename String,
typename Header>
4669 char_t* end = buf +
sizeof(buf) /
sizeof(buf[0]);
4672 return strcpy_insitu(dest, header, header_mask, begin, end - begin);
4675 template <
typename String,
typename Header>
4684 template <
typename String,
typename Header>
4693 template <
typename String,
typename Header>
4705 xml_encoding buffer_encoding = impl::get_buffer_encoding(encoding, contents, size);
4712 if (!impl::convert_buffer(buffer, length, buffer_encoding, contents, size, is_mutable))
return impl::make_parse_result(status_out_of_memory);
4715 if (own && buffer != contents && contents) impl::xml_memory::deallocate(contents);
4718 if (own || buffer != contents) *out_buffer = buffer;
4724 xml_parse_result res = impl::xml_parser::parse(buffer, length, doc, root, options);
4727 res.encoding = buffer_encoding;
4735 #if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 && !defined(_WIN32_WCE)
4737 typedef __int64 length_type;
4739 _fseeki64(file, 0, SEEK_END);
4740 length_type length = _ftelli64(file);
4741 _fseeki64(file, 0, SEEK_SET);
4742 #elif defined(__MINGW32__) && !defined(__NO_MINGW_LFS) && (!defined(__STRICT_ANSI__) || defined(__MINGW64_VERSION_MAJOR))
4744 typedef off64_t length_type;
4746 fseeko64(file, 0, SEEK_END);
4747 length_type length = ftello64(file);
4748 fseeko64(file, 0, SEEK_SET);
4751 typedef long length_type;
4753 fseek(file, 0, SEEK_END);
4754 length_type length = ftell(file);
4755 fseek(file, 0, SEEK_SET);
4759 if (length < 0)
return status_io_error;
4762 size_t result =
static_cast<size_t>(length);
4764 if (
static_cast<length_type
>(result) != length)
return status_out_of_memory;
4767 out_result = result;
4776 #ifdef PUGIXML_WCHAR_MODE
4779 if (encoding == wchar_encoding || need_endian_swap_utf(encoding, wchar_encoding))
4781 size_t length = size /
sizeof(char_t);
4783 static_cast<char_t*
>(buffer)[length] = 0;
4784 return (length + 1) *
sizeof(char_t);
4787 if (encoding == encoding_utf8)
4789 static_cast<char*
>(buffer)[size] = 0;
4806 size_t max_suffix_size =
sizeof(char_t);
4813 size_t read_size = fread(contents, 1, size, file);
4815 if (read_size != size)
4823 return load_buffer_impl(doc, doc, contents,
zero_terminate_buffer(contents, size, real_encoding), options, real_encoding,
true,
true, out_buffer);
4831#ifndef PUGIXML_NO_STL
4837 if (!memory)
return 0;
4862 T
data[xml_memory_page_size /
sizeof(T)];
4873 while (!stream.eof())
4877 if (!chunk)
return status_out_of_memory;
4880 if (last) last = last->next = chunk;
4881 else chunks.
data = last = chunk;
4884 stream.read(chunk->
data,
static_cast<std::streamsize
>(
sizeof(chunk->
data) /
sizeof(T)));
4885 chunk->
size =
static_cast<size_t>(stream.gcount()) *
sizeof(T);
4888 if (stream.bad() || (!stream.eof() && stream.fail()))
return status_io_error;
4891 if (total + chunk->
size < total)
return status_out_of_memory;
4892 total += chunk->
size;
4895 size_t max_suffix_size =
sizeof(char_t);
4899 if (!buffer)
return status_out_of_memory;
4901 char* write = buffer;
4905 assert(write + chunk->size <= buffer + total);
4906 memcpy(write, chunk->data, chunk->size);
4907 write += chunk->size;
4910 assert(write == buffer + total);
4913 *out_buffer = buffer;
4922 typename std::basic_istream<T>::pos_type pos = stream.tellg();
4923 stream.seekg(0, std::ios::end);
4924 std::streamoff length = stream.tellg() - pos;
4927 if (stream.fail() || pos < 0)
return status_io_error;
4930 size_t read_length =
static_cast<size_t>(length);
4932 if (
static_cast<std::streamsize
>(read_length) != length || length < 0)
return status_out_of_memory;
4934 size_t max_suffix_size =
sizeof(char_t);
4938 if (!buffer.
data)
return status_out_of_memory;
4940 stream.read(
static_cast<T*
>(buffer.
data),
static_cast<std::streamsize
>(read_length));
4943 if (stream.bad() || (!stream.eof() && stream.fail()))
return status_io_error;
4946 size_t actual_length =
static_cast<size_t>(stream.gcount());
4947 assert(actual_length <= read_length);
4949 *out_buffer = buffer.
release();
4950 *out_size = actual_length *
sizeof(T);
4959 xml_parse_status status = status_ok;
4965 if (stream.tellg() < 0)
4977 return load_buffer_impl(doc, doc, buffer,
zero_terminate_buffer(buffer, size, real_encoding), options, real_encoding,
true,
true, out_buffer);
4981#if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__) || (defined(__MINGW32__) && (!defined(__STRICT_ANSI__) || defined(__MINGW64_VERSION_MAJOR)))
4984#if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400
4986 return _wfopen_s(&file, path, mode) == 0 ? file : 0;
4988 return _wfopen(path, mode);
5002 if (!result)
return 0;
5017 if (!path_utf8)
return 0;
5020 char mode_ascii[4] = {0};
5021 for (
size_t i = 0; mode[i]; ++i) mode_ascii[i] =
static_cast<char>(mode[i]);
5024 FILE* result = fopen(path_utf8, mode_ascii);
5035#if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400
5037 return fopen_s(&file, path, mode) == 0 ? file : 0;
5039 return fopen(path, mode);
5043 PUGI__FN bool save_file_impl(
const xml_document& doc, FILE* file,
const char_t* indent,
unsigned int flags, xml_encoding encoding)
5045 if (!file)
return false;
5047 xml_writer_file
writer(file);
5048 doc.save(
writer, indent, flags, encoding);
5050 return ferror(file) == 0;
5078 size_t result = fwrite(data, 1, size,
static_cast<FILE*
>(file));
5082#ifndef PUGIXML_NO_STL
5095 assert(!wide_stream);
5096 narrow_stream->write(
reinterpret_cast<const char*
>(data),
static_cast<std::streamsize
>(size));
5100 assert(wide_stream);
5101 assert(size %
sizeof(
wchar_t) == 0);
5103 wide_stream->write(
reinterpret_cast<const wchar_t*
>(data),
static_cast<std::streamsize
>(size /
sizeof(
wchar_t)));
5143 PUGI__FN xml_attribute::operator xml_attribute::unspecified_bool_type()
const
5145 return _attr ? unspecified_bool_xml_attribute : 0;
5155 return (_attr == r._attr);
5160 return (_attr != r._attr);
5165 return (_attr < r._attr);
5170 return (_attr > r._attr);
5175 return (_attr <= r._attr);
5180 return (_attr >= r._attr);
5195 return (_attr && _attr->value) ? _attr->value + 0 : def;
5200 return (_attr && _attr->value) ? impl::get_value_int(_attr->value) : def;
5205 return (_attr && _attr->value) ? impl::get_value_uint(_attr->value) : def;
5210 return (_attr && _attr->value) ? impl::get_value_double(_attr->value) : def;
5215 return (_attr && _attr->value) ? impl::get_value_float(_attr->value) : def;
5220 return (_attr && _attr->value) ? impl::get_value_bool(_attr->value) : def;
5223#ifdef PUGIXML_HAS_LONG_LONG
5224 PUGI__FN long long xml_attribute::as_llong(
long long def)
const
5226 return (_attr && _attr->
value) ? impl::get_value_llong(_attr->
value) : def;
5229 PUGI__FN unsigned long long xml_attribute::as_ullong(
unsigned long long def)
const
5231 return (_attr && _attr->
value) ? impl::get_value_ullong(_attr->
value) : def;
5242 return (_attr && _attr->name) ? _attr->name + 0 :
PUGIXML_TEXT(
"");
5247 return (_attr && _attr->value) ? _attr->value + 0 :
PUGIXML_TEXT(
"");
5308#ifdef PUGIXML_HAS_LONG_LONG
5324 if (!_attr)
return false;
5326 return impl::strcpy_insitu(_attr->name, _attr->header, impl::xml_memory_page_name_allocated_mask, rhs, impl::strlength(rhs));
5331 if (!_attr)
return false;
5333 return impl::strcpy_insitu(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, impl::strlength(rhs));
5338 if (!_attr)
return false;
5340 return impl::set_value_integer<unsigned int>(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, rhs < 0);
5345 if (!_attr)
return false;
5347 return impl::set_value_integer<unsigned int>(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs,
false);
5352 if (!_attr)
return false;
5354 return impl::set_value_integer<unsigned long>(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, rhs < 0);
5359 if (!_attr)
return false;
5361 return impl::set_value_integer<unsigned long>(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs,
false);
5366 if (!_attr)
return false;
5368 return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs,
default_double_precision);
5373 if (!_attr)
return false;
5375 return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, precision);
5380 if (!_attr)
return false;
5382 return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs,
default_float_precision);
5387 if (!_attr)
return false;
5389 return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, precision);
5394 if (!_attr)
return false;
5396 return impl::set_value_bool(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
5399#ifdef PUGIXML_HAS_LONG_LONG
5402 if (!_attr)
return false;
5404 return impl::set_value_integer<unsigned long long>(_attr->
value, _attr->
header, impl::xml_memory_page_value_allocated_mask, rhs, rhs < 0);
5409 if (!_attr)
return false;
5411 return impl::set_value_integer<unsigned long long>(_attr->
value, _attr->
header, impl::xml_memory_page_value_allocated_mask, rhs,
false);
5418 return (
bool)lhs && rhs;
5423 return (
bool)lhs || rhs;
5441 return _root ? unspecified_bool_xml_node : 0;
5539 if (i->name && impl::strequal(name_, i->name))
return xml_node(i);
5549 if (i->name && impl::strequal(name_, i->name))
5560 if (i->name && impl::strequal(name_, i->name))
return xml_node(i);
5575 if (i->name && impl::strequal(name_, i->name))
return xml_node(i);
5585 assert(!hint || (
_root && impl::is_attribute_of(hint,
_root)));
5591 if (i->name && impl::strequal(name_, i->name))
5602 if (j->name && impl::strequal(name_, j->name))
5642 return _root->value;
5645 if (impl::is_text_node(i) && i->value)
5683 return impl::strcpy_insitu(
_root->name,
_root->header, impl::xml_memory_page_name_allocated_mask, rhs, impl::strlength(rhs));
5693 return impl::strcpy_insitu(
_root->value,
_root->header, impl::xml_memory_page_value_allocated_mask, rhs, impl::strlength(rhs));
5700 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5706 impl::append_attribute(a._attr,
_root);
5717 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5723 impl::prepend_attribute(a._attr,
_root);
5735 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5741 impl::insert_attribute_after(a._attr, attr._attr,
_root);
5753 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5759 impl::insert_attribute_before(a._attr, attr._attr,
_root);
5771 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5777 impl::append_attribute(a._attr,
_root);
5778 impl::node_copy_attribute(a._attr, proto._attr);
5788 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5794 impl::prepend_attribute(a._attr,
_root);
5795 impl::node_copy_attribute(a._attr, proto._attr);
5806 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5812 impl::insert_attribute_after(a._attr, attr._attr,
_root);
5813 impl::node_copy_attribute(a._attr, proto._attr);
5824 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5830 impl::insert_attribute_before(a._attr, attr._attr,
_root);
5831 impl::node_copy_attribute(a._attr, proto._attr);
5838 if (!impl::allow_insert_child(
type(), type_))
return xml_node();
5840 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5841 if (!alloc.reserve())
return xml_node();
5843 xml_node n(impl::allocate_node(alloc, type_));
5855 if (!impl::allow_insert_child(
type(), type_))
return xml_node();
5857 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5858 if (!alloc.reserve())
return xml_node();
5860 xml_node n(impl::allocate_node(alloc, type_));
5872 if (!impl::allow_insert_child(
type(), type_))
return xml_node();
5875 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5876 if (!alloc.reserve())
return xml_node();
5878 xml_node n(impl::allocate_node(alloc, type_));
5881 impl::insert_node_before(n.
_root, node.
_root);
5890 if (!impl::allow_insert_child(
type(), type_))
return xml_node();
5893 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5894 if (!alloc.reserve())
return xml_node();
5896 xml_node n(impl::allocate_node(alloc, type_));
5899 impl::insert_node_after(n.
_root, node.
_root);
5945 if (!impl::allow_insert_child(
type(), type_))
return xml_node();
5947 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5948 if (!alloc.reserve())
return xml_node();
5950 xml_node n(impl::allocate_node(alloc, type_));
5962 if (!impl::allow_insert_child(
type(), type_))
return xml_node();
5964 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5965 if (!alloc.reserve())
return xml_node();
5967 xml_node n(impl::allocate_node(alloc, type_));
5979 if (!impl::allow_insert_child(
type(), type_))
return xml_node();
5982 impl::xml_allocator& alloc = impl::get_allocator(
_root);
5983 if (!alloc.reserve())
return xml_node();
5985 xml_node n(impl::allocate_node(alloc, type_));
5988 impl::insert_node_after(n.
_root, node.
_root);
5997 if (!impl::allow_insert_child(
type(), type_))
return xml_node();
6000 impl::xml_allocator& alloc = impl::get_allocator(
_root);
6001 if (!alloc.reserve())
return xml_node();
6003 xml_node n(impl::allocate_node(alloc, type_));
6006 impl::insert_node_before(n.
_root, node.
_root);
6014 if (!impl::allow_move(*
this, moved))
return xml_node();
6016 impl::xml_allocator& alloc = impl::get_allocator(
_root);
6017 if (!alloc.reserve())
return xml_node();
6020 impl::get_document(
_root).header |= impl::xml_memory_page_contents_shared_mask;
6022 impl::remove_node(moved.
_root);
6030 if (!impl::allow_move(*
this, moved))
return xml_node();
6032 impl::xml_allocator& alloc = impl::get_allocator(
_root);
6033 if (!alloc.reserve())
return xml_node();
6036 impl::get_document(
_root).header |= impl::xml_memory_page_contents_shared_mask;
6038 impl::remove_node(moved.
_root);
6046 if (!impl::allow_move(*
this, moved))
return xml_node();
6050 impl::xml_allocator& alloc = impl::get_allocator(
_root);
6051 if (!alloc.reserve())
return xml_node();
6054 impl::get_document(
_root).header |= impl::xml_memory_page_contents_shared_mask;
6056 impl::remove_node(moved.
_root);
6057 impl::insert_node_after(moved.
_root, node.
_root);
6064 if (!impl::allow_move(*
this, moved))
return xml_node();
6068 impl::xml_allocator& alloc = impl::get_allocator(
_root);
6069 if (!alloc.reserve())
return xml_node();
6072 impl::get_document(
_root).header |= impl::xml_memory_page_contents_shared_mask;
6074 impl::remove_node(moved.
_root);
6075 impl::insert_node_before(moved.
_root, node.
_root);
6087 if (!
_root || !a._attr)
return false;
6088 if (!impl::is_attribute_of(a._attr,
_root))
return false;
6090 impl::xml_allocator& alloc = impl::get_allocator(
_root);
6091 if (!alloc.reserve())
return false;
6093 impl::remove_attribute(a._attr,
_root);
6094 impl::destroy_attribute(a._attr, alloc);
6101 if (!
_root)
return false;
6103 impl::xml_allocator& alloc = impl::get_allocator(
_root);
6104 if (!alloc.reserve())
return false;
6110 impl::destroy_attribute(attr, alloc);
6115 _root->first_attribute = 0;
6129 impl::xml_allocator& alloc = impl::get_allocator(
_root);
6130 if (!alloc.reserve())
return false;
6132 impl::remove_node(n.
_root);
6133 impl::destroy_node(n.
_root, alloc);
6140 if (!
_root)
return false;
6142 impl::xml_allocator& alloc = impl::get_allocator(
_root);
6143 if (!alloc.reserve())
return false;
6149 impl::destroy_node(cur, alloc);
6154 _root->first_child = 0;
6165 impl::xml_document_struct* doc = &impl::get_document(
_root);
6168 doc->header |= impl::xml_memory_page_contents_shared_mask;
6171 impl::xml_memory_page* page = 0;
6172 impl::xml_extra_buffer* extra =
static_cast<impl::xml_extra_buffer*
>(doc->allocate_memory(
sizeof(impl::xml_extra_buffer) +
sizeof(
void*), page));
6177 #ifdef PUGIXML_COMPACT
6180 extra =
reinterpret_cast<impl::xml_extra_buffer*
>((
reinterpret_cast<uintptr_t
>(extra) + (
sizeof(
void*) - 1)) & ~(
sizeof(
void*) - 1));
6185 extra->next = doc->extra_buffers;
6186 doc->extra_buffers = extra;
6189 impl::name_null_sentry sentry(
_root);
6191 return impl::load_buffer_impl(doc,
_root,
const_cast<void*
>(contents), size, options, encoding,
false,
false, &extra->buffer);
6199 if (i->name && impl::strequal(name_, i->name))
6202 if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 :
PUGIXML_TEXT(
"")))
6215 if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 :
PUGIXML_TEXT(
"")))
6221#ifndef PUGIXML_NO_STL
6230 offset += (i !=
_root);
6231 offset += i->name ? impl::strlength(i->name) : 0;
6235 result.resize(offset);
6240 result[--offset] = delimiter;
6244 size_t length = impl::strlength(j->name);
6247 memcpy(&result[offset], j->name, length *
sizeof(
char_t));
6251 assert(offset == 0);
6259 xml_node context = path_[0] == delimiter ?
root() : *
this;
6263 const char_t* path_segment = path_;
6265 while (*path_segment == delimiter) ++path_segment;
6267 const char_t* path_segment_end = path_segment;
6269 while (*path_segment_end && *path_segment_end != delimiter) ++path_segment_end;
6271 if (path_segment == path_segment_end)
return context;
6273 const char_t* next_segment = path_segment_end;
6275 while (*next_segment == delimiter) ++next_segment;
6277 if (*path_segment ==
'.' && path_segment + 1 == path_segment_end)
6279 else if (*path_segment ==
'.' && *(path_segment+1) ==
'.' && path_segment + 2 == path_segment_end)
6285 if (j->name && impl::strequalrange(j->name, path_segment,
static_cast<size_t>(path_segment_end - path_segment)))
6287 xml_node subsearch =
xml_node(j).first_element_by_path(next_segment, delimiter);
6289 if (subsearch)
return subsearch;
6302 if (!walker.
begin(arg_begin))
return false;
6313 if (!walker.
for_each(arg_for_each))
6335 while (cur && cur !=
_root);
6338 assert(walker._depth == -1);
6341 return walker.
end(arg_end);
6358 impl::xml_buffered_writer buffered_writer(
writer, encoding);
6360 impl::node_output(buffered_writer,
_root, indent, flags, depth);
6362 buffered_writer.flush();
6365#ifndef PUGIXML_NO_STL
6373 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
6383 if (!
_root)
return -1;
6385 impl::xml_document_struct& doc = impl::get_document(
_root);
6388 if (!doc.buffer || doc.extra_buffers)
return -1;
6398 return _root->name && (
_root->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0 ?
_root->name - doc.buffer : -1;
6404 return _root->value && (
_root->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0 ?
_root->value - doc.buffer : -1;
6407 assert(
false &&
"Invalid node type");
6415 return (
bool)lhs && rhs;
6418 PUGI__FN bool operator||(
const xml_node& lhs,
bool rhs)
6420 return (
bool)lhs || rhs;
6430 if (!_root || impl::is_text_node(_root))
return _root;
6436 for (xml_node_struct* node = _root->first_child; node; node = node->next_sibling)
6437 if (impl::is_text_node(node))
6445 xml_node_struct* d = _data();
6459 PUGI__FN xml_text::operator xml_text::unspecified_bool_type()
const
6461 return _data() ? unspecified_bool_xml_text : 0;
6471 return _data() == 0;
6492 return (d && d->
value) ? impl::get_value_int(d->
value) : def;
6499 return (d && d->
value) ? impl::get_value_uint(d->
value) : def;
6506 return (d && d->
value) ? impl::get_value_double(d->
value) : def;
6513 return (d && d->
value) ? impl::get_value_float(d->
value) : def;
6520 return (d && d->
value) ? impl::get_value_bool(d->
value) : def;
6523#ifdef PUGIXML_HAS_LONG_LONG
6524 PUGI__FN long long xml_text::as_llong(
long long def)
const
6528 return (d && d->
value) ? impl::get_value_llong(d->
value) : def;
6531 PUGI__FN unsigned long long xml_text::as_ullong(
unsigned long long def)
const
6533 xml_node_struct* d = _data();
6535 return (d && d->value) ? impl::get_value_ullong(d->value) : def;
6543 return dn ? impl::strcpy_insitu(dn->
value, dn->
header, impl::xml_memory_page_value_allocated_mask, rhs, impl::strlength(rhs)) :
false;
6550 return dn ? impl::set_value_integer<unsigned int>(dn->
value, dn->
header, impl::xml_memory_page_value_allocated_mask, rhs, rhs < 0) :
false;
6557 return dn ? impl::set_value_integer<unsigned int>(dn->
value, dn->
header, impl::xml_memory_page_value_allocated_mask, rhs,
false) :
false;
6564 return dn ? impl::set_value_integer<unsigned long>(dn->
value, dn->
header, impl::xml_memory_page_value_allocated_mask, rhs, rhs < 0) :
false;
6571 return dn ? impl::set_value_integer<unsigned long>(dn->
value, dn->
header, impl::xml_memory_page_value_allocated_mask, rhs,
false) :
false;
6585 return dn ? impl::set_value_convert(dn->
value, dn->
header, impl::xml_memory_page_value_allocated_mask, rhs, precision) :
false;
6599 return dn ? impl::set_value_convert(dn->
value, dn->
header, impl::xml_memory_page_value_allocated_mask, rhs, precision) :
false;
6606 return dn ? impl::set_value_bool(dn->
value, dn->
header, impl::xml_memory_page_value_allocated_mask, rhs) :
false;
6609#ifdef PUGIXML_HAS_LONG_LONG
6614 return dn ? impl::set_value_integer<unsigned long long>(dn->
value, dn->
header, impl::xml_memory_page_value_allocated_mask, rhs, rhs < 0) : false;
6619 xml_node_struct* dn = _data_new();
6621 return dn ? impl::set_value_integer<unsigned long long>(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs,
false) : false;
6673#ifdef PUGIXML_HAS_LONG_LONG
6695 return (
bool)lhs && rhs;
6698 PUGI__FN bool operator||(
const xml_text& lhs,
bool rhs)
6700 return (
bool)lhs || rhs;
6718 return _wrap._root == rhs._wrap.
_root && _parent._root == rhs._parent.
_root;
6723 return _wrap._root != rhs._wrap.
_root || _parent._root != rhs._parent.
_root;
6728 assert(_wrap._root);
6734 assert(_wrap._root);
6735 return const_cast<xml_node*
>(&_wrap);
6740 assert(_wrap._root);
6741 _wrap._root = _wrap._root->next_sibling;
6747 xml_node_iterator temp = *
this;
6754 _wrap = _wrap._root ? _wrap.previous_sibling() : _parent.last_child();
6760 xml_node_iterator temp = *
this;
6779 return _wrap._attr == rhs._wrap._attr && _parent._root == rhs._parent.
_root;
6784 return _wrap._attr != rhs._wrap._attr || _parent._root != rhs._parent.
_root;
6789 assert(_wrap._attr);
6795 assert(_wrap._attr);
6801 assert(_wrap._attr);
6802 _wrap._attr = _wrap._attr->next_attribute;
6808 xml_attribute_iterator temp = *
this;
6815 _wrap = _wrap._attr ? _wrap.previous_attribute() : _parent.last_attribute();
6821 xml_attribute_iterator temp = *
this;
6840 return _wrap._root == rhs._wrap.
_root && _parent._root == rhs._parent.
_root;
6845 return _wrap._root != rhs._wrap.
_root || _parent._root != rhs._parent.
_root;
6850 assert(_wrap._root);
6856 assert(_wrap._root);
6857 return const_cast<xml_node*
>(&_wrap);
6862 assert(_wrap._root);
6863 _wrap = _wrap.next_sibling(_name);
6877 _wrap = _wrap.previous_sibling(_name);
6880 _wrap = _parent.last_child();
6882 if (!impl::strequal(_wrap.name(), _name))
6883 _wrap = _wrap.previous_sibling(_name);
6918 case status_bad_pi:
return "Error parsing document declaration/processing instruction";
6932 default:
return "Unknown error";
6946#ifdef PUGIXML_HAS_MOVE
6955 if (
this == &rhs)
return *
this;
6978 PUGI__FN void xml_document::_create()
6982 #ifdef PUGIXML_COMPACT
6984 const size_t page_offset =
sizeof(
void*);
6986 const size_t page_offset = 0;
6990 PUGI__STATIC_ASSERT(
sizeof(impl::xml_memory_page) +
sizeof(impl::xml_document_struct) + page_offset <=
sizeof(_memory));
6993 impl::xml_memory_page* page = impl::xml_memory_page::construct(_memory);
6996 page->busy_size = impl::xml_memory_page_size;
6999 #ifdef PUGIXML_COMPACT
7001 page->compact_page_marker =
reinterpret_cast<uint32_t*
>(
static_cast<void*
>(
reinterpret_cast<char*
>(page) +
sizeof(impl::xml_memory_page)));
7002 *page->compact_page_marker =
sizeof(impl::xml_memory_page);
7006 _root =
new (
reinterpret_cast<char*
>(page) +
sizeof(impl::xml_memory_page) + page_offset) impl::xml_document_struct(page);
7010 page->allocator =
static_cast<impl::xml_document_struct*
>(
_root);
7013 #ifdef PUGIXML_COMPACT
7014 page->allocator->_hash = &
static_cast<impl::xml_document_struct*
>(
_root)->hash;
7018 assert(
reinterpret_cast<char*
>(
_root) +
sizeof(impl::xml_document_struct) <= _memory +
sizeof(_memory));
7021 PUGI__FN void xml_document::_destroy()
7028 impl::xml_memory::deallocate(_buffer);
7033 for (impl::xml_extra_buffer* extra =
static_cast<impl::xml_document_struct*
>(
_root)->extra_buffers; extra; extra = extra->next)
7035 if (extra->buffer) impl::xml_memory::deallocate(extra->buffer);
7040 assert(root_page && !root_page->prev);
7041 assert(
reinterpret_cast<char*
>(root_page) >= _memory &&
reinterpret_cast<char*
>(root_page) < _memory +
sizeof(_memory));
7043 for (impl::xml_memory_page* page = root_page->next; page; )
7045 impl::xml_memory_page* next = page->next;
7047 impl::xml_allocator::deallocate_page(page);
7052 #ifdef PUGIXML_COMPACT
7054 static_cast<impl::xml_document_struct*
>(
_root)->hash.clear();
7060#ifdef PUGIXML_HAS_MOVE
7063 impl::xml_document_struct* doc =
static_cast<impl::xml_document_struct*
>(_root);
7064 impl::xml_document_struct* other =
static_cast<impl::xml_document_struct*
>(rhs._root);
7069 #ifdef PUGIXML_COMPACT
7072 if (other_first_child)
7074 size_t other_children = 0;
7082 if (!other->_hash->reserve(other_children + 1))
7084 #ifdef PUGIXML_NO_EXCEPTIONS
7087 throw std::bad_alloc();
7094 doc->_root = other->_root;
7095 doc->_busy_size = other->_busy_size;
7098 doc->buffer = other->buffer;
7099 doc->extra_buffers = other->extra_buffers;
7100 _buffer = rhs._buffer;
7102 #ifdef PUGIXML_COMPACT
7104 doc->hash = other->hash;
7105 doc->_hash = &doc->hash;
7113 assert(doc_page && !doc_page->prev && !doc_page->next);
7116 assert(other_page && !other_page->prev);
7119 if (impl::xml_memory_page* page = other_page->next)
7121 assert(page->prev == other_page);
7123 page->prev = doc_page;
7125 doc_page->next = page;
7126 other_page->next = 0;
7130 for (impl::xml_memory_page* page = doc_page->next; page; page = page->next)
7132 assert(page->allocator == other);
7134 page->allocator = doc;
7136 #ifdef PUGIXML_COMPACT
7138 if (page->compact_shared_parent == other)
7139 page->compact_shared_parent = doc;
7144 assert(!doc->first_child);
7146 doc->first_child = other_first_child;
7150 #ifdef PUGIXML_COMPACT
7152 assert(node->parent == other || node->parent == doc);
7156 assert(node->parent == other);
7162 new (other) impl::xml_document_struct(
PUGI__GETPAGE(other));
7167#ifndef PUGIXML_NO_STL
7172 return impl::load_stream_impl(
static_cast<impl::xml_document_struct*
>(
_root), stream, options, encoding, &_buffer);
7179 return impl::load_stream_impl(
static_cast<impl::xml_document_struct*
>(
_root), stream, options,
encoding_wchar, &_buffer);
7186 #ifdef PUGIXML_WCHAR_MODE
7192 return load_buffer(contents, impl::strlength(contents) *
sizeof(
char_t), options, encoding);
7204 using impl::auto_deleter;
7207 return impl::load_file_impl(
static_cast<impl::xml_document_struct*
>(
_root), file.
data, options, encoding, &_buffer);
7214 using impl::auto_deleter;
7217 return impl::load_file_impl(
static_cast<impl::xml_document_struct*
>(
_root), file.
data, options, encoding, &_buffer);
7224 return impl::load_buffer_impl(
static_cast<impl::xml_document_struct*
>(
_root),
_root,
const_cast<void*
>(contents), size, options, encoding,
false,
false, &_buffer);
7231 return impl::load_buffer_impl(
static_cast<impl::xml_document_struct*
>(
_root),
_root, contents, size, options, encoding,
true,
false, &_buffer);
7238 return impl::load_buffer_impl(
static_cast<impl::xml_document_struct*
>(
_root),
_root, contents, size, options, encoding,
true,
true, &_buffer);
7243 impl::xml_buffered_writer buffered_writer(
writer, encoding);
7248 #ifdef PUGIXML_WCHAR_MODE
7249 unsigned int bom = 0xfeff;
7250 buffered_writer.write(
static_cast<wchar_t>(bom));
7252 buffered_writer.write(
'\xef',
'\xbb',
'\xbf');
7258 buffered_writer.write_string(
PUGIXML_TEXT(
"<?xml version=\"1.0\""));
7260 buffered_writer.write(
'?',
'>');
7261 if (!(flags &
format_raw)) buffered_writer.write(
'\n');
7264 impl::node_output(buffered_writer,
_root, indent, flags, 0);
7266 buffered_writer.flush();
7269#ifndef PUGIXML_NO_STL
7287 using impl::auto_deleter;
7290 return impl::save_file_impl(*
this, file.
data, indent, flags, encoding);
7295 using impl::auto_deleter;
7298 return impl::save_file_impl(*
this, file.
data, indent, flags, encoding);
7312#ifndef PUGIXML_NO_STL
7317 return impl::as_utf8_impl(str, impl::strlength_wide(str));
7322 return impl::as_utf8_impl(str.c_str(), str.size());
7329 return impl::as_wide_impl(str, strlen(str));
7334 return impl::as_wide_impl(str.c_str(), str.size());
7340 impl::xml_memory::allocate =
allocate;
7346 return impl::xml_memory::allocate;
7351 return impl::xml_memory::deallocate;
7355#if !defined(PUGIXML_NO_STL) && (defined(_MSC_VER) || defined(__ICC))
7359 PUGI__FN std::bidirectional_iterator_tag _Iter_cat(
const pugi::xml_node_iterator&)
7361 return std::bidirectional_iterator_tag();
7364 PUGI__FN std::bidirectional_iterator_tag _Iter_cat(
const pugi::xml_attribute_iterator&)
7366 return std::bidirectional_iterator_tag();
7369 PUGI__FN std::bidirectional_iterator_tag _Iter_cat(
const pugi::xml_named_node_iterator&)
7371 return std::bidirectional_iterator_tag();
7376#if !defined(PUGIXML_NO_STL) && defined(__SUNPRO_CC)
7380 PUGI__FN std::bidirectional_iterator_tag __iterator_category(
const pugi::xml_node_iterator&)
7382 return std::bidirectional_iterator_tag();
7385 PUGI__FN std::bidirectional_iterator_tag __iterator_category(
const pugi::xml_attribute_iterator&)
7387 return std::bidirectional_iterator_tag();
7390 PUGI__FN std::bidirectional_iterator_tag __iterator_category(
const pugi::xml_named_node_iterator&)
7392 return std::bidirectional_iterator_tag();
7397#ifndef PUGIXML_NO_XPATH
7402 template <
typename T>
bool operator()(
const T& lhs,
const T& rhs)
const
7410 template <
typename T>
bool operator()(
const T& lhs,
const T& rhs)
const
7418 template <
typename T>
bool operator()(
const T& lhs,
const T& rhs)
const
7426 template <
typename T>
bool operator()(
const T& lhs,
const T& rhs)
const
7432 template <
typename T>
inline void swap(T& lhs, T& rhs)
7443 for (I it = begin + 1; it != end; ++it)
7444 if (pred(*it, *result))
7452 while (end - begin > 1)
7453 swap(*begin++, *--end);
7459 while (end - begin > 1 && *begin != *(begin + 1))
7469 while (begin != end)
7471 if (*begin != *write)
7472 *++write = *begin++;
7486 for (T* it = begin + 1; it != end; ++it)
7492 while (hole > begin && pred(val, *(hole - 1)))
7494 *hole = *(hole - 1);
7503 template <
typename I,
typename Pred>
inline I
median3(I first, I middle, I last,
const Pred& pred)
7505 if (pred(*middle, *first))
7506 swap(middle, first);
7507 if (pred(*last, *middle))
7509 if (pred(*middle, *first))
7510 swap(middle, first);
7515 template <
typename T,
typename Pred>
PUGI__FN void partition3(T* begin, T* end, T pivot,
const Pred& pred, T** out_eqbeg, T** out_eqend)
7524 if (pred(*lt, pivot))
7526 else if (*lt == pivot)
7535 for (T* it = begin; it != eq; ++it)
7536 swap(*it, *--eqbeg);
7542 template <
typename I,
typename Pred>
PUGI__FN void sort(I begin, I end,
const Pred& pred)
7545 while (end - begin > 16)
7548 I middle = begin + (end - begin) / 2;
7549 I median =
median3(begin, middle, end - 1, pred);
7553 partition3(begin, end, *median, pred, &eqbeg, &eqend);
7556 if (eqbeg - begin > end - eqend)
7558 sort(eqend, end, pred);
7563 sort(begin, eqbeg, pred);
7576 unsigned int h =
static_cast<unsigned int>(
reinterpret_cast<uintptr_t
>(key));
7585 size_t hashmod = size - 1;
7586 size_t bucket = h & hashmod;
7588 for (
size_t probe = 0; probe <= hashmod; ++probe)
7590 if (table[bucket] == 0)
7592 table[bucket] = key;
7596 if (table[bucket] == key)
7600 bucket = (bucket + probe + 1) & hashmod;
7603 assert(
false &&
"Hash table is full");
7610 static const size_t xpath_memory_page_size =
7611 #ifdef PUGIXML_MEMORY_XPATH_PAGE_SIZE
7612 PUGIXML_MEMORY_XPATH_PAGE_SIZE
7618 static const uintptr_t xpath_memory_block_alignment =
sizeof(double) >
sizeof(
void*) ?
sizeof(double) : sizeof(void*);
7645 size = (size + xpath_memory_block_alignment - 1) & ~(xpath_memory_block_alignment - 1);
7656 size_t block_capacity_base =
sizeof(
_root->data);
7657 size_t block_capacity_req = size + block_capacity_base / 4;
7658 size_t block_capacity = (block_capacity_base > block_capacity_req) ? block_capacity_base : block_capacity_req;
7682 old_size = (old_size + xpath_memory_block_alignment - 1) & ~(xpath_memory_block_alignment - 1);
7683 new_size = (new_size + xpath_memory_block_alignment - 1) & ~(xpath_memory_block_alignment - 1);
7686 assert(ptr == 0 ||
static_cast<char*
>(ptr) + old_size == &
_root->data[0] +
_root_size);
7689 if (ptr &&
_root_size - old_size + new_size <= _root->capacity)
7697 if (!result)
return 0;
7703 assert(new_size >= old_size);
7704 memcpy(result, ptr, old_size);
7707 assert(
_root->data == result);
7708 assert(
_root->next);
7710 if (
_root->next->data == ptr)
7731 while (cur != state.
_root)
7811 const char_t* _buffer;
7813 size_t _length_heap;
7817 char_t* result =
static_cast<char_t*
>(alloc->
allocate((
length + 1) *
sizeof(char_t)));
7818 if (!result)
return 0;
7820 memcpy(result,
string,
length *
sizeof(char_t));
7826 xpath_string(
const char_t* buffer,
bool uses_heap_,
size_t length_heap): _buffer(buffer), _uses_heap(uses_heap_), _length_heap(length_heap)
7838 assert(begin <= end && *end == 0);
7840 return xpath_string(begin,
true,
static_cast<size_t>(end - begin));
7845 assert(begin <= end);
7850 size_t length =
static_cast<size_t>(end - begin);
7851 const char_t*
data = duplicate_string(begin,
length, alloc);
7863 if (!*o._buffer)
return;
7866 if (!*_buffer && !_uses_heap && !o._uses_heap)
7868 _buffer = o._buffer;
7873 size_t target_length =
length();
7874 size_t source_length = o.
length();
7875 size_t result_length = target_length + source_length;
7878 char_t* result =
static_cast<char_t*
>(alloc->
reallocate(_uses_heap ?
const_cast<char_t*
>(_buffer) : 0, (target_length + 1) *
sizeof(char_t), (result_length + 1) *
sizeof(char_t)));
7879 if (!result)
return;
7882 if (!_uses_heap) memcpy(result, _buffer, target_length *
sizeof(char_t));
7885 memcpy(result + target_length, o._buffer, source_length *
sizeof(char_t));
7886 result[result_length] = 0;
7891 _length_heap = result_length;
7902 return _uses_heap ? _length_heap :
strlength(_buffer);
7911 const char_t* data_ = duplicate_string(_buffer, length_, alloc);
7913 if (!data_)
return 0;
7917 _length_heap = length_;
7920 return const_cast<char_t*
>(_buffer);
7925 return *_buffer == 0;
7930 return strequal(_buffer, o._buffer);
7935 return !
strequal(_buffer, o._buffer);
7948 while (*pattern && *
string == *pattern)
7954 return *pattern == 0;
7959 #ifdef PUGIXML_WCHAR_MODE
7960 return wcschr(s, c);
7962 return strchr(s, c);
7968 #ifdef PUGIXML_WCHAR_MODE
7970 return (*p == 0) ? s : wcsstr(s, p);
7972 return strstr(s, p);
7979 return static_cast<unsigned int>(ch -
'A') < 26 ?
static_cast<char_t
>(ch |
' ') : ch;
7988 xml_node n = na.node();
8007 xml_node cur = n.first_child();
8009 while (cur && cur != n)
8011 if (cur.type() == node_pcdata || cur.type() == node_cdata)
8014 if (cur.first_child())
8015 cur = cur.first_child();
8016 else if (cur.next_sibling())
8017 cur = cur.next_sibling();
8020 while (!cur.next_sibling() && cur != n)
8023 if (cur != n) cur = cur.next_sibling();
8038 assert(ln->parent == rn->parent);
8041 if (!ln->parent)
return ln < rn;
8044 xml_node_struct* ls = ln;
8045 xml_node_struct* rs = rn;
8049 if (ls == rn)
return true;
8050 if (rs == ln)
return false;
8052 ls = ls->next_sibling;
8053 rs = rs->next_sibling;
8063 xml_node_struct* lp = ln;
8064 xml_node_struct* rp = rn;
8066 while (lp && rp && lp->parent != rp->parent)
8076 bool left_higher = !lp;
8091 if (ln == rn)
return left_higher;
8094 while (ln->parent != rn->parent)
8105 while (node && node != parent) node = node->parent;
8107 return parent && node == parent;
8112 xml_node_struct* node = xnode.node().internal_object();
8116 if ((
get_document(node).header & xml_memory_page_contents_shared_mask) == 0)
8118 if (node->name && (node->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0)
return node->name;
8119 if (node->value && (node->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0)
return node->value;
8125 xml_attribute_struct* attr = xnode.attribute().internal_object();
8129 if ((
get_document(attr).header & xml_memory_page_contents_shared_mask) == 0)
8131 if ((attr->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0)
return attr->name;
8132 if ((attr->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0)
return attr->value;
8143 bool operator()(
const xpath_node& lhs,
const xpath_node& rhs)
const
8149 if (lo && ro)
return lo < ro;
8152 xml_node ln = lhs.node(), rn = rhs.node();
8155 if (lhs.attribute() && rhs.attribute())
8158 if (lhs.parent() == rhs.parent())
8161 for (xml_attribute a = lhs.attribute(); a; a = a.next_attribute())
8162 if (a == rhs.attribute())
8172 else if (lhs.attribute())
8175 if (lhs.parent() == rhs.node())
return false;
8179 else if (rhs.attribute())
8182 if (rhs.parent() == lhs.node())
return true;
8187 if (ln == rn)
return false;
8189 if (!ln || !rn)
return ln < rn;
8191 return node_is_before(ln.internal_object(), rn.internal_object());
8197 #if defined(__STDC_IEC_559__) || ((FLT_RADIX - 0 == 2) && (FLT_MAX_EXP - 0 == 128) && (FLT_MANT_DIG - 0 == 24))
8199 typedef uint32_t UI;
8200 union {
float f; UI i; } u;
8205 const volatile double zero = 0.0;
8212 #if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__)
8213 return !!_isnan(value);
8214 #elif defined(fpclassify) && defined(FP_NAN)
8215 return fpclassify(value) == FP_NAN;
8218 const volatile double v = value;
8225 #if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__)
8226 if (_finite(value))
return (value == 0) ?
PUGIXML_TEXT(
"0") : 0;
8229 #elif defined(fpclassify) && defined(FP_NAN) && defined(FP_INFINITE) && defined(FP_ZERO)
8230 switch (fpclassify(value))
8246 const volatile double v = value;
8257 return (value != 0 && !
is_nan(value));
8262 while (begin != end && end[-1] ==
'0') end--;
8268#if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 && !defined(_WIN32_WCE)
8273 _ecvt_s(buffer,
sizeof(buffer), value, DBL_DIG + 1, &exponent, &sign);
8279 *out_mantissa = buffer;
8280 *out_exponent = exponent;
8289 char* exponent_string = strchr(buffer,
'e');
8290 assert(exponent_string);
8292 int exponent = atoi(exponent_string + 1);
8295 char* mantissa = buffer[0] ==
'-' ? buffer + 1 : buffer;
8296 assert(mantissa[0] !=
'0' && mantissa[1] ==
'.');
8299 mantissa[1] = mantissa[0];
8307 *out_mantissa = mantissa;
8308 *out_exponent = exponent;
8319 char mantissa_buffer[32];
8326 size_t result_size = strlen(mantissa_buffer) + (exponent > 0 ? exponent : -exponent) + 4;
8327 char_t* result =
static_cast<char_t*
>(alloc->
allocate(
sizeof(char_t) * result_size));
8334 if (value < 0) *s++ =
'-';
8343 while (exponent > 0)
8345 assert(*mantissa == 0 ||
static_cast<unsigned int>(*mantissa -
'0') <= 9);
8346 *s++ = *mantissa ? *mantissa++ :
'0';
8358 while (exponent < 0)
8367 assert(
static_cast<unsigned int>(*mantissa -
'0') <= 9);
8373 assert(s < result + result_size);
8385 if (*
string ==
'-') ++string;
8387 if (!*
string)
return false;
8406 return *
string == 0;
8415 #ifdef PUGIXML_WCHAR_MODE
8416 return wcstod(
string, 0);
8418 return strtod(
string, 0);
8424 size_t length =
static_cast<size_t>(end - begin);
8425 char_t* scratch = buffer;
8427 if (length >=
sizeof(buffer) /
sizeof(buffer[0]))
8431 if (!scratch)
return false;
8435 memcpy(scratch, begin, length *
sizeof(char_t));
8436 scratch[length] = 0;
8448 return floor(value + 0.5);
8455 return (value >= -0.5 && value <= 0) ? ceil(value) : floor(value + 0.5);
8460 return node.attribute() ? node.attribute().name() : node.node().name();
8468 return p ? p + 1 : name;
8478 const char_t* pos =
find_char(name,
':');
8486 const char_t* name = a.name();
8502 xml_attribute a = p.find_attribute(pred);
8504 if (a)
return a.value();
8519 xml_node p = parent;
8523 xml_attribute a = p.find_attribute(pred);
8525 if (a)
return a.value();
8540 char_t* write = buffer;
8542 for (char_t* it = buffer; *it; )
8552 if (write != buffer) *write++ =
' ';
8568 char_t* write = buffer;
8574 const char_t* pos =
find_char(from, ch);
8578 else if (
static_cast<size_t>(pos - from) < to_length)
8579 *write++ = to[pos - from];
8590 unsigned char table[128] = {0};
8594 unsigned int fc =
static_cast<unsigned int>(*from);
8595 unsigned int tc =
static_cast<unsigned int>(*to);
8597 if (fc >= 128 || tc >= 128)
8602 table[fc] =
static_cast<unsigned char>(tc ? tc : 128);
8608 for (
int i = 0; i < 128; ++i)
8610 table[i] =
static_cast<unsigned char>(i);
8612 void* result = alloc->
allocate(
sizeof(table));
8613 if (!result)
return 0;
8615 memcpy(result, table,
sizeof(table));
8617 return static_cast<unsigned char*
>(result);
8622 char_t* write = buffer;
8626 char_t ch = *buffer++;
8627 unsigned int index =
static_cast<unsigned int>(ch);
8631 unsigned char code = table[index];
8635 *write =
static_cast<char_t
>(code);
8636 write += 1 - (code >> 7);
8700 static const xpath_node_set dummy_node_set;
8705 unsigned int result = 0;
8709 result +=
static_cast<unsigned int>(*str++);
8710 result += result << 10;
8711 result ^= result >> 6;
8714 result += result << 3;
8715 result ^= result >> 11;
8716 result += result << 15;
8724 if (length == 0)
return 0;
8728 if (!memory)
return 0;
8730 T* result =
new (memory) T();
8732 memcpy(result->name, name, (length + 1) *
sizeof(char_t));
8741 case xpath_type_node_set:
8744 case xpath_type_number:
8747 case xpath_type_string:
8750 case xpath_type_boolean:
8768 case xpath_type_node_set:
8772 case xpath_type_number:
8776 case xpath_type_string:
8780 case xpath_type_boolean:
8785 assert(
false &&
"Invalid variable type");
8791 switch (rhs->type())
8793 case xpath_type_node_set:
8796 case xpath_type_number:
8799 case xpath_type_string:
8802 case xpath_type_boolean:
8806 assert(
false &&
"Invalid variable type");
8813 size_t length =
static_cast<size_t>(end - begin);
8814 char_t* scratch = buffer;
8816 if (length >=
sizeof(buffer) /
sizeof(buffer[0]))
8820 if (!scratch)
return false;
8824 memcpy(scratch, begin, length *
sizeof(char_t));
8825 scratch[length] = 0;
8827 *out_result = set->get(scratch);
8840 if (end - begin < 2)
8841 return xpath_node_set::type_sorted;
8845 bool first = cmp(begin[0], begin[1]);
8847 for (
const xpath_node* it = begin + 1; it + 1 < end; ++it)
8848 if (cmp(it[0], it[1]) != first)
8849 return xpath_node_set::type_unsorted;
8851 return first ? xpath_node_set::type_sorted : xpath_node_set::type_sorted_reverse;
8854 PUGI__FN xpath_node_set::type_t
xpath_sort(xpath_node* begin, xpath_node* end, xpath_node_set::type_t type,
bool rev)
8856 xpath_node_set::type_t order = rev ? xpath_node_set::type_sorted_reverse : xpath_node_set::type_sorted;
8858 if (type == xpath_node_set::type_unsorted)
8862 if (sorted == xpath_node_set::type_unsorted)
8866 type = xpath_node_set::type_sorted;
8872 if (type != order)
reverse(begin, end);
8879 if (begin == end)
return xpath_node();
8883 case xpath_node_set::type_sorted:
8886 case xpath_node_set::type_sorted_reverse:
8889 case xpath_node_set::type_unsorted:
8893 assert(
false &&
"Invalid node set type");
8894 return xpath_node();
8900 xpath_node_set::type_t _type;
8923 return _begin == _end;
8928 return static_cast<size_t>(_end - _begin);
8948 if (begin_ == end_)
return;
8950 size_t size_ =
static_cast<size_t>(_end - _begin);
8951 size_t capacity =
static_cast<size_t>(_eos - _begin);
8952 size_t count =
static_cast<size_t>(end_ - begin_);
8954 if (size_ + count > capacity)
8957 xpath_node* data =
static_cast<xpath_node*
>(alloc->
reallocate(_begin, capacity *
sizeof(xpath_node), (size_ + count) *
sizeof(xpath_node)));
8962 _end = data + size_;
8963 _eos = data + size_ + count;
8966 memcpy(_end, begin_, count *
sizeof(xpath_node));
8972 _type =
xpath_sort(_begin, _end, _type,
false);
8977 assert(_begin <= pos && pos <= _end);
8984 if (_type == xpath_node_set::type_unsorted && _end - _begin > 2)
8988 size_t size_ =
static_cast<size_t>(_end - _begin);
8990 size_t hash_size = 1;
8991 while (hash_size < size_ + size_ / 2) hash_size *= 2;
8993 const void** hash_data =
static_cast<const void**
>(alloc->
allocate(hash_size *
sizeof(
void**)));
8994 if (!hash_data)
return;
8996 memset(hash_data, 0, hash_size *
sizeof(
const void**));
8998 xpath_node* write = _begin;
9000 for (xpath_node* it = _begin; it != _end; ++it)
9002 const void* attr = it->attribute().internal_object();
9003 const void* node = it->node().internal_object();
9004 const void* key = attr ? attr : node;
9006 if (key &&
hash_insert(hash_data, hash_size, key))
9016 _end =
unique(_begin, _end);
9020 xpath_node_set::type_t
type()
const
9033 size_t capacity =
static_cast<size_t>(_eos - _begin);
9036 size_t new_capacity = capacity + capacity / 2 + 1;
9039 xpath_node* data =
static_cast<xpath_node*
>(alloc->
reallocate(_begin, capacity *
sizeof(xpath_node), new_capacity *
sizeof(xpath_node)));
9044 _end = data + capacity;
9045 _eos = data + new_capacity;
9105 size_t length =
static_cast<size_t>(
end -
begin);
9114 const char_t* _cur_lexeme_pos;
9132 const char_t* cur = _cur;
9137 _cur_lexeme_pos = cur;
9146 if (*(cur+1) ==
'=')
9159 if (*(cur+1) ==
'=')
9172 if (*(cur+1) ==
'=')
9218 _cur_lexeme_contents.begin = cur;
9229 _cur_lexeme_contents.end = cur;
9271 if (*(cur+1) ==
'/')
9284 if (*(cur+1) ==
'.')
9291 _cur_lexeme_contents.begin = cur;
9297 _cur_lexeme_contents.end = cur;
9317 char_t terminator = *cur;
9321 _cur_lexeme_contents.begin = cur;
9322 while (*cur && *cur != terminator) cur++;
9323 _cur_lexeme_contents.end = cur;
9337 if (*(cur+1) ==
':')
9351 _cur_lexeme_contents.begin = cur;
9362 _cur_lexeme_contents.end = cur;
9368 _cur_lexeme_contents.begin = cur;
9386 _cur_lexeme_contents.end = cur;
9406 return _cur_lexeme_pos;
9413 return _cur_lexeme_contents;
9534 class xpath_ast_node
9548 xpath_ast_node* _left;
9549 xpath_ast_node* _right;
9550 xpath_ast_node* _next;
9573 if (lt != xpath_type_node_set && rt != xpath_type_node_set)
9575 if (lt == xpath_type_boolean || rt == xpath_type_boolean)
9577 else if (lt == xpath_type_number || rt == xpath_type_number)
9579 else if (lt == xpath_type_string || rt == xpath_type_string)
9586 return comp(ls, rs);
9589 else if (lt == xpath_type_node_set && rt == xpath_type_node_set)
9591 xpath_allocator_capture cr(stack.
result);
9596 for (
const xpath_node* li = ls.
begin(); li != ls.
end(); ++li)
9597 for (
const xpath_node* ri = rs.
begin(); ri != rs.
end(); ++ri)
9599 xpath_allocator_capture cri(stack.
result);
9609 if (lt == xpath_type_node_set)
9615 if (lt == xpath_type_boolean)
9617 else if (lt == xpath_type_number)
9619 xpath_allocator_capture cr(stack.
result);
9624 for (
const xpath_node* ri = rs.
begin(); ri != rs.
end(); ++ri)
9626 xpath_allocator_capture cri(stack.
result);
9634 else if (lt == xpath_type_string)
9636 xpath_allocator_capture cr(stack.
result);
9641 for (
const xpath_node* ri = rs.
begin(); ri != rs.
end(); ++ri)
9643 xpath_allocator_capture cri(stack.
result);
9653 assert(
false &&
"Wrong types");
9657 static bool eval_once(xpath_node_set::type_t type,
nodeset_eval_t eval)
9662 template <
class Comp>
static bool compare_rel(xpath_ast_node* lhs, xpath_ast_node* rhs,
const xpath_context& c,
const xpath_stack& stack,
const Comp& comp)
9666 if (lt != xpath_type_node_set && rt != xpath_type_node_set)
9668 else if (lt == xpath_type_node_set && rt == xpath_type_node_set)
9670 xpath_allocator_capture cr(stack.
result);
9675 for (
const xpath_node* li = ls.
begin(); li != ls.
end(); ++li)
9677 xpath_allocator_capture cri(stack.
result);
9681 for (
const xpath_node* ri = rs.
begin(); ri != rs.
end(); ++ri)
9683 xpath_allocator_capture crii(stack.
result);
9692 else if (lt != xpath_type_node_set && rt == xpath_type_node_set)
9694 xpath_allocator_capture cr(stack.
result);
9699 for (
const xpath_node* ri = rs.
begin(); ri != rs.
end(); ++ri)
9701 xpath_allocator_capture cri(stack.
result);
9709 else if (lt == xpath_type_node_set && rt != xpath_type_node_set)
9711 xpath_allocator_capture cr(stack.
result);
9716 for (
const xpath_node* li = ls.
begin(); li != ls.
end(); ++li)
9718 xpath_allocator_capture cri(stack.
result);
9728 assert(
false &&
"Wrong types");
9733 static void apply_predicate_boolean(xpath_node_set_raw& ns,
size_t first, xpath_ast_node* expr,
const xpath_stack& stack,
bool once)
9735 assert(ns.
size() >= first);
9736 assert(expr->
rettype() != xpath_type_number);
9739 size_t size = ns.
size() - first;
9744 for (xpath_node* it = last; it != ns.
end(); ++it, ++i)
9746 xpath_context c(*it, i, size);
9759 static void apply_predicate_number(xpath_node_set_raw& ns,
size_t first, xpath_ast_node* expr,
const xpath_stack& stack,
bool once)
9761 assert(ns.
size() >= first);
9762 assert(expr->
rettype() == xpath_type_number);
9765 size_t size = ns.
size() - first;
9770 for (xpath_node* it = last; it != ns.
end(); ++it, ++i)
9772 xpath_context c(*it, i, size);
9774 if (expr->
eval_number(c, stack) ==
static_cast<double>(i))
9785 static void apply_predicate_number_const(xpath_node_set_raw& ns,
size_t first, xpath_ast_node* expr,
const xpath_stack& stack)
9787 assert(ns.
size() >= first);
9788 assert(expr->
rettype() == xpath_type_number);
9790 size_t size = ns.
size() - first;
9794 xpath_context c(xpath_node(), 1, size);
9798 if (er >= 1.0 && er <=
static_cast<double>(size))
9800 size_t eri =
static_cast<size_t>(er);
9802 if (er ==
static_cast<double>(eri))
9804 xpath_node r =
last[eri - 1];
9813 void apply_predicate(xpath_node_set_raw& ns,
size_t first,
const xpath_stack& stack,
bool once)
9815 if (ns.
size() == first)
return;
9820 apply_predicate_number_const(ns, first, _right, stack);
9821 else if (_right->rettype() == xpath_type_number)
9822 apply_predicate_number(ns, first, _right, stack, once);
9824 apply_predicate_boolean(ns, first, _right, stack, once);
9827 void apply_predicates(xpath_node_set_raw& ns,
size_t first,
const xpath_stack& stack,
nodeset_eval_t eval)
9829 if (ns.
size() == first)
return;
9831 bool last_once = eval_once(ns.
type(), eval);
9833 for (xpath_ast_node* pred = _right; pred; pred = pred->_next)
9834 pred->apply_predicate(ns, first, stack, !pred->_next && last_once);
9837 bool step_push(xpath_node_set_raw& ns, xml_attribute_struct* a, xml_node_struct* parent, xpath_allocator* alloc)
9848 ns.
push_back(xpath_node(xml_attribute(a), xml_node(parent)), alloc);
9857 ns.
push_back(xpath_node(xml_attribute(a), xml_node(parent)), alloc);
9865 ns.
push_back(xpath_node(xml_attribute(a), xml_node(parent)), alloc);
9877 bool step_push(xpath_node_set_raw& ns, xml_node_struct* n, xpath_allocator* alloc)
9886 if (type == node_element && n->name &&
strequal(n->name, _data.nodetest))
9898 if (type == node_comment)
9906 if (type == node_pcdata || type == node_cdata)
9914 if (type == node_pi)
9922 if (type == node_pi && n->name &&
strequal(n->name, _data.nodetest))
9930 if (type == node_element)
9938 if (type == node_element && n->name &&
starts_with(n->name, _data.nodetest))
9946 assert(
false &&
"Unknown axis");
9952 template <
class T>
void step_fill(xpath_node_set_raw& ns, xml_node_struct* n, xpath_allocator* alloc,
bool once, T)
9960 for (xml_attribute_struct* a = n->first_attribute; a; a = a->next_attribute)
9961 if (step_push(ns, a, n, alloc) & once)
9969 for (xml_node_struct* c = n->first_child; c; c = c->next_sibling)
9970 if (step_push(ns, c, alloc) & once)
9980 if (step_push(ns, n, alloc) & once)
9983 xml_node_struct* cur = n->first_child;
9987 if (step_push(ns, cur, alloc) & once)
9990 if (cur->first_child)
9991 cur = cur->first_child;
9994 while (!cur->next_sibling)
9998 if (cur == n)
return;
10001 cur = cur->next_sibling;
10010 for (xml_node_struct* c = n->next_sibling; c; c = c->next_sibling)
10011 if (step_push(ns, c, alloc) & once)
10019 for (xml_node_struct* c = n->prev_sibling_c; c->next_sibling; c = c->prev_sibling_c)
10020 if (step_push(ns, c, alloc) & once)
10028 xml_node_struct* cur = n;
10031 while (!cur->next_sibling)
10038 cur = cur->next_sibling;
10042 if (step_push(ns, cur, alloc) & once)
10045 if (cur->first_child)
10046 cur = cur->first_child;
10049 while (!cur->next_sibling)
10056 cur = cur->next_sibling;
10065 xml_node_struct* cur = n;
10068 while (!cur->prev_sibling_c->next_sibling)
10075 cur = cur->prev_sibling_c;
10079 if (cur->first_child)
10080 cur = cur->first_child->prev_sibling_c;
10084 if (step_push(ns, cur, alloc) & once)
10087 while (!cur->prev_sibling_c->next_sibling)
10094 if (step_push(ns, cur, alloc) & once)
10098 cur = cur->prev_sibling_c;
10109 if (step_push(ns, n, alloc) & once)
10112 xml_node_struct* cur = n->parent;
10116 if (step_push(ns, cur, alloc) & once)
10127 step_push(ns, n, alloc);
10135 step_push(ns, n->parent, alloc);
10141 assert(
false &&
"Unimplemented axis");
10145 template <
class T>
void step_fill(xpath_node_set_raw& ns, xml_attribute_struct* a, xml_node_struct* p, xpath_allocator* alloc,
bool once, T v)
10155 if (step_push(ns, a, p, alloc) & once)
10158 xml_node_struct* cur = p;
10162 if (step_push(ns, cur, alloc) & once)
10175 step_push(ns, a, p, alloc);
10182 xml_node_struct* cur = p;
10186 if (cur->first_child)
10187 cur = cur->first_child;
10190 while (!cur->next_sibling)
10197 cur = cur->next_sibling;
10200 if (step_push(ns, cur, alloc) & once)
10209 step_push(ns, p, alloc);
10217 step_fill(ns, p, alloc, once, v);
10222 assert(
false &&
"Unimplemented axis");
10226 template <
class T>
void step_fill(xpath_node_set_raw& ns,
const xpath_node& xn, xpath_allocator* alloc,
bool once, T v)
10232 step_fill(ns, xn.node().internal_object(), alloc, once, v);
10233 else if (axis_has_attributes && xn.attribute() && xn.parent())
10234 step_fill(ns, xn.attribute().internal_object(), xn.parent().internal_object(), alloc, once, v);
10237 template <
class T> xpath_node_set_raw step_do(
const xpath_context& c,
const xpath_stack& stack,
nodeset_eval_t eval, T v)
10241 const xpath_node_set::type_t axis_type = axis_reverse ? xpath_node_set::type_sorted_reverse : xpath_node_set::type_sorted;
10245 (!_right && eval_once(axis_type, eval)) ||
10249 xpath_node_set_raw ns;
10259 for (
const xpath_node* it = s.
begin(); it != s.
end(); ++it)
10261 size_t size = ns.
size();
10266 step_fill(ns, *it, stack.
result, once, v);
10267 if (_right) apply_predicates(ns, size, stack, eval);
10272 step_fill(ns, c.
n, stack.
result, once, v);
10273 if (_right) apply_predicates(ns, 0, stack, eval);
10286 _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(0), _right(0), _next(0)
10289 _data.string = value;
10293 _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(0), _right(0), _next(0)
10296 _data.number = value;
10300 _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(0), _right(0), _next(0)
10303 _data.variable = value;
10307 _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(left), _right(right), _next(0)
10312 _type(static_cast<char>(type)), _rettype(xpath_type_node_set), _axis(static_cast<char>(
axis)), _test(static_cast<char>(test)), _left(left), _right(0), _next(0)
10315 _data.nodetest = contents;
10319 _type(static_cast<char>(type)), _rettype(xpath_type_node_set), _axis(0), _test(static_cast<char>(test)), _left(left), _right(right), _next(0)
10339 return _left->eval_boolean(c, stack) || _right->eval_boolean(c, stack);
10342 return _left->eval_boolean(c, stack) && _right->eval_boolean(c, stack);
10345 return compare_eq(_left, _right, c, stack,
equal_to());
10348 return compare_eq(_left, _right, c, stack,
not_equal_to());
10351 return compare_rel(_left, _right, c, stack,
less());
10354 return compare_rel(_right, _left, c, stack,
less());
10357 return compare_rel(_left, _right, c, stack,
less_equal());
10360 return compare_rel(_right, _left, c, stack,
less_equal());
10383 return _left->eval_boolean(c, stack);
10386 return !_left->eval_boolean(c, stack);
10396 if (c.
n.attribute())
return false;
10402 for (xml_node n = c.
n.node(); n; n = n.parent())
10404 xml_attribute a = n.attribute(
PUGIXML_TEXT(
"xml:lang"));
10408 const char_t* value = a.value();
10411 for (
const char_t* lit = lang.
c_str(); *lit; ++lit)
10417 return *value == 0 || *value ==
'-';
10426 const char_t* value = (_right->_type ==
ast_string_constant) ? _right->_data.string : _right->_data.variable->get_string();
10428 xml_attribute attr = c.
n.node().attribute(_left->_data.nodetest);
10435 assert(_rettype == _data.variable->type());
10437 if (_rettype == xpath_type_boolean)
10438 return _data.variable->get_boolean();
10451 case xpath_type_number:
10454 case xpath_type_string:
10461 case xpath_type_node_set:
10469 assert(
false &&
"Wrong expression for return type boolean");
10479 return _left->eval_number(c, stack) + _right->eval_number(c, stack);
10482 return _left->eval_number(c, stack) - _right->eval_number(c, stack);
10485 return _left->eval_number(c, stack) * _right->eval_number(c, stack);
10488 return _left->eval_number(c, stack) / _right->eval_number(c, stack);
10491 return fmod(_left->eval_number(c, stack), _right->eval_number(c, stack));
10494 return -_left->eval_number(c, stack);
10497 return _data.number;
10500 return static_cast<double>(c.
size);
10503 return static_cast<double>(c.
position);
10509 return static_cast<double>(_left->eval_node_set(c, stack,
nodeset_eval_all).size());
10523 return static_cast<double>(_left->eval_string(c, stack).length());
10534 return _left->eval_number(c, stack);
10544 for (
const xpath_node* it = ns.
begin(); it != ns.
end(); ++it)
10556 double r = _left->eval_number(c, stack);
10558 return r == r ? floor(r) : r;
10563 double r = _left->eval_number(c, stack);
10565 return r == r ? ceil(r) : r;
10573 assert(_rettype == _data.variable->type());
10575 if (_rettype == xpath_type_number)
10576 return _data.variable->get_number();
10589 case xpath_type_boolean:
10592 case xpath_type_string:
10599 case xpath_type_node_set:
10607 assert(
false &&
"Wrong expression for return type number");
10620 for (xpath_ast_node* nc = _right; nc; nc = nc->_next) count++;
10629 buffer[0] = _left->eval_string(c, swapped_stack);
10632 for (xpath_ast_node* n = _right; n; n = n->_next, ++pos) buffer[pos] = n->eval_string(c, swapped_stack);
10633 assert(pos == count);
10637 for (
size_t i = 0; i < count; ++i) length += buffer[i].length();
10640 char_t* result =
static_cast<char_t*
>(stack.
result->
allocate((length + 1) *
sizeof(char_t)));
10643 char_t* ri = result;
10645 for (
size_t j = 0; j < count; ++j)
10646 for (
const char_t* bi = buffer[j].c_str(); *bi; ++bi)
10663 xpath_node na = c.
n;
10673 xpath_node na = ns.
first();
10680 xpath_node na = c.
n;
10690 xpath_node na = ns.
first();
10697 xpath_node na = c.
n;
10707 xpath_node na = ns.
first();
10716 return _left->eval_string(c, stack);
10727 xpath_string s = _left->eval_string(c, swapped_stack);
10728 xpath_string p = _right->eval_string(c, swapped_stack);
10741 xpath_string s = _left->eval_string(c, swapped_stack);
10742 xpath_string p = _right->eval_string(c, swapped_stack);
10747 const char_t* rbegin = pos + p.
length();
10759 xpath_string s = _left->eval_string(c, swapped_stack);
10760 size_t s_length = s.
length();
10762 double first =
round_nearest(_right->eval_number(c, stack));
10765 else if (first >=
static_cast<double>(s_length + 1))
return xpath_string();
10767 size_t pos = first < 1 ? 1 : static_cast<size_t>(first);
10768 assert(1 <= pos && pos <= s_length + 1);
10770 const char_t* rbegin = s.
c_str() + (pos - 1);
10782 xpath_string s = _left->eval_string(c, swapped_stack);
10783 size_t s_length = s.
length();
10785 double first =
round_nearest(_right->eval_number(c, stack));
10786 double last = first +
round_nearest(_right->_next->eval_number(c, stack));
10789 else if (first >=
static_cast<double>(s_length + 1))
return xpath_string();
10793 size_t pos = first < 1 ? 1 : static_cast<size_t>(first);
10794 size_t end = last >=
static_cast<double>(s_length + 1) ? s_length + 1 :
static_cast<size_t>(last);
10796 assert(1 <= pos && pos <= end && end <= s_length + 1);
10797 const char_t* rbegin = s.
c_str() + (pos - 1);
10798 const char_t* rend = s.
c_str() + (end - 1);
10834 xpath_string from = _right->eval_string(c, swapped_stack);
10835 xpath_string to = _right->_next->eval_string(c, swapped_stack);
10859 assert(_rettype == _data.variable->type());
10861 if (_rettype == xpath_type_string)
10875 case xpath_type_boolean:
10878 case xpath_type_number:
10881 case xpath_type_node_set:
10892 assert(
false &&
"Wrong expression for return type string");
10911 ls.
set_type(xpath_node_set::type_unsorted);
10926 bool once = eval_once(set.
type(), eval);
10928 apply_predicate(set, 0, stack, once);
10981 assert(
false &&
"Unknown axis");
10992 ns.
set_type(xpath_node_set::type_sorted);
11002 assert(_rettype == _data.variable->type());
11004 if (_rettype == xpath_type_node_set)
11006 const xpath_node_set& s = _data.variable->get_node_set();
11025 assert(
false &&
"Wrong expression for return type node set");
11032 _left->optimize(alloc);
11035 _right->optimize(alloc);
11038 _next->optimize(alloc);
11052 _right = _right->_right;
11064 else if (_right->_rettype != xpath_type_number && _right->is_posinv_expr())
11081 _left = _left->_left;
11094 _data.table =
table;
11131 if (_left && !_left->is_posinv_expr())
return false;
11133 for (xpath_ast_node* n = _right; n; n = n->_next)
11134 if (!n->is_posinv_expr())
return false;
11144 for (xpath_ast_node* n = _right; n; n = n->_next)
11157 return static_cast<xpath_value_type
>(_rettype);
11161 static const size_t xpath_ast_depth_limit =
11162 #ifdef PUGIXML_XPATH_DEPTH_LIMIT
11163 PUGIXML_XPATH_DEPTH_LIMIT
11201 return error(
"Exceeded maximum allowed query depth");
11212 return memory ?
new (memory)
xpath_ast_node(type, rettype, value) : 0;
11218 return memory ?
new (memory)
xpath_ast_node(type, rettype, value) : 0;
11224 return memory ?
new (memory)
xpath_ast_node(type, rettype, value) : 0;
11230 return memory ?
new (memory)
xpath_ast_node(type, rettype, left, right) : 0;
11242 return memory ?
new (memory)
xpath_ast_node(type, left, right, test) : 0;
11250 size_t length =
static_cast<size_t>(value.
end - value.
begin);
11252 char_t* c =
static_cast<char_t*
>(
_alloc->allocate((length + 1) *
sizeof(char_t)));
11255 memcpy(c, value.
begin, length *
sizeof(char_t));
11263 switch (name.
begin[0])
11274 if (args[0]->rettype() != xpath_type_node_set)
return error(
"Function has to be applied to node set");
11277 else if (name ==
PUGIXML_TEXT(
"contains") && argc == 2)
11281 else if (name ==
PUGIXML_TEXT(
"ceiling") && argc == 1)
11305 else if (name ==
PUGIXML_TEXT(
"local-name") && argc <= 1)
11307 if (argc == 1 && args[0]->rettype() != xpath_type_node_set)
return error(
"Function has to be applied to node set");
11316 if (argc == 1 && args[0]->rettype() != xpath_type_node_set)
return error(
"Function has to be applied to node set");
11319 else if (name ==
PUGIXML_TEXT(
"namespace-uri") && argc <= 1)
11321 if (argc == 1 && args[0]->rettype() != xpath_type_node_set)
return error(
"Function has to be applied to node set");
11324 else if (name ==
PUGIXML_TEXT(
"normalize-space") && argc <= 1)
11348 else if (name ==
PUGIXML_TEXT(
"string-length") && argc <= 1)
11350 else if (name ==
PUGIXML_TEXT(
"starts-with") && argc == 2)
11352 else if (name ==
PUGIXML_TEXT(
"substring-before") && argc == 2)
11354 else if (name ==
PUGIXML_TEXT(
"substring-after") && argc == 2)
11356 else if (name ==
PUGIXML_TEXT(
"substring") && (argc == 2 || argc == 3))
11360 if (args[0]->rettype() != xpath_type_node_set)
return error(
"Function has to be applied to node set");
11378 return error(
"Unrecognized function or wrong parameter count");
11385 switch (name.
begin[0])
11451 switch (name.
begin[0])
11487 switch (
_lexer.current())
11494 return error(
"Unknown variable: variable set is not provided");
11496 xpath_variable* var = 0;
11501 return error(
"Unknown variable: variable set does not contain the given name");
11516 return error(
"Expected ')' to match an opening '('");
11526 if (!value)
return 0;
11556 return error(
"Unrecognized function call");
11559 size_t old_depth =
_depth;
11566 return error(
"No comma between function arguments");
11570 if (++
_depth > xpath_ast_depth_limit)
11576 if (argc < 2) args[argc] = n;
11591 return error(
"Unrecognizable primary expression");
11603 size_t old_depth =
_depth;
11609 if (++
_depth > xpath_ast_depth_limit)
11612 if (n->
rettype() != xpath_type_node_set)
11613 return error(
"Predicate has to be applied to node set");
11616 if (!expr)
return 0;
11622 return error(
"Expected ']' to match an opening '['");
11639 if (set && set->
rettype() != xpath_type_node_set)
11640 return error(
"Step has to be applied to node set");
11642 bool axis_specified =
false;
11648 axis_specified =
true;
11657 return error(
"Predicates are not allowed after an abbreviated step");
11666 return error(
"Predicates are not allowed after an abbreviated step");
11677 nt_name =
_lexer.contents();
11684 if (axis_specified)
11685 return error(
"Two axis specifiers in one step");
11689 if (!axis_specified)
11690 return error(
"Unknown axis");
11703 nt_name =
_lexer.contents();
11708 return error(
"Unrecognized node test");
11726 return error(
"Unrecognized node type");
11730 else if (nt_name ==
PUGIXML_TEXT(
"processing-instruction"))
11733 return error(
"Only literals are allowed as arguments to processing-instruction()");
11736 nt_name =
_lexer.contents();
11740 return error(
"Unmatched brace near processing-instruction()");
11745 return error(
"Unmatched brace near node type test");
11751 if (nt_name.
end - nt_name.
begin > 2 && nt_name.
end[-2] ==
':' && nt_name.
end[-1] ==
'*')
11771 return error(
"Unrecognized node test");
11775 if (!nt_name_copy)
return 0;
11780 size_t old_depth =
_depth;
11788 if (++
_depth > xpath_ast_depth_limit)
11792 if (!expr)
return 0;
11795 if (!pred)
return 0;
11798 return error(
"Expected ']' to match an opening '['");
11801 if (last) last->set_next(pred);
11818 size_t old_depth =
_depth;
11825 if (++
_depth > xpath_ast_depth_limit)
11900 const char_t* state =
_lexer.state();
11922 if (n->
rettype() != xpath_type_node_set)
11923 return error(
"Step has to be applied to node set");
12025 if (++
_depth > xpath_ast_depth_limit)
12029 if (!rhs)
return 0;
12036 if (!rhs)
return 0;
12042 return error(
"Union operator has to be applied to node sets");
12045 if (!lhs)
return 0;
12073 size_t old_depth =
_depth;
12075 if (++
_depth > xpath_ast_depth_limit)
12101 return error(
"Incorrect query");
12110 return parser.
parse();
12119 if (!memory)
return 0;
12147 if (!impl)
return 0;
12149 if (impl->
root->
rettype() != xpath_type_node_set)
12151 #ifdef PUGIXML_NO_EXCEPTIONS
12154 xpath_parse_result res;
12155 res.error =
"Expression does not evaluate to node set";
12157 throw xpath_exception(res);
12167#ifndef PUGIXML_NO_EXCEPTIONS
12170 assert(_result.error);
12175 return _result.error;
12198 return _attribute ?
xml_node() : _node;
12208 return _attribute ? _node : _node.
parent();
12215 PUGI__FN xpath_node::operator xpath_node::unspecified_bool_type()
const
12217 return (_node || _attribute) ? unspecified_bool_xpath_node : 0;
12222 return !(_node || _attribute);
12227 return _node == n._node && _attribute == n._attribute;
12232 return _node != n._node || _attribute != n._attribute;
12238 return (
bool)lhs && rhs;
12241 PUGI__FN bool operator||(
const xpath_node& lhs,
bool rhs)
12243 return (
bool)lhs || rhs;
12247 PUGI__FN void xpath_node_set::_assign(const_iterator begin_, const_iterator end_, type_t type_)
12249 assert(begin_ <= end_);
12251 size_t size_ =
static_cast<size_t>(end_ - begin_);
12254 xpath_node* storage = (size_ <= 1) ? _storage : static_cast<xpath_node*>(impl::xml_memory::allocate(size_ *
sizeof(xpath_node)));
12258 #ifdef PUGIXML_NO_EXCEPTIONS
12261 throw std::bad_alloc();
12266 if (_begin != _storage)
12267 impl::xml_memory::deallocate(_begin);
12271 memcpy(storage, begin_, size_ *
sizeof(xpath_node));
12274 _end = storage + size_;
12278#ifdef PUGIXML_HAS_MOVE
12282 _storage[0] = rhs._storage[0];
12283 _begin = (rhs._begin == rhs._storage) ? _storage : rhs._begin;
12284 _end = _begin + (rhs._end - rhs._begin);
12286 rhs._type = type_unsorted;
12287 rhs._begin = rhs._storage;
12288 rhs._end = rhs._storage;
12298 _assign(begin_, end_, type_);
12303 if (_begin != _storage)
12304 impl::xml_memory::deallocate(_begin);
12309 _assign(ns._begin, ns._end, ns._type);
12314 if (
this == &ns)
return *
this;
12316 _assign(ns._begin, ns._end, ns._type);
12321#ifdef PUGIXML_HAS_MOVE
12329 if (
this == &rhs)
return *
this;
12331 if (_begin != _storage)
12332 impl::xml_memory::deallocate(_begin);
12347 return _end - _begin;
12352 return _begin == _end;
12357 assert(index <
size());
12358 return _begin[index];
12373 _type = impl::xpath_sort(_begin, _end, _type,
reverse);
12378 return impl::xpath_first(_begin, _end, _type);
12404 return static_cast<const impl::xpath_variable_node_set*
>(
this)->
name;
12407 return static_cast<const impl::xpath_variable_number*
>(
this)->
name;
12410 return static_cast<const impl::xpath_variable_string*
>(
this)->
name;
12413 return static_cast<const impl::xpath_variable_boolean*
>(
this)->
name;
12416 assert(
false &&
"Invalid variable type");
12428 return (
_type ==
xpath_type_boolean) ?
static_cast<const impl::xpath_variable_boolean*
>(
this)->value :
false;
12433 return (
_type ==
xpath_type_number) ?
static_cast<const impl::xpath_variable_number*
>(
this)->value : impl::gen_nan();
12444 return (
_type ==
xpath_type_node_set) ?
static_cast<const impl::xpath_variable_node_set*
>(
this)->value : impl::dummy_node_set;
12451 static_cast<impl::xpath_variable_boolean*
>(
this)->value = value;
12459 static_cast<impl::xpath_variable_number*
>(
this)->value = value;
12467 impl::xpath_variable_string* var =
static_cast<impl::xpath_variable_string*
>(
this);
12470 size_t size = (impl::strlength(value) + 1) *
sizeof(
char_t);
12472 char_t* copy =
static_cast<char_t*
>(impl::xml_memory::allocate(size));
12473 if (!copy)
return false;
12475 memcpy(copy, value, size);
12478 if (var->value) impl::xml_memory::deallocate(var->value);
12488 static_cast<impl::xpath_variable_node_set*
>(
this)->value = value;
12494 for (
size_t i = 0; i <
sizeof(_data) /
sizeof(_data[0]); ++i)
12500 for (
size_t i = 0; i <
sizeof(_data) /
sizeof(_data[0]); ++i)
12501 _destroy(_data[i]);
12506 for (
size_t i = 0; i <
sizeof(_data) /
sizeof(_data[0]); ++i)
12514 if (
this == &rhs)
return *
this;
12521#ifdef PUGIXML_HAS_MOVE
12524 for (
size_t i = 0; i <
sizeof(_data) /
sizeof(_data[0]); ++i)
12526 _data[i] = rhs._data[i];
12533 for (
size_t i = 0; i <
sizeof(_data) /
sizeof(_data[0]); ++i)
12535 _destroy(_data[i]);
12537 _data[i] = rhs._data[i];
12549 for (
size_t i = 0; i <
sizeof(_data) /
sizeof(_data[0]); ++i)
12550 if (rhs._data[i] && !_clone(rhs._data[i], &temp._data[i]))
12558 for (
size_t i = 0; i <
sizeof(_data) /
sizeof(_data[0]); ++i)
12560 xpath_variable* chain = _data[i];
12562 _data[i] = rhs._data[i];
12563 rhs._data[i] = chain;
12569 const size_t hash_size =
sizeof(_data) /
sizeof(_data[0]);
12570 size_t hash = impl::hash_string(name) % hash_size;
12573 for (xpath_variable* var = _data[hash]; var; var = var->_next)
12574 if (impl::strequal(var->name(), name))
12582 xpath_variable*
last = 0;
12587 xpath_variable* nvar = impl::new_xpath_variable(var->_type, var->name());
12588 if (!nvar)
return false;
12592 last->_next = nvar;
12594 *out_result = nvar;
12599 if (!impl::copy_xpath_variable(nvar, var))
return false;
12611 xpath_variable* next = var->_next;
12613 impl::delete_xpath_variable(var->_type, var);
12621 const size_t hash_size =
sizeof(_data) /
sizeof(_data[0]);
12622 size_t hash = impl::hash_string(name) % hash_size;
12626 if (impl::strequal(var->name(), name))
12627 return var->type() == type ? var : 0;
12634 result->
_next = _data[hash];
12636 _data[hash] = result;
12645 return var ? var->
set(value) :
false;
12651 return var ? var->
set(value) :
false;
12657 return var ? var->
set(value) :
false;
12663 return var ? var->
set(value) :
false;
12668 return _find(name);
12673 return _find(name);
12678 impl::xpath_query_impl* qimpl = impl::xpath_query_impl::create();
12682 #ifdef PUGIXML_NO_EXCEPTIONS
12683 _result.error =
"Out of memory";
12685 throw std::bad_alloc();
12690 using impl::auto_deleter;
12693 qimpl->root = impl::xpath_parser::parse(query, variables, &qimpl->alloc, &_result);
12697 qimpl->root->optimize(&qimpl->alloc);
12704 #ifdef PUGIXML_NO_EXCEPTIONS
12705 if (qimpl->oom) _result.error =
"Out of memory";
12707 if (qimpl->oom)
throw std::bad_alloc();
12721 impl::xpath_query_impl::destroy(
static_cast<impl::xpath_query_impl*
>(_impl));
12724#ifdef PUGIXML_HAS_MOVE
12728 _result = rhs._result;
12735 if (
this == &rhs)
return *
this;
12738 impl::xpath_query_impl::destroy(
static_cast<impl::xpath_query_impl*
>(_impl));
12741 _result = rhs._result;
12743 rhs._result = xpath_parse_result();
12753 return static_cast<impl::xpath_query_impl*
>(_impl)->root->rettype();
12758 if (!_impl)
return false;
12760 impl::xpath_context c(n, 1, 1);
12761 impl::xpath_stack_data sd;
12763 bool r =
static_cast<impl::xpath_query_impl*
>(_impl)->root->eval_boolean(c, sd.stack);
12767 #ifdef PUGIXML_NO_EXCEPTIONS
12770 throw std::bad_alloc();
12779 if (!_impl)
return impl::gen_nan();
12781 impl::xpath_context c(n, 1, 1);
12782 impl::xpath_stack_data sd;
12784 double r =
static_cast<impl::xpath_query_impl*
>(_impl)->root->eval_number(c, sd.stack);
12788 #ifdef PUGIXML_NO_EXCEPTIONS
12789 return impl::gen_nan();
12791 throw std::bad_alloc();
12798#ifndef PUGIXML_NO_STL
12803 impl::xpath_context c(n, 1, 1);
12804 impl::xpath_stack_data sd;
12806 impl::xpath_string r =
static_cast<impl::xpath_query_impl*
>(_impl)->root->eval_string(c, sd.stack);
12810 #ifdef PUGIXML_NO_EXCEPTIONS
12813 throw std::bad_alloc();
12817 return string_t(r.c_str(), r.length());
12823 impl::xpath_context c(n, 1, 1);
12824 impl::xpath_stack_data sd;
12826 impl::xpath_string r = _impl ?
static_cast<impl::xpath_query_impl*
>(_impl)->root->eval_string(c, sd.stack) : impl::xpath_string();
12830 #ifdef PUGIXML_NO_EXCEPTIONS
12831 r = impl::xpath_string();
12833 throw std::bad_alloc();
12837 size_t full_size = r.length() + 1;
12841 size_t size = (full_size < capacity) ? full_size : capacity;
12844 memcpy(buffer, r.c_str(), (size - 1) *
sizeof(
char_t));
12845 buffer[size - 1] = 0;
12853 impl::xpath_ast_node* root = impl::evaluate_node_set_prepare(
static_cast<impl::xpath_query_impl*
>(_impl));
12856 impl::xpath_context c(n, 1, 1);
12857 impl::xpath_stack_data sd;
12859 impl::xpath_node_set_raw r = root->eval_node_set(c, sd.stack, impl::nodeset_eval_all);
12863 #ifdef PUGIXML_NO_EXCEPTIONS
12866 throw std::bad_alloc();
12875 impl::xpath_ast_node* root = impl::evaluate_node_set_prepare(
static_cast<impl::xpath_query_impl*
>(_impl));
12878 impl::xpath_context c(n, 1, 1);
12879 impl::xpath_stack_data sd;
12881 impl::xpath_node_set_raw r = root->eval_node_set(c, sd.stack, impl::nodeset_eval_first);
12885 #ifdef PUGIXML_NO_EXCEPTIONS
12888 throw std::bad_alloc();
12904 PUGI__FN xpath_query::operator xpath_query::unspecified_bool_type()
const
12906 return _impl ? unspecified_bool_xpath_query : 0;
12956#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
12957# pragma warning(pop)
12960#if defined(_MSC_VER) && defined(__c2__)
12961# pragma clang diagnostic pop
12965#undef PUGI__NO_INLINE
12966#undef PUGI__UNLIKELY
12967#undef PUGI__STATIC_ASSERT
12968#undef PUGI__DMC_VOLATILE
12969#undef PUGI__UNSIGNED_OVERFLOW
12970#undef PUGI__MSVC_CRT_VERSION
12971#undef PUGI__SNPRINTF
12972#undef PUGI__NS_BEGIN
12975#undef PUGI__FN_NO_INLINE
12976#undef PUGI__GETHEADER_IMPL
12977#undef PUGI__GETPAGE_IMPL
12978#undef PUGI__GETPAGE
12979#undef PUGI__NODETYPE
12980#undef PUGI__IS_CHARTYPE_IMPL
12981#undef PUGI__IS_CHARTYPE
12982#undef PUGI__IS_CHARTYPEX
12983#undef PUGI__ENDSWITH
12986#undef PUGI__PUSHNODE
12987#undef PUGI__POPNODE
12988#undef PUGI__SCANFOR
12989#undef PUGI__SCANWHILE
12990#undef PUGI__SCANWHILE_UNROLL
12992#undef PUGI__THROW_ERROR
12993#undef PUGI__CHECK_ERROR
void destroy_node(xml_node_struct *n, xml_allocator &alloc)
#define PUGI__CHECK_ERROR(err, m)
PUGI__FN void default_deallocate(void *ptr)
PUGI__FN size_t strlength_wide(const wchar_t *s)
PUGI__FN void node_copy_string(String &dest, Header &header, uintptr_t header_mask, char_t *source, Header &source_header, xml_allocator *alloc)
PUGI__FN I unique(I begin, I end)
#define PUGI__OPTSET(OPT)
PUGI__FN double round_nearest_nzero(double value)
PUGI__FN size_t get_latin1_7bit_prefix_length(const uint8_t *data, size_t size)
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)
#define PUGI__NODETYPE(n)
@ ast_opt_translate_table
@ ast_opt_compare_attribute
@ ast_func_substring_before
@ ast_func_string_length_0
@ ast_func_namespace_uri_0
@ ast_func_normalize_space_1
@ ast_func_normalize_space_0
@ ast_func_substring_after
@ ast_func_namespace_uri_1
@ ast_func_string_length_1
@ ast_op_greater_or_equal
I median3(I first, I middle, I last, const Pred &pred)
PUGI__FN xml_encoding get_write_native_encoding()
PUGI__FN const char_t * qualified_name(const xpath_node &node)
PUGI__FN bool convert_number_to_boolean(double value)
PUGI__FN bool allow_insert_attribute(xml_node_type parent)
char_t *(* strconv_attribute_t)(char_t *, char_t)
#define PUGI__SCANWHILE_UNROLL(X)
PUGI__FN void reverse(I begin, I end)
PUGI__FN const void * document_buffer_order(const xpath_node &xnode)
PUGI__FN void as_utf8_end(char *buffer, size_t size, const wchar_t *str, size_t length)
wchar_selector< sizeof(wchar_t)>::writer wchar_writer
PUGI__FN xml_encoding get_wchar_encoding()
PUGI__FN PUGI__UNSIGNED_OVERFLOW U string_to_integer(const char_t *value, U minv, U maxv)
PUGI__FN void node_copy_attribute(xml_attribute_struct *da, xml_attribute_struct *sa)
PUGI__FN bool allow_insert_child(xml_node_type parent, xml_node_type child)
PUGI__FN FILE * open_file_wide(const wchar_t *path, const wchar_t *mode)
PUGI__FN size_t convert_buffer_output_generic(typename T::value_type dest, const char_t *data, size_t length, D, T)
PUGI__FN void text_output_cdata(xml_buffered_writer &writer, const char_t *s)
PUGI__FN PUGI__UNSIGNED_OVERFLOW unsigned int hash_string(const char_t *str)
PUGI__FN bool get_mutable_buffer(char_t *&out_buffer, size_t &out_length, const void *contents, size_t size, bool is_mutable)
PUGI__FN bool set_value_integer(String &dest, Header &header, uintptr_t header_mask, U value, bool negative)
PUGI__FN bool convert_buffer_generic(char_t *&out_buffer, size_t &out_length, const void *contents, size_t size, D)
#define PUGI__UNSIGNED_OVERFLOW
PUGI__FN void node_copy_tree(xml_node_struct *dn, xml_node_struct *sn)
PUGI__FN xpath_string convert_number_to_string(double value, xpath_allocator *alloc)
PUGI__FN bool is_little_endian()
PUGI__FN void node_output(xml_buffered_writer &writer, xml_node_struct *root, const char_t *indent, unsigned int flags, unsigned int depth)
PUGI__FN size_t zero_terminate_buffer(void *buffer, size_t size, xml_encoding encoding)
PUGI__FN void node_output_simple(xml_buffered_writer &writer, xml_node_struct *node, unsigned int flags)
PUGI__FN float get_value_float(const char_t *value)
PUGI__FN char_t * strconv_comment(char_t *s, char_t endch)
#define PUGI__IS_CHARTYPE(c, ct)
PUGI__FN void node_copy_contents(xml_node_struct *dn, xml_node_struct *sn, xml_allocator *shared_alloc)
PUGI__FN double gen_nan()
PUGI__FN strconv_attribute_t get_strconv_attribute(unsigned int optmask)
PUGI__FN strconv_pcdata_t get_strconv_pcdata(unsigned int optmask)
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)
PUGI__FN xml_parse_status load_stream_data_seek(std::basic_istream< T > &stream, void **out_buffer, size_t *out_size)
PUGI__FN bool set_value_bool(String &dest, Header &header, uintptr_t header_mask, bool value)
PUGI__FN bool hash_insert(const void **table, size_t size, const void *key)
xml_document_struct & get_document(const Object *object)
const axis_t axis_to_type< N >::axis
PUGI__FN xml_encoding guess_buffer_encoding(const uint8_t *data, size_t size)
PUGI__FN void text_output_escaped(xml_buffered_writer &writer, const char_t *s, chartypex_t type, unsigned int flags)
PUGI__FN char_t * strconv_escape(char_t *s, gap &g)
PUGI__FN void node_output_comment(xml_buffered_writer &writer, const char_t *s)
PUGI__FN bool node_is_ancestor(xml_node_struct *parent, xml_node_struct *node)
void destroy_attribute(xml_attribute_struct *a, xml_allocator &alloc)
PUGI__FN void node_output_pi_value(xml_buffered_writer &writer, const char_t *s)
wchar_selector< sizeof(wchar_t)>::counter wchar_counter
PUGI__NS_END PUGI__NS_BEGIN PUGI__FN bool starts_with(const char_t *string, const char_t *pattern)
#define PUGI__IS_CHARTYPEX(c, ct)
void remove_attribute(xml_attribute_struct *attr, xml_node_struct *node)
PUGI__NS_END PUGI__NS_BEGIN PUGI__FN xpath_node_set::type_t xpath_get_order(const xpath_node *begin, const xpath_node *end)
PUGI__FN bool set_value_convert(String &dest, Header &header, uintptr_t header_mask, float value, int precision)
PUGI__FN int get_value_int(const char_t *value)
PUGI__FN bool node_is_before_sibling(xml_node_struct *ln, xml_node_struct *rn)
PUGI__FN bool check_string_to_number_format(const char_t *string)
void prepend_node(xml_node_struct *child, xml_node_struct *node)
PUGI__FN_NO_INLINE xml_attribute_struct * append_new_attribute(xml_node_struct *node, xml_allocator &alloc)
PUGI__FN T * new_xpath_variable(const char_t *name)
PUGI__FN const char_t * find_char(const char_t *s, char_t c)
PUGI__FN size_t get_valid_length(const char_t *data, size_t length)
void insert_node_before(xml_node_struct *child, xml_node_struct *node)
PUGI__FN std::string as_utf8_impl(const wchar_t *str, size_t length)
xml_memory_management_function_storage< int > xml_memory
PUGI__FN bool node_is_before(xml_node_struct *ln, xml_node_struct *rn)
PUGI__FN xml_parse_status get_file_size(FILE *file, size_t &out_result)
bool is_xpath_attribute(const char_t *name)
#define PUGI__SCANCHARTYPE(ct)
char_t *(* strconv_pcdata_t)(char_t *)
void remove_node(xml_node_struct *node)
#define PUGI__GETPAGE_IMPL(header)
#define PUGI__ENDSWITH(c, e)
PUGI__NS_END PUGI__NS_BEGIN uint16_t endian_swap(uint16_t value)
PUGI__FN void sort(I begin, I end, const Pred &pred)
PUGI__FN impl::xpath_ast_node * evaluate_node_set_prepare(xpath_query_impl *impl)
PUGI__FN bool get_variable_scratch(char_t(&buffer)[32], xpath_variable_set *set, const char_t *begin, const char_t *end, xpath_variable **out_result)
PUGI__FN char_t * normalize_space(char_t *buffer)
bool is_text_node(xml_node_struct *node)
PUGI__FN xpath_string string_value(const xpath_node &na, xpath_allocator *alloc)
PUGI__FN bool parse_declaration_encoding(const uint8_t *data, size_t size, const uint8_t *&out_encoding, size_t &out_length)
#define PUGI__FN_NO_INLINE
#define PUGI__STATIC_ASSERT(cond)
PUGI__FN void text_output_indent(xml_buffered_writer &writer, const char_t *indent, size_t indent_length, unsigned int depth)
PUGI__FN void node_output_end(xml_buffered_writer &writer, xml_node_struct *node)
PUGI__FN double get_value_double(const char_t *value)
PUGI__FN xml_parse_status load_stream_data_noseek(std::basic_istream< T > &stream, void **out_buffer, size_t *out_size)
PUGI__FN void close_file(FILE *file)
PUGI__FN_NO_INLINE xml_node_struct * append_new_node(xml_node_struct *node, xml_allocator &alloc, xml_node_type type=node_element)
PUGI__FN PUGI__UNSIGNED_OVERFLOW char_t * integer_to_string(char_t *begin, char_t *end, U value, bool negative)
#define PUGI__DMC_VOLATILE
PUGI__FN FILE * open_file(const char *path, const char *mode)
PUGI__NS_END PUGI__NS_BEGIN xml_attribute_struct * allocate_attribute(xml_allocator &alloc)
#define PUGI__UNLIKELY(cond)
#define PUGI__THROW_ERROR(err, m)
PUGI__FN const char_t * namespace_uri(xml_node node)
xml_parse_result make_parse_result(xml_parse_status status, ptrdiff_t offset=0)
xml_allocator & get_allocator(const Object *object)
PUGI__FN void partition3(T *begin, T *end, T pivot, const Pred &pred, T **out_eqbeg, T **out_eqend)
PUGI__FN bool allow_move(xml_node parent, xml_node child)
PUGI__FN size_t convert_buffer_output(char_t *, uint8_t *r_u8, uint16_t *r_u16, uint32_t *r_u32, const char_t *data, size_t length, xml_encoding encoding)
PUGI__FN char_t * translate_table(char_t *buffer, const unsigned char *table)
PUGI__FN const char_t * convert_number_to_string_special(double value)
PUGI__FN xml_parse_result load_stream_impl(xml_document_struct *doc, std::basic_istream< T > &stream, unsigned int options, xml_encoding encoding, char_t **out_buffer)
PUGI__FN bool is_attribute_of(xml_attribute_struct *attr, xml_node_struct *node)
PUGI__FN bool convert_string_to_number_scratch(char_t(&buffer)[32], const char_t *begin, const char_t *end, double *out_result)
PUGI__FN xpath_node_set::type_t xpath_sort(xpath_node *begin, xpath_node *end, xpath_node_set::type_t type, bool rev)
PUGI__FN xml_encoding get_write_encoding(xml_encoding encoding)
PUGI__FN void delete_xpath_variable(T *var)
PUGI__FN bool convert_buffer_latin1(char_t *&out_buffer, size_t &out_length, const void *contents, size_t size, bool is_mutable)
void swap(T &lhs, T &rhs)
PUGI__NS_END PUGI__NS_BEGIN PUGI__FN size_t strlength(const char_t *s)
PUGI__FN char * convert_path_heap(const wchar_t *str)
PUGI__FN double round_nearest(double value)
PUGI__FN char_t * translate(char_t *buffer, const char_t *from, const char_t *to, size_t to_length)
void insert_node_after(xml_node_struct *child, xml_node_struct *node)
@ nodetest_all_in_namespace
PUGI__FN I min_element(I begin, I end, const Pred &pred)
void insert_attribute_before(xml_attribute_struct *attr, xml_attribute_struct *place, xml_node_struct *node)
PUGI__FN const char_t * find_substring(const char_t *s, const char_t *p)
PUGI__FN bool strequalrange(const char_t *lhs, const char_t *rhs, size_t count)
#define PUGI__SCANCHAR(ch)
PUGI__FN bool save_file_impl(const xml_document &doc, FILE *file, const char_t *indent, unsigned int flags, xml_encoding encoding)
PUGI__FN bool node_output_start(xml_buffered_writer &writer, xml_node_struct *node, const char_t *indent, size_t indent_length, unsigned int flags, unsigned int depth)
PUGI__FN bool has_declaration(xml_node_struct *node)
PUGI__FN double convert_string_to_number(const char_t *string)
PUGI__FN bool is_nan(double value)
PUGI__FN bool set_value_ascii(String &dest, Header &header, uintptr_t header_mask, char *buf)
PUGI__FN xpath_node xpath_first(const xpath_node *begin, const xpath_node *end, xpath_node_set::type_t type)
#define PUGI__PUSHNODE(TYPE)
PUGI__FN size_t as_utf8_begin(const wchar_t *str, size_t length)
PUGI__FN unsigned char * translate_table_generate(xpath_allocator *alloc, const char_t *from, const char_t *to)
PUGI__FN void text_output(xml_buffered_writer &writer, const char_t *s, chartypex_t type, unsigned int flags)
void append_attribute(xml_attribute_struct *attr, xml_node_struct *node)
void prepend_attribute(xml_attribute_struct *attr, xml_node_struct *node)
xml_node_struct * allocate_node(xml_allocator &alloc, xml_node_type type)
#define PUGI__GETHEADER_IMPL(object, page, flags)
allocation_function xml_memory_management_function_storage< T >::allocate
PUGI__FN bool strcpy_insitu(String &dest, Header &header, uintptr_t header_mask, const char_t *source, size_t source_length)
deallocation_function xml_memory_management_function_storage< T >::deallocate
#define PUGI__SCANWHILE(X)
PUGI__FN void insertion_sort(T *begin, T *end, const Pred &pred)
PUGI__FN void node_output_attributes(xml_buffered_writer &writer, xml_node_struct *node, const char_t *indent, size_t indent_length, unsigned int flags, unsigned int depth)
PUGI__FN void truncate_zeros(char *begin, char *end)
@ axis_descendant_or_self
PUGI__FN xml_encoding get_buffer_encoding(xml_encoding encoding, const void *contents, size_t size)
PUGI__FN xml_parse_result load_file_impl(xml_document_struct *doc, FILE *file, unsigned int options, xml_encoding encoding, char_t **out_buffer)
PUGI__FN bool copy_xpath_variable(xpath_variable *lhs, const xpath_variable *rhs)
PUGI__FN const char_t * local_name(const xpath_node &node)
void append_node(xml_node_struct *child, xml_node_struct *node)
PUGI__FN unsigned int get_value_uint(const char_t *value)
PUGI__FN void convert_number_to_mantissa_exponent(double value, char(&buffer)[32], char **out_mantissa, int *out_exponent)
PUGI__FN bool get_value_bool(const char_t *value)
PUGI__FN bool strequal(const char_t *src, const char_t *dst)
PUGI__FN std::basic_string< wchar_t > as_wide_impl(const char *str, size_t size)
PUGI__FN char_t * strconv_cdata(char_t *s, char_t endch)
PUGI__NS_BEGIN PUGI__FN void * default_allocate(size_t size)
bool strcpy_insitu_allow(size_t length, const Header &header, uintptr_t header_mask, char_t *target)
PUGI__FN char_t tolower_ascii(char_t ch)
void insert_attribute_after(xml_attribute_struct *attr, xml_attribute_struct *place, xml_node_struct *node)
#define PUGIXML_NOEXCEPT_IF_NOT_COMPACT
CONSTDATA date::last_spec last
std::basic_string< PUGIXML_CHAR, std::char_traits< PUGIXML_CHAR >, std::allocator< PUGIXML_CHAR > > string_t
PUGI__FN deallocation_function PUGIXML_FUNCTION get_memory_deallocation_function()
PUGI__FN allocation_function PUGIXML_FUNCTION get_memory_allocation_function()
const unsigned int format_no_declaration
const unsigned int format_raw
void(* deallocation_function)(void *ptr)
const int default_double_precision
void *(* allocation_function)(size_t size)
PUGI__FN std::string PUGIXML_FUNCTION as_utf8(const wchar_t *str)
@ status_append_invalid_root
@ status_end_element_mismatch
@ status_bad_start_element
@ status_unrecognized_tag
@ status_no_document_element
const unsigned int format_save_file_text
const unsigned int format_write_bom
PUGI__FN void PUGIXML_FUNCTION set_memory_management_functions(allocation_function allocate, deallocation_function deallocate)
PUGI__FN std::basic_string< wchar_t > PUGIXML_FUNCTION as_wide(const char *str)
const int default_float_precision
static deallocation_function deallocate
static allocation_function allocate
auto_deleter(T *data_, D deleter_)
static xml_memory_page * construct(void *memory)
xml_allocator * allocator
void * allocate_memory(size_t size, xml_memory_page *&out_page)
void * allocate_object(size_t size, xml_memory_page *&out_page)
char_t * allocate_string(size_t length)
static void deallocate_page(xml_memory_page *page)
void * allocate_memory_oob(size_t size, xml_memory_page *&out_page)
void deallocate_memory(void *ptr, size_t size, xml_memory_page *page)
xml_memory_page * allocate_page(size_t data_size)
xml_allocator(xml_memory_page *root)
void deallocate_string(char_t *string)
xml_attribute_struct * prev_attribute_c
xml_attribute_struct(impl::xml_memory_page *page)
xml_attribute_struct * next_attribute
xml_attribute_struct * first_attribute
xml_node_struct * prev_sibling_c
xml_node_struct * next_sibling
xml_node_struct * first_child
xml_node_struct(impl::xml_memory_page *page, xml_node_type type)
xml_document_struct(xml_memory_page *page)
xml_extra_buffer * extra_buffers
static value_type low(value_type result, uint32_t ch)
static value_type high(value_type result, uint32_t)
static value_type any(value_type result, uint32_t ch)
static value_type high(value_type result, uint32_t ch)
static value_type low(value_type result, uint32_t ch)
static value_type low(value_type result, uint32_t)
static value_type high(value_type result, uint32_t)
static value_type high(value_type result, uint32_t ch)
static value_type low(value_type result, uint32_t ch)
static value_type any(value_type result, uint32_t ch)
static value_type low(value_type result, uint32_t)
static value_type high(value_type result, uint32_t)
static value_type low(value_type result, uint32_t ch)
static value_type high(value_type result, uint32_t ch)
static value_type any(value_type result, uint32_t ch)
static value_type high(value_type result, uint32_t ch)
static value_type low(value_type result, uint32_t ch)
static Traits::value_type process(const uint8_t *data, size_t size, typename Traits::value_type result, Traits)
static Traits::value_type process(const uint16_t *data, size_t size, typename Traits::value_type result, Traits)
static Traits::value_type process(const uint32_t *data, size_t size, typename Traits::value_type result, Traits)
static Traits::value_type process(const uint8_t *data, size_t size, typename Traits::value_type result, Traits)
utf16_decoder< opt_false > decoder
utf32_decoder< opt_false > decoder
static Traits::value_type process(const wchar_t *data, size_t size, typename Traits::value_type result, Traits traits)
char_t * flush(char_t *s)
void push(char_t *&s, size_t count)
static char_t * parse(char_t *s)
static char_t * parse_eol(char_t *s, char_t end_quote)
static char_t * parse_simple(char_t *s, char_t end_quote)
static char_t * parse_wnorm(char_t *s, char_t end_quote)
static char_t * parse_wconv(char_t *s, char_t end_quote)
static char_t * parse_skip_bom(char_t *s)
xml_parse_status error_status
xml_parser(xml_allocator *alloc_)
char_t * parse_doctype_primitive(char_t *s)
char_t * parse_tree(char_t *s, xml_node_struct *root, unsigned int optmsk, char_t endch)
static xml_parse_result parse(char_t *buffer, size_t length, xml_document_struct *xmldoc, xml_node_struct *root, unsigned int optmsk)
static bool has_element_node_siblings(xml_node_struct *node)
char_t * parse_doctype_group(char_t *s, char_t endch)
char_t * parse_question(char_t *s, xml_node_struct *&ref_cursor, unsigned int optmsk, char_t endch)
char_t * parse_doctype_ignore(char_t *s)
char_t * parse_exclamation(char_t *s, xml_node_struct *cursor, unsigned int optmsk, char_t endch)
void write_direct(const char_t *data, size_t length)
void write_string(const char_t *data)
xml_buffered_writer(xml_writer &writer_, xml_encoding user_encoding)
void write(char_t d0, char_t d1)
char_t data_char[bufcapacity]
char_t buffer[bufcapacity]
void flush(const char_t *data, size_t size)
uint16_t data_u16[2 *bufcapacity]
void write(char_t d0, char_t d1, char_t d2, char_t d3, char_t d4)
union xml_buffered_writer::@315251124043025016235211374143311267112311046367 scratch
uint8_t data_u8[4 *bufcapacity]
void write_buffer(const char_t *data, size_t length)
void write(char_t d0, char_t d1, char_t d2, char_t d3, char_t d4, char_t d5)
void write(char_t d0, char_t d1, char_t d2, char_t d3)
void write(char_t d0, char_t d1, char_t d2)
uint32_t data_u32[bufcapacity]
static xml_stream_chunk * create()
T data[xml_memory_page_size/sizeof(T)]
static void destroy(xml_stream_chunk *chunk)
name_null_sentry(xml_node_struct *node_)
bool operator()(const T &lhs, const T &rhs) const
bool operator()(const T &lhs, const T &rhs) const
bool operator()(const T &lhs, const T &rhs) const
bool operator()(const T &lhs, const T &rhs) const
char data[xpath_memory_page_size]
xpath_memory_block * next
void * reallocate(void *ptr, size_t old_size, size_t new_size)
void * allocate(size_t size)
xpath_memory_block * _root
void revert(const xpath_allocator &state)
xpath_allocator(xpath_memory_block *root, bool *error=0)
~xpath_allocator_capture()
xpath_allocator * _target
xpath_allocator_capture(xpath_allocator *alloc)
xpath_memory_block blocks[2]
static xpath_string from_heap_preallocated(const char_t *begin, const char_t *end)
static xpath_string from_const(const char_t *str)
bool operator!=(const xpath_string &o) const
void append(const xpath_string &o, xpath_allocator *alloc)
static xpath_string from_heap(const char_t *begin, const char_t *end, xpath_allocator *alloc)
char_t * data(xpath_allocator *alloc)
bool operator==(const xpath_string &o) const
const char_t * c_str() const
bool operator()(const xpath_node &lhs, const xpath_node &rhs) const
namespace_uri_predicate(const char_t *name)
bool operator()(xml_attribute a) const
xpath_variable_node_set()
void append(const xpath_node *begin_, const xpath_node *end_, xpath_allocator *alloc)
xpath_node_set::type_t type() const
void push_back(const xpath_node &node, xpath_allocator *alloc)
void remove_duplicates(xpath_allocator *alloc)
xpath_node * begin() const
void truncate(xpath_node *pos)
void push_back_grow(const xpath_node &node, xpath_allocator *alloc)
void set_type(xpath_node_set::type_t value)
xpath_context(const xpath_node &n_, size_t position_, size_t size_)
bool operator==(const char_t *other) const
const char_t * current_pos() const
const xpath_lexer_string & contents() const
xpath_lexer(const char_t *query)
const char_t * state() const
const unsigned char * table
xpath_variable * variable
void set_next(xpath_ast_node *value)
xpath_value_type rettype() const
void optimize_self(xpath_allocator *alloc)
bool is_posinv_expr() const
xpath_node_set_raw eval_node_set(const xpath_context &c, const xpath_stack &stack, nodeset_eval_t eval)
xpath_string eval_string(const xpath_context &c, const xpath_stack &stack)
xpath_ast_node(ast_type_t type, xpath_ast_node *left, axis_t axis, nodetest_t test, const char_t *contents)
xpath_ast_node(ast_type_t type, xpath_value_type rettype_, xpath_variable *value)
double eval_number(const xpath_context &c, const xpath_stack &stack)
void optimize(xpath_allocator *alloc)
bool is_posinv_step() const
xpath_string eval_string_concat(const xpath_context &c, const xpath_stack &stack)
bool eval_boolean(const xpath_context &c, const xpath_stack &stack)
xpath_ast_node(ast_type_t type, xpath_value_type rettype_, double value)
xpath_ast_node(ast_type_t type, xpath_ast_node *left, xpath_ast_node *right, predicate_t test)
xpath_ast_node(ast_type_t type, xpath_value_type rettype_, const char_t *value)
xpath_ast_node(ast_type_t type, xpath_value_type rettype_, xpath_ast_node *left=0, xpath_ast_node *right=0)
void set_right(xpath_ast_node *value)
xpath_ast_node * error(const char *message)
xpath_ast_node * parse_step(xpath_ast_node *set)
xpath_ast_node * error_oom()
xpath_variable_set * _variables
xpath_parser(const char_t *query, xpath_variable_set *variables, xpath_allocator *alloc, xpath_parse_result *result)
xpath_ast_node * alloc_node(ast_type_t type, xpath_ast_node *left, axis_t axis, nodetest_t test, const char_t *contents)
xpath_ast_node * parse_primary_expression()
xpath_ast_node * alloc_node(ast_type_t type, xpath_ast_node *left, xpath_ast_node *right, predicate_t test)
xpath_ast_node * alloc_node(ast_type_t type, xpath_value_type rettype, xpath_ast_node *left=0, xpath_ast_node *right=0)
xpath_ast_node * parse_filter_expression()
xpath_ast_node * error_rec()
xpath_ast_node * parse_expression(int limit=0)
nodetest_t parse_node_test_type(const xpath_lexer_string &name)
const char_t * alloc_string(const xpath_lexer_string &value)
xpath_parse_result * _result
xpath_ast_node * parse_function(const xpath_lexer_string &name, size_t argc, xpath_ast_node *args[2])
xpath_ast_node * alloc_node(ast_type_t type, xpath_value_type rettype, xpath_variable *value)
xpath_ast_node * alloc_node(ast_type_t type, xpath_value_type rettype, double value)
static xpath_ast_node * parse(const char_t *query, xpath_variable_set *variables, xpath_allocator *alloc, xpath_parse_result *result)
xpath_ast_node * parse_path_or_unary_expression()
axis_t parse_axis_name(const xpath_lexer_string &name, bool &specified)
xpath_ast_node * parse_expression_rec(xpath_ast_node *lhs, int limit)
xpath_ast_node * parse_relative_location_path(xpath_ast_node *set)
xpath_ast_node * alloc_node(ast_type_t type, xpath_value_type rettype, const char_t *value)
xpath_ast_node * parse_location_path()
binary_op_t(ast_type_t asttype_, xpath_value_type rettype_, int precedence_)
static binary_op_t parse(xpath_lexer &lexer)
static void destroy(xpath_query_impl *impl)
static xpath_query_impl * create()
xml_writer_file(void *file)
virtual void write(const void *data, size_t size) PUGIXML_OVERRIDE
xml_writer_stream(std::basic_ostream< char, std::char_traits< char > > &stream)
virtual void write(const void *data, size_t size) PUGIXML_OVERRIDE
const char_t * as_string(const char_t *def=PUGIXML_TEXT("")) const
bool operator>=(const xml_attribute &r) const
bool as_bool(bool def=false) const
bool operator!=(const xml_attribute &r) const
bool operator<(const xml_attribute &r) const
int as_int(int def=0) const
float as_float(float def=0) const
bool operator==(const xml_attribute &r) const
size_t hash_value() const
const char_t * name() const
xml_attribute & operator=(const char_t *rhs)
xml_attribute previous_attribute() const
xml_attribute next_attribute() const
bool operator<=(const xml_attribute &r) const
double as_double(double def=0) const
bool set_name(const char_t *rhs)
bool set_value(const char_t *rhs)
unsigned int as_uint(unsigned int def=0) const
xml_attribute_struct * internal_object() const
bool operator>(const xml_attribute &r) const
const char_t * value() const
string_t path(char_t delimiter='/') const
size_t hash_value() const
bool set_value(const char_t *rhs)
xml_node_type type() const
xml_node append_child(xml_node_type type=node_element)
xml_node child(const char_t *name) const
friend class xml_named_node_iterator
xml_node insert_move_after(const xml_node &moved, const xml_node &node)
xml_node last_child() const
xml_node append_move(const xml_node &moved)
xml_node first_child() const
bool operator>(const xml_node &r) const
xml_object_range< xml_node_iterator > children() const
xml_node prepend_move(const xml_node &moved)
xml_attribute append_attribute(const char_t *name)
xml_node next_sibling() const
xml_node_struct * internal_object() const
xml_object_range< xml_attribute_iterator > attributes() const
bool remove_child(const xml_node &n)
xml_attribute append_copy(const xml_attribute &proto)
xml_node insert_child_after(xml_node_type type, const xml_node &node)
xml_attribute last_attribute() const
attribute_iterator attributes_end() const
xml_node previous_sibling() const
ptrdiff_t offset_debug() const
xml_attribute insert_attribute_before(const char_t *name, const xml_attribute &attr)
xml_attribute insert_attribute_after(const char_t *name, const xml_attribute &attr)
bool operator!=(const xml_node &r) const
bool operator<(const xml_node &r) const
xml_attribute prepend_attribute(const char_t *name)
xml_parse_result append_buffer(const void *contents, size_t size, unsigned int options=parse_default, xml_encoding encoding=encoding_auto)
const char_t * value() const
void(* unspecified_bool_type)(xml_node ***)
xml_attribute_iterator attribute_iterator
bool traverse(xml_tree_walker &walker)
xml_node find_child_by_attribute(const char_t *name, const char_t *attr_name, const char_t *attr_value) const
const char_t * child_value() const
bool set_name(const char_t *rhs)
xml_node prepend_child(xml_node_type type=node_element)
PUGIXML_DEPRECATED xpath_node select_single_node(const char_t *query, xpath_variable_set *variables=PUGIXML_NULL) const
xml_attribute first_attribute() const
bool operator>=(const xml_node &r) const
xml_attribute insert_copy_after(const xml_attribute &proto, const xml_attribute &attr)
xml_attribute prepend_copy(const xml_attribute &proto)
xml_node insert_move_before(const xml_node &moved, const xml_node &node)
xml_node first_element_by_path(const char_t *path, char_t delimiter='/') const
xml_attribute attribute(const char_t *name) const
const char_t * name() const
xml_attribute insert_copy_before(const xml_attribute &proto, const xml_attribute &attr)
attribute_iterator attributes_begin() const
xpath_node select_node(const char_t *query, xpath_variable_set *variables=PUGIXML_NULL) const
xml_node_iterator iterator
bool operator<=(const xml_node &r) const
xpath_node_set select_nodes(const char_t *query, xpath_variable_set *variables=PUGIXML_NULL) const
bool remove_attribute(const xml_attribute &a)
void print(xml_writer &writer, const char_t *indent=PUGIXML_TEXT("\t"), unsigned int flags=format_default, xml_encoding encoding=encoding_auto, unsigned int depth=0) const
xml_node insert_child_before(xml_node_type type, const xml_node &node)
bool operator==(const xml_node &r) const
const char_t * as_string(const char_t *def=PUGIXML_TEXT("")) const
double as_double(double def=0) const
xml_text & operator=(const char_t *rhs)
float as_float(float def=0) const
bool as_bool(bool def=false) const
bool set(const char_t *rhs)
int as_int(int def=0) const
unsigned int as_uint(unsigned int def=0) const
const char_t * get() const
bool operator==(const xml_node_iterator &rhs) const
const xml_node_iterator & operator--()
xml_node * operator->() const
bool operator!=(const xml_node_iterator &rhs) const
const xml_node_iterator & operator++()
xml_node & operator*() const
bool operator!=(const xml_attribute_iterator &rhs) const
const xml_attribute_iterator & operator--()
xml_attribute * operator->() const
xml_attribute & operator*() const
bool operator==(const xml_attribute_iterator &rhs) const
const xml_attribute_iterator & operator++()
xml_named_node_iterator()
bool operator!=(const xml_named_node_iterator &rhs) const
xml_node * operator->() const
const xml_named_node_iterator & operator--()
bool operator==(const xml_named_node_iterator &rhs) const
xml_node & operator*() const
const xml_named_node_iterator & operator++()
virtual bool end(xml_node &node)
virtual bool for_each(xml_node &node)=0
virtual bool begin(xml_node &node)
virtual ~xml_tree_walker()
const char * description() const
xml_parse_result load_buffer_inplace(void *contents, size_t size, unsigned int options=parse_default, xml_encoding encoding=encoding_auto)
xml_node document_element() const
xml_parse_result load_string(const char_t *contents, unsigned int options=parse_default)
bool save_file(const char *path, const char_t *indent=PUGIXML_TEXT("\t"), unsigned int flags=format_default, xml_encoding encoding=encoding_auto) const
xml_parse_result load_buffer_inplace_own(void *contents, size_t size, unsigned int options=parse_default, xml_encoding encoding=encoding_auto)
xml_parse_result load_file(const char *path, unsigned int options=parse_default, xml_encoding encoding=encoding_auto)
xml_parse_result load_buffer(const void *contents, size_t size, unsigned int options=parse_default, xml_encoding encoding=encoding_auto)
xml_parse_result load(std::basic_istream< char, std::char_traits< char > > &stream, unsigned int options=parse_default, xml_encoding encoding=encoding_auto)
void save(xml_writer &writer, const char_t *indent=PUGIXML_TEXT("\t"), unsigned int flags=format_default, xml_encoding encoding=encoding_auto) const
const char * description() const
xpath_variable(xpath_value_type type)
double get_number() const
const char_t * get_string() const
const xpath_node_set & get_node_set() const
xpath_value_type type() const
const char_t * name() const
xpath_variable * add(const char_t *name, xpath_value_type type)
bool set(const char_t *name, bool value)
xpath_variable * get(const char_t *name)
xpath_variable_set & operator=(const xpath_variable_set &rhs)
string_t evaluate_string(const xpath_node &n) const
double evaluate_number(const xpath_node &n) const
xpath_node_set evaluate_node_set(const xpath_node &n) const
xpath_value_type return_type() const
const xpath_parse_result & result() const
bool evaluate_boolean(const xpath_node &n) const
xpath_node evaluate_node(const xpath_node &n) const
virtual const char * what() const PUGIXML_OVERRIDE
const xpath_parse_result & result() const
xpath_exception(const xpath_parse_result &result)
xml_attribute attribute() const
bool operator!=(const xpath_node &n) const
bool operator==(const xpath_node &n) const
xpath_node_set & operator=(const xpath_node_set &ns)
const_iterator end() const
const xpath_node * const_iterator
void sort(bool reverse=false)
const_iterator begin() const
const xpath_node & operator[](size_t index) const