Skip to content

Commit 69c732e

Browse files
committed
use geode utils here
1 parent 791980a commit 69c732e

1 file changed

Lines changed: 3 additions & 44 deletions

File tree

nodes/nodes.cpp

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#include "nodes.hpp"
2-
#include <array>
3-
#include <charconv>
42
#include <fmt/core.h>
53

64
using namespace cocos2d;
@@ -50,11 +48,7 @@ void NumberInputNode::set_value(int value) {
5048
}
5149

5250
int NumberInputNode::get_value() {
53-
try {
54-
return std::stoi(input_node->getString());
55-
} catch (const std::invalid_argument&) {
56-
return -1;
57-
}
51+
return geode::utils::numFromString<int>(input_node->getString()).unwrapOr(-1);
5852
}
5953

6054

@@ -69,46 +63,11 @@ void FloatInputNode::textChanged(CCTextInputNode*) {
6963
}
7064

7165
void FloatInputNode::set_value(float value) {
72-
// TODO: not hardcode this
73-
std::array<char, 10> arr;
74-
#ifndef __cpp_lib_to_chars
75-
// macos std::to_chars does not support floats at all, awesome
76-
const auto str = fmt::format("{}", value);
77-
if (str.size() >= 10) {
78-
input_node->setString("1");
79-
} else {
80-
input_node->setString(str);
81-
}
82-
#else
83-
const auto result = std::to_chars(arr.data(), arr.data() + arr.size(), value, std::chars_format::fixed);
84-
if (result.ec == std::errc::value_too_large) {
85-
// TODO: have this be adjustable
86-
input_node->setString("1");
87-
} else {
88-
*result.ptr = 0;
89-
input_node->setString(arr.data());
90-
}
91-
#endif
66+
input_node->setString(fmt::format("{}", value));
9267
}
9368

9469
std::optional<float> parse_float(const std::string_view str) {
95-
#ifndef __cpp_lib_to_chars
96-
// amazing i know, do this to avoid exceptions
97-
std::stringstream stream;
98-
stream << str;
99-
stream.imbue(std::locale("C"));
100-
float value;
101-
if (!(stream >> value)) {
102-
return {};
103-
}
104-
return value;
105-
#else
106-
float value = 0.f;
107-
const auto result = std::from_chars(str.data(), str.data() + str.size(), value);
108-
if (result.ec == std::errc::invalid_argument)
109-
return {};
110-
return value;
111-
#endif
70+
return geode::utils::numFromString<float>(str).ok();
11271
}
11372

11473
std::optional<float> FloatInputNode::get_value() {

0 commit comments

Comments
 (0)