stream.cpp 12.4 KB
Newer Older
1
2
#include "stream.h"
#include <iostream>
3
4
5
6
7
8
9
10
11
12
#include "exp.h"

#ifndef YAML_PREFETCH_SIZE
#define YAML_PREFETCH_SIZE 2048
#endif

#define S_ARRAY_SIZE( A ) (sizeof(A)/sizeof(*(A)))
#define S_ARRAY_END( A ) ((A) + S_ARRAY_SIZE(A))

#define CP_REPLACEMENT_CHARACTER (0xFFFD)
13
14
15

namespace YAML
{
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
	enum UtfIntroState {
		uis_start,
		uis_utfbe_b1,
		uis_utf32be_b2,
		uis_utf32be_bom3,
		uis_utf32be,
		uis_utf16be,
		uis_utf16be_bom1,
		uis_utfle_bom1,
		uis_utf16le_bom2,
		uis_utf32le_bom3,
		uis_utf16le,
		uis_utf32le,
		uis_utf8_imp,
		uis_utf16le_imp,
		uis_utf32le_imp3,
		uis_utf8_bom1,
		uis_utf8_bom2,
		uis_utf8,
		uis_error
	};

	enum UtfIntroCharType {
		uict00,
		uictBB,
		uictBF,
		uictEF,
		uictFE,
		uictFF,
		uictAscii,
		uictOther,
		uictMax
	};

	static bool s_introFinalState[] = {
		false, //uis_start
		false, //uis_utfbe_b1
		false, //uis_utf32be_b2
		false, //uis_utf32be_bom3
		true,  //uis_utf32be
		true,  //uis_utf16be
		false, //uis_utf16be_bom1
		false, //uis_utfle_bom1
		false, //uis_utf16le_bom2
		false, //uis_utf32le_bom3
		true,  //uis_utf16le
		true,  //uis_utf32le
		false, //uis_utf8_imp
		false, //uis_utf16le_imp
		false, //uis_utf32le_imp3
		false, //uis_utf8_bom1
		false, //uis_utf8_bom2
		true,  //uis_utf8
		true,  //uis_error
	};

	static UtfIntroState s_introTransitions[][uictMax] = {
		// uict00,           uictBB,           uictBF,           uictEF,           uictFE,           uictFF,           uictAscii,        uictOther
		  {uis_utfbe_b1,     uis_utf8,         uis_utf8,         uis_utf8_bom1,    uis_utf16be_bom1, uis_utfle_bom1,   uis_utf8_imp,     uis_utf8},
		  {uis_utf32be_b2,   uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf16be,      uis_utf8},
		  {uis_utf32be,      uis_utf8,         uis_utf8,         uis_utf8,         uis_utf32be_bom3, uis_utf8,         uis_utf8,         uis_utf8},
		  {uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf32be,      uis_utf8,         uis_utf8},
		  {uis_utf32be,      uis_utf32be,      uis_utf32be,      uis_utf32be,      uis_utf32be,      uis_utf32be,      uis_utf32be,      uis_utf32be},
		  {uis_utf16be,      uis_utf16be,      uis_utf16be,      uis_utf16be,      uis_utf16be,      uis_utf16be,      uis_utf16be,      uis_utf16be},
		  {uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf16be,      uis_utf8,         uis_utf8},
		  {uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf16le_bom2, uis_utf8,         uis_utf8,         uis_utf8},
		  {uis_utf32le_bom3, uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le},
		  {uis_utf32le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le},
		  {uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le},
		  {uis_utf32le,      uis_utf32le,      uis_utf32le,      uis_utf32le,      uis_utf32le,      uis_utf32le,      uis_utf32le,      uis_utf32le},
		  {uis_utf16le_imp,  uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8},
		  {uis_utf32le_imp3, uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le},
		  {uis_utf32le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le,      uis_utf16le},
		  {uis_utf8,         uis_utf8_bom2,    uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8},
		  {uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8},
		  {uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8,         uis_utf8},
	};

	static char s_introUngetCount[][uictMax] = {
		// uict00, uictBB, uictBF, uictEF, uictFE, uictFF, uictAscii, uictOther
		  {0,      1,      1,      0,      0,      0,      0,         1},
		  {0,      2,      2,      2,      2,      2,      2,         2},
		  {3,      3,      3,      3,      0,      3,      3,         3},
		  {4,      4,      4,      4,      4,      0,      4,         4},
		  {1,      1,      1,      1,      1,      1,      1,         1},
		  {1,      1,      1,      1,      1,      1,      1,         1},
		  {2,      2,      2,      2,      2,      0,      2,         2},
		  {2,      2,      2,      2,      0,      2,      2,         2},
		  {0,      1,      1,      1,      1,      1,      1,         1},
		  {0,      2,      2,      2,      2,      2,      2,         2},
		  {1,      1,      1,      1,      1,      1,      1,         1},
		  {1,      1,      1,      1,      1,      1,      1,         1},
		  {0,      2,      2,      2,      2,      2,      2,         2},
		  {0,      3,      3,      3,      3,      3,      3,         3},
		  {4,      4,      4,      4,      4,      4,      4,         4},
		  {2,      0,      2,      2,      2,      2,      2,         2},
		  {3,      3,      0,      3,      3,      3,      3,         3},
		  {1,      1,      1,      1,      1,      1,      1,         1},
	};

	inline UtfIntroCharType IntroCharTypeOf(std::istream::int_type ch)
	{
		if (std::istream::traits_type::eof() == ch) {
			return uictOther;
		}

		switch (ch) {
		case 0: return uict00;
		case 0xBB: return uictBB;
		case 0xBF: return uictBF;
		case 0xEF: return uictEF;
		case 0xFE: return uictFE;
		case 0xFF: return uictFF;
		}

		if ((ch > 0) && (ch < 0xFF)) {
			return uictAscii;
		}

		return uictOther;
	}

	inline char Utf8Adjust(unsigned long ch, unsigned char lead_bits, unsigned char rshift)
	{
		const unsigned char header = ((1 << lead_bits) - 1) << (8 - lead_bits);
		const unsigned char mask = (0xFF >> (lead_bits + 1));
		return static_cast<char>(static_cast<unsigned char>(
			header | ((ch >> rshift) & mask)
			));
	}

	inline void QueueUnicodeCodepoint(std::deque<char>& q, unsigned long ch)
	{
		// We are not allowed to queue the Stream::eof() codepoint, so
		// replace it with CP_REPLACEMENT_CHARACTER
		if (static_cast<unsigned long>(Stream::eof()) == ch)
		{
			ch = CP_REPLACEMENT_CHARACTER;
		}

		if (ch < 0x80)
		{
			q.push_back(Utf8Adjust(ch, 0, 0));
		}
		else if (ch < 0x800)
		{
			q.push_back(Utf8Adjust(ch, 2, 6));
			q.push_back(Utf8Adjust(ch, 1, 0));
		}
		else if (ch < 0x10000)
		{
			q.push_back(Utf8Adjust(ch, 3, 12));
			q.push_back(Utf8Adjust(ch, 1, 6));
			q.push_back(Utf8Adjust(ch, 1, 0));
		}
		else
		{
			q.push_back(Utf8Adjust(ch, 4, 18));
			q.push_back(Utf8Adjust(ch, 1, 12));
			q.push_back(Utf8Adjust(ch, 1, 6));
			q.push_back(Utf8Adjust(ch, 1, 0));
		}
	}

	Stream::Stream(std::istream& input)
181
		: m_input(input),
182
183
		m_pPrefetched(new unsigned char[YAML_PREFETCH_SIZE]), 
		m_nPrefetchedAvailable(0), m_nPrefetchedUsed(0)
184
	{
185
186
		typedef std::istream::traits_type char_traits;

187
188
189
		if(!input)
			return;

190
191
192
193
194
		// Determine (or guess) the character-set by reading the BOM, if any.  See
		// the YAML specification for the determination algorithm.
		char_traits::int_type intro[4];
		int nIntroUsed = 0;
		UtfIntroState state = uis_start;
195
		for(; !s_introFinalState[state]; ) {
196
197
198
199
200
			std::istream::int_type ch = input.get();
			intro[nIntroUsed++] = ch;
			UtfIntroCharType charType = IntroCharTypeOf(ch);
			UtfIntroState newState = s_introTransitions[state][charType];
			int nUngets = s_introUngetCount[state][charType];
201
202
203
204
205
			if(nUngets > 0) {
				input.clear();
				for(; nUngets > 0; --nUngets) {
					if(char_traits::eof() != intro[--nIntroUsed])
						input.putback(char_traits::to_char_type(intro[nIntroUsed]));
206
207
208
209
210
211
212
213
214
215
216
217
218
				}
			}
			state = newState;
		}

		switch (state) {
		case uis_utf8: m_charSet = utf8; break;
		case uis_utf16le: m_charSet = utf16le; break;
		case uis_utf16be: m_charSet = utf16be; break;
		case uis_utf32le: m_charSet = utf32le; break;
		case uis_utf32be: m_charSet = utf32be; break;
		default: m_charSet = utf8; break;
		}
219

220
		ReadAheadTo(0);
221
	}
222
223
224

	Stream::~Stream()
	{
225
		delete[] m_pPrefetched;
226
227
	}

228
	char Stream::peek() const
229
	{
230
231
232
233
234
235
		if (m_readahead.empty())
		{
			return Stream::eof();
		}

		return m_readahead[0];
236
237
	}
	
238
	Stream::operator bool() const
239
	{
240
		return m_input.good() || (!m_readahead.empty() && m_readahead[0] != Stream::eof());
241
242
243
244
245
246
	}

	// get
	// . Extracts a character from the stream and updates our position
	char Stream::get()
	{
247
248
		char ch = peek();
		AdvanceCurrent();
249
		m_mark.column++;
250
		
251
		if(ch == '\n') {
252
253
			m_mark.column = 0;
			m_mark.line++;
254
		}
255
		
256
257
258
259
260
261
262
263
		return ch;
	}

	// get
	// . Extracts 'n' characters from the stream and updates our position
	std::string Stream::get(int n)
	{
		std::string ret;
264
		ret.reserve(n);
265
266
267
268
269
270
271
272
273
274
275
276
277
		for(int i=0;i<n;i++)
			ret += get();
		return ret;
	}

	// eat
	// . Eats 'n' characters and updates our position.
	void Stream::eat(int n)
	{
		for(int i=0;i<n;i++)
			get();
	}

278
279
280
281
282
	void Stream::AdvanceCurrent()
	{
		if (!m_readahead.empty())
		{
			m_readahead.pop_front();
283
			m_mark.pos++;
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
		}

		ReadAheadTo(0);
	}

	bool Stream::_ReadAheadTo(size_t i) const
	{
		while (m_input.good() && (m_readahead.size() <= i))
		{
			switch (m_charSet)
			{
			case utf8: StreamInUtf8(); break;
			case utf16le: StreamInUtf16(); break;
			case utf16be: StreamInUtf16(); break;
			case utf32le: StreamInUtf32(); break;
			case utf32be: StreamInUtf32(); break;
			}
		}
		
		// signal end of stream
		if(!m_input.good())
			m_readahead.push_back(Stream::eof());

		return m_readahead.size() > i;
	}

	void Stream::StreamInUtf8() const
	{
		unsigned char b = GetNextByte();
		if (m_input.good())
		{
			m_readahead.push_back(b);
		}
	}

	void Stream::StreamInUtf16() const
	{
		unsigned long ch = 0;
		unsigned char bytes[2];
		int nBigEnd = (m_charSet == utf16be) ? 0 : 1;

		bytes[0] = GetNextByte();
		bytes[1] = GetNextByte();
		if (!m_input.good())
		{
			return;
		}
		ch = (static_cast<unsigned long>(bytes[nBigEnd]) << 8) |
			static_cast<unsigned long>(bytes[1 ^ nBigEnd]);

		if (ch >= 0xDC00 && ch < 0xE000)
		{
			// Trailing (low) surrogate...ugh, wrong order
			QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER);
			return;
		}
		else if (ch >= 0xD800 && ch < 0xDC00)
		{
			// ch is a leading (high) surrogate

			// Four byte UTF-8 code point

			// Read the trailing (low) surrogate
			for (;;)
			{
				bytes[0] = GetNextByte();
				bytes[1] = GetNextByte();
				if (!m_input.good())
				{
					QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER);
					return;
				}
				unsigned long chLow = (static_cast<unsigned long>(bytes[nBigEnd]) << 8) |
					static_cast<unsigned long>(bytes[1 ^ nBigEnd]);
				if (chLow < 0xDC00 || ch >= 0xE000)
				{
					// Trouble...not a low surrogate.  Dump a REPLACEMENT CHARACTER into the stream.
					QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER);

					// Deal with the next UTF-16 unit
					if (chLow < 0xD800 || ch >= 0xE000)
					{
						// Easiest case: queue the codepoint and return
						QueueUnicodeCodepoint(m_readahead, ch);
						return;
					}
					else
					{
						// Start the loop over with the new high surrogate
						ch = chLow;
						continue;
					}
				}

				// Select the payload bits from the high surrogate
				ch &= 0x3FF;
				ch <<= 10;

				// Include bits from low surrogate
				ch |= (chLow & 0x3FF);

				// Add the surrogacy offset
				ch += 0x10000;
			}
		}

		QueueUnicodeCodepoint(m_readahead, ch);
	}

	inline char* ReadBuffer(unsigned char* pBuffer)
	{
		return reinterpret_cast<char*>(pBuffer);
	}

	unsigned char Stream::GetNextByte() const
	{
		if (m_nPrefetchedUsed >= m_nPrefetchedAvailable)
		{
			std::streambuf *pBuf = m_input.rdbuf();
403
			m_nPrefetchedAvailable = static_cast<std::size_t>(pBuf->sgetn(ReadBuffer(m_pPrefetched), YAML_PREFETCH_SIZE));
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
			m_nPrefetchedUsed = 0;
			if (!m_nPrefetchedAvailable)
			{
				m_input.setstate(std::ios_base::eofbit);
			}

			if (0 == m_nPrefetchedAvailable)
			{
				return 0;
			}
		}

		return m_pPrefetched[m_nPrefetchedUsed++];
	}

	void Stream::StreamInUtf32() const
	{
		static int indexes[2][4] = {
			{3, 2, 1, 0},
			{0, 1, 2, 3}
		};

		unsigned long ch = 0;
		unsigned char bytes[4];
		int* pIndexes = (m_charSet == utf32be) ? indexes[1] : indexes[0];

		bytes[0] = GetNextByte();
		bytes[1] = GetNextByte();
		bytes[2] = GetNextByte();
		bytes[3] = GetNextByte();
		if (!m_input.good())
		{
			return;
		}

		for (int i = 0; i < 4; ++i)
		{
			ch <<= 8;
			ch |= bytes[pIndexes[i]];
		}

		QueueUnicodeCodepoint(m_readahead, ch);
	}
447
}