J
Juha Nieminen
I have made a program which parses strings in certain specified
format. This string format can contain numerical values in the format
understood by std::strtod().
The problem is: How std::strtod() interprets the string depends on
locale. This messes things up. For example, if the string to interpret
is "f(1,2)", in other words, a function call with two parameters, it
will work fine if the locale says that the decimal separator is a '.'
(as for example in the English locale), but it will mess up if the
locale says that the decimal separator is a ',' (as for example in the
Finnish locale). In the latter case std::strtod() will parse the entire
"1,2" as the number (resulting in the value 1.2), after which my parser
sees that only one parameter was given to the function even though the
function requires two, giving thus a syntax error.
Of course it also messes up things like "f(1.2,3.4)" in the latter
case, as std::strtod() will stop parsing at the '.' so my parser will
see that a '.' immediately follows a numerical value, which is against
syntax, and thus a syntax error once again happens.
(The string is actually parsed correctly in the latter case if you
write "f(1,2 , 3,4)", but it's a nuisance, and a nightmare to document!)
Is there any way to make std::strtod() ignore locale and always parse
the string in the same way?
format. This string format can contain numerical values in the format
understood by std::strtod().
The problem is: How std::strtod() interprets the string depends on
locale. This messes things up. For example, if the string to interpret
is "f(1,2)", in other words, a function call with two parameters, it
will work fine if the locale says that the decimal separator is a '.'
(as for example in the English locale), but it will mess up if the
locale says that the decimal separator is a ',' (as for example in the
Finnish locale). In the latter case std::strtod() will parse the entire
"1,2" as the number (resulting in the value 1.2), after which my parser
sees that only one parameter was given to the function even though the
function requires two, giving thus a syntax error.
Of course it also messes up things like "f(1.2,3.4)" in the latter
case, as std::strtod() will stop parsing at the '.' so my parser will
see that a '.' immediately follows a numerical value, which is against
syntax, and thus a syntax error once again happens.
(The string is actually parsed correctly in the latter case if you
write "f(1,2 , 3,4)", but it's a nuisance, and a nightmare to document!)
Is there any way to make std::strtod() ignore locale and always parse
the string in the same way?