Skip to content

Commit f15f936

Browse files
committed
Value: Fix string assignment operator
1 parent 9a83fd4 commit f15f936

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/scratch/value.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,7 @@ Value::Value(const std::string &stringValue) :
5353
ValueVariant(stringValue),
5454
m_type(Type::String)
5555
{
56-
if (stringValue.empty())
57-
return;
58-
else if (stringValue == "Infinity") {
59-
m_type = Type::Infinity;
60-
return;
61-
} else if (stringValue == "-Infinity") {
62-
m_type = Type::NegativeInfinity;
63-
return;
64-
} else if (stringValue == "NaN") {
65-
m_type = Type::NaN;
66-
return;
67-
}
68-
69-
bool ok;
70-
float f = stringToDouble(stringValue, &ok);
71-
if (ok) {
72-
*this = f;
73-
m_type = Type::Number;
74-
}
56+
initString(stringValue);
7557
}
7658

7759
/*! Constructs a string Value. */

src/scratch/value.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,15 @@ class LIBSCRATCHCPP_EXPORT Value : public ValueVariant
289289
{
290290
m_type = Type::String;
291291
ValueVariant::operator=(v);
292+
initString(v);
292293
return *this;
293294
}
294295

295296
inline const Value &operator=(const char *v)
296297
{
297298
m_type = Type::String;
298299
ValueVariant::operator=(std::string(v));
300+
initString(v);
299301
return *this;
300302
}
301303

@@ -305,6 +307,31 @@ class LIBSCRATCHCPP_EXPORT Value : public ValueVariant
305307
static double stringToDouble(const std::string &s, bool *ok = nullptr);
306308
static long stringToLong(const std::string &s, bool *ok = nullptr);
307309

310+
inline void initString(const std::string &str)
311+
{
312+
if (str.empty())
313+
return;
314+
else if (str == "Infinity") {
315+
m_type = Type::Infinity;
316+
return;
317+
} else if (str == "-Infinity") {
318+
m_type = Type::NegativeInfinity;
319+
return;
320+
} else if (str == "NaN") {
321+
m_type = Type::NaN;
322+
return;
323+
}
324+
325+
bool ok;
326+
float f = stringToDouble(str, &ok);
327+
if (ok) {
328+
*this = f;
329+
m_type = Type::Number;
330+
}
331+
}
332+
333+
inline void initString(const char *str) { initString(std::string(str)); }
334+
308335
Type m_type;
309336

310337
friend inline bool operator==(const Value &v1, const Value &v2)

0 commit comments

Comments
 (0)