33#include " value.h"
44#include < algorithm>
55#include < limits>
6- #include < utf8.h>
76
87using namespace libscratchcpp ;
98
109/* ! Constructs a number Value. */
1110Value::Value (float numberValue) :
12- m_value (numberValue),
11+ ValueVariant (numberValue),
1312 m_type(Type::Number)
1413{
1514}
1615
1716/* ! Constructs a number Value. */
1817Value::Value (double numberValue) :
19- m_value (numberValue),
18+ ValueVariant (numberValue),
2019 m_type(Type::Number)
2120{
2221}
2322
2423/* ! Constructs a number Value. */
2524Value::Value (int numberValue) :
26- m_value (numberValue),
25+ ValueVariant (numberValue),
2726 m_type(Type::Number)
2827{
2928}
3029
3130/* ! Constructs a number Value. */
3231Value::Value (size_t numberValue) :
33- m_value (static_cast <long >(numberValue)),
32+ ValueVariant (static_cast <long >(numberValue)),
3433 m_type(Type::Number)
3534{
3635}
3736
3837/* ! Constructs a number Value. */
3938Value::Value (long numberValue) :
40- m_value (numberValue),
39+ ValueVariant (numberValue),
4140 m_type(Type::Number)
4241{
4342}
4443
4544/* ! Constructs a boolean Value. */
4645Value::Value (bool boolValue) :
47- m_value (boolValue),
46+ ValueVariant (boolValue),
4847 m_type(Type::Bool)
4948{
5049}
5150
5251/* ! Constructs a string Value. */
5352Value::Value (const std::string &stringValue) :
54- m_value (stringValue),
53+ ValueVariant (stringValue),
5554 m_type(Type::String)
5655{
5756 if (stringValue.empty ())
5857 return ;
5958 else if (stringValue == " Infinity" ) {
60- m_isInfinity = true ;
61- m_type = Type::Special;
59+ m_type = Type::Infinity;
6260 return ;
6361 } else if (stringValue == " -Infinity" ) {
64- m_isNegativeInfinity = true ;
65- m_type = Type::Special;
62+ m_type = Type::NegativeInfinity;
6663 return ;
6764 } else if (stringValue == " NaN" ) {
68- m_isNaN = true ;
69- m_type = Type::Special;
65+ m_type = Type::NaN;
7066 return ;
7167 }
7268
7369 bool ok;
7470 float f = stringToDouble (stringValue, &ok);
7571 if (ok) {
76- m_value = f;
72+ * this = f;
7773 m_type = Type::Number;
7874 }
7975}
@@ -86,18 +82,16 @@ Value::Value(const char *stringValue) :
8682
8783/* ! Constructs a special Value. */
8884Value::Value (SpecialValue specialValue) :
89- m_type(Type::Special )
85+ ValueVariant( 0 )
9086{
9187 if (specialValue == SpecialValue::Infinity)
92- m_isInfinity = true ;
88+ m_type = Type::Infinity ;
9389 else if (specialValue == SpecialValue::NegativeInfinity)
94- m_isNegativeInfinity = true ;
90+ m_type = Type::NegativeInfinity ;
9591 else if (specialValue == SpecialValue::NaN)
96- m_isNaN = true ;
97- else {
92+ m_type = Type::NaN ;
93+ else
9894 m_type = Type::Number;
99- m_value = 0 ;
100- }
10195}
10296
10397/* ! Returns the type of the value. */
@@ -109,19 +103,19 @@ Value::Type Value::type() const
109103/* ! Returns true if the value is infinity. */
110104bool Value::isInfinity () const
111105{
112- return m_isInfinity ;
106+ return m_type == Type::Infinity ;
113107}
114108
115109/* ! Returns true if the value is negative infinity. */
116110bool Value::isNegativeInfinity () const
117111{
118- return m_isNegativeInfinity ;
112+ return m_type == Type::NegativeInfinity ;
119113}
120114
121115/* ! Returns true if the value is NaN (Not a Number). */
122116bool Value::isNaN () const
123117{
124- return m_isNaN ;
118+ return m_type == Type::NaN ;
125119}
126120
127121/* ! Returns true if the value is a number. */
@@ -142,113 +136,6 @@ bool Value::isString() const
142136 return m_type == Type::String;
143137}
144138
145- /* ! Returns the int representation of the value. */
146- int Value::toInt () const
147- {
148- return toLong ();
149- }
150-
151- /* ! Returns the long representation of the value. */
152- inline long Value::toLong () const
153- {
154- if (std::holds_alternative<long >(m_value))
155- return std::get<long >(m_value);
156- else if (std::holds_alternative<double >(m_value))
157- return std::get<double >(m_value);
158- else if (std::holds_alternative<bool >(m_value))
159- return std::get<bool >(m_value);
160- else if (std::holds_alternative<std::string>(m_value))
161- return stringToLong (std::get<std::string>(m_value));
162- else if (m_isInfinity)
163- return std::numeric_limits<long >::infinity ();
164- else if (m_isNegativeInfinity)
165- return -std::numeric_limits<long >::infinity ();
166- else
167- return 0 ;
168- }
169-
170- /* ! Returns the double representation of the value. */
171- double Value::toDouble () const
172- {
173- if (std::holds_alternative<double >(m_value))
174- return std::get<double >(m_value);
175- else if (std::holds_alternative<long >(m_value))
176- return std::get<long >(m_value);
177- else if (std::holds_alternative<bool >(m_value))
178- return std::get<bool >(m_value);
179- else if (std::holds_alternative<std::string>(m_value))
180- return stringToDouble (std::get<std::string>(m_value));
181- else if (m_isInfinity)
182- return std::numeric_limits<double >::infinity ();
183- else if (m_isNegativeInfinity)
184- return -std::numeric_limits<double >::infinity ();
185- else
186- return 0 ;
187- }
188-
189- /* ! Returns the boolean representation of the value. */
190- bool Value::toBool () const
191- {
192- if (std::holds_alternative<bool >(m_value))
193- return std::get<bool >(m_value);
194- else if (std::holds_alternative<long >(m_value))
195- return std::get<long >(m_value) == 1 ? true : false ;
196- else if (std::holds_alternative<double >(m_value))
197- return std::get<double >(m_value) == 1 ? true : false ;
198- else if (std::holds_alternative<std::string>(m_value)) {
199- const std::string &str = std::get<std::string>(m_value);
200- return (stringsEqual (str, " true" ) || str == " 1" );
201- } else
202- return false ;
203- }
204-
205- /* ! Returns the string representation of the value. */
206- std::string Value::toString () const
207- {
208- if (std::holds_alternative<std::string>(m_value))
209- return std::get<std::string>(m_value);
210- else if (std::holds_alternative<long >(m_value))
211- return std::to_string (std::get<long >(m_value));
212- else if (std::holds_alternative<double >(m_value)) {
213- std::string s = std::to_string (std::get<double >(m_value));
214- s.erase (s.find_last_not_of (' 0' ) + 1 , std::string::npos);
215- if (s.back () == ' .' ) {
216- s.pop_back ();
217- }
218- return s;
219- } else if (std::holds_alternative<bool >(m_value))
220- return std::get<bool >(m_value) ? " true" : " false" ;
221- else if (m_isInfinity)
222- return " Infinity" ;
223- else if (m_isNegativeInfinity)
224- return " -Infinity" ;
225- else
226- return " NaN" ;
227- }
228-
229- /* ! Returns the UTF-16 representation of the value. */
230- std::u16string Value::toUtf16 () const
231- {
232- return utf8::utf8to16 (toString ());
233- }
234-
235- /* ! Adds the given value to the value. */
236- void Value::add (const Value &v)
237- {
238- if ((m_isInfinity && v.m_isNegativeInfinity ) || (m_isNegativeInfinity && v.m_isInfinity )) {
239- m_isInfinity = false ;
240- m_isNegativeInfinity = false ;
241- m_isNaN = true ;
242- } else if (m_isInfinity || v.m_isInfinity ) {
243- m_type = Type::Special;
244- m_isInfinity = true ;
245- } else if (m_isNegativeInfinity || v.m_isNegativeInfinity ) {
246- m_type = Type::Special;
247- m_isNegativeInfinity = true ;
248- }
249- m_value = toDouble () + v.toDouble ();
250- }
251-
252139bool Value::stringsEqual (std::u16string s1, std::u16string s2)
253140{
254141 std::transform (s1.begin (), s1.end (), s1.begin (), ::tolower);
0 commit comments