rapidxml.hpp 117 KB

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