Helper functions for conversions that are locale independent
#233 opened on Jul 26, 2021
Repository metrics
- Stars
- (28 stars)
- PR merge metrics
- (PR metrics pending)
Description
Desired behavior
Different software in the Ignition family uses all kind of C/C++ functions to convert between types that are locale dependent and make functionality to fail under non C locales. Examples are:
- https://github.com/ignitionrobotics/ign-rendering/issues/136
- https://github.com/ignitionrobotics/ign-common/pull/120
- https://github.com/ignitionrobotics/ign-math/issues/215
Trying to complete the list (split due to limitations in github search) based on https://www.gnu.org/software/libc/manual/html_node/Parsing-of-Numbers.html:
- https://github.com/search?q=org%3Aignitionrobotics+in%3Afile+strtol+OR+wcstol+OR+strtoul+OR+wcstoul+OR+strtoll+OR+wcstoll&type=Code
- https://github.com/search?q=org%3Aignitionrobotics+in%3Afile+strtoq+OR+wcstoq+OR+strtoull+OR+wcstoull+OR+strtouq+OR+wcstouq&type=Code
- https://github.com/search?q=org%3Aignitionrobotics+in%3Afile+strtoimax+OR+wcstoimax+OR+strtoumax+OR+wcstoumax+OR+atol+OR+atoi&type=Code
- https://github.com/search?q=org%3Aignitionrobotics+in%3Afile+strtod+OR+strtof+OR+strtold+OR+strtofN+OR+strtofNx+OR+wcstod&type=Code
- https://github.com/search?q=org%3Aignitionrobotics+in%3Afile+wcstof+OR+wcstold+OR+wcstofN+OR+wcstofNx+OR+atof+OR+atoll&type=Code
Alternatives considered
There is a native function in C++17 https://en.cppreference.com/w/cpp/utility/to_chars which is not available in all supported platforms at this time. The boost::lexical_cast could be an option but would be ideal not to depend on boost if possible.
Implementation suggestion
I think that the best approach would be to implement helper functions that encapsulate the implementation in this ign-common repository. The implementation can rely on to_chars if C++17 is available or use other workarounds (see https://github.com/ros/urdfdom_headers/pull/42).
A similar work was done for sdformat years ago. Following the locale_fix test can lead to details.