Skip to content

Commit 4f15d1d

Browse files
authored
Merge pull request #177 from scratchcpp/fix_string_to_long
Fix string (with space separated numbers) to long conversion
2 parents 3e7ca1f + b84ea52 commit 4f15d1d

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

include/scratchcpp/value.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,14 @@ class LIBSCRATCHCPP_EXPORT Value : public ValueVariant
660660
return 0;
661661
}
662662

663+
static const std::string digits = "0123456789+-";
664+
665+
for (char c : s) {
666+
if (digits.find(c) == std::string::npos) {
667+
return 0;
668+
}
669+
}
670+
663671
try {
664672
if (ok)
665673
*ok = true;

test/scratch_classes/value_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,30 @@ TEST(ValueTest, StdStringConstructor)
123123
ASSERT_TRUE(v.isString());
124124
}
125125

126+
{
127+
Value v(std::string("532"));
128+
ASSERT_EQ(v.toString(), "532");
129+
ASSERT_EQ(v.type(), Value::Type::Number);
130+
ASSERT_FALSE(v.isInfinity());
131+
ASSERT_FALSE(v.isNegativeInfinity());
132+
ASSERT_FALSE(v.isNaN());
133+
ASSERT_TRUE(v.isNumber());
134+
ASSERT_FALSE(v.isBool());
135+
ASSERT_FALSE(v.isString());
136+
}
137+
138+
{
139+
Value v(std::string("1 2 3"));
140+
ASSERT_EQ(v.toString(), "1 2 3");
141+
ASSERT_EQ(v.type(), Value::Type::String);
142+
ASSERT_FALSE(v.isInfinity());
143+
ASSERT_FALSE(v.isNegativeInfinity());
144+
ASSERT_FALSE(v.isNaN());
145+
ASSERT_FALSE(v.isNumber());
146+
ASSERT_FALSE(v.isBool());
147+
ASSERT_TRUE(v.isString());
148+
}
149+
126150
{
127151
Value v(std::string("Infinity"));
128152
ASSERT_EQ(v.toString(), "Infinity");

0 commit comments

Comments
 (0)