Skip to content

Commit de2ddb6

Browse files
committed
lexer: Start using std::from_chars
1 parent 553cdd4 commit de2ddb6

4 files changed

Lines changed: 14 additions & 13 deletions

File tree

lib/source/pl/core/ast/ast_node_attribute.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <pl/patterns/pattern_bitfield.hpp>
1111

1212
#include <bit>
13+
#include <wolv/utils/charconv.hpp>
1314

1415
namespace pl::core::ast {
1516

@@ -348,7 +349,7 @@ namespace pl::core::ast {
348349
if (!pattern->hasOverriddenColor()) {
349350
if (const auto &arguments = attributable->getAttributeArguments("color"); arguments.size() == 1) {
350351
auto colorString = getAttributeValueAsString(arguments.front(), evaluator);
351-
u32 color = strtoul(colorString.c_str(), nullptr, 16);
352+
u32 color = wolv::util::from_chars<u32>(colorString, 16).value_or(0);
352353
pattern->setColor(hlp::changeEndianess(color, std::endian::big) >> 8);
353354
} else if (auto singleColor = attributable->hasAttribute("single_color", false); singleColor) {
354355
pattern->setColor(pattern->getColor());
@@ -406,7 +407,7 @@ namespace pl::core::ast {
406407

407408
if (const auto &arguments = attributable->getAttributeArguments("color"); arguments.size() == 1) {
408409
auto colorString = getAttributeValueAsString(arguments.front(), evaluator);
409-
u32 color = strtoul(colorString.c_str(), nullptr, 16);
410+
u32 color = wolv::util::from_chars<u32>(colorString, 16).value_or(0);
410411
pattern->setColor(hlp::changeEndianess(color, std::endian::big) >> 8);
411412
} else if (auto singleColor = attributable->hasAttribute("single_color", false); singleColor) {
412413
pattern->setColor(pattern->getColor());

lib/source/pl/core/lexer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <pl/api.hpp>
55

66
#include <optional>
7+
#include <wolv/utils/charconv.hpp>
78

89
namespace pl::core {
910
using namespace tkn;
@@ -243,10 +244,9 @@ namespace pl::core {
243244
}
244245

245246
std::optional<double> Lexer::parseFloatingPoint(std::string_view literal, const char suffix) {
246-
char *end = nullptr;
247-
double val = std::strtod(literal.data(), &end);
247+
const auto value = wolv::util::from_chars<double>(literal);
248248

249-
if(end != literal.data() + literal.size()) {
249+
if(!value.has_value()) {
250250
m_errorLength = literal.size();
251251
error("Invalid float literal: {}", literal);
252252
return std::nullopt;
@@ -255,11 +255,11 @@ namespace pl::core {
255255
switch (suffix) {
256256
case 'f':
257257
case 'F':
258-
return float(val);
258+
return float(*value);
259259
case 'd':
260260
case 'D':
261261
default:
262-
return val;
262+
return *value;
263263
}
264264
}
265265

@@ -684,4 +684,4 @@ namespace pl::core {
684684
}
685685
return Location { m_source, m_line, column, m_errorLength };
686686
}
687-
}
687+
}

lib/source/pl/lib/std/string.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <fmt/args.h>
1313

14+
#include <wolv/utils/charconv.hpp>
15+
1416
namespace pl::lib::libstd::string {
1517

1618
void registerFunctions(pl::PatternLanguage &runtime) {
@@ -63,16 +65,14 @@ namespace pl::lib::libstd::string {
6365
auto string = params[0].toString(false);
6466
auto base = u64(params[1].toUnsigned());
6567

66-
// TODO: support 128-bit integers
67-
68-
return i128(std::strtoll(string.c_str(), nullptr, base));
68+
return wolv::util::from_chars<i128>(string, base).value_or(0);
6969
});
7070

7171
/* parse_float(string) */
7272
runtime.addFunction(nsStdString, "parse_float", FunctionParameterCount::exactly(1), [](Evaluator *, auto params) -> std::optional<Token::Literal> {
7373
auto string = params[0].toString(false);
7474

75-
return double(std::strtod(string.c_str(), nullptr));
75+
return wolv::util::from_chars<double>(string).value_or(0.0);
7676
});
7777
}
7878

0 commit comments

Comments
 (0)