pugixml icon indicating copy to clipboard operation
pugixml copied to clipboard

parsing float is local dependant

Open gvollant opened this issue 3 years ago • 5 comments
trafficstars

In France, decimal separator is "," and not "."

strtod used in pugixml.cpp uses strtod which is local dependant

possible alternative:

  1. use own double parsing code like https://github.com/michael-hartmann/parsefloat
  2. use std::from_chars is c++17 is detected

gvollant avatar Jan 22 '22 12:01 gvollant

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

s-trinh avatar Jan 22 '22 13:01 s-trinh

is std::setlocale always tread safe?

gvollant avatar Jan 22 '22 14:01 gvollant

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.

zeux avatar Feb 05 '22 16:02 zeux

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.

gvollant avatar Feb 05 '22 19:02 gvollant