"script/docker-rocm4.3.1.sh" did not exist on "6fe3627a9eb35f1237266f1b6cc8fd3456aed67d"
emitterutils.cpp 6.52 KB
Newer Older
1
2
3
4
#include "emitterutils.h"
#include "exp.h"
#include "indentation.h"
#include "exceptions.h"
5
#include "stringsource.h"
6
7
#include <sstream>
#include <iomanip>
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

namespace YAML
{
	namespace Utils
	{
		namespace {
			bool IsPrintable(char ch) {
				return (0x20 <= ch && ch <= 0x7E);
			}
			
			bool IsValidPlainScalar(const std::string& str, bool inFlow) {
				// first check the start
				const RegEx& start = (inFlow ? Exp::PlainScalarInFlow : Exp::PlainScalar);
				if(!start.Matches(str))
					return false;
				
				// and check the end for plain whitespace (which can't be faithfully kept in a plain scalar)
				if(!str.empty() && *str.rbegin() == ' ')
					return false;

				// then check until something is disallowed
				const RegEx& disallowed = (inFlow ? Exp::EndScalarInFlow : Exp::EndScalar)
				                          || (Exp::BlankOrBreak + Exp::Comment)
				                          || (!Exp::Printable)
				                          || Exp::Break
				                          || Exp::Tab;
34
35
				StringCharSource buffer(str.c_str(), str.size());
				while(buffer) {
36
37
38
39
40
41
42
					if(disallowed.Matches(buffer))
						return false;
					++buffer;
				}
				
				return true;
			}
Jesse Beder's avatar
Jesse Beder committed
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
			
			unsigned ToUnsigned(char ch) { return static_cast<unsigned int>(static_cast<unsigned char>(ch)); }
			unsigned AdvanceAndGetNextChar(std::string::const_iterator& it, std::string::const_iterator end) {
				std::string::const_iterator jt = it;
				++jt;
				if(jt == end)
					return 0;
				
				++it;
				return ToUnsigned(*it);
			}
			
			std::string WriteUnicode(unsigned value) {
				std::stringstream str;
				// TODO: for the common escaped characters, give their usual symbol
				if(value <= 0xFF)
					str << "\\x" << std::hex << std::setfill('0') << std::setw(2) << value;
				else if(value <= 0xFFFF)
					str << "\\u" << std::hex << std::setfill('0') << std::setw(4) << value;
				else
					str << "\\U" << std::hex << std::setfill('0') << std::setw(8) << value;
				return str.str();
			}
			
			std::string WriteSingleByte(unsigned ch) {
				return WriteUnicode(ch);
			}
			
			std::string WriteTwoBytes(unsigned ch, unsigned ch1) {
				// Note: if no second byte is provided (signalled by ch1 == 0)
				//       then we just write the first one as a single byte.
				//       Should we throw an error instead? Or write something else?
				// (The same question goes for the other WriteNBytes functions)
				if(ch1 == 0)
					return WriteSingleByte(ch);
				
				unsigned value = ((ch - 0xC0) << 6) + (ch1 - 0x80);
				return WriteUnicode(value);
			}
			
			std::string WriteThreeBytes(unsigned ch, unsigned ch1, unsigned ch2) {
				if(ch1 == 0)
					return WriteSingleByte(ch);
				if(ch2 == 0)
					return WriteSingleByte(ch) + WriteSingleByte(ch1);

				unsigned value = ((ch - 0xE0) << 12) + ((ch1 - 0x80) << 6) + (ch2 - 0x80);
				return WriteUnicode(value);
			}

			std::string WriteFourBytes(unsigned ch, unsigned ch1, unsigned ch2, unsigned ch3) {
				if(ch1 == 0)
					return WriteSingleByte(ch);
				if(ch2 == 0)
					return WriteSingleByte(ch) + WriteSingleByte(ch1);
				if(ch3 == 0)
					return WriteSingleByte(ch) + WriteSingleByte(ch1) + WriteSingleByte(ch2);
				
				unsigned value = ((ch - 0xF0) << 18) + ((ch1 - 0x80) << 12) + ((ch2 - 0x80) << 6) + (ch3 - 0x80);
				return WriteUnicode(value);
			}

			// WriteNonPrintable
			// . Writes the next UTF-8 code point to the stream
			std::string::const_iterator WriteNonPrintable(ostream& out, std::string::const_iterator start, std::string::const_iterator end) {
				std::string::const_iterator it = start;
				unsigned ch = ToUnsigned(*it);
				if(ch <= 0xC1) {
					// this may include invalid first characters (0x80 - 0xBF)
					// or "overlong" UTF-8 (0xC0 - 0xC1)
					// We just copy them as bytes
					// TODO: should we do something else? throw an error?
					out << WriteSingleByte(ch);
					return start;
				} else if(ch <= 0xDF) {
					unsigned ch1 = AdvanceAndGetNextChar(it, end);
					out << WriteTwoBytes(ch, ch1);
					return it;
				} else if(ch <= 0xEF) {
					unsigned ch1 = AdvanceAndGetNextChar(it, end);
					unsigned ch2 = AdvanceAndGetNextChar(it, end);
					out << WriteThreeBytes(ch, ch1, ch2);
					return it;
				} else {
					unsigned ch1 = AdvanceAndGetNextChar(it, end);
					unsigned ch2 = AdvanceAndGetNextChar(it, end);
					unsigned ch3 = AdvanceAndGetNextChar(it, end);
					out << WriteFourBytes(ch, ch1, ch2, ch3);
					return it;
				}
				
				return start;
			}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
		}
		
		bool WriteString(ostream& out, const std::string& str, bool inFlow)
		{
			if(IsValidPlainScalar(str, inFlow)) {
				out << str;
				return true;
			} else
				return WriteDoubleQuotedString(out, str);
		}
		
		bool WriteSingleQuotedString(ostream& out, const std::string& str)
		{
			out << "'";
150
			for(std::size_t i=0;i<str.size();i++) {
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
				char ch = str[i];
				if(!IsPrintable(ch))
					return false;
				
				if(ch == '\'')
					out << "''";
				else
					out << ch;
			}
			out << "'";
			return true;
		}
		
		bool WriteDoubleQuotedString(ostream& out, const std::string& str)
		{
			out << "\"";
Jesse Beder's avatar
Jesse Beder committed
167
168
			for(std::string::const_iterator it=str.begin();it!=str.end();++it) {
				char ch = *it;
169
170
171
172
173
174
175
176
				if(IsPrintable(ch)) {
					if(ch == '\"')
						out << "\\\"";
					else if(ch == '\\')
						out << "\\\\";
					else
						out << ch;
				} else {
Jesse Beder's avatar
Jesse Beder committed
177
					it = WriteNonPrintable(out, it, str.end());
178
179
180
181
182
183
184
185
186
187
				}
			}
			out << "\"";
			return true;
		}

		bool WriteLiteralString(ostream& out, const std::string& str, int indent)
		{
			out << "|\n";
			out << IndentTo(indent);
188
			for(std::size_t i=0;i<str.size();i++) {
189
190
191
192
193
194
195
196
197
198
199
200
				if(str[i] == '\n')
					out << "\n" << IndentTo(indent);
				else
					out << str[i];
			}
			return true;
		}
		
		bool WriteComment(ostream& out, const std::string& str, int postCommentIndent)
		{
			unsigned curIndent = out.col();
			out << "#" << Indentation(postCommentIndent);
201
			for(std::size_t i=0;i<str.size();i++) {
202
203
204
205
206
207
208
209
210
211
212
				if(str[i] == '\n')
					out << "\n" << IndentTo(curIndent) << "#" << Indentation(postCommentIndent);
				else
					out << str[i];
			}
			return true;
		}

		bool WriteAlias(ostream& out, const std::string& str)
		{
			out << "*";
213
			for(std::size_t i=0;i<str.size();i++) {
214
215
216
217
218
219
220
221
222
223
224
				if(!IsPrintable(str[i]) || str[i] == ' ' || str[i] == '\t' || str[i] == '\n' || str[i] == '\r')
					return false;
				
				out << str[i];
			}
			return true;
		}
		
		bool WriteAnchor(ostream& out, const std::string& str)
		{
			out << "&";
225
			for(std::size_t i=0;i<str.size();i++) {
226
227
228
229
230
231
232
233
234
235
				if(!IsPrintable(str[i]) || str[i] == ' ' || str[i] == '\t' || str[i] == '\n' || str[i] == '\r')
					return false;
				
				out << str[i];
			}
			return true;
		}
	}
}