locale independent util::parse_double
This commit is contained in:
+2
-15
@@ -4,19 +4,6 @@
|
||||
#include <stdexcept>
|
||||
#include <math.h>
|
||||
|
||||
inline int char2int(int c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
if (c >= 'a' && c <= 'f') {
|
||||
return 10 + c - 'a';
|
||||
}
|
||||
if (c >= 'A' && c <= 'F') {
|
||||
return 10 + c - 'A';
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
inline double power(double base, int64_t power) {
|
||||
double result = 1.0;
|
||||
for (int64_t i = 0; i < power; i++) {
|
||||
@@ -216,7 +203,7 @@ std::string BasicParser::parseName() {
|
||||
|
||||
int64_t BasicParser::parseSimpleInt(int base) {
|
||||
char c = peek();
|
||||
int index = char2int(c);
|
||||
int index = hexchar2int(c);
|
||||
if (index == -1 || index >= base) {
|
||||
throw error("invalid number literal");
|
||||
}
|
||||
@@ -227,7 +214,7 @@ int64_t BasicParser::parseSimpleInt(int base) {
|
||||
while (c == '_') {
|
||||
c = source[++pos];
|
||||
}
|
||||
index = char2int(c);
|
||||
index = hexchar2int(c);
|
||||
if (index == -1 || index >= base) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -41,6 +41,19 @@ inline bool is_identifier_part(int c) {
|
||||
return is_identifier_start(c) || is_digit(c);
|
||||
}
|
||||
|
||||
inline int hexchar2int(int c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
if (c >= 'a' && c <= 'f') {
|
||||
return 10 + c - 'a';
|
||||
}
|
||||
if (c >= 'A' && c <= 'F') {
|
||||
return 10 + c - 'A';
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
extern std::string escape_string(std::string s);
|
||||
|
||||
class parsing_error : public std::runtime_error {
|
||||
|
||||
+2
-7
@@ -25,12 +25,7 @@ int64_t Attribute::asInt() const {
|
||||
}
|
||||
|
||||
double Attribute::asFloat() const {
|
||||
double value;
|
||||
auto res = std::from_chars(text.data(), text.data()+text.size(), value);
|
||||
if (res.ptr != text.data()+text.size()) {
|
||||
throw std::runtime_error("invalid number format "+escape_string(text));
|
||||
}
|
||||
return value;
|
||||
return util::parse_double(text);
|
||||
}
|
||||
|
||||
bool Attribute::asBool() const {
|
||||
@@ -45,7 +40,7 @@ void Node::add(xmlelement element) {
|
||||
}
|
||||
|
||||
void Node::set(std::string name, std::string text) {
|
||||
attrs.insert_or_assign(name, Attribute(name, text));
|
||||
attrs[name] = Attribute(name, text);
|
||||
}
|
||||
|
||||
const std::string& Node::getTag() const {
|
||||
|
||||
@@ -22,6 +22,7 @@ namespace xml {
|
||||
std::string name;
|
||||
std::string text;
|
||||
public:
|
||||
Attribute() {};
|
||||
Attribute(std::string name, std::string text);
|
||||
|
||||
const std::string& getName() const;
|
||||
|
||||
Reference in New Issue
Block a user