rapidxml.hpp 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  1. #ifndef RAPIDXML_HPP_INCLUDED
  2. #define RAPIDXML_HPP_INCLUDED
  3. // Copyright (C) 2006, 2009 Marcin Kalicinski
  4. // Version 1.13
  5. // Revision $DateTime: 2009/05/13 01:46:17 $
  6. //! \file rapidxml.hpp This file contains rapidxml parser and DOM implementation
  7. // If standard library is disabled, user must provide implementations of required functions and typedefs
  8. #if !defined(RAPIDXML_NO_STDLIB)
  9. #include <cstdlib> // For std::size_t
  10. #include <cassert> // For assert
  11. #include <new> // For placement new
  12. #endif
  13. // On MSVC, disable "conditional expression is constant" warning (level 4).
  14. // This warning is almost impossible to avoid with certain types of templated code
  15. #ifdef _MSC_VER
  16. #pragma warning(push)
  17. #pragma warning(disable:4127) // Conditional expression is constant
  18. #endif
  19. ///////////////////////////////////////////////////////////////////////////
  20. // RAPIDXML_PARSE_ERROR
  21. #if defined(RAPIDXML_NO_EXCEPTIONS)
  22. #define RAPIDXML_PARSE_ERROR(what, where) { parse_error_handler(what, where); assert(0); }
  23. namespace rapidxml
  24. {
  25. //! When exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS,
  26. //! this function is called to notify user about the error.
  27. //! It must be defined by the user.
  28. //! <br><br>
  29. //! This function cannot return. If it does, the results are undefined.
  30. //! <br><br>
  31. //! A very simple definition might look like that:
  32. //! <pre>
  33. //! void %rapidxml::%parse_error_handler(const char *what, void *where)
  34. //! {
  35. //! std::cout << "Parse error: " << what << "\n";
  36. //! std::abort();
  37. //! }
  38. //! </pre>
  39. //! \param what Human readable description of the error.
  40. //! \param where Pointer to character data where error was detected.
  41. void parse_error_handler(const char *what, void *where);
  42. }
  43. #else
  44. #include <exception> // For std::exception
  45. #define RAPIDXML_PARSE_ERROR(what, where) throw parse_error(what, where)
  46. namespace rapidxml
  47. {
  48. //! Parse error exception.
  49. //! This exception is thrown by the parser when an error occurs.
  50. //! Use what() function to get human-readable error message.
  51. //! Use where() function to get a pointer to position within source text where error was detected.
  52. //! <br><br>
  53. //! If throwing exceptions by the parser is undesirable,
  54. //! it can be disabled by defining RAPIDXML_NO_EXCEPTIONS macro before rapidxml.hpp is included.
  55. //! This will cause the parser to call rapidxml::parse_error_handler() function instead of throwing an exception.
  56. //! This function must be defined by the user.
  57. //! <br><br>
  58. //! This class derives from <code>std::exception</code> class.
  59. class parse_error : public std::exception
  60. {
  61. public:
  62. //! Constructs parse error
  63. parse_error(const char *what, void *where)
  64. : m_what(what)
  65. , m_where(where)
  66. {
  67. }
  68. //! Gets human readable description of error.
  69. //! \return Pointer to null terminated description of the error.
  70. virtual const char *what() const throw()
  71. {
  72. return m_what;
  73. }
  74. //! Gets pointer to character data where error happened.
  75. //! Ch should be the same as char type of xml_document that produced the error.
  76. //! \return Pointer to location within the parsed string where error occured.
  77. template<class Ch>
  78. Ch *where() const
  79. {
  80. return reinterpret_cast<Ch *>(m_where);
  81. }
  82. private:
  83. const char *m_what;
  84. void *m_where;
  85. };
  86. }
  87. #endif
  88. ///////////////////////////////////////////////////////////////////////////
  89. // Pool sizes
  90. #ifndef RAPIDXML_STATIC_POOL_SIZE
  91. // Size of static memory block of memory_pool.
  92. // Define RAPIDXML_STATIC_POOL_SIZE before including rapidxml.hpp if you want to override the default value.
  93. // No dynamic memory allocations are performed by memory_pool until static memory is exhausted.
  94. #define RAPIDXML_STATIC_POOL_SIZE (64 * 1024)
  95. #endif
  96. #ifndef RAPIDXML_DYNAMIC_POOL_SIZE
  97. // Size of dynamic memory block of memory_pool.
  98. // Define RAPIDXML_DYNAMIC_POOL_SIZE before including rapidxml.hpp if you want to override the default value.
  99. // After the static block is exhausted, dynamic blocks with approximately this size are allocated by memory_pool.
  100. #define RAPIDXML_DYNAMIC_POOL_SIZE (64 * 1024)
  101. #endif
  102. #ifndef RAPIDXML_ALIGNMENT
  103. // Memory allocation alignment.
  104. // Define RAPIDXML_ALIGNMENT before including rapidxml.hpp if you want to override the default value, which is the size of pointer.
  105. // All memory allocations for nodes, attributes and strings will be aligned to this value.
  106. // This must be a power of 2 and at least 1, otherwise memory_pool will not work.
  107. #define RAPIDXML_ALIGNMENT sizeof(void *)
  108. #endif
  109. namespace rapidxml
  110. {
  111. // Forward declarations
  112. template<class Ch> class xml_node;
  113. template<class Ch> class xml_attribute;
  114. template<class Ch> class xml_document;
  115. //! Enumeration listing all node types produced by the parser.
  116. //! Use xml_node::type() function to query node type.
  117. enum node_type
  118. {
  119. node_document, //!< A document node. Name and value are empty.
  120. node_element, //!< An element node. Name contains element name. Value contains text of first data node.
  121. node_data, //!< A data node. Name is empty. Value contains data text.
  122. node_cdata, //!< A CDATA node. Name is empty. Value contains data text.
  123. node_comment, //!< A comment node. Name is empty. Value contains comment text.
  124. node_declaration, //!< A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalone) are in node attributes.
  125. node_doctype, //!< A DOCTYPE node. Name is empty. Value contains DOCTYPE text.
  126. node_pi //!< A PI node. Name contains target. Value contains instructions.
  127. };
  128. ///////////////////////////////////////////////////////////////////////
  129. // Parsing flags
  130. //! Parse flag instructing the parser to not create data nodes.
  131. //! Text of first data node will still be placed in value of parent element, unless rapidxml::parse_no_element_values flag is also specified.
  132. //! Can be combined with other flags by use of | operator.
  133. //! <br><br>
  134. //! See xml_document::parse() function.
  135. const int parse_no_data_nodes = 0x1;
  136. //! Parse flag instructing the parser to not use text of first data node as a value of parent element.
  137. //! Can be combined with other flags by use of | operator.
  138. //! Note that child data nodes of element node take precendence over its value when printing.
  139. //! That is, if element has one or more child data nodes <em>and</em> a value, the value will be ignored.
  140. //! Use rapidxml::parse_no_data_nodes flag to prevent creation of data nodes if you want to manipulate data using values of elements.
  141. //! <br><br>
  142. //! See xml_document::parse() function.
  143. const int parse_no_element_values = 0x2;
  144. //! Parse flag instructing the parser to not place zero terminators after strings in the source text.
  145. //! By default zero terminators are placed, modifying source text.
  146. //! Can be combined with other flags by use of | operator.
  147. //! <br><br>
  148. //! See xml_document::parse() function.
  149. const int parse_no_string_terminators = 0x4;
  150. //! Parse flag instructing the parser to not translate entities in the source text.
  151. //! By default entities are translated, modifying source text.
  152. //! Can be combined with other flags by use of | operator.
  153. //! <br><br>
  154. //! See xml_document::parse() function.
  155. const int parse_no_entity_translation = 0x8;
  156. //! Parse flag instructing the parser to disable UTF-8 handling and assume plain 8 bit characters.
  157. //! By default, UTF-8 handling is enabled.
  158. //! Can be combined with other flags by use of | operator.
  159. //! <br><br>
  160. //! See xml_document::parse() function.
  161. const int parse_no_utf8 = 0x10;
  162. //! Parse flag instructing the parser to create XML declaration node.
  163. //! By default, declaration node is not created.
  164. //! Can be combined with other flags by use of | operator.
  165. //! <br><br>
  166. //! See xml_document::parse() function.
  167. const int parse_declaration_node = 0x20;
  168. //! Parse flag instructing the parser to create comments nodes.
  169. //! By default, comment nodes are not created.
  170. //! Can be combined with other flags by use of | operator.
  171. //! <br><br>
  172. //! See xml_document::parse() function.
  173. const int parse_comment_nodes = 0x40;
  174. //! Parse flag instructing the parser to create DOCTYPE node.
  175. //! By default, doctype node is not created.
  176. //! Although W3C specification allows at most one DOCTYPE node, RapidXml will silently accept documents with more than one.
  177. //! Can be combined with other flags by use of | operator.
  178. //! <br><br>
  179. //! See xml_document::parse() function.
  180. const int parse_doctype_node = 0x80;
  181. //! Parse flag instructing the parser to create PI nodes.
  182. //! By default, PI nodes are not created.
  183. //! Can be combined with other flags by use of | operator.
  184. //! <br><br>
  185. //! See xml_document::parse() function.
  186. const int parse_pi_nodes = 0x100;
  187. //! Parse flag instructing the parser to validate closing tag names.
  188. //! If not set, name inside closing tag is irrelevant to the parser.
  189. //! By default, closing tags are not validated.
  190. //! Can be combined with other flags by use of | operator.
  191. //! <br><br>
  192. //! See xml_document::parse() function.
  193. const int parse_validate_closing_tags = 0x200;
  194. //! Parse flag instructing the parser to trim all leading and trailing whitespace of data nodes.
  195. //! By default, whitespace is not trimmed.
  196. //! This flag does not cause the parser to modify source text.
  197. //! Can be combined with other flags by use of | operator.
  198. //! <br><br>
  199. //! See xml_document::parse() function.
  200. const int parse_trim_whitespace = 0x400;
  201. //! Parse flag instructing the parser to condense all whitespace runs of data nodes to a single space character.
  202. //! Trimming of leading and trailing whitespace of data is controlled by rapidxml::parse_trim_whitespace flag.
  203. //! By default, whitespace is not normalized.
  204. //! If this flag is specified, source text will be modified.
  205. //! Can be combined with other flags by use of | operator.
  206. //! <br><br>
  207. //! See xml_document::parse() function.
  208. const int parse_normalize_whitespace = 0x800;
  209. // Compound flags
  210. //! Parse flags which represent default behaviour of the parser.
  211. //! This is always equal to 0, so that all other flags can be simply ored together.
  212. //! Normally there is no need to inconveniently disable flags by anding with their negated (~) values.
  213. //! This also means that meaning of each flag is a <i>negation</i> of the default setting.
  214. //! For example, if flag name is rapidxml::parse_no_utf8, it means that utf-8 is <i>enabled</i> by default,
  215. //! and using the flag will disable it.
  216. //! <br><br>
  217. //! See xml_document::parse() function.
  218. const int parse_default = 0;
  219. //! A combination of parse flags that forbids any modifications of the source text.
  220. //! This also results in faster parsing. However, note that the following will occur:
  221. //! <ul>
  222. //! <li>names and values of nodes will not be zero terminated, you have to use xml_base::name_size() and xml_base::value_size() functions to determine where name and value ends</li>
  223. //! <li>entities will not be translated</li>
  224. //! <li>whitespace will not be normalized</li>
  225. //! </ul>
  226. //! See xml_document::parse() function.
  227. const int parse_non_destructive = parse_no_string_terminators | parse_no_entity_translation;
  228. //! A combination of parse flags resulting in fastest possible parsing, without sacrificing important data.
  229. //! <br><br>
  230. //! See xml_document::parse() function.
  231. const int parse_fastest = parse_non_destructive | parse_no_data_nodes;
  232. //! A combination of parse flags resulting in largest amount of data being extracted.
  233. //! This usually results in slowest parsing.
  234. //! <br><br>
  235. //! See xml_document::parse() function.
  236. const int parse_full = parse_declaration_node | parse_comment_nodes | parse_doctype_node | parse_pi_nodes | parse_validate_closing_tags;
  237. ///////////////////////////////////////////////////////////////////////
  238. // Internals
  239. //! \cond internal
  240. namespace internal
  241. {
  242. // Struct that contains lookup tables for the parser
  243. // It must be a template to allow correct linking (because it has static data members, which are defined in a header file).
  244. template<int Dummy>
  245. struct lookup_tables
  246. {
  247. static const unsigned char lookup_whitespace[256]; // Whitespace table
  248. static const unsigned char lookup_node_name[256]; // Node name table
  249. static const unsigned char lookup_text[256]; // Text table
  250. static const unsigned char lookup_text_pure_no_ws[256]; // Text table
  251. static const unsigned char lookup_text_pure_with_ws[256]; // Text table
  252. static const unsigned char lookup_attribute_name[256]; // Attribute name table
  253. static const unsigned char lookup_attribute_data_1[256]; // Attribute data table with single quote
  254. static const unsigned char lookup_attribute_data_1_pure[256]; // Attribute data table with single quote
  255. static const unsigned char lookup_attribute_data_2[256]; // Attribute data table with double quotes
  256. static const unsigned char lookup_attribute_data_2_pure[256]; // Attribute data table with double quotes
  257. static const unsigned char lookup_digits[256]; // Digits
  258. static const unsigned char lookup_upcase[256]; // To uppercase conversion table for ASCII characters
  259. };
  260. // Find length of the string
  261. template<class Ch>
  262. inline std::size_t measure(const Ch *p)
  263. {
  264. const Ch *tmp = p;
  265. while (*tmp)
  266. ++tmp;
  267. return tmp - p;
  268. }
  269. // Compare strings for equality
  270. template<class Ch>
  271. inline bool compare(const Ch *p1, std::size_t size1, const Ch *p2, std::size_t size2, bool case_sensitive)
  272. {
  273. if (size1 != size2)
  274. return false;
  275. if (case_sensitive)
  276. {
  277. for (const Ch *end = p1 + size1; p1 < end; ++p1, ++p2)
  278. if (*p1 != *p2)
  279. return false;
  280. }
  281. else
  282. {
  283. for (const Ch *end = p1 + size1; p1 < end; ++p1, ++p2)
  284. if (lookup_tables<0>::lookup_upcase[static_cast<unsigned char>(*p1)] != lookup_tables<0>::lookup_upcase[static_cast<unsigned char>(*p2)])
  285. return false;
  286. }
  287. return true;
  288. }
  289. }
  290. //! \endcond
  291. ///////////////////////////////////////////////////////////////////////
  292. // Memory pool
  293. //! This class is used by the parser to create new nodes and attributes, without overheads of dynamic memory allocation.
  294. //! In most cases, you will not need to use this class directly.
  295. //! However, if you need to create nodes manually or modify names/values of nodes,
  296. //! you are encouraged to use memory_pool of relevant xml_document to allocate the memory.
  297. //! Not only is this faster than allocating them by using <code>new</code> operator,
  298. //! but also their lifetime will be tied to the lifetime of document,
  299. //! possibly simplyfing memory management.
  300. //! <br><br>
  301. //! Call allocate_node() or allocate_attribute() functions to obtain new nodes or attributes from the pool.
  302. //! You can also call allocate_string() function to allocate strings.
  303. //! Such strings can then be used as names or values of nodes without worrying about their lifetime.
  304. //! Note that there is no <code>free()</code> function -- all allocations are freed at once when clear() function is called,
  305. //! or when the pool is destroyed.
  306. //! <br><br>
  307. //! It is also possible to create a standalone memory_pool, and use it
  308. //! to allocate nodes, whose lifetime will not be tied to any document.
  309. //! <br><br>
  310. //! Pool maintains <code>RAPIDXML_STATIC_POOL_SIZE</code> bytes of statically allocated memory.
  311. //! Until static memory is exhausted, no dynamic memory allocations are done.
  312. //! When static memory is exhausted, pool allocates additional blocks of memory of size <code>RAPIDXML_DYNAMIC_POOL_SIZE</code> each,
  313. //! by using global <code>new[]</code> and <code>delete[]</code> operators.
  314. //! This behaviour can be changed by setting custom allocation routines.
  315. //! Use set_allocator() function to set them.
  316. //! <br><br>
  317. //! Allocations for nodes, attributes and strings are aligned at <code>RAPIDXML_ALIGNMENT</code> bytes.
  318. //! This value defaults to the size of pointer on target architecture.
  319. //! <br><br>
  320. //! To obtain absolutely top performance from the parser,
  321. //! it is important that all nodes are allocated from a single, contiguous block of memory.
  322. //! Otherwise, cache misses when jumping between two (or more) disjoint blocks of memory can slow down parsing quite considerably.
  323. //! If required, you can tweak <code>RAPIDXML_STATIC_POOL_SIZE</code>, <code>RAPIDXML_DYNAMIC_POOL_SIZE</code> and <code>RAPIDXML_ALIGNMENT</code>
  324. //! to obtain best wasted memory to performance compromise.
  325. //! To do it, define their values before rapidxml.hpp file is included.
  326. //! \param Ch Character type of created nodes.
  327. template<class Ch = char>
  328. class memory_pool
  329. {
  330. public:
  331. //! \cond internal
  332. typedef void *(alloc_func)(std::size_t); // Type of user-defined function used to allocate memory
  333. typedef void (free_func)(void *); // Type of user-defined function used to free memory
  334. //! \endcond
  335. //! Constructs empty pool with default allocator functions.
  336. memory_pool()
  337. : m_alloc_func(0)
  338. , m_free_func(0)
  339. {
  340. init();
  341. }
  342. //! Destroys pool and frees all the memory.
  343. //! This causes memory occupied by nodes allocated by the pool to be freed.
  344. //! Nodes allocated from the pool are no longer valid.
  345. ~memory_pool()
  346. {
  347. clear();
  348. }
  349. //! Allocates a new node from the pool, and optionally assigns name and value to it.
  350. //! If the allocation request cannot be accomodated, this function will throw <code>std::bad_alloc</code>.
  351. //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function
  352. //! will call rapidxml::parse_error_handler() function.
  353. //! \param type Type of node to create.
  354. //! \param name Name to assign to the node, or 0 to assign no name.
  355. //! \param value Value to assign to the node, or 0 to assign no value.
  356. //! \param name_size Size of name to assign, or 0 to automatically calculate size from name string.
  357. //! \param value_size Size of value to assign, or 0 to automatically calculate size from value string.
  358. //! \return Pointer to allocated node. This pointer will never be NULL.
  359. xml_node<Ch> *allocate_node(node_type type,
  360. const Ch *name = 0, const Ch *value = 0,
  361. std::size_t name_size = 0, std::size_t value_size = 0)
  362. {
  363. void *memory = allocate_aligned(sizeof(xml_node<Ch>));
  364. xml_node<Ch> *node = new(memory) xml_node<Ch>(type);
  365. if (name)
  366. {
  367. if (name_size > 0)
  368. node->name(name, name_size);
  369. else
  370. node->name(name);
  371. }
  372. if (value)
  373. {
  374. if (value_size > 0)
  375. node->value(value, value_size);
  376. else
  377. node->value(value);
  378. }
  379. // halx99: store allocator pointer for edit element attributes & children standalone.
  380. node->set_allocator(this);
  381. return node;
  382. }
  383. //! Allocates a new attribute from the pool, and optionally assigns name and value to it.
  384. //! If the allocation request cannot be accomodated, this function will throw <code>std::bad_alloc</code>.
  385. //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function
  386. //! will call rapidxml::parse_error_handler() function.
  387. //! \param name Name to assign to the attribute, or 0 to assign no name.
  388. //! \param value Value to assign to the attribute, or 0 to assign no value.
  389. //! \param name_size Size of name to assign, or 0 to automatically calculate size from name string.
  390. //! \param value_size Size of value to assign, or 0 to automatically calculate size from value string.
  391. //! \return Pointer to allocated attribute. This pointer will never be NULL.
  392. xml_attribute<Ch> *allocate_attribute(const Ch *name = 0, const Ch *value = 0,
  393. std::size_t name_size = 0, std::size_t value_size = 0)
  394. {
  395. void *memory = allocate_aligned(sizeof(xml_attribute<Ch>));
  396. xml_attribute<Ch> *attribute = new(memory) xml_attribute<Ch>;
  397. if (name)
  398. {
  399. if (name_size > 0)
  400. attribute->name(name, name_size);
  401. else
  402. attribute->name(name);
  403. }
  404. if (value)
  405. {
  406. if (value_size > 0)
  407. attribute->value(value, value_size);
  408. else
  409. attribute->value(value);
  410. }
  411. return attribute;
  412. }
  413. //! Allocates a char array of given size from the pool, and optionally copies a given string to it.
  414. //! If the allocation request cannot be accomodated, this function will throw <code>std::bad_alloc</code>.
  415. //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function
  416. //! will call rapidxml::parse_error_handler() function.
  417. //! \param source String to initialize the allocated memory with, or 0 to not initialize it.
  418. //! \param size Number of characters to allocate, or zero to calculate it automatically from source string length; if size is 0, source string must be specified and null terminated.
  419. //! \return Pointer to allocated char array. This pointer will never be NULL.
  420. Ch *allocate_string(const Ch *source = 0, std::size_t size = 0)
  421. {
  422. assert(source || size); // Either source or size (or both) must be specified
  423. if (size == 0)
  424. size = internal::measure(source) + 1;
  425. Ch *result = static_cast<Ch *>(allocate_aligned(size * sizeof(Ch)));
  426. if (source)
  427. memcpy(result, source, size * sizeof(Ch));
  428. /*for (std::size_t i = 0; i < size; ++i)
  429. result[i] = source[i];*/
  430. return result;
  431. }
  432. //! Clones an xml_node and its hierarchy of child nodes and attributes.
  433. //! Nodes and attributes are allocated from this memory pool.
  434. //! Names and values are not cloned, they are shared between the clone and the source.
  435. //! Result node can be optionally specified as a second parameter,
  436. //! in which case its contents will be replaced with cloned source node.
  437. //! This is useful when you want to clone entire document.
  438. //! \param source Node to clone.
  439. //! \param result Node to put results in, or 0 to automatically allocate result node
  440. //! \return Pointer to cloned node. This pointer will never be NULL.
  441. xml_node<Ch> *clone_node(const xml_node<Ch> *source, xml_node<Ch> *result = 0)
  442. {
  443. // Prepare result node
  444. if (result)
  445. {
  446. result->remove_all_attributes();
  447. result->remove_all_nodes();
  448. result->type(source->type());
  449. }
  450. else
  451. result = allocate_node(source->type());
  452. // Clone name and value
  453. result->name(source->name(), source->name_size());
  454. result->value(source->value(), source->value_size());
  455. // Clone child nodes and attributes
  456. for (xml_node<Ch> *child = source->first_node(); child; child = child->next_sibling())
  457. result->append_node(clone_node(child));
  458. for (xml_attribute<Ch> *attr = source->first_attribute(); attr; attr = attr->next_attribute())
  459. result->append_attribute(allocate_attribute(attr->name(), attr->value(), attr->name_size(), attr->value_size()));
  460. return result;
  461. }
  462. //! Clears the pool.
  463. //! This causes memory occupied by nodes allocated by the pool to be freed.
  464. //! Any nodes or strings allocated from the pool will no longer be valid.
  465. void clear()
  466. {
  467. while (m_begin != m_static_memory)
  468. {
  469. char *previous_begin = reinterpret_cast<header *>(align(m_begin))->previous_begin;
  470. if (m_free_func)
  471. m_free_func(m_begin);
  472. else
  473. delete[] m_begin;
  474. m_begin = previous_begin;
  475. }
  476. init();
  477. }
  478. //! Sets or resets the user-defined memory allocation functions for the pool.
  479. //! This can only be called when no memory is allocated from the pool yet, otherwise results are undefined.
  480. //! Allocation function must not return invalid pointer on failure. It should either throw,
  481. //! stop the program, or use <code>longjmp()</code> function to pass control to other place of program.
  482. //! If it returns invalid pointer, results are undefined.
  483. //! <br><br>
  484. //! User defined allocation functions must have the following forms:
  485. //! <br><code>
  486. //! <br>void *allocate(std::size_t size);
  487. //! <br>void free(void *pointer);
  488. //! </code><br>
  489. //! \param af Allocation function, or 0 to restore default function
  490. //! \param ff Free function, or 0 to restore default function
  491. void set_allocator(alloc_func *af, free_func *ff)
  492. {
  493. assert(m_begin == m_static_memory && m_ptr == align(m_begin)); // Verify that no memory is allocated yet
  494. m_alloc_func = af;
  495. m_free_func = ff;
  496. }
  497. private:
  498. struct header
  499. {
  500. char *previous_begin;
  501. };
  502. void init()
  503. {
  504. m_begin = m_static_memory;
  505. m_ptr = align(m_begin);
  506. m_end = m_static_memory + sizeof(m_static_memory);
  507. }
  508. char *align(char *ptr)
  509. {
  510. std::size_t alignment = ((RAPIDXML_ALIGNMENT - (std::size_t(ptr) & (RAPIDXML_ALIGNMENT - 1))) & (RAPIDXML_ALIGNMENT - 1));
  511. return ptr + alignment;
  512. }
  513. char *allocate_raw(std::size_t size)
  514. {
  515. // Allocate
  516. void *memory;
  517. if (m_alloc_func) // Allocate memory using either user-specified allocation function or global operator new[]
  518. {
  519. memory = m_alloc_func(size);
  520. assert(memory); // Allocator is not allowed to return 0, on failure it must either throw, stop the program or use longjmp
  521. }
  522. else
  523. {
  524. memory = new char[size];
  525. #ifdef RAPIDXML_NO_EXCEPTIONS
  526. if (!memory) // If exceptions are disabled, verify memory allocation, because new will not be able to throw bad_alloc
  527. RAPIDXML_PARSE_ERROR("out of memory", 0);
  528. #endif
  529. }
  530. return static_cast<char *>(memory);
  531. }
  532. void *allocate_aligned(std::size_t size)
  533. {
  534. // Calculate aligned pointer
  535. char *result = align(m_ptr);
  536. // If not enough memory left in current pool, allocate a new pool
  537. if (result + size > m_end)
  538. {
  539. // Calculate required pool size (may be bigger than RAPIDXML_DYNAMIC_POOL_SIZE)
  540. std::size_t pool_size = RAPIDXML_DYNAMIC_POOL_SIZE;
  541. if (pool_size < size)
  542. pool_size = size;
  543. // Allocate
  544. std::size_t alloc_size = sizeof(header) + (2 * RAPIDXML_ALIGNMENT - 2) + pool_size; // 2 alignments required in worst case: one for header, one for actual allocation
  545. char *raw_memory = allocate_raw(alloc_size);
  546. // Setup new pool in allocated memory
  547. char *pool = align(raw_memory);
  548. header *new_header = reinterpret_cast<header *>(pool);
  549. new_header->previous_begin = m_begin;
  550. m_begin = raw_memory;
  551. m_ptr = pool + sizeof(header);
  552. m_end = raw_memory + alloc_size;
  553. // Calculate aligned pointer again using new pool
  554. result = align(m_ptr);
  555. }
  556. // Update pool and return aligned pointer
  557. m_ptr = result + size;
  558. return result;
  559. }
  560. char *m_begin; // Start of raw memory making up current pool
  561. char *m_ptr; // First free byte in current pool
  562. char *m_end; // One past last available byte in current pool
  563. char m_static_memory[RAPIDXML_STATIC_POOL_SIZE]; // Static raw memory
  564. alloc_func *m_alloc_func; // Allocator function, or 0 if default is to be used
  565. free_func *m_free_func; // Free function, or 0 if default is to be used
  566. };
  567. ///////////////////////////////////////////////////////////////////////////
  568. // XML base
  569. //! Base class for xml_node and xml_attribute implementing common functions:
  570. //! name(), name_size(), value(), value_size() and parent().
  571. //! \param Ch Character type to use
  572. template<class Ch = char>
  573. class xml_base
  574. {
  575. public:
  576. ///////////////////////////////////////////////////////////////////////////
  577. // Construction & destruction
  578. // Construct a base with empty name, value and parent
  579. xml_base()
  580. : m_name(0)
  581. , m_value(0)
  582. , m_parent(0)
  583. {
  584. }
  585. ///////////////////////////////////////////////////////////////////////////
  586. // Node data access
  587. //! Gets name of the node.
  588. //! Interpretation of name depends on type of node.
  589. //! Note that name will not be zero-terminated if rapidxml::parse_no_string_terminators option was selected during parse.
  590. //! <br><br>
  591. //! Use name_size() function to determine length of the name.
  592. //! \return Name of node, or empty string if node has no name.
  593. Ch *name() const
  594. {
  595. return m_name ? m_name : nullstr();
  596. }
  597. //! Gets size of node name, not including terminator character.
  598. //! This function works correctly irrespective of whether name is or is not zero terminated.
  599. //! \return Size of node name, in characters.
  600. std::size_t name_size() const
  601. {
  602. return m_name ? m_name_size : 0;
  603. }
  604. //! Gets value of node.
  605. //! Interpretation of value depends on type of node.
  606. //! Note that value will not be zero-terminated if rapidxml::parse_no_string_terminators option was selected during parse.
  607. //! <br><br>
  608. //! Use value_size() function to determine length of the value.
  609. //! \return Value of node, or empty string if node has no value.
  610. Ch *value() const
  611. {
  612. return m_value ? m_value : nullstr();
  613. }
  614. //! Gets size of node value, not including terminator character.
  615. //! This function works correctly irrespective of whether value is or is not zero terminated.
  616. //! \return Size of node value, in characters.
  617. std::size_t value_size() const
  618. {
  619. return m_value ? m_value_size : 0;
  620. }
  621. ///////////////////////////////////////////////////////////////////////////
  622. // Node modification
  623. //! Sets name of node to a non zero-terminated string.
  624. //! See \ref ownership_of_strings.
  625. //! <br><br>
  626. //! Note that node does not own its name or value, it only stores a pointer to it.
  627. //! It will not delete or otherwise free the pointer on destruction.
  628. //! It is reponsibility of the user to properly manage lifetime of the string.
  629. //! The easiest way to achieve it is to use memory_pool of the document to allocate the string -
  630. //! on destruction of the document the string will be automatically freed.
  631. //! <br><br>
  632. //! Size of name must be specified separately, because name does not have to be zero terminated.
  633. //! Use name(const Ch *) function to have the length automatically calculated (string must be zero terminated).
  634. //! \param name Name of node to set. Does not have to be zero terminated.
  635. //! \param size Size of name, in characters. This does not include zero terminator, if one is present.
  636. void name(const Ch *name, std::size_t size)
  637. {
  638. m_name = const_cast<Ch *>(name);
  639. m_name_size = size;
  640. }
  641. //! Sets name of node to a zero-terminated string.
  642. //! See also \ref ownership_of_strings and xml_node::name(const Ch *, std::size_t).
  643. //! \param name Name of node to set. Must be zero terminated.
  644. void name(const Ch *name)
  645. {
  646. this->name(name, internal::measure(name));
  647. }
  648. //! Sets value of node to a non zero-terminated string.
  649. //! See \ref ownership_of_strings.
  650. //! <br><br>
  651. //! Note that node does not own its name or value, it only stores a pointer to it.
  652. //! It will not delete or otherwise free the pointer on destruction.
  653. //! It is reponsibility of the user to properly manage lifetime of the string.
  654. //! The easiest way to achieve it is to use memory_pool of the document to allocate the string -
  655. //! on destruction of the document the string will be automatically freed.
  656. //! <br><br>
  657. //! Size of value must be specified separately, because it does not have to be zero terminated.
  658. //! Use value(const Ch *) function to have the length automatically calculated (string must be zero terminated).
  659. //! <br><br>
  660. //! If an element has a child node of type node_data, it will take precedence over element value when printing.
  661. //! If you want to manipulate data of elements using values, use parser flag rapidxml::parse_no_data_nodes to prevent creation of data nodes by the parser.
  662. //! \param value value of node to set. Does not have to be zero terminated.
  663. //! \param size Size of value, in characters. This does not include zero terminator, if one is present.
  664. void value(const Ch *value, std::size_t size)
  665. {
  666. m_value = const_cast<Ch *>(value);
  667. m_value_size = size;
  668. }
  669. //! Sets value of node to a zero-terminated string.
  670. //! See also \ref ownership_of_strings and xml_node::value(const Ch *, std::size_t).
  671. //! \param value Vame of node to set. Must be zero terminated.
  672. void value(const Ch *value)
  673. {
  674. this->value(value, internal::measure(value));
  675. }
  676. ///////////////////////////////////////////////////////////////////////////
  677. // Related nodes access
  678. //! Gets node parent.
  679. //! \return Pointer to parent node, or 0 if there is no parent.
  680. xml_node<Ch> *parent() const
  681. {
  682. return m_parent;
  683. }
  684. protected:
  685. // Return empty string
  686. static Ch *nullstr()
  687. {
  688. static Ch zero = Ch('\0');
  689. return &zero;
  690. }
  691. Ch *m_name; // Name of node, or 0 if no name
  692. Ch *m_value; // Value of node, or 0 if no value
  693. std::size_t m_name_size; // Length of node name, or undefined of no name
  694. std::size_t m_value_size; // Length of node value, or undefined if no value
  695. xml_node<Ch> *m_parent; // Pointer to parent node, or 0 if none
  696. };
  697. //! Class representing attribute node of XML document.
  698. //! Each attribute has name and value strings, which are available through name() and value() functions (inherited from xml_base).
  699. //! Note that after parse, both name and value of attribute will point to interior of source text used for parsing.
  700. //! Thus, this text must persist in memory for the lifetime of attribute.
  701. //! \param Ch Character type to use.
  702. template<class Ch = char>
  703. class xml_attribute : public xml_base<Ch>
  704. {
  705. friend class xml_node<Ch>;
  706. public:
  707. ///////////////////////////////////////////////////////////////////////////
  708. // Construction & destruction
  709. //! Constructs an empty attribute with the specified type.
  710. //! Consider using memory_pool of appropriate xml_document if allocating attributes manually.
  711. xml_attribute()
  712. {
  713. }
  714. ///////////////////////////////////////////////////////////////////////////
  715. // Related nodes access
  716. //! Gets document of which attribute is a child.
  717. //! \return Pointer to document that contains this attribute, or 0 if there is no parent document.
  718. xml_document<Ch> *document() const
  719. {
  720. if (xml_node<Ch> *node = this->parent())
  721. {
  722. while (node->parent())
  723. node = node->parent();
  724. return node->type() == node_document ? static_cast<xml_document<Ch> *>(node) : 0;
  725. }
  726. else
  727. return 0;
  728. }
  729. //! Gets previous attribute, optionally matching attribute name.
  730. //! \param name Name of attribute to find, or 0 to return previous attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
  731. //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
  732. //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
  733. //! \return Pointer to found attribute, or 0 if not found.
  734. xml_attribute<Ch> *previous_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
  735. {
  736. if (name)
  737. {
  738. if (name_size == 0)
  739. name_size = internal::measure(name);
  740. for (xml_attribute<Ch> *attribute = m_prev_attribute; attribute; attribute = attribute->m_prev_attribute)
  741. if (internal::compare(attribute->name(), attribute->name_size(), name, name_size, case_sensitive))
  742. return attribute;
  743. return 0;
  744. }
  745. else
  746. return this->m_parent ? m_prev_attribute : 0;
  747. }
  748. //! Gets next attribute, optionally matching attribute name.
  749. //! \param name Name of attribute to find, or 0 to return next attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
  750. //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
  751. //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
  752. //! \return Pointer to found attribute, or 0 if not found.
  753. xml_attribute<Ch> *next_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
  754. {
  755. if (name)
  756. {
  757. if (name_size == 0)
  758. name_size = internal::measure(name);
  759. for (xml_attribute<Ch> *attribute = m_next_attribute; attribute; attribute = attribute->m_next_attribute)
  760. if (internal::compare(attribute->name(), attribute->name_size(), name, name_size, case_sensitive))
  761. return attribute;
  762. return 0;
  763. }
  764. else
  765. return this->m_parent ? m_next_attribute : 0;
  766. }
  767. private:
  768. xml_attribute<Ch> *m_prev_attribute; // Pointer to previous sibling of attribute, or 0 if none; only valid if parent is non-zero
  769. xml_attribute<Ch> *m_next_attribute; // Pointer to next sibling of attribute, or 0 if none; only valid if parent is non-zero
  770. };
  771. ///////////////////////////////////////////////////////////////////////////
  772. // XML node
  773. //! Class representing a node of XML document.
  774. //! Each node may have associated name and value strings, which are available through name() and value() functions.
  775. //! Interpretation of name and value depends on type of the node.
  776. //! Type of node can be determined by using type() function.
  777. //! <br><br>
  778. //! Note that after parse, both name and value of node, if any, will point interior of source text used for parsing.
  779. //! Thus, this text must persist in the memory for the lifetime of node.
  780. //! \param Ch Character type to use.
  781. template<class Ch = char>
  782. class xml_node : public xml_base<Ch>
  783. {
  784. public:
  785. ///////////////////////////////////////////////////////////////////////////
  786. // Construction & destruction
  787. //! Constructs an empty node with the specified type.
  788. //! Consider using memory_pool of appropriate document to allocate nodes manually.
  789. //! \param type Type of node to construct.
  790. xml_node(node_type type)
  791. : m_type(type)
  792. , m_first_node(nullptr)
  793. , m_first_attribute(nullptr)
  794. , m_last_node(nullptr)
  795. , m_last_attribute(nullptr)
  796. , m_next_sibling(nullptr)
  797. , m_prev_sibling(nullptr)
  798. , m_allocator(nullptr)
  799. {
  800. }
  801. ///////////////////////////////////////////////////////////////////////////
  802. // Node data access
  803. //! Gets type of node.
  804. //! \return Type of node.
  805. node_type type() const
  806. {
  807. return m_type;
  808. }
  809. ///////////////////////////////////////////////////////////////////////////
  810. // Related nodes access
  811. //! Gets document of which node is a child.
  812. //! \return Pointer to document that contains this node, or 0 if there is no parent document.
  813. xml_document<Ch> *document() const
  814. {
  815. xml_node<Ch> *node = const_cast<xml_node<Ch> *>(this);
  816. while (node->parent())
  817. node = node->parent();
  818. return node->type() == node_document ? static_cast<xml_document<Ch> *>(node) : 0;
  819. }
  820. //! Gets first child node, optionally matching node name.
  821. //! \param name Name of child to find, or 0 to return first child regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
  822. //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
  823. //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
  824. //! \return Pointer to found child, or 0 if not found.
  825. xml_node<Ch> *first_node(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
  826. {
  827. if (name)
  828. {
  829. if (name_size == 0)
  830. name_size = internal::measure(name);
  831. for (xml_node<Ch> *child = m_first_node; child; child = child->next_sibling())
  832. if (internal::compare(child->name(), child->name_size(), name, name_size, case_sensitive))
  833. return child;
  834. return 0;
  835. }
  836. else
  837. return m_first_node;
  838. }
  839. //! Gets last child node, optionally matching node name.
  840. //! Behaviour is undefined if node has no children.
  841. //! Use first_node() to test if node has children.
  842. //! \param name Name of child to find, or 0 to return last child regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
  843. //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
  844. //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
  845. //! \return Pointer to found child, or 0 if not found.
  846. xml_node<Ch> *last_node(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
  847. {
  848. assert(m_first_node); // Cannot query for last child if node has no children
  849. if (name)
  850. {
  851. if (name_size == 0)
  852. name_size = internal::measure(name);
  853. for (xml_node<Ch> *child = m_last_node; child; child = child->previous_sibling())
  854. if (internal::compare(child->name(), child->name_size(), name, name_size, case_sensitive))
  855. return child;
  856. return 0;
  857. }
  858. else
  859. return m_last_node;
  860. }
  861. //! Gets previous sibling node, optionally matching node name.
  862. //! Behaviour is undefined if node has no parent.
  863. //! Use parent() to test if node has a parent.
  864. //! \param name Name of sibling to find, or 0 to return previous sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
  865. //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
  866. //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
  867. //! \return Pointer to found sibling, or 0 if not found.
  868. xml_node<Ch> *previous_sibling(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
  869. {
  870. assert(this->m_parent); // Cannot query for siblings if node has no parent
  871. if (name)
  872. {
  873. if (name_size == 0)
  874. name_size = internal::measure(name);
  875. for (xml_node<Ch> *sibling = m_prev_sibling; sibling; sibling = sibling->m_prev_sibling)
  876. if (internal::compare(sibling->name(), sibling->name_size(), name, name_size, case_sensitive))
  877. return sibling;
  878. return 0;
  879. }
  880. else
  881. return m_prev_sibling;
  882. }
  883. //! Gets next sibling node, optionally matching node name.
  884. //! Behaviour is undefined if node has no parent.
  885. //! Use parent() to test if node has a parent.
  886. //! \param name Name of sibling to find, or 0 to return next sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
  887. //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
  888. //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
  889. //! \return Pointer to found sibling, or 0 if not found.
  890. xml_node<Ch> *next_sibling(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
  891. {
  892. assert(this->m_parent); // Cannot query for siblings if node has no parent
  893. if (name)
  894. {
  895. if (name_size == 0)
  896. name_size = internal::measure(name);
  897. for (xml_node<Ch> *sibling = m_next_sibling; sibling; sibling = sibling->m_next_sibling)
  898. if (internal::compare(sibling->name(), sibling->name_size(), name, name_size, case_sensitive))
  899. return sibling;
  900. return 0;
  901. }
  902. else
  903. return m_next_sibling;
  904. }
  905. //! Gets first attribute of node, optionally matching attribute name.
  906. //! \param name Name of attribute to find, or 0 to return first attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
  907. //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
  908. //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
  909. //! \return Pointer to found attribute, or 0 if not found.
  910. xml_attribute<Ch> *first_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
  911. {
  912. if (name)
  913. {
  914. if (name_size == 0)
  915. name_size = internal::measure(name);
  916. for (xml_attribute<Ch> *attribute = m_first_attribute; attribute; attribute = attribute->m_next_attribute)
  917. if (internal::compare(attribute->name(), attribute->name_size(), name, name_size, case_sensitive))
  918. return attribute;
  919. return 0;
  920. }
  921. else
  922. return m_first_attribute;
  923. }
  924. //! Gets last attribute of node, optionally matching attribute name.
  925. //! \param name Name of attribute to find, or 0 to return last attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero
  926. //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string
  927. //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters
  928. //! \return Pointer to found attribute, or 0 if not found.
  929. xml_attribute<Ch> *last_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
  930. {
  931. if (name)
  932. {
  933. if (name_size == 0)
  934. name_size = internal::measure(name);
  935. for (xml_attribute<Ch> *attribute = m_last_attribute; attribute; attribute = attribute->m_prev_attribute)
  936. if (internal::compare(attribute->name(), attribute->name_size(), name, name_size, case_sensitive))
  937. return attribute;
  938. return 0;
  939. }
  940. else
  941. return m_first_attribute ? m_last_attribute : 0;
  942. }
  943. ///////////////////////////////////////////////////////////////////////////
  944. // Node modification
  945. //! Sets type of node.
  946. //! \param type Type of node to set.
  947. void type(node_type type)
  948. {
  949. m_type = type;
  950. }
  951. ///////////////////////////////////////////////////////////////////////////
  952. // Node manipulation
  953. //! Prepends a new child node.
  954. //! The prepended child becomes the first child, and all existing children are moved one position back.
  955. //! \param child Node to prepend.
  956. void prepend_node(xml_node<Ch> *child)
  957. {
  958. assert(child && !child->parent() && child->type() != node_document);
  959. if (first_node())
  960. {
  961. child->m_next_sibling = m_first_node;
  962. m_first_node->m_prev_sibling = child;
  963. }
  964. else
  965. {
  966. child->m_next_sibling = 0;
  967. m_last_node = child;
  968. }
  969. m_first_node = child;
  970. child->m_parent = this;
  971. child->m_prev_sibling = 0;
  972. }
  973. //! Appends a new child node.
  974. //! The appended child becomes the last child.
  975. //! \param child Node to append.
  976. void append_node(xml_node<Ch> *child)
  977. {
  978. assert(child && !child->parent() && child->type() != node_document);
  979. if (first_node())
  980. {
  981. child->m_prev_sibling = m_last_node;
  982. m_last_node->m_next_sibling = child;
  983. }
  984. else
  985. {
  986. child->m_prev_sibling = 0;
  987. m_first_node = child;
  988. }
  989. m_last_node = child;
  990. child->m_parent = this;
  991. child->m_next_sibling = 0;
  992. }
  993. //! Inserts a new child node at specified place inside the node.
  994. //! All children after and including the specified node are moved one position back.
  995. //! \param where Place where to insert the child, or 0 to insert at the back.
  996. //! \param child Node to insert.
  997. void insert_node(xml_node<Ch> *where, xml_node<Ch> *child)
  998. {
  999. assert(!where || where->parent() == this);
  1000. assert(child && !child->parent() && child->type() != node_document);
  1001. if (where == m_first_node)
  1002. prepend_node(child);
  1003. else if (where == 0)
  1004. append_node(child);
  1005. else
  1006. {
  1007. child->m_prev_sibling = where->m_prev_sibling;
  1008. child->m_next_sibling = where;
  1009. where->m_prev_sibling->m_next_sibling = child;
  1010. where->m_prev_sibling = child;
  1011. child->m_parent = this;
  1012. }
  1013. }
  1014. //! Removes first child node.
  1015. //! If node has no children, behaviour is undefined.
  1016. //! Use first_node() to test if node has children.
  1017. void remove_first_node()
  1018. {
  1019. assert(first_node());
  1020. xml_node<Ch> *child = m_first_node;
  1021. m_first_node = child->m_next_sibling;
  1022. if (child->m_next_sibling)
  1023. child->m_next_sibling->m_prev_sibling = 0;
  1024. else
  1025. m_last_node = 0;
  1026. child->m_parent = 0;
  1027. }
  1028. //! Removes last child of the node.
  1029. //! If node has no children, behaviour is undefined.
  1030. //! Use first_node() to test if node has children.
  1031. void remove_last_node()
  1032. {
  1033. assert(first_node());
  1034. xml_node<Ch> *child = m_last_node;
  1035. if (child->m_prev_sibling)
  1036. {
  1037. m_last_node = child->m_prev_sibling;
  1038. child->m_prev_sibling->m_next_sibling = 0;
  1039. }
  1040. else
  1041. m_first_node = 0;
  1042. child->m_parent = 0;
  1043. }
  1044. //! Removes specified child from the node
  1045. // \param where Pointer to child to be removed.
  1046. void remove_node(xml_node<Ch> *where)
  1047. {
  1048. assert(where && where->parent() == this);
  1049. assert(first_node());
  1050. if (where == m_first_node)
  1051. remove_first_node();
  1052. else if (where == m_last_node)
  1053. remove_last_node();
  1054. else
  1055. {
  1056. where->m_prev_sibling->m_next_sibling = where->m_next_sibling;
  1057. where->m_next_sibling->m_prev_sibling = where->m_prev_sibling;
  1058. where->m_parent = 0;
  1059. }
  1060. }
  1061. //! Removes all child nodes (but not attributes).
  1062. void remove_all_nodes()
  1063. {
  1064. for (xml_node<Ch> *node = first_node(); node; node = node->m_next_sibling)
  1065. node->m_parent = 0;
  1066. m_first_node = 0;
  1067. }
  1068. //! Prepends a new attribute to the node.
  1069. //! \param attribute Attribute to prepend.
  1070. void prepend_attribute(xml_attribute<Ch> *attribute)
  1071. {
  1072. assert(attribute && !attribute->parent());
  1073. if (first_attribute())
  1074. {
  1075. attribute->m_next_attribute = m_first_attribute;
  1076. m_first_attribute->m_prev_attribute = attribute;
  1077. }
  1078. else
  1079. {
  1080. attribute->m_next_attribute = 0;
  1081. m_last_attribute = attribute;
  1082. }
  1083. m_first_attribute = attribute;
  1084. attribute->m_parent = this;
  1085. attribute->m_prev_attribute = 0;
  1086. }
  1087. //! Appends a new attribute to the node.
  1088. //! \param attribute Attribute to append.
  1089. void append_attribute(xml_attribute<Ch> *attribute)
  1090. {
  1091. assert(attribute && !attribute->parent());
  1092. if (first_attribute())
  1093. {
  1094. attribute->m_prev_attribute = m_last_attribute;
  1095. m_last_attribute->m_next_attribute = attribute;
  1096. }
  1097. else
  1098. {
  1099. attribute->m_prev_attribute = 0;
  1100. m_first_attribute = attribute;
  1101. }
  1102. m_last_attribute = attribute;
  1103. attribute->m_parent = this;
  1104. attribute->m_next_attribute = 0;
  1105. }
  1106. //! Inserts a new attribute at specified place inside the node.
  1107. //! All attributes after and including the specified attribute are moved one position back.
  1108. //! \param where Place where to insert the attribute, or 0 to insert at the back.
  1109. //! \param attribute Attribute to insert.
  1110. void insert_attribute(xml_attribute<Ch> *where, xml_attribute<Ch> *attribute)
  1111. {
  1112. assert(!where || where->parent() == this);
  1113. assert(attribute && !attribute->parent());
  1114. if (where == m_first_attribute)
  1115. prepend_attribute(attribute);
  1116. else if (where == 0)
  1117. append_attribute(attribute);
  1118. else
  1119. {
  1120. attribute->m_prev_attribute = where->m_prev_attribute;
  1121. attribute->m_next_attribute = where;
  1122. where->m_prev_attribute->m_next_attribute = attribute;
  1123. where->m_prev_attribute = attribute;
  1124. attribute->m_parent = this;
  1125. }
  1126. }
  1127. //! Removes first attribute of the node.
  1128. //! If node has no attributes, behaviour is undefined.
  1129. //! Use first_attribute() to test if node has attributes.
  1130. void remove_first_attribute()
  1131. {
  1132. assert(first_attribute());
  1133. xml_attribute<Ch> *attribute = m_first_attribute;
  1134. if (attribute->m_next_attribute)
  1135. {
  1136. attribute->m_next_attribute->m_prev_attribute = 0;
  1137. }
  1138. else
  1139. m_last_attribute = 0;
  1140. attribute->m_parent = 0;
  1141. m_first_attribute = attribute->m_next_attribute;
  1142. }
  1143. //! Removes last attribute of the node.
  1144. //! If node has no attributes, behaviour is undefined.
  1145. //! Use first_attribute() to test if node has attributes.
  1146. void remove_last_attribute()
  1147. {
  1148. assert(first_attribute());
  1149. xml_attribute<Ch> *attribute = m_last_attribute;
  1150. if (attribute->m_prev_attribute)
  1151. {
  1152. attribute->m_prev_attribute->m_next_attribute = 0;
  1153. m_last_attribute = attribute->m_prev_attribute;
  1154. }
  1155. else
  1156. m_first_attribute = 0;
  1157. attribute->m_parent = 0;
  1158. }
  1159. //! Removes specified attribute from node.
  1160. //! \param where Pointer to attribute to be removed.
  1161. void remove_attribute(xml_attribute<Ch> *where)
  1162. {
  1163. assert(first_attribute() && where->parent() == this);
  1164. if (where == m_first_attribute)
  1165. remove_first_attribute();
  1166. else if (where == m_last_attribute)
  1167. remove_last_attribute();
  1168. else
  1169. {
  1170. where->m_prev_attribute->m_next_attribute = where->m_next_attribute;
  1171. where->m_next_attribute->m_prev_attribute = where->m_prev_attribute;
  1172. where->m_parent = 0;
  1173. }
  1174. }
  1175. //! Removes all attributes of node.
  1176. void remove_all_attributes()
  1177. {
  1178. for (xml_attribute<Ch> *attribute = first_attribute(); attribute; attribute = attribute->m_next_attribute)
  1179. attribute->m_parent = 0;
  1180. m_first_attribute = 0;
  1181. }
  1182. memory_pool<Ch>* get_allocator()
  1183. {
  1184. return m_allocator != nullptr ? m_allocator : this->document();
  1185. }
  1186. void set_allocator(memory_pool<Ch>* allocator)
  1187. {
  1188. this->m_allocator = allocator;
  1189. }
  1190. private:
  1191. ///////////////////////////////////////////////////////////////////////////
  1192. // Restrictions
  1193. // No copying
  1194. xml_node(const xml_node &);
  1195. void operator =(const xml_node &);
  1196. ///////////////////////////////////////////////////////////////////////////
  1197. // Data members
  1198. // Note that some of the pointers below have UNDEFINED values if certain other pointers are 0.
  1199. // This is required for maximum performance, as it allows the parser to omit initialization of
  1200. // unneded/redundant values.
  1201. //
  1202. // The rules are as follows:
  1203. // 1. first_node and first_attribute contain valid pointers, or 0 if node has no children/attributes respectively
  1204. // 2. last_node and last_attribute are valid only if node has at least one child/attribute respectively, otherwise they contain garbage
  1205. // 3. prev_sibling and next_sibling are valid only if node has a parent, otherwise they contain garbage
  1206. node_type m_type; // Type of node; always valid
  1207. xml_node<Ch> *m_first_node; // Pointer to first child node, or 0 if none; always valid
  1208. xml_node<Ch> *m_last_node; // Pointer to last child node, or 0 if none; this value is only valid if m_first_node is non-zero
  1209. xml_attribute<Ch> *m_first_attribute; // Pointer to first attribute of node, or 0 if none; always valid
  1210. xml_attribute<Ch> *m_last_attribute; // Pointer to last attribute of node, or 0 if none; this value is only valid if m_first_attribute is non-zero
  1211. xml_node<Ch> *m_prev_sibling; // Pointer to previous sibling of node, or 0 if none; this value is only valid if m_parent is non-zero
  1212. xml_node<Ch> *m_next_sibling; // Pointer to next sibling of node, or 0 if none; this value is only valid if m_parent is non-zero
  1213. memory_pool<Ch>* m_allocator;
  1214. };
  1215. ///////////////////////////////////////////////////////////////////////////
  1216. // XML document
  1217. //! This class represents root of the DOM hierarchy.
  1218. //! It is also an xml_node and a memory_pool through public inheritance.
  1219. //! Use parse() function to build a DOM tree from a zero-terminated XML text string.
  1220. //! parse() function allocates memory for nodes and attributes by using functions of xml_document,
  1221. //! which are inherited from memory_pool.
  1222. //! To access root node of the document, use the document itself, as if it was an xml_node.
  1223. //! \param Ch Character type to use.
  1224. template<class Ch = char>
  1225. class xml_document : public xml_node<Ch>, public memory_pool<Ch>
  1226. {
  1227. public:
  1228. //! Constructs empty XML document
  1229. xml_document()
  1230. : xml_node<Ch>(node_document)
  1231. {
  1232. endptr_ = nullptr;
  1233. }
  1234. Ch *endptr_;
  1235. //! Parses zero-terminated XML string according to given flags.
  1236. //! Passed string will be modified by the parser, unless rapidxml::parse_non_destructive flag is used.
  1237. //! The string must persist for the lifetime of the document.
  1238. //! In case of error, rapidxml::parse_error exception will be thrown.
  1239. //! <br><br>
  1240. //! If you want to parse contents of a file, you must first load the file into the memory, and pass pointer to its beginning.
  1241. //! Make sure that data is zero-terminated.
  1242. //! <br><br>
  1243. //! Document can be parsed into multiple times.
  1244. //! Each new call to parse removes previous nodes and attributes (if any), but does not clear memory pool.
  1245. //! \param text XML data to parse; pointer is non-const to denote fact that this data may be modified by the parser.
  1246. template<int Flags>
  1247. void parse(Ch *text, int nLen)
  1248. {
  1249. assert(text);
  1250. // Remove current contents
  1251. this->remove_all_nodes();
  1252. this->remove_all_attributes();
  1253. endptr_ = nullptr;
  1254. if (nLen > 0)
  1255. {
  1256. endptr_ = text + nLen;
  1257. }
  1258. // Parse BOM, if any
  1259. parse_bom<Flags>(text);
  1260. // Parse children
  1261. while (1)
  1262. {
  1263. // Skip whitespace before node
  1264. skip<whitespace_pred, Flags>(text, endptr_);
  1265. if (*text == 0 || text >= endptr_)
  1266. break;
  1267. // Parse and append new child
  1268. if (*text == Ch('<'))
  1269. {
  1270. ++text; // Skip '<'
  1271. if (xml_node<Ch> *node = parse_node<Flags>(text))
  1272. this->append_node(node);
  1273. }
  1274. else
  1275. RAPIDXML_PARSE_ERROR("expected <", text);
  1276. }
  1277. }
  1278. //! Clears the document by deleting all nodes and clearing the memory pool.
  1279. //! All nodes owned by document pool are destroyed.
  1280. void clear()
  1281. {
  1282. this->remove_all_nodes();
  1283. this->remove_all_attributes();
  1284. memory_pool<Ch>::clear();
  1285. }
  1286. private:
  1287. ///////////////////////////////////////////////////////////////////////
  1288. // Internal character utility functions
  1289. // Detect whitespace character
  1290. struct whitespace_pred
  1291. {
  1292. static unsigned char test(Ch ch)
  1293. {
  1294. return internal::lookup_tables<0>::lookup_whitespace[static_cast<unsigned char>(ch)];
  1295. }
  1296. };
  1297. // Detect node name character
  1298. struct node_name_pred
  1299. {
  1300. static unsigned char test(Ch ch)
  1301. {
  1302. return internal::lookup_tables<0>::lookup_node_name[static_cast<unsigned char>(ch)];
  1303. }
  1304. };
  1305. // Detect attribute name character
  1306. struct attribute_name_pred
  1307. {
  1308. static unsigned char test(Ch ch)
  1309. {
  1310. return internal::lookup_tables<0>::lookup_attribute_name[static_cast<unsigned char>(ch)];
  1311. }
  1312. };
  1313. // Detect text character (PCDATA)
  1314. struct text_pred
  1315. {
  1316. static unsigned char test(Ch ch)
  1317. {
  1318. return internal::lookup_tables<0>::lookup_text[static_cast<unsigned char>(ch)];
  1319. }
  1320. };
  1321. // Detect text character (PCDATA) that does not require processing
  1322. struct text_pure_no_ws_pred
  1323. {
  1324. static unsigned char test(Ch ch)
  1325. {
  1326. return internal::lookup_tables<0>::lookup_text_pure_no_ws[static_cast<unsigned char>(ch)];
  1327. }
  1328. };
  1329. // Detect text character (PCDATA) that does not require processing
  1330. struct text_pure_with_ws_pred
  1331. {
  1332. static unsigned char test(Ch ch)
  1333. {
  1334. return internal::lookup_tables<0>::lookup_text_pure_with_ws[static_cast<unsigned char>(ch)];
  1335. }
  1336. };
  1337. // Detect attribute value character
  1338. template<Ch Quote>
  1339. struct attribute_value_pred
  1340. {
  1341. static unsigned char test(Ch ch)
  1342. {
  1343. if (Quote == Ch('\''))
  1344. return internal::lookup_tables<0>::lookup_attribute_data_1[static_cast<unsigned char>(ch)];
  1345. if (Quote == Ch('\"'))
  1346. return internal::lookup_tables<0>::lookup_attribute_data_2[static_cast<unsigned char>(ch)];
  1347. return 0; // Should never be executed, to avoid warnings on Comeau
  1348. }
  1349. };
  1350. // Detect attribute value character
  1351. template<Ch Quote>
  1352. struct attribute_value_pure_pred
  1353. {
  1354. static unsigned char test(Ch ch)
  1355. {
  1356. if (Quote == Ch('\''))
  1357. return internal::lookup_tables<0>::lookup_attribute_data_1_pure[static_cast<unsigned char>(ch)];
  1358. if (Quote == Ch('\"'))
  1359. return internal::lookup_tables<0>::lookup_attribute_data_2_pure[static_cast<unsigned char>(ch)];
  1360. return 0; // Should never be executed, to avoid warnings on Comeau
  1361. }
  1362. };
  1363. // Insert coded character, using UTF8 or 8-bit ASCII
  1364. template<int Flags>
  1365. static void insert_coded_character(Ch *&text, unsigned long code)
  1366. {
  1367. if (Flags & parse_no_utf8)
  1368. {
  1369. // Insert 8-bit ASCII character
  1370. // Todo: possibly verify that code is less than 256 and use replacement char otherwise?
  1371. text[0] = static_cast<unsigned char>(code);
  1372. text += 1;
  1373. }
  1374. else
  1375. {
  1376. // Insert UTF8 sequence
  1377. if (code < 0x80) // 1 byte sequence
  1378. {
  1379. text[0] = static_cast<unsigned char>(code);
  1380. text += 1;
  1381. }
  1382. else if (code < 0x800) // 2 byte sequence
  1383. {
  1384. text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
  1385. text[0] = static_cast<unsigned char>(code | 0xC0);
  1386. text += 2;
  1387. }
  1388. else if (code < 0x10000) // 3 byte sequence
  1389. {
  1390. text[2] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
  1391. text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
  1392. text[0] = static_cast<unsigned char>(code | 0xE0);
  1393. text += 3;
  1394. }
  1395. else if (code < 0x110000) // 4 byte sequence
  1396. {
  1397. text[3] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
  1398. text[2] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
  1399. text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF); code >>= 6;
  1400. text[0] = static_cast<unsigned char>(code | 0xF0);
  1401. text += 4;
  1402. }
  1403. else // Invalid, only codes up to 0x10FFFF are allowed in Unicode
  1404. {
  1405. RAPIDXML_PARSE_ERROR("invalid numeric character entity", text);
  1406. }
  1407. }
  1408. }
  1409. // Skip characters until predicate evaluates to true
  1410. template<class StopPred, int Flags>
  1411. static void skip(Ch *&text, Ch *textEnd = NULL)
  1412. {
  1413. Ch *tmp = text;
  1414. while ((textEnd == NULL || tmp < textEnd) && StopPred::test(*tmp))
  1415. ++tmp;
  1416. text = tmp;
  1417. }
  1418. // Skip characters until predicate evaluates to true while doing the following:
  1419. // - replacing XML character entity references with proper characters (&apos; &amp; &quot; &lt; &gt; &#...;)
  1420. // - condensing whitespace sequences to single space character
  1421. template<class StopPred, class StopPredPure, int Flags>
  1422. static Ch *skip_and_expand_character_refs(Ch *&text)
  1423. {
  1424. // If entity translation, whitespace condense and whitespace trimming is disabled, use plain skip
  1425. if (Flags & parse_no_entity_translation &&
  1426. !(Flags & parse_normalize_whitespace) &&
  1427. !(Flags & parse_trim_whitespace))
  1428. {
  1429. skip<StopPred, Flags>(text);
  1430. return text;
  1431. }
  1432. // Use simple skip until first modification is detected
  1433. skip<StopPredPure, Flags>(text);
  1434. // Use translation skip
  1435. Ch *src = text;
  1436. Ch *dest = src;
  1437. while (StopPred::test(*src))
  1438. {
  1439. // If entity translation is enabled
  1440. if (!(Flags & parse_no_entity_translation))
  1441. {
  1442. // Test if replacement is needed
  1443. if (src[0] == Ch('&'))
  1444. {
  1445. switch (src[1])
  1446. {
  1447. // &amp; &apos;
  1448. case Ch('a'):
  1449. if (src[2] == Ch('m') && src[3] == Ch('p') && src[4] == Ch(';'))
  1450. {
  1451. *dest = Ch('&');
  1452. ++dest;
  1453. src += 5;
  1454. continue;
  1455. }
  1456. if (src[2] == Ch('p') && src[3] == Ch('o') && src[4] == Ch('s') && src[5] == Ch(';'))
  1457. {
  1458. *dest = Ch('\'');
  1459. ++dest;
  1460. src += 6;
  1461. continue;
  1462. }
  1463. break;
  1464. // &quot;
  1465. case Ch('q'):
  1466. if (src[2] == Ch('u') && src[3] == Ch('o') && src[4] == Ch('t') && src[5] == Ch(';'))
  1467. {
  1468. *dest = Ch('"');
  1469. ++dest;
  1470. src += 6;
  1471. continue;
  1472. }
  1473. break;
  1474. // &gt;
  1475. case Ch('g'):
  1476. if (src[2] == Ch('t') && src[3] == Ch(';'))
  1477. {
  1478. *dest = Ch('>');
  1479. ++dest;
  1480. src += 4;
  1481. continue;
  1482. }
  1483. break;
  1484. // &lt;
  1485. case Ch('l'):
  1486. if (src[2] == Ch('t') && src[3] == Ch(';'))
  1487. {
  1488. *dest = Ch('<');
  1489. ++dest;
  1490. src += 4;
  1491. continue;
  1492. }
  1493. break;
  1494. // &#...; - assumes ASCII
  1495. case Ch('#'):
  1496. if (src[2] == Ch('x'))
  1497. {
  1498. unsigned long code = 0;
  1499. src += 3; // Skip &#x
  1500. while (1)
  1501. {
  1502. unsigned char digit = internal::lookup_tables<0>::lookup_digits[static_cast<unsigned char>(*src)];
  1503. if (digit == 0xFF)
  1504. break;
  1505. code = code * 16 + digit;
  1506. ++src;
  1507. }
  1508. insert_coded_character<Flags>(dest, code); // Put character in output
  1509. }
  1510. else
  1511. {
  1512. unsigned long code = 0;
  1513. src += 2; // Skip &#
  1514. while (1)
  1515. {
  1516. unsigned char digit = internal::lookup_tables<0>::lookup_digits[static_cast<unsigned char>(*src)];
  1517. if (digit == 0xFF)
  1518. break;
  1519. code = code * 10 + digit;
  1520. ++src;
  1521. }
  1522. insert_coded_character<Flags>(dest, code); // Put character in output
  1523. }
  1524. if (*src == Ch(';'))
  1525. ++src;
  1526. else
  1527. RAPIDXML_PARSE_ERROR("expected ;", src);
  1528. continue;
  1529. // Something else
  1530. default:
  1531. // Ignore, just copy '&' verbatim
  1532. break;
  1533. }
  1534. }
  1535. }
  1536. // If whitespace condensing is enabled
  1537. if (Flags & parse_normalize_whitespace)
  1538. {
  1539. // Test if condensing is needed
  1540. if (whitespace_pred::test(*src))
  1541. {
  1542. *dest = Ch(' '); ++dest; // Put single space in dest
  1543. ++src; // Skip first whitespace char
  1544. // Skip remaining whitespace chars
  1545. while (whitespace_pred::test(*src))
  1546. ++src;
  1547. continue;
  1548. }
  1549. }
  1550. // No replacement, only copy character
  1551. *dest++ = *src++;
  1552. }
  1553. // Return new end
  1554. text = src;
  1555. return dest;
  1556. }
  1557. ///////////////////////////////////////////////////////////////////////
  1558. // Internal parsing functions
  1559. // Parse UTF-8 BOM, if any
  1560. template<int Flags>
  1561. void parse_bom(char *&text)
  1562. {
  1563. if (static_cast<unsigned char>(text[0]) == 0xEF &&
  1564. static_cast<unsigned char>(text[1]) == 0xBB &&
  1565. static_cast<unsigned char>(text[2]) == 0xBF)
  1566. {
  1567. text += 3;
  1568. }
  1569. }
  1570. // Parse UTF-16/32 BOM, if any
  1571. template<int Flags>
  1572. void parse_bom(wchar_t *&text)
  1573. {
  1574. const wchar_t bom = 0xFEFF;
  1575. if (text[0] == bom)
  1576. {
  1577. ++text;
  1578. }
  1579. }
  1580. // Parse XML declaration (<?xml...)
  1581. template<int Flags>
  1582. xml_node<Ch> *parse_xml_declaration(Ch *&text)
  1583. {
  1584. // If parsing of declaration is disabled
  1585. if (!(Flags & parse_declaration_node))
  1586. {
  1587. // Skip until end of declaration
  1588. while (text[0] != Ch('?') || text[1] != Ch('>'))
  1589. {
  1590. if (!text[0])
  1591. RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  1592. ++text;
  1593. }
  1594. text += 2; // Skip '?>'
  1595. return 0;
  1596. }
  1597. // Create declaration
  1598. xml_node<Ch> *declaration = this->allocate_node(node_declaration);
  1599. // Skip whitespace before attributes or ?>
  1600. skip<whitespace_pred, Flags>(text, endptr_);
  1601. // Parse declaration attributes
  1602. parse_node_attributes<Flags>(text, declaration);
  1603. // Skip ?>
  1604. if (text[0] != Ch('?') || text[1] != Ch('>'))
  1605. RAPIDXML_PARSE_ERROR("expected ?>", text);
  1606. text += 2;
  1607. return declaration;
  1608. }
  1609. // Parse XML comment (<!--...)
  1610. template<int Flags>
  1611. xml_node<Ch> *parse_comment(Ch *&text)
  1612. {
  1613. // If parsing of comments is disabled
  1614. if (!(Flags & parse_comment_nodes))
  1615. {
  1616. // Skip until end of comment
  1617. while (text[0] != Ch('-') || text[1] != Ch('-') || text[2] != Ch('>'))
  1618. {
  1619. if (!text[0])
  1620. RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  1621. ++text;
  1622. }
  1623. text += 3; // Skip '-->'
  1624. return 0; // Do not produce comment node
  1625. }
  1626. // Remember value start
  1627. Ch *value = text;
  1628. // Skip until end of comment
  1629. while (text[0] != Ch('-') || text[1] != Ch('-') || text[2] != Ch('>'))
  1630. {
  1631. if (!text[0])
  1632. RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  1633. ++text;
  1634. }
  1635. // Create comment node
  1636. xml_node<Ch> *comment = this->allocate_node(node_comment);
  1637. comment->value(value, text - value);
  1638. // Place zero terminator after comment value
  1639. if (!(Flags & parse_no_string_terminators))
  1640. *text = Ch('\0');
  1641. text += 3; // Skip '-->'
  1642. return comment;
  1643. }
  1644. // Parse DOCTYPE
  1645. template<int Flags>
  1646. xml_node<Ch> *parse_doctype(Ch *&text)
  1647. {
  1648. // Remember value start
  1649. Ch *value = text;
  1650. // Skip to >
  1651. while (*text != Ch('>'))
  1652. {
  1653. // Determine character type
  1654. switch (*text)
  1655. {
  1656. // If '[' encountered, scan for matching ending ']' using naive algorithm with depth
  1657. // This works for all W3C test files except for 2 most wicked
  1658. case Ch('['):
  1659. {
  1660. ++text; // Skip '['
  1661. int depth = 1;
  1662. while (depth > 0)
  1663. {
  1664. switch (*text)
  1665. {
  1666. case Ch('['): ++depth; break;
  1667. case Ch(']'): --depth; break;
  1668. case 0: RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  1669. default: break;
  1670. }
  1671. ++text;
  1672. }
  1673. break;
  1674. }
  1675. // Error on end of text
  1676. case Ch('\0'):
  1677. RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  1678. // Other character, skip it
  1679. default:
  1680. ++text;
  1681. }
  1682. }
  1683. // If DOCTYPE nodes enabled
  1684. if (Flags & parse_doctype_node)
  1685. {
  1686. // Create a new doctype node
  1687. xml_node<Ch> *doctype = this->allocate_node(node_doctype);
  1688. doctype->value(value, text - value);
  1689. // Place zero terminator after value
  1690. if (!(Flags & parse_no_string_terminators))
  1691. *text = Ch('\0');
  1692. text += 1; // skip '>'
  1693. return doctype;
  1694. }
  1695. else
  1696. {
  1697. text += 1; // skip '>'
  1698. return 0;
  1699. }
  1700. }
  1701. // Parse PI
  1702. template<int Flags>
  1703. xml_node<Ch> *parse_pi(Ch *&text)
  1704. {
  1705. // If creation of PI nodes is enabled
  1706. if (Flags & parse_pi_nodes)
  1707. {
  1708. // Create pi node
  1709. xml_node<Ch> *pi = this->allocate_node(node_pi);
  1710. // Extract PI target name
  1711. Ch *name = text;
  1712. skip<node_name_pred, Flags>(text, endptr_);
  1713. if (text == name)
  1714. RAPIDXML_PARSE_ERROR("expected PI target", text);
  1715. pi->name(name, text - name);
  1716. // Skip whitespace between pi target and pi
  1717. skip<whitespace_pred, Flags>(text, endptr_);
  1718. // Remember start of pi
  1719. Ch *value = text;
  1720. // Skip to '?>'
  1721. while (text[0] != Ch('?') || text[1] != Ch('>'))
  1722. {
  1723. if (*text == Ch('\0'))
  1724. RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  1725. ++text;
  1726. }
  1727. // Set pi value (verbatim, no entity expansion or whitespace normalization)
  1728. pi->value(value, text - value);
  1729. // Place zero terminator after name and value
  1730. if (!(Flags & parse_no_string_terminators))
  1731. {
  1732. pi->name()[pi->name_size()] = Ch('\0');
  1733. pi->value()[pi->value_size()] = Ch('\0');
  1734. }
  1735. text += 2; // Skip '?>'
  1736. return pi;
  1737. }
  1738. else
  1739. {
  1740. // Skip to '?>'
  1741. while (text[0] != Ch('?') || text[1] != Ch('>'))
  1742. {
  1743. if (*text == Ch('\0'))
  1744. RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  1745. ++text;
  1746. }
  1747. text += 2; // Skip '?>'
  1748. return 0;
  1749. }
  1750. }
  1751. // Parse and append data
  1752. // Return character that ends data.
  1753. // This is necessary because this character might have been overwritten by a terminating 0
  1754. template<int Flags>
  1755. Ch parse_and_append_data(xml_node<Ch> *node, Ch *&text, Ch *contents_start)
  1756. {
  1757. // Backup to contents start if whitespace trimming is disabled
  1758. if (!(Flags & parse_trim_whitespace))
  1759. text = contents_start;
  1760. // Skip until end of data
  1761. Ch *value = text, *end;
  1762. if (Flags & parse_normalize_whitespace)
  1763. end = skip_and_expand_character_refs<text_pred, text_pure_with_ws_pred, Flags>(text);
  1764. else
  1765. end = skip_and_expand_character_refs<text_pred, text_pure_no_ws_pred, Flags>(text);
  1766. // Trim trailing whitespace if flag is set; leading was already trimmed by whitespace skip after >
  1767. if (Flags & parse_trim_whitespace)
  1768. {
  1769. if (Flags & parse_normalize_whitespace)
  1770. {
  1771. // Whitespace is already condensed to single space characters by skipping function, so just trim 1 char off the end
  1772. if (*(end - 1) == Ch(' '))
  1773. --end;
  1774. }
  1775. else
  1776. {
  1777. // Backup until non-whitespace character is found
  1778. while (whitespace_pred::test(*(end - 1)))
  1779. --end;
  1780. }
  1781. }
  1782. // If characters are still left between end and value (this test is only necessary if normalization is enabled)
  1783. // Create new data node
  1784. if (!(Flags & parse_no_data_nodes))
  1785. {
  1786. xml_node<Ch> *data = this->allocate_node(node_data);
  1787. data->value(value, end - value);
  1788. node->append_node(data);
  1789. }
  1790. // Add data to parent node if no data exists yet
  1791. if (!(Flags & parse_no_element_values))
  1792. if (*node->value() == Ch('\0'))
  1793. node->value(value, end - value);
  1794. // Place zero terminator after value
  1795. if (!(Flags & parse_no_string_terminators))
  1796. {
  1797. Ch ch = *text;
  1798. *end = Ch('\0');
  1799. return ch; // Return character that ends data; this is required because zero terminator overwritten it
  1800. }
  1801. // Return character that ends data
  1802. return *text;
  1803. }
  1804. // Parse CDATA
  1805. template<int Flags>
  1806. xml_node<Ch> *parse_cdata(Ch *&text)
  1807. {
  1808. // If CDATA is disabled
  1809. if (Flags & parse_no_data_nodes)
  1810. {
  1811. // Skip until end of cdata
  1812. while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>'))
  1813. {
  1814. if (!text[0])
  1815. RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  1816. ++text;
  1817. }
  1818. text += 3; // Skip ]]>
  1819. return 0; // Do not produce CDATA node
  1820. }
  1821. // Skip until end of cdata
  1822. Ch *value = text;
  1823. while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>'))
  1824. {
  1825. if (!text[0])
  1826. RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  1827. ++text;
  1828. }
  1829. // Create new cdata node
  1830. xml_node<Ch> *cdata = this->allocate_node(node_cdata);
  1831. cdata->value(value, text - value);
  1832. // Place zero terminator after value
  1833. if (!(Flags & parse_no_string_terminators))
  1834. *text = Ch('\0');
  1835. text += 3; // Skip ]]>
  1836. return cdata;
  1837. }
  1838. // Parse element node
  1839. template<int Flags>
  1840. xml_node<Ch> *parse_element(Ch *&text)
  1841. {
  1842. // Create element node
  1843. xml_node<Ch> *element = this->allocate_node(node_element);
  1844. // Extract element name
  1845. Ch *name = text;
  1846. skip<node_name_pred, Flags>(text, endptr_);
  1847. if (text == name)
  1848. RAPIDXML_PARSE_ERROR("expected element name", text);
  1849. element->name(name, text - name);
  1850. // Skip whitespace between element name and attributes or >
  1851. skip<whitespace_pred, Flags>(text, endptr_);
  1852. // Parse attributes, if any
  1853. parse_node_attributes<Flags>(text, element);
  1854. // Determine ending type
  1855. if (*text == Ch('>'))
  1856. {
  1857. ++text;
  1858. parse_node_contents<Flags>(text, element);
  1859. }
  1860. else if (*text == Ch('/'))
  1861. {
  1862. ++text;
  1863. if (*text != Ch('>'))
  1864. RAPIDXML_PARSE_ERROR("expected >", text);
  1865. ++text;
  1866. }
  1867. else
  1868. RAPIDXML_PARSE_ERROR("expected >", text);
  1869. // Place zero terminator after name
  1870. if (!(Flags & parse_no_string_terminators))
  1871. element->name()[element->name_size()] = Ch('\0');
  1872. // Return parsed element
  1873. return element;
  1874. }
  1875. // Determine node type, and parse it
  1876. template<int Flags>
  1877. xml_node<Ch> *parse_node(Ch *&text)
  1878. {
  1879. // Parse proper node type
  1880. switch (text[0])
  1881. {
  1882. // <...
  1883. default:
  1884. // Parse and append element node
  1885. return parse_element<Flags>(text);
  1886. // <?...
  1887. case Ch('?'):
  1888. ++text; // Skip ?
  1889. if ((text[0] == Ch('x') || text[0] == Ch('X')) &&
  1890. (text[1] == Ch('m') || text[1] == Ch('M')) &&
  1891. (text[2] == Ch('l') || text[2] == Ch('L')) &&
  1892. whitespace_pred::test(text[3]))
  1893. {
  1894. // '<?xml ' - xml declaration
  1895. text += 4; // Skip 'xml '
  1896. return parse_xml_declaration<Flags>(text);
  1897. }
  1898. else
  1899. {
  1900. // Parse PI
  1901. return parse_pi<Flags>(text);
  1902. }
  1903. // <!...
  1904. case Ch('!'):
  1905. // Parse proper subset of <! node
  1906. switch (text[1])
  1907. {
  1908. // <!-
  1909. case Ch('-'):
  1910. if (text[2] == Ch('-'))
  1911. {
  1912. // '<!--' - xml comment
  1913. text += 3; // Skip '!--'
  1914. return parse_comment<Flags>(text);
  1915. }
  1916. break;
  1917. // <![
  1918. case Ch('['):
  1919. if (text[2] == Ch('C') && text[3] == Ch('D') && text[4] == Ch('A') &&
  1920. text[5] == Ch('T') && text[6] == Ch('A') && text[7] == Ch('['))
  1921. {
  1922. // '<![CDATA[' - cdata
  1923. text += 8; // Skip '![CDATA['
  1924. return parse_cdata<Flags>(text);
  1925. }
  1926. break;
  1927. // <!D
  1928. case Ch('D'):
  1929. if (text[2] == Ch('O') && text[3] == Ch('C') && text[4] == Ch('T') &&
  1930. text[5] == Ch('Y') && text[6] == Ch('P') && text[7] == Ch('E') &&
  1931. whitespace_pred::test(text[8]))
  1932. {
  1933. // '<!DOCTYPE ' - doctype
  1934. text += 9; // skip '!DOCTYPE '
  1935. return parse_doctype<Flags>(text);
  1936. }
  1937. break;
  1938. default: break;
  1939. } // switch
  1940. // Attempt to skip other, unrecognized node types starting with <!
  1941. ++text; // Skip !
  1942. while (*text != Ch('>'))
  1943. {
  1944. if (*text == 0)
  1945. RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  1946. ++text;
  1947. }
  1948. ++text; // Skip '>'
  1949. return 0; // No node recognized
  1950. }
  1951. }
  1952. // Parse contents of the node - children, data etc.
  1953. template<int Flags>
  1954. void parse_node_contents(Ch *&text, xml_node<Ch> *node)
  1955. {
  1956. // For all children and text
  1957. while (1)
  1958. {
  1959. // Skip whitespace between > and node contents
  1960. Ch *contents_start = text; // Store start of node contents before whitespace is skipped
  1961. skip<whitespace_pred, Flags>(text, endptr_);
  1962. Ch next_char = *text;
  1963. // After data nodes, instead of continuing the loop, control jumps here.
  1964. // This is because zero termination inside parse_and_append_data() function
  1965. // would wreak havoc with the above code.
  1966. // Also, skipping whitespace after data nodes is unnecessary.
  1967. after_data_node:
  1968. // Determine what comes next: node closing, child node, data node, or 0?
  1969. switch (next_char)
  1970. {
  1971. // Node closing or child node
  1972. case Ch('<'):
  1973. if (text[1] == Ch('/'))
  1974. {
  1975. // Node closing
  1976. text += 2; // Skip '</'
  1977. if (Flags & parse_validate_closing_tags)
  1978. {
  1979. // Skip and validate closing tag name
  1980. Ch *closing_name = text;
  1981. skip<node_name_pred, Flags>(text, endptr_);
  1982. if (!internal::compare(node->name(), node->name_size(), closing_name, text - closing_name, true))
  1983. RAPIDXML_PARSE_ERROR("invalid closing tag name", text);
  1984. }
  1985. else
  1986. {
  1987. // No validation, just skip name
  1988. skip<node_name_pred, Flags>(text, endptr_);
  1989. }
  1990. // Skip remaining whitespace after node name
  1991. skip<whitespace_pred, Flags>(text, endptr_);
  1992. if (*text != Ch('>'))
  1993. RAPIDXML_PARSE_ERROR("expected >", text);
  1994. ++text; // Skip '>'
  1995. return; // Node closed, finished parsing contents
  1996. }
  1997. else
  1998. {
  1999. // Child node
  2000. ++text; // Skip '<'
  2001. if (xml_node<Ch> *child = parse_node<Flags>(text))
  2002. node->append_node(child);
  2003. }
  2004. break;
  2005. // End of data - error
  2006. case Ch('\0'):
  2007. RAPIDXML_PARSE_ERROR("unexpected end of data", text);
  2008. // Data node
  2009. default:
  2010. next_char = parse_and_append_data<Flags>(node, text, contents_start);
  2011. goto after_data_node; // Bypass regular processing after data nodes
  2012. }
  2013. }
  2014. }
  2015. // Parse XML attributes of the node
  2016. template<int Flags>
  2017. void parse_node_attributes(Ch *&text, xml_node<Ch> *node)
  2018. {
  2019. // For all attributes
  2020. while (attribute_name_pred::test(*text))
  2021. {
  2022. // Extract attribute name
  2023. Ch *name = text;
  2024. ++text; // Skip first character of attribute name
  2025. skip<attribute_name_pred, Flags>(text, endptr_);
  2026. if (text == name)
  2027. RAPIDXML_PARSE_ERROR("expected attribute name", name);
  2028. // Create new attribute
  2029. xml_attribute<Ch> *attribute = this->allocate_attribute();
  2030. attribute->name(name, text - name);
  2031. node->append_attribute(attribute);
  2032. // Skip whitespace after attribute name
  2033. skip<whitespace_pred, Flags>(text, endptr_);
  2034. // Skip =
  2035. if (*text != Ch('='))
  2036. RAPIDXML_PARSE_ERROR("expected =", text);
  2037. ++text;
  2038. // Add terminating zero after name
  2039. if (!(Flags & parse_no_string_terminators))
  2040. attribute->name()[attribute->name_size()] = 0;
  2041. // Skip whitespace after =
  2042. skip<whitespace_pred, Flags>(text, endptr_);
  2043. // Skip quote and remember if it was ' or "
  2044. Ch quote = *text;
  2045. if (quote != Ch('\'') && quote != Ch('"'))
  2046. RAPIDXML_PARSE_ERROR("expected ' or \"", text);
  2047. ++text;
  2048. // Extract attribute value and expand char refs in it
  2049. Ch *value = text, *end;
  2050. const int AttFlags = Flags & ~parse_normalize_whitespace; // No whitespace normalization in attributes
  2051. if (quote == Ch('\''))
  2052. end = skip_and_expand_character_refs<attribute_value_pred<Ch('\'')>, attribute_value_pure_pred<Ch('\'')>, AttFlags>(text);
  2053. else
  2054. end = skip_and_expand_character_refs<attribute_value_pred<Ch('"')>, attribute_value_pure_pred<Ch('"')>, AttFlags>(text);
  2055. // Set attribute value
  2056. attribute->value(value, end - value);
  2057. // Make sure that end quote is present
  2058. if (*text != quote)
  2059. RAPIDXML_PARSE_ERROR("expected ' or \"", text);
  2060. ++text; // Skip quote
  2061. // Add terminating zero after value
  2062. if (!(Flags & parse_no_string_terminators))
  2063. attribute->value()[attribute->value_size()] = 0;
  2064. // Skip whitespace after attribute value
  2065. skip<whitespace_pred, Flags>(text, endptr_);
  2066. }
  2067. }
  2068. };
  2069. //! \cond internal
  2070. namespace internal
  2071. {
  2072. // Whitespace (space \n \r \t)
  2073. template<int Dummy>
  2074. const unsigned char lookup_tables<Dummy>::lookup_whitespace[256] =
  2075. {
  2076. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2077. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, // 0
  2078. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1
  2079. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2
  2080. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3
  2081. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4
  2082. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 5
  2083. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6
  2084. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7
  2085. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8
  2086. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9
  2087. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A
  2088. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // B
  2089. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // C
  2090. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D
  2091. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // E
  2092. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F
  2093. };
  2094. // Node name (anything but space \n \r \t / > ? \0)
  2095. template<int Dummy>
  2096. const unsigned char lookup_tables<Dummy>::lookup_node_name[256] =
  2097. {
  2098. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2099. 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0
  2100. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
  2101. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 2
  2102. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, // 3
  2103. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
  2104. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
  2105. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
  2106. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
  2107. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
  2108. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
  2109. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
  2110. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
  2111. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
  2112. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
  2113. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
  2114. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
  2115. };
  2116. // Text (i.e. PCDATA) (anything but < \0)
  2117. template<int Dummy>
  2118. const unsigned char lookup_tables<Dummy>::lookup_text[256] =
  2119. {
  2120. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2121. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
  2122. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
  2123. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
  2124. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3
  2125. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
  2126. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
  2127. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
  2128. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
  2129. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
  2130. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
  2131. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
  2132. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
  2133. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
  2134. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
  2135. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
  2136. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
  2137. };
  2138. // Text (i.e. PCDATA) that does not require processing when ws normalization is disabled
  2139. // (anything but < \0 &)
  2140. template<int Dummy>
  2141. const unsigned char lookup_tables<Dummy>::lookup_text_pure_no_ws[256] =
  2142. {
  2143. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2144. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
  2145. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
  2146. 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
  2147. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3
  2148. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
  2149. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
  2150. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
  2151. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
  2152. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
  2153. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
  2154. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
  2155. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
  2156. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
  2157. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
  2158. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
  2159. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
  2160. };
  2161. // Text (i.e. PCDATA) that does not require processing when ws normalizationis is enabled
  2162. // (anything but < \0 & space \n \r \t)
  2163. template<int Dummy>
  2164. const unsigned char lookup_tables<Dummy>::lookup_text_pure_with_ws[256] =
  2165. {
  2166. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2167. 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0
  2168. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
  2169. 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
  2170. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 3
  2171. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
  2172. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
  2173. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
  2174. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
  2175. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
  2176. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
  2177. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
  2178. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
  2179. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
  2180. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
  2181. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
  2182. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
  2183. };
  2184. // Attribute name (anything but space \n \r \t / < > = ? ! \0)
  2185. template<int Dummy>
  2186. const unsigned char lookup_tables<Dummy>::lookup_attribute_name[256] =
  2187. {
  2188. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2189. 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, // 0
  2190. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
  2191. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 2
  2192. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, // 3
  2193. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
  2194. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
  2195. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
  2196. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
  2197. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
  2198. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
  2199. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
  2200. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
  2201. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
  2202. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
  2203. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
  2204. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
  2205. };
  2206. // Attribute data with single quote (anything but ' \0)
  2207. template<int Dummy>
  2208. const unsigned char lookup_tables<Dummy>::lookup_attribute_data_1[256] =
  2209. {
  2210. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2211. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
  2212. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
  2213. 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, // 2
  2214. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
  2215. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
  2216. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
  2217. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
  2218. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
  2219. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
  2220. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
  2221. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
  2222. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
  2223. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
  2224. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
  2225. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
  2226. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
  2227. };
  2228. // Attribute data with single quote that does not require processing (anything but ' \0 &)
  2229. template<int Dummy>
  2230. const unsigned char lookup_tables<Dummy>::lookup_attribute_data_1_pure[256] =
  2231. {
  2232. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2233. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
  2234. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
  2235. 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, // 2
  2236. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
  2237. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
  2238. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
  2239. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
  2240. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
  2241. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
  2242. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
  2243. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
  2244. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
  2245. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
  2246. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
  2247. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
  2248. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
  2249. };
  2250. // Attribute data with double quote (anything but " \0)
  2251. template<int Dummy>
  2252. const unsigned char lookup_tables<Dummy>::lookup_attribute_data_2[256] =
  2253. {
  2254. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2255. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
  2256. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
  2257. 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
  2258. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
  2259. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
  2260. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
  2261. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
  2262. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
  2263. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
  2264. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
  2265. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
  2266. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
  2267. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
  2268. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
  2269. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
  2270. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
  2271. };
  2272. // Attribute data with double quote that does not require processing (anything but " \0 &)
  2273. template<int Dummy>
  2274. const unsigned char lookup_tables<Dummy>::lookup_attribute_data_2_pure[256] =
  2275. {
  2276. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2277. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0
  2278. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
  2279. 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
  2280. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3
  2281. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4
  2282. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5
  2283. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6
  2284. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7
  2285. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8
  2286. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9
  2287. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A
  2288. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B
  2289. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // C
  2290. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // D
  2291. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // E
  2292. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F
  2293. };
  2294. // Digits (dec and hex, 255 denotes end of numeric character reference)
  2295. template<int Dummy>
  2296. const unsigned char lookup_tables<Dummy>::lookup_digits[256] =
  2297. {
  2298. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  2299. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 0
  2300. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 1
  2301. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 2
  2302. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,255,255,255,255,255,255, // 3
  2303. 255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, // 4
  2304. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 5
  2305. 255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, // 6
  2306. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 7
  2307. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 8
  2308. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 9
  2309. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // A
  2310. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // B
  2311. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // C
  2312. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // D
  2313. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // E
  2314. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 // F
  2315. };
  2316. // Upper case conversion
  2317. template<int Dummy>
  2318. const unsigned char lookup_tables<Dummy>::lookup_upcase[256] =
  2319. {
  2320. // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A B C D E F
  2321. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 0
  2322. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // 1
  2323. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, // 2
  2324. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, // 3
  2325. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, // 4
  2326. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, // 5
  2327. 96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, // 6
  2328. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 123,124,125,126,127, // 7
  2329. 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, // 8
  2330. 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, // 9
  2331. 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, // A
  2332. 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, // B
  2333. 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, // C
  2334. 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, // D
  2335. 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, // E
  2336. 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 // F
  2337. };
  2338. }
  2339. //! \endcond
  2340. }
  2341. // Undefine internal macros
  2342. #undef RAPIDXML_PARSE_ERROR
  2343. // On MSVC, restore warnings state
  2344. #ifdef _MSC_VER
  2345. #pragma warning(pop)
  2346. #endif
  2347. #endif