Skip to content

Commit f9f0501

Browse files
committed
fix: update int format
1 parent 23a507b commit f9f0501

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/mm_plugin.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,18 @@ namespace mm {
119119
}
120120

121121
ptrdiff_t FormatInt(const std::string& str) {
122-
try {
123-
size_t pos;
124-
ptrdiff_t result = std::stoul(str, &pos);
125-
if (pos != str.length()) {
126-
throw std::invalid_argument("Trailing characters after the valid part");
127-
}
128-
return result;
129-
} catch (const std::invalid_argument& e) {
130-
CONPRINTE(std::format("Invalid argument: {}\n", e.what()).c_str());
131-
} catch (const std::out_of_range& e) {
132-
CONPRINTE(std::format("Out of range: {}\n", e.what()).c_str());
133-
} catch (const std::exception& e) {
134-
CONPRINTE(std::format("Conversion error: {}\n", e.what()).c_str());
122+
ptrdiff_t result;
123+
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
124+
125+
if (ec != std::errc{}) {
126+
CONPRINTE(std::format("Error: {}", std::make_error_code(ec).message()).c_str());
127+
return -1;
128+
} else if (ptr != str.data() + str.size()) {
129+
CONPRINTE("Invalid argument: trailing characters after the valid part");
130+
return -1;
135131
}
136132

137-
return -1;
133+
return result;
138134
}
139135

140136
CON_COMMAND_F(plugify, "Plugify control options", FCVAR_NONE) {

0 commit comments

Comments
 (0)