1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include <qstring.h> |
41 | |
42 | #include "qxmlutils_p.h" |
43 | |
44 | QT_BEGIN_NAMESPACE |
45 | |
46 | /* TODO: |
47 | * - isNameChar() doesn't have to be public, it's only needed in |
48 | * qdom.cpp -- refactor fixedXmlName() to use isNCName() |
49 | * - A lot of functions can be inlined. |
50 | */ |
51 | |
52 | class QXmlCharRange |
53 | { |
54 | public: |
55 | ushort min; |
56 | ushort max; |
57 | }; |
58 | typedef const QXmlCharRange *RangeIter; |
59 | |
60 | /*! |
61 | Performs a binary search between \a begin and \a end inclusive, to check whether \a |
62 | c is contained. Remember that the QXmlCharRange instances must be in numeric order. |
63 | */ |
64 | bool QXmlUtils::rangeContains(RangeIter begin, RangeIter end, const QChar c) |
65 | { |
66 | const ushort cp(c.unicode()); |
67 | |
68 | // check the first two ranges "manually" as characters in that |
69 | // range are checked very often and we avoid the binary search below. |
70 | |
71 | if (cp <= begin->max) |
72 | return cp >= begin->min; |
73 | |
74 | ++begin; |
75 | |
76 | if (begin == end) |
77 | return false; |
78 | |
79 | if (cp <= begin->max) |
80 | return cp >= begin->min; |
81 | |
82 | while (begin != end) { |
83 | int delta = (end - begin) / 2; |
84 | RangeIter mid = begin + delta; |
85 | |
86 | if (mid->min > cp) |
87 | end = mid; |
88 | else if (mid->max < cp) |
89 | begin = mid; |
90 | else |
91 | return true; |
92 | |
93 | if (delta == 0) |
94 | break; |
95 | } |
96 | |
97 | return false; |
98 | } |
99 | |
100 | // [85] BaseChar ::= ... |
101 | |
102 | static const QXmlCharRange g_base_begin[] = |
103 | { |
104 | {.min: 0x0041, .max: 0x005A}, {.min: 0x0061, .max: 0x007A}, {.min: 0x00C0, .max: 0x00D6}, {.min: 0x00D8, .max: 0x00F6}, {.min: 0x00F8, .max: 0x00FF}, |
105 | {.min: 0x0100, .max: 0x0131}, {.min: 0x0134, .max: 0x013E}, {.min: 0x0141, .max: 0x0148}, {.min: 0x014A, .max: 0x017E}, {.min: 0x0180, .max: 0x01C3}, |
106 | {.min: 0x01CD, .max: 0x01F0}, {.min: 0x01F4, .max: 0x01F5}, {.min: 0x01FA, .max: 0x0217}, {.min: 0x0250, .max: 0x02A8}, {.min: 0x02BB, .max: 0x02C1}, |
107 | {.min: 0x0386, .max: 0x0386}, {.min: 0x0388, .max: 0x038A}, {.min: 0x038C, .max: 0x038C}, {.min: 0x038E, .max: 0x03A1}, {.min: 0x03A3, .max: 0x03CE}, |
108 | {.min: 0x03D0, .max: 0x03D6}, {.min: 0x03DA, .max: 0x03DA}, {.min: 0x03DC, .max: 0x03DC}, {.min: 0x03DE, .max: 0x03DE}, {.min: 0x03E0, .max: 0x03E0}, |
109 | {.min: 0x03E2, .max: 0x03F3}, {.min: 0x0401, .max: 0x040C}, {.min: 0x040E, .max: 0x044F}, {.min: 0x0451, .max: 0x045C}, {.min: 0x045E, .max: 0x0481}, |
110 | {.min: 0x0490, .max: 0x04C4}, {.min: 0x04C7, .max: 0x04C8}, {.min: 0x04CB, .max: 0x04CC}, {.min: 0x04D0, .max: 0x04EB}, {.min: 0x04EE, .max: 0x04F5}, |
111 | {.min: 0x04F8, .max: 0x04F9}, {.min: 0x0531, .max: 0x0556}, {.min: 0x0559, .max: 0x0559}, {.min: 0x0561, .max: 0x0586}, {.min: 0x05D0, .max: 0x05EA}, |
112 | {.min: 0x05F0, .max: 0x05F2}, {.min: 0x0621, .max: 0x063A}, {.min: 0x0641, .max: 0x064A}, {.min: 0x0671, .max: 0x06B7}, {.min: 0x06BA, .max: 0x06BE}, |
113 | {.min: 0x06C0, .max: 0x06CE}, {.min: 0x06D0, .max: 0x06D3}, {.min: 0x06D5, .max: 0x06D5}, {.min: 0x06E5, .max: 0x06E6}, {.min: 0x0905, .max: 0x0939}, |
114 | {.min: 0x093D, .max: 0x093D}, {.min: 0x0958, .max: 0x0961}, {.min: 0x0985, .max: 0x098C}, {.min: 0x098F, .max: 0x0990}, {.min: 0x0993, .max: 0x09A8}, |
115 | {.min: 0x09AA, .max: 0x09B0}, {.min: 0x09B2, .max: 0x09B2}, {.min: 0x09B6, .max: 0x09B9}, {.min: 0x09DC, .max: 0x09DD}, {.min: 0x09DF, .max: 0x09E1}, |
116 | {.min: 0x09F0, .max: 0x09F1}, {.min: 0x0A05, .max: 0x0A0A}, {.min: 0x0A0F, .max: 0x0A10}, {.min: 0x0A13, .max: 0x0A28}, {.min: 0x0A2A, .max: 0x0A30}, |
117 | {.min: 0x0A32, .max: 0x0A33}, {.min: 0x0A35, .max: 0x0A36}, {.min: 0x0A38, .max: 0x0A39}, {.min: 0x0A59, .max: 0x0A5C}, {.min: 0x0A5E, .max: 0x0A5E}, |
118 | {.min: 0x0A72, .max: 0x0A74}, {.min: 0x0A85, .max: 0x0A8B}, {.min: 0x0A8D, .max: 0x0A8D}, {.min: 0x0A8F, .max: 0x0A91}, {.min: 0x0A93, .max: 0x0AA8}, |
119 | {.min: 0x0AAA, .max: 0x0AB0}, {.min: 0x0AB2, .max: 0x0AB3}, {.min: 0x0AB5, .max: 0x0AB9}, {.min: 0x0ABD, .max: 0x0ABD}, {.min: 0x0AE0, .max: 0x0AE0}, |
120 | {.min: 0x0B05, .max: 0x0B0C}, {.min: 0x0B0F, .max: 0x0B10}, {.min: 0x0B13, .max: 0x0B28}, {.min: 0x0B2A, .max: 0x0B30}, {.min: 0x0B32, .max: 0x0B33}, |
121 | {.min: 0x0B36, .max: 0x0B39}, {.min: 0x0B3D, .max: 0x0B3D}, {.min: 0x0B5C, .max: 0x0B5D}, {.min: 0x0B5F, .max: 0x0B61}, {.min: 0x0B85, .max: 0x0B8A}, |
122 | {.min: 0x0B8E, .max: 0x0B90}, {.min: 0x0B92, .max: 0x0B95}, {.min: 0x0B99, .max: 0x0B9A}, {.min: 0x0B9C, .max: 0x0B9C}, {.min: 0x0B9E, .max: 0x0B9F}, |
123 | {.min: 0x0BA3, .max: 0x0BA4}, {.min: 0x0BA8, .max: 0x0BAA}, {.min: 0x0BAE, .max: 0x0BB5}, {.min: 0x0BB7, .max: 0x0BB9}, {.min: 0x0C05, .max: 0x0C0C}, |
124 | {.min: 0x0C0E, .max: 0x0C10}, {.min: 0x0C12, .max: 0x0C28}, {.min: 0x0C2A, .max: 0x0C33}, {.min: 0x0C35, .max: 0x0C39}, {.min: 0x0C60, .max: 0x0C61}, |
125 | {.min: 0x0C85, .max: 0x0C8C}, {.min: 0x0C8E, .max: 0x0C90}, {.min: 0x0C92, .max: 0x0CA8}, {.min: 0x0CAA, .max: 0x0CB3}, {.min: 0x0CB5, .max: 0x0CB9}, |
126 | {.min: 0x0CDE, .max: 0x0CDE}, {.min: 0x0CE0, .max: 0x0CE1}, {.min: 0x0D05, .max: 0x0D0C}, {.min: 0x0D0E, .max: 0x0D10}, {.min: 0x0D12, .max: 0x0D28}, |
127 | {.min: 0x0D2A, .max: 0x0D39}, {.min: 0x0D60, .max: 0x0D61}, {.min: 0x0E01, .max: 0x0E2E}, {.min: 0x0E30, .max: 0x0E30}, {.min: 0x0E32, .max: 0x0E33}, |
128 | {.min: 0x0E40, .max: 0x0E45}, {.min: 0x0E81, .max: 0x0E82}, {.min: 0x0E84, .max: 0x0E84}, {.min: 0x0E87, .max: 0x0E88}, {.min: 0x0E8A, .max: 0x0E8A}, |
129 | {.min: 0x0E8D, .max: 0x0E8D}, {.min: 0x0E94, .max: 0x0E97}, {.min: 0x0E99, .max: 0x0E9F}, {.min: 0x0EA1, .max: 0x0EA3}, {.min: 0x0EA5, .max: 0x0EA5}, |
130 | {.min: 0x0EA7, .max: 0x0EA7}, {.min: 0x0EAA, .max: 0x0EAB}, {.min: 0x0EAD, .max: 0x0EAE}, {.min: 0x0EB0, .max: 0x0EB0}, {.min: 0x0EB2, .max: 0x0EB3}, |
131 | {.min: 0x0EBD, .max: 0x0EBD}, {.min: 0x0EC0, .max: 0x0EC4}, {.min: 0x0F40, .max: 0x0F47}, {.min: 0x0F49, .max: 0x0F69}, {.min: 0x10A0, .max: 0x10C5}, |
132 | {.min: 0x10D0, .max: 0x10F6}, {.min: 0x1100, .max: 0x1100}, {.min: 0x1102, .max: 0x1103}, {.min: 0x1105, .max: 0x1107}, {.min: 0x1109, .max: 0x1109}, |
133 | {.min: 0x110B, .max: 0x110C}, {.min: 0x110E, .max: 0x1112}, {.min: 0x113C, .max: 0x113C}, {.min: 0x113E, .max: 0x113E}, {.min: 0x1140, .max: 0x1140}, |
134 | {.min: 0x114C, .max: 0x114C}, {.min: 0x114E, .max: 0x114E}, {.min: 0x1150, .max: 0x1150}, {.min: 0x1154, .max: 0x1155}, {.min: 0x1159, .max: 0x1159}, |
135 | {.min: 0x115F, .max: 0x1161}, {.min: 0x1163, .max: 0x1163}, {.min: 0x1165, .max: 0x1165}, {.min: 0x1167, .max: 0x1167}, {.min: 0x1169, .max: 0x1169}, |
136 | {.min: 0x116D, .max: 0x116E}, {.min: 0x1172, .max: 0x1173}, {.min: 0x1175, .max: 0x1175}, {.min: 0x119E, .max: 0x119E}, {.min: 0x11A8, .max: 0x11A8}, |
137 | {.min: 0x11AB, .max: 0x11AB}, {.min: 0x11AE, .max: 0x11AF}, {.min: 0x11B7, .max: 0x11B8}, {.min: 0x11BA, .max: 0x11BA}, {.min: 0x11BC, .max: 0x11C2}, |
138 | {.min: 0x11EB, .max: 0x11EB}, {.min: 0x11F0, .max: 0x11F0}, {.min: 0x11F9, .max: 0x11F9}, {.min: 0x1E00, .max: 0x1E9B}, {.min: 0x1EA0, .max: 0x1EF9}, |
139 | {.min: 0x1F00, .max: 0x1F15}, {.min: 0x1F18, .max: 0x1F1D}, {.min: 0x1F20, .max: 0x1F45}, {.min: 0x1F48, .max: 0x1F4D}, {.min: 0x1F50, .max: 0x1F57}, |
140 | {.min: 0x1F59, .max: 0x1F59}, {.min: 0x1F5B, .max: 0x1F5B}, {.min: 0x1F5D, .max: 0x1F5D}, {.min: 0x1F5F, .max: 0x1F7D}, {.min: 0x1F80, .max: 0x1FB4}, |
141 | {.min: 0x1FB6, .max: 0x1FBC}, {.min: 0x1FBE, .max: 0x1FBE}, {.min: 0x1FC2, .max: 0x1FC4}, {.min: 0x1FC6, .max: 0x1FCC}, {.min: 0x1FD0, .max: 0x1FD3}, |
142 | {.min: 0x1FD6, .max: 0x1FDB}, {.min: 0x1FE0, .max: 0x1FEC}, {.min: 0x1FF2, .max: 0x1FF4}, {.min: 0x1FF6, .max: 0x1FFC}, {.min: 0x2126, .max: 0x2126}, |
143 | {.min: 0x212A, .max: 0x212B}, {.min: 0x212E, .max: 0x212E}, {.min: 0x2180, .max: 0x2182}, {.min: 0x3041, .max: 0x3094}, {.min: 0x30A1, .max: 0x30FA}, |
144 | {.min: 0x3105, .max: 0x312C}, {.min: 0xAC00, .max: 0xD7A3} |
145 | }; |
146 | static const RangeIter g_base_end = g_base_begin + sizeof(g_base_begin) / sizeof(QXmlCharRange); |
147 | |
148 | static const QXmlCharRange g_ideographic_begin[] = |
149 | { |
150 | {.min: 0x3007, .max: 0x3007}, {.min: 0x3021, .max: 0x3029}, {.min: 0x4E00, .max: 0x9FA5} |
151 | }; |
152 | static const RangeIter g_ideographic_end = g_ideographic_begin + sizeof(g_ideographic_begin) / sizeof(QXmlCharRange); |
153 | |
154 | bool QXmlUtils::isIdeographic(const QChar c) |
155 | { |
156 | return rangeContains(begin: g_ideographic_begin, end: g_ideographic_end, c); |
157 | } |
158 | |
159 | static const QXmlCharRange g_combining_begin[] = |
160 | { |
161 | {.min: 0x0300, .max: 0x0345}, {.min: 0x0360, .max: 0x0361}, {.min: 0x0483, .max: 0x0486}, {.min: 0x0591, .max: 0x05A1}, {.min: 0x05A3, .max: 0x05B9}, |
162 | {.min: 0x05BB, .max: 0x05BD}, {.min: 0x05BF, .max: 0x05BF}, {.min: 0x05C1, .max: 0x05C2}, {.min: 0x05C4, .max: 0x05C4}, {.min: 0x064B, .max: 0x0652}, |
163 | {.min: 0x0670, .max: 0x0670}, {.min: 0x06D6, .max: 0x06DC}, {.min: 0x06DD, .max: 0x06DF}, {.min: 0x06E0, .max: 0x06E4}, {.min: 0x06E7, .max: 0x06E8}, |
164 | {.min: 0x06EA, .max: 0x06ED}, {.min: 0x0901, .max: 0x0903}, {.min: 0x093C, .max: 0x093C}, {.min: 0x093E, .max: 0x094C}, {.min: 0x094D, .max: 0x094D}, |
165 | {.min: 0x0951, .max: 0x0954}, {.min: 0x0962, .max: 0x0963}, {.min: 0x0981, .max: 0x0983}, {.min: 0x09BC, .max: 0x09BC}, {.min: 0x09BE, .max: 0x09BE}, |
166 | {.min: 0x09BF, .max: 0x09BF}, {.min: 0x09C0, .max: 0x09C4}, {.min: 0x09C7, .max: 0x09C8}, {.min: 0x09CB, .max: 0x09CD}, {.min: 0x09D7, .max: 0x09D7}, |
167 | {.min: 0x09E2, .max: 0x09E3}, {.min: 0x0A02, .max: 0x0A02}, {.min: 0x0A3C, .max: 0x0A3C}, {.min: 0x0A3E, .max: 0x0A3E}, {.min: 0x0A3F, .max: 0x0A3F}, |
168 | {.min: 0x0A40, .max: 0x0A42}, {.min: 0x0A47, .max: 0x0A48}, {.min: 0x0A4B, .max: 0x0A4D}, {.min: 0x0A70, .max: 0x0A71}, {.min: 0x0A81, .max: 0x0A83}, |
169 | {.min: 0x0ABC, .max: 0x0ABC}, {.min: 0x0ABE, .max: 0x0AC5}, {.min: 0x0AC7, .max: 0x0AC9}, {.min: 0x0ACB, .max: 0x0ACD}, {.min: 0x0B01, .max: 0x0B03}, |
170 | {.min: 0x0B3C, .max: 0x0B3C}, {.min: 0x0B3E, .max: 0x0B43}, {.min: 0x0B47, .max: 0x0B48}, {.min: 0x0B4B, .max: 0x0B4D}, {.min: 0x0B56, .max: 0x0B57}, |
171 | {.min: 0x0B82, .max: 0x0B83}, {.min: 0x0BBE, .max: 0x0BC2}, {.min: 0x0BC6, .max: 0x0BC8}, {.min: 0x0BCA, .max: 0x0BCD}, {.min: 0x0BD7, .max: 0x0BD7}, |
172 | {.min: 0x0C01, .max: 0x0C03}, {.min: 0x0C3E, .max: 0x0C44}, {.min: 0x0C46, .max: 0x0C48}, {.min: 0x0C4A, .max: 0x0C4D}, {.min: 0x0C55, .max: 0x0C56}, |
173 | {.min: 0x0C82, .max: 0x0C83}, {.min: 0x0CBE, .max: 0x0CC4}, {.min: 0x0CC6, .max: 0x0CC8}, {.min: 0x0CCA, .max: 0x0CCD}, {.min: 0x0CD5, .max: 0x0CD6}, |
174 | {.min: 0x0D02, .max: 0x0D03}, {.min: 0x0D3E, .max: 0x0D43}, {.min: 0x0D46, .max: 0x0D48}, {.min: 0x0D4A, .max: 0x0D4D}, {.min: 0x0D57, .max: 0x0D57}, |
175 | {.min: 0x0E31, .max: 0x0E31}, {.min: 0x0E34, .max: 0x0E3A}, {.min: 0x0E47, .max: 0x0E4E}, {.min: 0x0EB1, .max: 0x0EB1}, {.min: 0x0EB4, .max: 0x0EB9}, |
176 | {.min: 0x0EBB, .max: 0x0EBC}, {.min: 0x0EC8, .max: 0x0ECD}, {.min: 0x0F18, .max: 0x0F19}, {.min: 0x0F35, .max: 0x0F35}, {.min: 0x0F37, .max: 0x0F37}, |
177 | {.min: 0x0F39, .max: 0x0F39}, {.min: 0x0F3E, .max: 0x0F3E}, {.min: 0x0F3F, .max: 0x0F3F}, {.min: 0x0F71, .max: 0x0F84}, {.min: 0x0F86, .max: 0x0F8B}, |
178 | {.min: 0x0F90, .max: 0x0F95}, {.min: 0x0F97, .max: 0x0F97}, {.min: 0x0F99, .max: 0x0FAD}, {.min: 0x0FB1, .max: 0x0FB7}, {.min: 0x0FB9, .max: 0x0FB9}, |
179 | {.min: 0x20D0, .max: 0x20DC}, {.min: 0x20E1, .max: 0x20E1}, {.min: 0x302A, .max: 0x302F}, {.min: 0x3099, .max: 0x3099}, {.min: 0x309A, .max: 0x309A} |
180 | }; |
181 | static const RangeIter g_combining_end = g_combining_begin + sizeof(g_combining_begin) / sizeof(QXmlCharRange); |
182 | |
183 | bool QXmlUtils::isCombiningChar(const QChar c) |
184 | { |
185 | return rangeContains(begin: g_combining_begin, end: g_combining_end, c); |
186 | } |
187 | |
188 | // [88] Digit ::= ... |
189 | static const QXmlCharRange g_digit_begin[] = |
190 | { |
191 | {.min: 0x0030, .max: 0x0039}, {.min: 0x0660, .max: 0x0669}, {.min: 0x06F0, .max: 0x06F9}, {.min: 0x0966, .max: 0x096F}, {.min: 0x09E6, .max: 0x09EF}, |
192 | {.min: 0x0A66, .max: 0x0A6F}, {.min: 0x0AE6, .max: 0x0AEF}, {.min: 0x0B66, .max: 0x0B6F}, {.min: 0x0BE7, .max: 0x0BEF}, {.min: 0x0C66, .max: 0x0C6F}, |
193 | {.min: 0x0CE6, .max: 0x0CEF}, {.min: 0x0D66, .max: 0x0D6F}, {.min: 0x0E50, .max: 0x0E59}, {.min: 0x0ED0, .max: 0x0ED9}, {.min: 0x0F20, .max: 0x0F29} |
194 | }; |
195 | static const RangeIter g_digit_end = g_digit_begin + sizeof(g_digit_begin) / sizeof(QXmlCharRange); |
196 | |
197 | bool QXmlUtils::isDigit(const QChar c) |
198 | { |
199 | return rangeContains(begin: g_digit_begin, end: g_digit_end, c); |
200 | } |
201 | |
202 | // [89] Extender ::= ... |
203 | static const QXmlCharRange g_extender_begin[] = |
204 | { |
205 | {.min: 0x00B7, .max: 0x00B7}, {.min: 0x02D0, .max: 0x02D0}, {.min: 0x02D1, .max: 0x02D1}, {.min: 0x0387, .max: 0x0387}, {.min: 0x0640, .max: 0x0640}, |
206 | {.min: 0x0E46, .max: 0x0E46}, {.min: 0x0EC6, .max: 0x0EC6}, {.min: 0x3005, .max: 0x3005}, {.min: 0x3031, .max: 0x3035}, {.min: 0x309D, .max: 0x309E}, |
207 | {.min: 0x30FC, .max: 0x30FE} |
208 | }; |
209 | static const RangeIter g_extender_end = g_extender_begin + sizeof(g_extender_begin) / sizeof(QXmlCharRange); |
210 | |
211 | bool QXmlUtils::isExtender(const QChar c) |
212 | { |
213 | return rangeContains(begin: g_extender_begin, end: g_extender_end, c); |
214 | } |
215 | |
216 | bool QXmlUtils::isBaseChar(const QChar c) |
217 | { |
218 | return rangeContains(begin: g_base_begin, end: g_base_end, c); |
219 | } |
220 | |
221 | /*! |
222 | \internal |
223 | |
224 | Determines whether \a encName is a valid instance of production [81]EncName in the XML 1.0 |
225 | specification. If it is, true is returned, otherwise false. |
226 | |
227 | \sa {http://www.w3.org/TR/REC-xml/#NT-EncName}, |
228 | {Extensible Markup Language (XML) 1.0 (Fourth Edition), [81] EncName} |
229 | */ |
230 | bool QXmlUtils::isEncName(QStringView encName) |
231 | { |
232 | // Valid encoding names are given by "[A-Za-z][A-Za-z0-9._\\-]*" |
233 | if (encName.isEmpty()) |
234 | return false; |
235 | const auto first = encName.front().unicode(); |
236 | if (!((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z'))) |
237 | return false; |
238 | for (QChar ch : encName.mid(pos: 1)) { |
239 | const auto cp = ch.unicode(); |
240 | if ((cp >= 'a' && cp <= 'z') |
241 | || (cp >= 'A' && cp <= 'Z') |
242 | || (cp >= '0' && cp <= '9') |
243 | || cp == '.' || cp == '_' || cp == '-') { |
244 | continue; |
245 | } |
246 | return false; |
247 | } |
248 | return true; |
249 | } |
250 | |
251 | /*! |
252 | \internal |
253 | |
254 | Determines whether \a c is a valid instance of production [84]Letter in the XML 1.0 |
255 | specification. If it is, true is returned, otherwise false. |
256 | |
257 | \sa {http://www.w3.org/TR/REC-xml/#NT-Letter}, |
258 | {Extensible Markup Language (XML) 1.0 (Fourth Edition), [84] Letter} |
259 | */ |
260 | bool QXmlUtils::isLetter(const QChar c) |
261 | { |
262 | return isBaseChar(c) || isIdeographic(c); |
263 | } |
264 | |
265 | /*! |
266 | \internal |
267 | |
268 | Determines whether \a c is a valid instance of production [2]Char in the XML 1.0 |
269 | specification. If it is, true is returned, otherwise false. |
270 | |
271 | \sa {http://www.w3.org/TR/REC-xml/#NT-Char}, |
272 | {Extensible Markup Language (XML) 1.0 (Fourth Edition), [2] Char} |
273 | */ |
274 | bool QXmlUtils::isChar(const QChar c) |
275 | { |
276 | return (c.unicode() >= 0x0020 && c.unicode() <= 0xD7FF) |
277 | || c.unicode() == 0x0009 |
278 | || c.unicode() == 0x000A |
279 | || c.unicode() == 0x000D |
280 | || (c.unicode() >= 0xE000 && c.unicode() <= 0xFFFD); |
281 | } |
282 | |
283 | /*! |
284 | \internal |
285 | |
286 | Determines whether \a c is a valid instance of |
287 | production [4]NameChar in the XML 1.0 specification. If it |
288 | is, true is returned, otherwise false. |
289 | |
290 | \sa {http://www.w3.org/TR/REC-xml/#NT-NameChar}, |
291 | {Extensible Markup Language (XML) 1.0 (Fourth Edition), [4] NameChar} |
292 | */ |
293 | bool QXmlUtils::isNameChar(const QChar c) |
294 | { |
295 | return isBaseChar(c) |
296 | || isDigit(c) |
297 | || c.unicode() == '.' |
298 | || c.unicode() == '-' |
299 | || c.unicode() == '_' |
300 | || c.unicode() == ':' |
301 | || isCombiningChar(c) |
302 | || isIdeographic(c) |
303 | || isExtender(c); |
304 | } |
305 | |
306 | /*! |
307 | \internal |
308 | |
309 | Determines whether \a c is a valid instance of |
310 | production [12] PubidLiteral in the XML 1.0 specification. If it |
311 | is, true is returned, otherwise false. |
312 | |
313 | \sa {http://www.w3.org/TR/REC-xml/#NT-PubidLiteral}, |
314 | {Extensible Markup Language (XML) 1.0 (Fourth Edition), [12] PubidLiteral} |
315 | */ |
316 | bool QXmlUtils::isPublicID(QStringView candidate) |
317 | { |
318 | for (QChar ch : candidate) { |
319 | const ushort cp = ch.unicode(); |
320 | |
321 | if ((cp >= 'a' && cp <= 'z') |
322 | || (cp >= 'A' && cp <= 'Z') |
323 | || (cp >= '0' && cp <= '9')) |
324 | { |
325 | continue; |
326 | } |
327 | |
328 | switch (cp) |
329 | { |
330 | /* Fallthrough all these. */ |
331 | case 0x20: |
332 | case 0x0D: |
333 | case 0x0A: |
334 | case '-': |
335 | case '\'': |
336 | case '(': |
337 | case ')': |
338 | case '+': |
339 | case ',': |
340 | case '.': |
341 | case '/': |
342 | case ':': |
343 | case '=': |
344 | case '?': |
345 | case ';': |
346 | case '!': |
347 | case '*': |
348 | case '#': |
349 | case '@': |
350 | case '$': |
351 | case '_': |
352 | case '%': |
353 | continue; |
354 | default: |
355 | return false; |
356 | } |
357 | } |
358 | |
359 | return true; |
360 | } |
361 | |
362 | /*! |
363 | \internal |
364 | |
365 | Determines whether \a c is a valid instance of |
366 | production [4]NCName in the XML 1.0 Namespaces specification. If it |
367 | is, true is returned, otherwise false. |
368 | |
369 | \sa {http://www.w3.org/TR/REC-xml-names/#NT-NCName}, |
370 | {W3CNamespaces in XML 1.0 (Second Edition), [4] NCName} |
371 | */ |
372 | bool QXmlUtils::isNCName(QStringView ncName) |
373 | { |
374 | if(ncName.isEmpty()) |
375 | return false; |
376 | |
377 | const QChar first(ncName.at(n: 0)); |
378 | |
379 | if(!QXmlUtils::isLetter(c: first) && first.unicode() != '_' && first.unicode() != ':') |
380 | return false; |
381 | |
382 | for (QChar at : ncName) { |
383 | if(!QXmlUtils::isNameChar(c: at) || at == QLatin1Char(':')) |
384 | return false; |
385 | } |
386 | |
387 | return true; |
388 | } |
389 | |
390 | QT_END_NAMESPACE |
391 | |