Skip to content

Commit e33c6ef

Browse files
committed
Value: Fix special value conversion
1 parent a11178c commit e33c6ef

File tree

1 file changed

+76
-57
lines changed

1 file changed

+76
-57
lines changed

src/scratch/value.h

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -59,79 +59,98 @@ class LIBSCRATCHCPP_EXPORT Value : public ValueVariant
5959
/*! Returns the long representation of the value. */
6060
inline long toLong() const
6161
{
62-
if (auto p = std::get_if<long>(this))
63-
return *p;
64-
else if (auto p = std::get_if<double>(this))
65-
return *p;
66-
else if (auto p = std::get_if<bool>(this))
67-
return *p;
68-
else if (auto p = std::get_if<std::string>(this))
69-
return stringToLong(*p);
70-
else if (m_type == Type::Infinity)
71-
return std::numeric_limits<long>::infinity();
72-
else if (m_type == Type::NegativeInfinity)
73-
return -std::numeric_limits<long>::infinity();
74-
else
75-
return 0;
62+
if (static_cast<int>(m_type) < 0) {
63+
if (m_type == Type::Infinity)
64+
return std::numeric_limits<long>::infinity();
65+
else if (m_type == Type::NegativeInfinity)
66+
return -std::numeric_limits<long>::infinity();
67+
else
68+
return 0;
69+
} else {
70+
if (auto p = std::get_if<long>(this))
71+
return *p;
72+
else if (auto p = std::get_if<double>(this))
73+
return *p;
74+
else if (auto p = std::get_if<bool>(this))
75+
return *p;
76+
else if (auto p = std::get_if<std::string>(this))
77+
return stringToLong(*p);
78+
else
79+
return 0;
80+
}
7681
}
7782

7883
/*! Returns the double representation of the value. */
7984
inline double toDouble() const
8085
{
81-
if (std::holds_alternative<double>(*this))
82-
return std::get<double>(*this);
83-
else if (std::holds_alternative<long>(*this))
84-
return std::get<long>(*this);
85-
else if (std::holds_alternative<bool>(*this))
86-
return std::get<bool>(*this);
87-
else if (std::holds_alternative<std::string>(*this))
88-
return stringToDouble(std::get<std::string>(*this));
89-
else if (m_type == Type::Infinity)
90-
return std::numeric_limits<double>::infinity();
91-
else if (m_type == Type::NegativeInfinity)
92-
return -std::numeric_limits<double>::infinity();
93-
else
94-
return 0;
86+
if (static_cast<int>(m_type) < 0) {
87+
if (m_type == Type::Infinity)
88+
return std::numeric_limits<double>::infinity();
89+
if (m_type == Type::NegativeInfinity)
90+
return -std::numeric_limits<double>::infinity();
91+
else
92+
return 0;
93+
} else {
94+
if (std::holds_alternative<double>(*this))
95+
return std::get<double>(*this);
96+
else if (std::holds_alternative<long>(*this))
97+
return std::get<long>(*this);
98+
else if (std::holds_alternative<bool>(*this))
99+
return std::get<bool>(*this);
100+
else if (std::holds_alternative<std::string>(*this))
101+
return stringToDouble(std::get<std::string>(*this));
102+
else
103+
return 0;
104+
}
95105
};
96106

97107
/*! Returns the boolean representation of the value. */
98108
inline bool toBool() const
99109
{
100-
if (std::holds_alternative<bool>(*this))
101-
return std::get<bool>(*this);
102-
else if (std::holds_alternative<long>(*this))
103-
return std::get<long>(*this) == 1 ? true : false;
104-
else if (std::holds_alternative<double>(*this))
105-
return std::get<double>(*this) == 1 ? true : false;
106-
else if (std::holds_alternative<std::string>(*this)) {
107-
const std::string &str = std::get<std::string>(*this);
108-
return (stringsEqual(str, "true") || str == "1");
109-
} else
110+
if (static_cast<int>(m_type) < 0) {
110111
return false;
112+
} else {
113+
if (std::holds_alternative<bool>(*this))
114+
return std::get<bool>(*this);
115+
else if (std::holds_alternative<long>(*this))
116+
return std::get<long>(*this) == 1 ? true : false;
117+
else if (std::holds_alternative<double>(*this))
118+
return std::get<double>(*this) == 1 ? true : false;
119+
else if (std::holds_alternative<std::string>(*this)) {
120+
const std::string &str = std::get<std::string>(*this);
121+
return (stringsEqual(str, "true") || str == "1");
122+
} else
123+
return false;
124+
}
111125
};
112126

113127
/*! Returns the string representation of the value. */
114128
inline std::string toString() const
115129
{
116-
if (std::holds_alternative<std::string>(*this))
117-
return std::get<std::string>(*this);
118-
else if (std::holds_alternative<long>(*this))
119-
return std::to_string(std::get<long>(*this));
120-
else if (std::holds_alternative<double>(*this)) {
121-
std::string s = std::to_string(std::get<double>(*this));
122-
s.erase(s.find_last_not_of('0') + 1, std::string::npos);
123-
if (s.back() == '.') {
124-
s.pop_back();
125-
}
126-
return s;
127-
} else if (std::holds_alternative<bool>(*this))
128-
return std::get<bool>(*this) ? "true" : "false";
129-
else if (m_type == Type::Infinity)
130-
return "Infinity";
131-
else if (m_type == Type::NegativeInfinity)
132-
return "-Infinity";
133-
else
134-
return "NaN";
130+
if (static_cast<int>(m_type) < 0) {
131+
if (m_type == Type::Infinity)
132+
return "Infinity";
133+
else if (m_type == Type::NegativeInfinity)
134+
return "-Infinity";
135+
else
136+
return "NaN";
137+
} else {
138+
if (std::holds_alternative<std::string>(*this))
139+
return std::get<std::string>(*this);
140+
else if (std::holds_alternative<long>(*this))
141+
return std::to_string(std::get<long>(*this));
142+
else if (std::holds_alternative<double>(*this)) {
143+
std::string s = std::to_string(std::get<double>(*this));
144+
s.erase(s.find_last_not_of('0') + 1, std::string::npos);
145+
if (s.back() == '.') {
146+
s.pop_back();
147+
}
148+
return s;
149+
} else if (std::holds_alternative<bool>(*this))
150+
return std::get<bool>(*this) ? "true" : "false";
151+
else
152+
return "";
153+
}
135154
};
136155

137156
/*! Returns the UTF-16 representation of the value. */

0 commit comments

Comments
 (0)