-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxsxml.hpp
1570 lines (1412 loc) · 56.4 KB
/
xsxml.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////////////////
// The embedded xml SAX parser, extract from rapidxml DOM parser
// please see: http://rapidxml.sourceforge.net/
//////////////////////////////////////////////////////////////////////////////////////////
/*
The MIT License (MIT)
Copyright (c) 2019 halx99
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef SIMDSOFT__XSXML_HPP
#define SIMDSOFT__XSXML_HPP
#include <assert.h>
#include <vector>
#include <utility>
#include <functional>
// On MSVC, disable "conditional expression is constant" warning (level 4).
// This warning is almost impossible to avoid with certain types of templated code
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4127) // Conditional expression is constant
#endif
#if !defined(XSXML__PARSE_ERROR)
# define XSXML__PARSE_ERROR(what, where) throw parse_error(what, where)
#endif
namespace xsxml
{
//! Parse flag instructing the parser to not create data nodes.
//! Text of first data node will still be placed in value of parent element, unless
//! xsxml::parse_no_element_values flag is also specified. Can be combined with other flags by
//! use of | operator. <br><br> See xml_document::parse() function.
const int parse_no_data_nodes = 0x1;
//! Parse flag instructing the parser to not use text of first data node as a value of parent
//! element. Can be combined with other flags by use of | operator. Note that child data nodes of
//! element node take precendence over its value when printing. That is, if element has one or more
//! child data nodes <em>and</em> a value, the value will be ignored. Use
//! xsxml::parse_no_data_nodes flag to prevent creation of data nodes if you want to manipulate
//! data using values of elements. <br><br> See xml_document::parse() function.
const int parse_no_element_values = 0x2;
//! Parse flag instructing the parser to not place zero terminators after strings in the source
//! text. By default zero terminators are placed, modifying source text. Can be combined with other
//! flags by use of | operator. <br><br> See xml_document::parse() function.
const int parse_no_string_terminators = 0x4;
//! Parse flag instructing the parser to not translate entities in the source text.
//! By default entities are translated, modifying source text.
//! Can be combined with other flags by use of | operator.
//! <br><br>
//! See xml_document::parse() function.
static const int parse_no_entity_translation = 0x8;
//! Parse flag instructing the parser to disable UTF-8 handling and assume plain 8 bit characters.
//! By default, UTF-8 handling is enabled.
//! Can be combined with other flags by use of | operator.
//! <br><br>
//! See xml_document::parse() function.
static const int parse_no_utf8 = 0x10;
//! Parse flag instructing the parser to create XML declaration node.
//! By default, declaration node is not created.
//! Can be combined with other flags by use of | operator.
//! <br><br>
//! See xml_document::parse() function.
static const int parse_declaration_node = 0x20;
//! Parse flag instructing the parser to create comments nodes.
//! By default, comment nodes are not created.
//! Can be combined with other flags by use of | operator.
//! <br><br>
//! See xml_document::parse() function.
static const int parse_comment_nodes = 0x40;
//! Parse flag instructing the parser to create DOCTYPE node.
//! By default, doctype node is not created.
//! Although W3C specification allows at most one DOCTYPE node, RapidXml will silently accept
//! documents with more than one. Can be combined with other flags by use of | operator. <br><br>
//! See xml_document::parse() function.
static const int parse_doctype_node = 0x80;
//! Parse flag instructing the parser to create PI nodes.
//! By default, PI nodes are not created.
//! Can be combined with other flags by use of | operator.
//! <br><br>
//! See xml_document::parse() function.
static const int parse_pi_nodes = 0x100;
//! Parse flag instructing the parser to validate closing tag names.
//! If not set, name inside closing tag is irrelevant to the parser.
//! By default, closing tags are not validated.
//! Can be combined with other flags by use of | operator.
//! <br><br>
//! See xml_document::parse() function.
static const int parse_validate_closing_tags = 0x200;
//! Parse flag instructing the parser to trim all leading and trailing whitespace of data nodes.
//! By default, whitespace is not trimmed.
//! This flag does not cause the parser to modify source text.
//! Can be combined with other flags by use of | operator.
//! <br><br>
//! See xml_document::parse() function.
static const int parse_trim_whitespace = 0x400;
//! Parse flag instructing the parser to condense all whitespace runs of data nodes to a single
//! space character. Trimming of leading and trailing whitespace of data is controlled by
//! xsxml::parse_trim_whitespace flag. By default, whitespace is not normalized. If this flag is
//! specified, source text will be modified. Can be combined with other flags by use of | operator.
//! <br><br>
//! See xml_document::parse() function.
static const int parse_normalize_whitespace = 0x800;
//! Parse flag instructing the parser to convert html entity
//! this flag only works when the flag 'parse_no_entity_translation' not specified
//! <br><br>
//! See xml_document::parse() function.
static const int parse_html_entity_translation = 0x1000;
// Compound flags
//! Parse flags which represent default behaviour of the parser.
//! This is always equal to 0, so that all other flags can be simply ored together.
//! Normally there is no need to inconveniently disable flags by anding with their negated (~)
//! values. This also means that meaning of each flag is a <i>negation</i> of the default setting.
//! For example, if flag name is xsxml::parse_no_utf8, it means that utf-8 is <i>enabled</i> by
//! default, and using the flag will disable it. <br><br> See xml_document::parse() function.
static const int parse_default = 0;
//! A combination of parse flags that forbids any modifications of the source text.
//! This also results in faster parsing. However, note that the following will occur:
//! <ul>
//! <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> <li>entities
//! will not be translated</li> <li>whitespace will not be normalized</li>
//! </ul>
//! See xml_document::parse() function.
static const int parse_non_destructive = parse_no_string_terminators | parse_no_entity_translation;
//! A combination of parse flags resulting in fastest possible parsing, without sacrificing
//! important data. <br><br> See xml_document::parse() function.
static const int parse_fastest = parse_non_destructive | parse_no_data_nodes;
//! A combination of parse flags resulting in largest amount of data being extracted.
//! This usually results in slowest parsing.
//! <br><br>
//! See xml_document::parse() function.
static const int parse_full = parse_declaration_node | parse_comment_nodes | parse_doctype_node |
parse_pi_nodes | parse_validate_closing_tags;
static const int parse_normal = parse_no_data_nodes;
typedef char char_t;
class string_view
{
public:
string_view() : _Mystr(nullptr), _Mysize(0) {}
string_view(char_t* str, size_t size) : _Mystr(str), _Mysize(size) {}
const char* c_str() const { return _Mystr != nullptr ? _Mystr : ""; }
size_t length() const { return _Mysize; }
bool empty() const { return _Mysize == 0; }
private:
char_t* _Mystr;
size_t _Mysize;
};
// The sax3 parse callbacks
struct xml_sax3_parse_cb
{
std::function<void(char* name, size_t)> xml_start_element_cb;
std::function<void(const char* name, size_t, const char* value, size_t)> xml_attr_cb;
std::function<void()> xml_end_attr_cb;
std::function<void(const char* name, size_t)> xml_end_element_cb;
std::function<void(const char* text, size_t len)> xml_text_cb;
};
namespace internal
{
//! \cond internal
// Struct that contains lookup tables for the parser
// It must be a template to allow correct linking (because it has static data members, which are
// defined in a header file).
template <int Dummy> struct lookup_tables
{
static const unsigned char lookup_whitespace[256]; // Whitespace table
static const unsigned char lookup_node_name[256]; // Node name table
static const unsigned char lookup_text[256]; // Text table
static const unsigned char lookup_text_pure_no_ws[256]; // Text table
static const unsigned char lookup_text_pure_with_ws[256]; // Text table
static const unsigned char lookup_attribute_name[256]; // Attribute name table
static const unsigned char lookup_attribute_data_1[256]; // Attribute data table with single quote
static const unsigned char
lookup_attribute_data_1_pure[256]; // Attribute data table with single quote
static const unsigned char
lookup_attribute_data_2[256]; // Attribute data table with double quotes
static const unsigned char
lookup_attribute_data_2_pure[256]; // Attribute data table with double quotes
static const unsigned char lookup_digits[256]; // Digits
static const unsigned char
lookup_upcase[256]; // To uppercase conversion table for ASCII characters
};
// Compare strings for equality
template <class _CharT>
inline bool compare(const _CharT* p1, std::size_t size1, const _CharT* p2, std::size_t size2,
bool case_sensitive)
{
if (size1 != size2)
return false;
if (case_sensitive)
{
for (const _CharT* end = p1 + size1; p1 < end; ++p1, ++p2)
if (*p1 != *p2)
return false;
}
else
{
for (const _CharT* end = p1 + size1; p1 < end; ++p1, ++p2)
if (lookup_tables<0>::lookup_upcase[static_cast<unsigned char>(*p1)] !=
lookup_tables<0>::lookup_upcase[static_cast<unsigned char>(*p2)])
return false;
}
return true;
}
} // namespace internal
//! Parse error exception.
//! This exception is thrown by the parser when an error occurs.
//! Use what() function to get human-readable error message.
//! Use where() function to get a pointer to position within source text where error was detected.
//! <br><br>
//! If throwing exceptions by the parser is undesirable,
//! it can be disabled by defining RAPIDXML_NO_EXCEPTIONS macro before xsxml.hpp is included.
//! This will cause the parser to call xsxml::parse_error_handler() function instead of throwing
//! an exception. This function must be defined by the user. <br><br> This class derives from
//! <code>std::exception</code> class.
class parse_error : public std::exception
{
public:
//! Constructs parse error
parse_error(const char* what, void* where) : m_what(what), m_where(where) {}
//! Gets human readable description of error.
//! \return Pointer to null terminated description of the error.
virtual const char* what() const throw() { return m_what; }
//! Gets pointer to character data where error happened.
//! _CharT should be the same as char type of xml_document that produced the error.
//! \return Pointer to location within the parsed string where error occured.
template <class _CharT> _CharT* where() const { return reinterpret_cast<_CharT*>(m_where); }
private:
const char* m_what;
void* m_where;
};
//! This class represents root of the DOM hierarchy.
//! It is also an xml_node and a memory_pool through public inheritance.
//! Use parse() function to build a DOM tree from a zero-terminated XML text string.
//! parse() function allocates memory for nodes and attributes by using functions of xml_document,
//! which are inherited from memory_pool.
//! To access root node of the document, use the document itself, as if it was an xml_node.
//! \param char_t Character type to use.
class xml_sax3_parser
{
xml_sax3_parse_cb* handler_;
enum class parse_result
{
ok,
expected_close_tag,
unrecognized_tag,
};
parse_result parse_result_ = parse_result::ok;
public:
template <int Flags = parse_normal>
static parse_result parse(char_t* text, int length, xml_sax3_parse_cb* handler)
{
xml_sax3_parser parser(handler);
parser.parse<Flags>(text, length);
return parser.parse_result_;
}
//! Constructs empty XML document
xml_sax3_parser(xml_sax3_parse_cb* handler) { handler_ = handler; }
//! Parses zero-terminated XML string according to given flags.
//! Passed string will be modified by the parser, unless xsxml::parse_non_destructive flag is
//! used. The string must persist for the lifetime of the document. In case of error,
//! xsxml::parse_error exception will be thrown. <br><br> If you want to parse contents of a
//! file, you must first load the file into the memory, and pass pointer to its beginning. Make
//! sure that data is zero-terminated. <br><br> Document can be parsed into multiple times. Each
//! new call to parse removes previous nodes and attributes (if any), but does not clear memory
//! pool. \param text XML data to parse; pointer is non-const to denote fact that this data may be
//! modified by the parser.
template <int Flags = parse_normal> void parse(char_t* text, int length)
{
assert(text);
// save last character and make buffer zero-terminated (speeds up parsing)
auto endch = text[length - 1];
text[length - 1] = 0;
// Parse BOM, if any
parse_bom(text);
// Parse children
_L_Loop : {
// Skip whitespace before node
skip<whitespace_pred, Flags>(text);
if (*text == 0)
goto _L_end;
// Parse and append new child
if (*text == char_t('<'))
{
++text; // Skip '<'
parse_node<Flags>(text);
}
else
XSXML__PARSE_ERROR("expected <", text);
goto _L_Loop;
}
_L_end:
// check parse result.
if (parse_result_ == parse_result::ok)
{
if (endch == '<')
{
parse_result_ = parse_result::unrecognized_tag;
XSXML__PARSE_ERROR("unrecognized tag", text);
}
}
else
{ // parse_result_: parse_result::expected_close_tag
if (endch == '>')
parse_result_ = parse_result::ok;
else
XSXML__PARSE_ERROR("expected >", text);
}
}
//! Clears the document by deleting all nodes and clearing the memory pool.
//! All nodes owned by document pool are destroyed.
void clear()
{
// this->remove_all_nodes();
// this->remove_all_attributes();
// memory_pool<char_t>::clear();
}
private:
///////////////////////////////////////////////////////////////////////
// Internal character utility functions
// Detect whitespace character
struct whitespace_pred
{
static unsigned char test(char_t ch)
{
return internal::lookup_tables<0>::lookup_whitespace[static_cast<unsigned char>(ch)];
}
};
// Detect node name character
struct node_name_pred
{
static unsigned char test(char_t ch)
{
return internal::lookup_tables<0>::lookup_node_name[static_cast<unsigned char>(ch)];
}
};
// Detect attribute name character
struct attribute_name_pred
{
static unsigned char test(char_t ch)
{
return internal::lookup_tables<0>::lookup_attribute_name[static_cast<unsigned char>(ch)];
}
};
// Detect text character (PCDATA)
struct text_pred
{
static unsigned char test(char_t ch)
{
return internal::lookup_tables<0>::lookup_text[static_cast<unsigned char>(ch)];
}
};
// Detect text character (PCDATA) that does not require processing
struct text_pure_no_ws_pred
{
static unsigned char test(char_t ch)
{
return internal::lookup_tables<0>::lookup_text_pure_no_ws[static_cast<unsigned char>(ch)];
}
};
// Detect text character (PCDATA) that does not require processing
struct text_pure_with_ws_pred
{
static unsigned char test(char_t ch)
{
return internal::lookup_tables<0>::lookup_text_pure_with_ws[static_cast<unsigned char>(ch)];
}
};
// Detect attribute value character
template <char_t Quote> struct attribute_value_pred
{
static unsigned char test(char_t ch)
{
if (Quote == char_t('\''))
return internal::lookup_tables<0>::lookup_attribute_data_1[static_cast<unsigned char>(ch)];
if (Quote == char_t('\"'))
return internal::lookup_tables<0>::lookup_attribute_data_2[static_cast<unsigned char>(ch)];
return 0; // Should never be executed, to avoid warnings on Comeau
}
};
// Detect attribute value character
template <char_t Quote> struct attribute_value_pure_pred
{
static unsigned char test(char_t ch)
{
if (Quote == char_t('\''))
return internal::lookup_tables<0>::lookup_attribute_data_1_pure[static_cast<unsigned char>(
ch)];
if (Quote == char_t('\"'))
return internal::lookup_tables<0>::lookup_attribute_data_2_pure[static_cast<unsigned char>(
ch)];
return 0; // Should never be executed, to avoid warnings on Comeau
}
};
// Insert coded character, using UTF8 or 8-bit ASCII
template <int Flags> static void insert_coded_character(char_t*& text, unsigned long code)
{
if (Flags & parse_no_utf8)
{
// Insert 8-bit ASCII character
// Todo: possibly verify that code is less than 256 and use replacement char otherwise?
text[0] = static_cast<unsigned char>(code);
text += 1;
}
else
{
// Insert UTF8 sequence
if (code < 0x80) // 1 byte sequence
{
text[0] = static_cast<unsigned char>(code);
text += 1;
}
else if (code < 0x800) // 2 byte sequence
{
text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF);
code >>= 6;
text[0] = static_cast<unsigned char>(code | 0xC0);
text += 2;
}
else if (code < 0x10000) // 3 byte sequence
{
text[2] = static_cast<unsigned char>((code | 0x80) & 0xBF);
code >>= 6;
text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF);
code >>= 6;
text[0] = static_cast<unsigned char>(code | 0xE0);
text += 3;
}
else if (code < 0x110000) // 4 byte sequence
{
text[3] = static_cast<unsigned char>((code | 0x80) & 0xBF);
code >>= 6;
text[2] = static_cast<unsigned char>((code | 0x80) & 0xBF);
code >>= 6;
text[1] = static_cast<unsigned char>((code | 0x80) & 0xBF);
code >>= 6;
text[0] = static_cast<unsigned char>(code | 0xF0);
text += 4;
}
else // Invalid, only codes up to 0x10FFFF are allowed in Unicode
{
XSXML__PARSE_ERROR("invalid numeric character entity", text);
}
}
}
// Skip characters until predicate evaluates to true
template <class StopPred, int Flags> static void skip(char_t*& text)
{
char_t* tmp = text;
while (StopPred::test(*tmp))
++tmp;
text = tmp;
}
// Skip characters until predicate evaluates to true while doing the following:
// - replacing XML character entity references with proper characters (' & " <
// > &#...;)
// - condensing whitespace sequences to single space character
template <class StopPred, class StopPredPure, int Flags>
static char_t* skip_and_expand_character_refs(char_t*& text)
{
// If entity translation, whitespace condense and whitespace trimming is disabled, use plain
// skip
if (Flags & parse_no_entity_translation && !(Flags & parse_normalize_whitespace) &&
!(Flags & parse_trim_whitespace))
{
skip<StopPred, Flags>(text);
return text;
}
// Use simple skip until first modification is detected
skip<StopPredPure, Flags>(text);
// Use translation skip
char_t* src = text;
char_t* dest = src;
while (StopPred::test(*src))
{
// If entity translation is enabled
if (!(Flags & parse_no_entity_translation))
{
// Test if replacement is needed
if (src[0] == char_t('&'))
{
switch (src[1])
{
// & '
case char_t('a'):
if (src[2] == char_t('m') && src[3] == char_t('p') && src[4] == char_t(';'))
{
*dest = char_t('&');
++dest;
src += 5;
continue;
}
if (src[2] == char_t('p') && src[3] == char_t('o') && src[4] == char_t('s') &&
src[5] == char_t(';'))
{
*dest = char_t('\'');
++dest;
src += 6;
continue;
}
break;
// "
case char_t('q'):
if (src[2] == char_t('u') && src[3] == char_t('o') && src[4] == char_t('t') &&
src[5] == char_t(';'))
{
*dest = char_t('"');
++dest;
src += 6;
continue;
}
break;
// >
case char_t('g'):
if (src[2] == char_t('t') && src[3] == char_t(';'))
{
*dest = char_t('>');
++dest;
src += 4;
continue;
}
break;
// <
case char_t('l'):
if (src[2] == char_t('t') && src[3] == char_t(';'))
{
*dest = char_t('<');
++dest;
src += 4;
continue;
}
break;
// &#...; - assumes ASCII
case char_t('#'):
if (src[2] == char_t('x'))
{
unsigned long code = 0;
src += 3; // Skip &#x
while (1)
{
unsigned char digit =
internal::lookup_tables<0>::lookup_digits[static_cast<unsigned char>(*src)];
if (digit == 0xFF)
break;
code = code * 16 + digit;
++src;
}
insert_coded_character<Flags>(dest, code); // Put character in output
}
else
{
unsigned long code = 0;
src += 2; // Skip &#
while (1)
{
unsigned char digit =
internal::lookup_tables<0>::lookup_digits[static_cast<unsigned char>(*src)];
if (digit == 0xFF)
break;
code = code * 10 + digit;
++src;
}
insert_coded_character<Flags>(dest, code); // Put character in output
}
if (*src == char_t(';'))
++src;
else
XSXML__PARSE_ERROR("expected ;", src);
continue;
// Something else
default:
if (Flags & parse_html_entity_translation)
{
switch (src[1])
{ //
case char_t('n'):
if (src[2] == char_t('b') && src[3] == char_t('s') && src[4] == char_t('p') &&
src[5] == char_t(';'))
{
*dest = char_t(' ');
++dest;
src += 6;
continue;
}
break;
//  
case char_t('e'):
if (src[2] == char_t('m') && src[3] == char_t('s') && src[4] == char_t('p') &&
src[5] == char_t(';'))
{
*dest = char_t(' ');
++dest;
*dest = char_t(' ');
++dest;
src += 6;
continue;
}
break;
}
}
// Ignore, just copy '&' verbatim
break;
}
}
}
// If whitespace condensing is enabled
if (Flags & parse_normalize_whitespace)
{
// Test if condensing is needed
if (whitespace_pred::test(*src))
{
*dest = char_t(' ');
++dest; // Put single space in dest
++src; // Skip first whitespace char
// Skip remaining whitespace chars
while (whitespace_pred::test(*src))
++src;
continue;
}
}
// No replacement, only copy character
*dest++ = *src++;
}
// Return new end
text = src;
return dest;
}
///////////////////////////////////////////////////////////////////////
// Internal parsing functions
// Parse UTF-8 BOM, if any
inline void parse_bom(char*& text)
{
if (static_cast<unsigned char>(text[0]) == 0xEF &&
static_cast<unsigned char>(text[1]) == 0xBB && static_cast<unsigned char>(text[2]) == 0xBF)
{
text += 3;
}
}
// Parse UTF-16/32 BOM, if any
inline void parse_bom(wchar_t*& text)
{
const wchar_t bom = 0xFEFF;
if (text[0] == bom)
{
++text;
}
}
// Parse XML declaration (<?xml...)
template <int Flags> void parse_xml_declaration(char_t*& text)
{
// If parsing of declaration is disabled
if (!(Flags & parse_declaration_node))
{
// Skip until end of declaration
while (text[0] != char_t('?') || text[1] != char_t('>'))
{
if (!text[0])
XSXML__PARSE_ERROR("unexpected end of data", text);
++text;
}
text += 2; // Skip '?>'
return; // return 0;
}
// Create declaration
// xml_node<char_t> *declaration = this->allocate_node(node_declaration);
// Skip whitespace before attributes or ?>
skip<whitespace_pred, Flags>(text);
// Parse declaration attributes
parse_node_attributes<Flags>(text /*, declaration*/);
// Skip ?>
if (text[0] != char_t('?') || text[1] != char_t('>'))
XSXML__PARSE_ERROR("expected ?>", text);
text += 2;
// return declaration;
}
// Parse XML comment (<!--...)
template <int Flags> void parse_comment(char_t*& text)
{
// If parsing of comments is disabled
if (!(Flags & parse_comment_nodes))
{
// Skip until end of comment
while (text[0] != char_t('-') || text[1] != char_t('-') || text[2] != char_t('>'))
{
if (!text[0])
XSXML__PARSE_ERROR("unexpected end of data", text);
++text;
}
text += 3; // Skip '-->'
return; // return 0; // Do not produce comment node
}
// Skip until end of comment
while (text[0] != char_t('-') || text[1] != char_t('-') || text[2] != char_t('>'))
{
if (!text[0])
XSXML__PARSE_ERROR("unexpected end of data", text);
++text;
}
// Create comment node
// xml_node<char_t> *comment = this->allocate_node(node_comment);
// comment->value(value, text - value); // TODO: DNT implement comment
// Place zero terminator after comment value
if (!(Flags & parse_no_string_terminators))
*text = char_t('\0');
text += 3; // Skip '-->'
return;
}
// Parse DOCTYPE
template <int Flags> void parse_doctype(char_t*& text)
{
// Skip to >
while (*text != char_t('>'))
{
// Determine character type
switch (*text)
{
// If '[' encountered, scan for matching ending ']' using naive algorithm with depth
// This works for all W3C test files except for 2 most wicked
case char_t('['): {
++text; // Skip '['
int depth = 1;
while (depth > 0)
{
switch (*text)
{
case char_t('['):
++depth;
break;
case char_t(']'):
--depth;
break;
case 0:
XSXML__PARSE_ERROR("unexpected end of data", text);
default:
break;
}
++text;
}
break;
}
// Error on end of text
case char_t('\0'):
XSXML__PARSE_ERROR("unexpected end of data", text);
// Other character, skip it
default:
++text;
}
}
// If DOCTYPE nodes enabled
if (Flags & parse_doctype_node)
{ // SAX3: ignore doctype node
// Place zero terminator after value
if (!(Flags & parse_no_string_terminators))
*text = char_t('\0');
text += 1; // skip '>'
return; // return doctype;
}
else
{
text += 1; // skip '>'
return; // return 0;
}
}
// Parse PI
template <int Flags> void parse_pi(char_t*& text)
{
// If creation of PI nodes is enabled
if (Flags & parse_pi_nodes)
{
// Create pi node
// xml_node<char_t> *pi = this->allocate_node(node_pi);
// Extract PI target name
char_t* name = text;
skip<node_name_pred, Flags>(text);
if (text == name)
XSXML__PARSE_ERROR("expected PI target", text);
// pi->name(name, text - name); // TODO: DNT notify for pi
// Skip whitespace between pi target and pi
skip<whitespace_pred, Flags>(text);
// Skip to '?>'
while (text[0] != char_t('?') || text[1] != char_t('>'))
{
if (*text == char_t('\0'))
XSXML__PARSE_ERROR("unexpected end of data", text);
++text;
}
text += 2; // Skip '?>'
return; // return pi;
}
else
{
// Skip to '?>'
while (text[0] != char_t('?') || text[1] != char_t('>'))
{
if (*text == char_t('\0'))
XSXML__PARSE_ERROR("unexpected end of data", text);
++text;
}
text += 2; // Skip '?>'
return; // return 0;
}
}
// Parse and append data
// Return character that ends data.
// This is necessary because this character might have been overwritten by a terminating 0
template <int Flags>
char_t parse_and_append_data(/*const string_view& elementName unused for SAX,*/ char_t*& text,
char_t* contents_start)
{
// Backup to contents start if whitespace trimming is disabled
if (!(Flags & parse_trim_whitespace))
text = contents_start;
// Skip until end of data
char_t *value = text, *end;
if (Flags & parse_normalize_whitespace)
end = skip_and_expand_character_refs<text_pred, text_pure_with_ws_pred, Flags>(text);
else
end = skip_and_expand_character_refs<text_pred, text_pure_no_ws_pred, Flags>(text);
// Trim trailing whitespace if flag is set; leading was already trimmed by whitespace skip after
// >
if (Flags & parse_trim_whitespace)
{
if (Flags & parse_normalize_whitespace)
{
// Whitespace is already condensed to single space characters by skipping function, so just
// trim 1 char off the end
if (*(end - 1) == char_t(' '))
--end;
}
else
{
// Backup until non-whitespace character is found
while (whitespace_pred::test(*(end - 1)))
--end;
}
}
char_t ch = *text;
// Place zero terminator after value
if (!(Flags & parse_no_string_terminators))
{
// char_t ch = *text;
*end = char_t('\0');
// return ch; // Return character that ends data; this is required because zero
// terminator overwritten it
}
handler_->xml_text_cb(value, end - value);
// Return character that ends data
return ch;
}
// Parse CDATA
template <int Flags> void parse_cdata(char_t*& text)
{
// If CDATA is disabled
if (Flags & parse_no_data_nodes)
{
// Skip until end of cdata
while (text[0] != char_t(']') || text[1] != char_t(']') || text[2] != char_t('>'))
{
if (!text[0])
XSXML__PARSE_ERROR("unexpected end of data", text);
++text;
}
text += 3; // Skip ]]>
return; // return 0; // Do not produce CDATA node
}
// Skip until end of cdata
while (text[0] != char_t(']') || text[1] != char_t(']') || text[2] != char_t('>'))
{
if (!text[0])
XSXML__PARSE_ERROR("unexpected end of data", text);
++text;
}
// Place zero terminator after value
if (!(Flags & parse_no_string_terminators))
*text = char_t('\0');
text += 3; // Skip ]]>
return; // return cdata;
}
// Parse element node
template <int Flags> void parse_element(char_t*& text)
{
// Create element node
// xml_node<char_t> *element = this->allocate_node(node_element);
// Extract element name
auto mark = text;