pugixml
pugixml copied to clipboard
parsing float is local dependant
In France, decimal separator is "," and not "."
strtod used in pugixml.cpp uses strtod which is local dependant
possible alternative:
- use own double parsing code like https://github.com/michael-hartmann/parsefloat
- use std::from_chars is c++17 is detected
I think you can also define the locale in your code before calling pugixml, something like this to use . point decimal:
if (std::setlocale(LC_ALL, "C") == NULL) {
std::cerr << "Cannot set locale to C" << std::endl;
}
See: https://en.cppreference.com/w/cpp/locale/setlocale
is std::setlocale always tread safe?
The canonical recommendation is to use setlocale at the beginning of main, which automatically makes both parsing and converting to strings locale-independent. Unfortunately std::to_chars specifically doesn't enjoy wide support, so I don't think we should solve this problem with C++17, especially since this means changing behavior in a pretty significant way between standard versions. I'm not sure if there's a way to achieve the same result without calling setlocale in C.
Thank you for answering
I suggest using code like https://github.com/michael-hartmann/parsefloat (which can be converted to wide), which not use C or C++ standard library.
pugixml can be used on a multithreaded library or code which did not want change locale.