-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathtagresolver.cpp
More file actions
187 lines (158 loc) · 4.28 KB
/
tagresolver.cpp
File metadata and controls
187 lines (158 loc) · 4.28 KB
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
#include "../include/yaml-cpp/tagresolver.h"
#include <vector>
#include <algorithm>
namespace YAML {
bool CoreTagResolver::IsBase8Int(const string & text)
{
if (text.empty())
return false;
for (string::size_type i = 0; i < text.size(); ++i)
{
if (!IsBase8Digit(text[i]))
return false;
}
return true;
}
bool CoreTagResolver::IsBase16Int(const string & text)
{
if (text.empty())
return false;
for (string::size_type i = 0; i < text.size(); ++i)
{
if (!IsBase16Digit(text[i]))
return false;
}
return true;
}
bool CoreTagResolver::isBase10Int(const string & text)
{
using namespace std;
if (text.empty())
return false;
string::size_type pos = 0;
if ( text[pos] == '-' ||
text[pos] == '+' )
pos += 1;
if (pos == text.size())
return false;
for (; pos < text.size(); ++pos)
{
if (!IsBase10Digit(text[pos]))
return false;
}
return true;
}
bool CoreTagResolver::ScalarIsInt(const string & text)
{
if (text.substr(0,2) == "0o")
{
return IsBase8Int(text.substr(2));
}
else if (text.substr(0,2) == "0x")
{
return IsBase16Int(text.substr(2));
}
else
{
return isBase10Int(text);
}
}
bool CoreTagResolver::ScalarIsFloat(const string & text)
{
static std::vector<string> inf = { ".inf", ".Inf", ".INF" };
static std::vector<string> nan = { ".nan", ".NaN", ".NAN" };
if (text.empty())
return false;
if (std::find(nan.begin(), nan.end(), text) != nan.end())
return true;
string::size_type pos = 0;
if ( text[0] == '-' ||
text[0] == '+' )
{
pos += 1;
}
if (pos == text.size())
return false;
if (std::find(inf.begin(), inf.end(), text.substr(pos)) != inf.end())
return true;
string::size_type intStart = pos;
while(pos < text.size() && IsBase10Digit(text[pos]))
{
++pos;
}
bool hasInt = pos > intStart;
if (pos == text.size())
return false;
if (text[pos] != '.')
return false;
++pos;
string::size_type fracStart = pos;
while(pos < text.size() && IsBase10Digit(text[pos]))
{
++pos;
}
bool hasFrac = pos > fracStart;
if (!hasInt && !hasFrac)
return false;
if (pos == text.size())
return true;
if (text[pos] != 'e' && text[pos] != 'E')
return false;
++pos;
if ( text[pos] == '-' || text[pos] == '+' )
++pos;
string::size_type expStart = pos;
while(pos < text.size() && IsBase10Digit(text[pos]))
{
++pos;
}
if (pos == expStart)
return false;
if (pos != text.size())
return false;
return true;
}
void CoreTagResolver::OnScalar(const Mark& mark, const std::string& tag,
anchor_t anchor, const std::string& value)
{
if (tag == "!")
{
TagResolver::OnScalar(mark, "tag:yaml.org,2002:str", anchor, value);
}
else if (tag == "?")
{
string resolved_tag;
if (ScalarIsNull(value))
resolved_tag = "tag:yaml.org,2002:null";
else if (ScalarIsBool(value))
resolved_tag = "tag:yaml.org,2002:bool";
else if (ScalarIsInt(value))
resolved_tag = "tag:yaml.org,2002:int";
else if (ScalarIsFloat(value))
resolved_tag = "tag:yaml.org,2002:float";
else
resolved_tag = "tag:yaml.org,2002:str";
TagResolver::OnScalar(mark, resolved_tag, anchor, value);
}
else
{
TagResolver::OnScalar(mark, tag, anchor, value);
}
}
void CoreTagResolver::OnSequenceStart(const Mark& mark, const std::string& tag,
anchor_t anchor, EmitterStyle::value style)
{
if (TagIsNonSpecific(tag))
TagResolver::OnSequenceStart(mark, "tag:yaml.org,2002:seq", anchor, style);
else
TagResolver::OnSequenceStart(mark, tag, anchor, style);
}
void CoreTagResolver::OnMapStart(const Mark& mark, const std::string& tag,
anchor_t anchor, EmitterStyle::value style)
{
if (TagIsNonSpecific(tag))
TagResolver::OnMapStart(mark, "tag:yaml.org,2002:map", anchor, style);
else
TagResolver::OnMapStart(mark, tag, anchor, style);
}
}