Skip to content

Commit 37ff25a

Browse files
inumanagarshajii
andauthored
Fix underscore float parsing (#596)
* Fix underscore float parsing * Add tests * Update float parsing --------- Co-authored-by: A. R. Shajii <ars@ars.me>
1 parent d45646d commit 37ff25a

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

codon/parser/ast/expr.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "expr.h"
44

5+
#include <algorithm>
6+
#include <iterator>
57
#include <memory>
68
#include <string>
79
#include <vector>
@@ -149,9 +151,14 @@ FloatExpr::FloatExpr(double floatValue)
149151
this->floatValue = std::make_unique<double>(floatValue);
150152
}
151153
FloatExpr::FloatExpr(const std::string &value, std::string suffix)
152-
: Expr(), value(value), suffix(std::move(suffix)) {
154+
: Expr(), value(), suffix(std::move(suffix)) {
155+
this->value.reserve(value.size());
156+
std::copy_if(value.begin(), value.end(), std::back_inserter(this->value),
157+
[](char c) { return c != '_'; });
158+
153159
double result;
154-
auto r = fast_float::from_chars(value.data(), value.data() + value.size(), result);
160+
auto r = fast_float::from_chars(this->value.data(),
161+
this->value.data() + this->value.size(), result);
155162
if (r.ec == std::errc() || r.ec == std::errc::result_out_of_range)
156163
floatValue = std::make_unique<double>(result);
157164
else

test/parser/simplify_expr.codon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ print 1844674407_3709551999 #! integer '18446744073709551999' cannot fit into 64
3939
print 5.15 #: 5.15
4040
print 2e2 #: 200
4141
print 2.e-2 #: 0.02
42+
print 1_000.0 #: 1000
43+
print 1_000e9 #: 1e+12
4244

4345
#%% float_suffix,barebones
4446
@extend

0 commit comments

Comments
 (0)