summaryrefslogtreecommitdiff
path: root/src/pugiutf.hpp
blob: dfca94083507e0fc2b4922b8124ead5356f6ee70 (plain)
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
/**
 * pugixml parser - version 0.5
 * --------------------------------------------------------
 * Copyright (C) 2006-2009, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
 * Report bugs and download new versions at http://code.google.com/p/pugixml/
 *
 * This library is distributed under the MIT License. See notice at the end
 * of this file.
 *
 * This work is based on the pugxml parser, which is:
 * Copyright (C) 2003, by Kristen Wegner (kristen@tima.net)
 */

#ifndef HEADER_PUGIUTF_HPP
#define HEADER_PUGIUTF_HPP

namespace pugi
{
	namespace impl
	{
		typedef unsigned char char8_t;
		typedef unsigned short char16_t;
		typedef unsigned int char32_t;

		inline char16_t endian_swap(char16_t value)
		{
			return static_cast<char16_t>(((value & 0xff) << 8) | (value >> 8));
		}

		inline char32_t endian_swap(char32_t value)
		{
			return ((value & 0xff) << 24) | ((value & 0xff00) << 8) | ((value & 0xff0000) >> 8) | (value >> 24);
		}

		struct utf8_counter
		{
			typedef size_t value_type;

			static value_type low(value_type result, char32_t ch)
			{
				// U+0000..U+007F
				if (ch < 0x80) return result + 1;
				// U+0080..U+07FF
				else if (ch < 0x800) return result + 2;
				// U+0800..U+FFFF
				else return result + 3;
			}

			static value_type high(value_type result, char32_t)
			{
				// U+10000..U+10FFFF
				return result + 4;
			}
		};

		struct utf8_writer
		{
			typedef char8_t* value_type;

			static value_type low(value_type result, char32_t ch)
			{
				// U+0000..U+007F
				if (ch < 0x80)
				{
					*result = static_cast<char8_t>(ch);
					return result + 1;
				}
				// U+0080..U+07FF
				else if (ch < 0x800)
				{
					result[0] = static_cast<char8_t>(0xC0 | (ch >> 6));
					result[1] = static_cast<char8_t>(0x80 | (ch & 0x3F));
					return result + 2;
				}
				// U+0800..U+FFFF
				else
				{
					result[0] = static_cast<char8_t>(0xE0 | (ch >> 12));
					result[1] = static_cast<char8_t>(0x80 | ((ch >> 6) & 0x3F));
					result[2] = static_cast<char8_t>(0x80 | (ch & 0x3F));
					return result + 3;
				}
			}

			static value_type high(value_type result, char32_t ch)
			{
				// U+10000..U+10FFFF
				result[0] = static_cast<char8_t>(0xF0 | (ch >> 18));
				result[1] = static_cast<char8_t>(0x80 | ((ch >> 12) & 0x3F));
				result[2] = static_cast<char8_t>(0x80 | ((ch >> 6) & 0x3F));
				result[3] = static_cast<char8_t>(0x80 | (ch & 0x3F));
				return result + 4;
			}

			static value_type any(value_type result, char32_t ch)
			{
				return (ch < 0x10000) ? low(result, ch) : high(result, ch);
			}
		};

		struct utf16_counter
		{
			typedef size_t value_type;

			static value_type low(value_type result, char32_t)
			{
				return result + 1;
			}

			static value_type high(value_type result, char32_t)
			{
				return result + 2;
			}
		};

		struct utf16_writer
		{
			typedef char16_t* value_type;

			static value_type low(value_type result, char32_t ch)
			{
				*result = static_cast<char16_t>(ch);

				return result + 1;
			}

			static value_type high(value_type result, char32_t ch)
			{
				char32_t msh = (char32_t)(ch - 0x10000) >> 10;
				char32_t lsh = (char32_t)(ch - 0x10000) & 0x3ff;

				result[0] = static_cast<char16_t>(0xD800 + msh);
				result[1] = static_cast<char16_t>(0xDC00 + lsh);

				return result + 2;
			}

			static value_type any(value_type result, char32_t ch)
			{
				return (ch < 0x10000) ? low(result, ch) : high(result, ch);
			}
		};

		struct utf32_counter
		{
			typedef size_t value_type;

			static value_type low(value_type result, char32_t)
			{
				return result + 1;
			}

			static value_type high(value_type result, char32_t)
			{
				return result + 1;
			}
		};

		struct utf32_writer
		{
			typedef char32_t* value_type;

			static value_type low(value_type result, char32_t ch)
			{
				*result = ch;

				return result + 1;
			}

			static value_type high(value_type result, char32_t ch)
			{
				*result = ch;

				return result + 1;
			}

			static value_type any(value_type result, char32_t ch)
			{
				*result = ch;

				return result + 1;
			}
		};

		template <size_t size> struct wchar_selector;

		template <> struct wchar_selector<2>
		{
			typedef char16_t type;
			typedef utf16_counter counter;
			typedef utf16_writer writer;
		};

		template <> struct wchar_selector<4>
		{
			typedef char32_t type;
			typedef utf32_counter counter;
			typedef utf32_writer writer;
		};

		typedef wchar_selector<sizeof(wchar_t)>::counter wchar_counter;
		typedef wchar_selector<sizeof(wchar_t)>::writer wchar_writer;

		template <typename Traits> static inline typename Traits::value_type decode_utf8_block(const char8_t* data, size_t size, typename Traits::value_type result, Traits = Traits())
		{
			const char8_t utf8_byte_mask = 0x3f;

			const char8_t* end = data + size;

			while (data < end)
			{
				char8_t lead = *data;

				// 0xxxxxxx -> U+0000..U+007F
				if (lead < 0x80)
				{
					result = Traits::low(result, lead);
					data += 1;
				}
				// 110xxxxx -> U+0080..U+07FF
				else if ((unsigned)(lead - 0xC0) < 0x20 && data + 1 < end && (data[1] & 0xc0) == 0x80)
				{
					result = Traits::low(result, ((lead & ~0xC0) << 6) | (data[1] & utf8_byte_mask));
					data += 2;
				}
				// 1110xxxx -> U+0800-U+FFFF
				else if ((unsigned)(lead - 0xE0) < 0x10 && data + 2 < end && (data[1] & 0xc0) == 0x80 && (data[2] & 0xc0) == 0x80)
				{
					result = Traits::low(result, ((lead & ~0xE0) << 12) | ((data[1] & utf8_byte_mask) << 6) | (data[2] & utf8_byte_mask));
					data += 3;
				}
				// 11110xxx -> U+10000..U+10FFFF
				else if ((unsigned)(lead - 0xF0) < 0x08 && data + 3 < end && (data[1] & 0xc0) == 0x80 && (data[2] & 0xc0) == 0x80 && (data[3] & 0xc0) == 0x80)
				{
					result = Traits::high(result, ((lead & ~0xF0) << 18) | ((data[1] & utf8_byte_mask) << 12) | ((data[2] & utf8_byte_mask) << 6) | (data[3] & utf8_byte_mask));
					data += 4;
				}
				// 10xxxxxx or 11111xxx -> invalid
				else
				{
					data += 1;
				}
			}

			return result;
		}

		template <typename Traits, typename opt1> static inline typename Traits::value_type decode_utf16_block(const char16_t* data, size_t size, typename Traits::value_type result, opt1, Traits = Traits())
		{
			const bool swap = opt1::o1;

			const char16_t* end = data + size;

			while (data < end)
			{
				char16_t lead = swap ? endian_swap(*data) : *data;

				// U+0000..U+D7FF
				if (lead < 0xD800)
				{
					result = Traits::low(result, lead);
					data += 1;
				}
				// U+E000..U+FFFF
				else if ((unsigned)(lead - 0xE000) < 0x2000)
				{
					result = Traits::low(result, lead);
					data += 1;
				}
				// surrogate pair lead
				else if ((unsigned)(lead - 0xD800) < 0x400 && data + 1 < end)
				{
					char16_t next = swap ? endian_swap(data[1]) : data[1];

					if ((unsigned)(next - 0xDC00) < 0x400)
					{
						result = Traits::high(result, 0x10000 + ((lead & 0x3ff) << 10) + (next & 0x3ff));
						data += 2;
					}
					else
					{
						data += 1;
					}
				}
				else
				{
					data += 1;
				}
			}

			return result;
		}

		template <typename Traits, typename opt1> static inline typename Traits::value_type decode_utf32_block(const char32_t* data, size_t size, typename Traits::value_type result, opt1, Traits = Traits())
		{
			const bool swap = opt1::o1;

			const char32_t* end = data + size;

			while (data < end)
			{
				char32_t lead = swap ? endian_swap(*data) : *data;

				// U+0000..U+FFFF
				if (lead < 0x10000)
				{
					result = Traits::low(result, lead);
					data += 1;
				}
				// U+10000..U+10FFFF
				else
				{
					result = Traits::high(result, lead);
					data += 1;
				}
			}

			return result;
		}

		template <typename T> inline void convert_utf_endian_swap(T* result, const T* data, size_t length)
		{
			for (size_t i = 0; i < length; ++i) result[i] = endian_swap(data[i]);
		}

		inline void convert_wchar_endian_swap(wchar_t* result, const wchar_t* data, size_t length)
		{
			for (size_t i = 0; i < length; ++i) result[i] = static_cast<wchar_t>(endian_swap(static_cast<wchar_selector<sizeof(wchar_t)>::type>(data[i])));
		}
	}
}

#endif

/**
 * Copyright (c) 2006-2009 Arseny Kapoulkine
 *
 * 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.
 */