mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
Перевел parse_double на from_chars
This commit is contained in:
parent
db53076c62
commit
7f408c11de
2 changed files with 32 additions and 17 deletions
|
|
@ -1,9 +1,11 @@
|
|||
#include "stringutil.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <locale>
|
||||
#include "stringutil.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <locale>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
|
|
@ -509,17 +511,22 @@ int util::replaceAll(
|
|||
return replace_all(str, from, to);
|
||||
}
|
||||
|
||||
// replace it with std::from_chars in the far far future
|
||||
double util::parse_double(const std::string& str) {
|
||||
std::istringstream ss(str);
|
||||
ss.imbue(std::locale("C"));
|
||||
double d;
|
||||
ss >> d;
|
||||
if (ss.fail()) {
|
||||
throw std::runtime_error("invalid number format");
|
||||
}
|
||||
return d;
|
||||
}
|
||||
double util::parse_double(const std::string& str) {
|
||||
const char* begin = str.data();
|
||||
const char* end = begin + str.size();
|
||||
while (begin != end && std::isspace(static_cast<unsigned char>(*begin))) {
|
||||
begin++;
|
||||
}
|
||||
if (begin != end && *begin == '+') {
|
||||
begin++;
|
||||
}
|
||||
double d = 0.0;
|
||||
const auto result = std::from_chars(begin, end, d);
|
||||
if (result.ec != std::errc()) {
|
||||
throw std::runtime_error("invalid number format");
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
double util::parse_double(const std::string& str, size_t offset, size_t len) {
|
||||
return parse_double(str.substr(offset, len));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue