Skip to content

Commit 494fe3a

Browse files
committed
Add support for converting numbers with a trailing dot
1 parent cef78ba commit 494fe3a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/scratch/value_functions_p.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,26 @@ extern "C"
320320
}
321321
}
322322

323+
// If there's a trailing dot, copy the string and append zero next to it (e. g. '1.' -> '1.0')
324+
if (end[-1] == '.') {
325+
if (copy) {
326+
char *tmpCopy = new char[end - begin + 2];
327+
memcpy(tmpCopy, begin, end - begin);
328+
delete[] copy;
329+
copy = tmpCopy;
330+
end = copy + (end - begin) + 1;
331+
begin = copy;
332+
} else {
333+
copy = new char[end - begin + 2];
334+
memcpy(copy, begin, end - begin);
335+
end = copy + (end - begin) + 1;
336+
begin = copy;
337+
}
338+
339+
copy[end - begin - 1] = '0';
340+
copy[end - begin] = '\0';
341+
}
342+
323343
// Trim leading zeros
324344
bool trimmed = false;
325345

test/scratch_classes/value_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,13 @@ TEST(ValueTest, ToDouble)
12971297
v = "-.15";
12981298
ASSERT_EQ(v.toDouble(), -0.15);
12991299

1300+
v = "1.";
1301+
ASSERT_EQ(v.toDouble(), 1);
1302+
v = "+1.";
1303+
ASSERT_EQ(v.toDouble(), 1);
1304+
v = "-1.";
1305+
ASSERT_EQ(v.toDouble(), -1);
1306+
13001307
v = "0+5";
13011308
ASSERT_EQ(v.toDouble(), 0);
13021309
v = "0-5";

0 commit comments

Comments
 (0)