rapidxml.hpp 117 KB

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