-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use stringstream instead of stod to work around locale issues. #42
Changes from 3 commits
739624e
678ab93
c5f6b9f
caf2e8c
567ba15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,28 @@ void split_string(std::vector<std::string> &result, | |
} | ||
} | ||
|
||
// This is a locale-safe version of string-to-double, which is suprisingly | ||
// difficult to do correctly. This function ensures that the C locale is used | ||
// for parsing, as that matches up with what the XSD for double specifies. | ||
// On success, the double is returned; on failure, a std::runtime_error is | ||
// thrown. | ||
static inline double strToDouble(const char *in) | ||
{ | ||
std::stringstream ss; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
ss.imbue(std::locale::classic()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
ss << in; | ||
|
||
double out; | ||
ss >> out; | ||
|
||
if (ss.fail() || !ss.eof()) { | ||
throw std::runtime_error(""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
return out; | ||
} | ||
|
||
} | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use
const char *
instead ofconst std::string &
? Just curious, not asking you to change it, just hoping to learn something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a good (but sneaky) reason for it. I'm hoping that once we get this basic support in, we can use this more generally throughout the urdfdom libraries to avoid this problem. As one example, https://github.com/ros/urdfdom/blob/master/urdf_parser/src/link.cpp#L120 probably suffers from a similar problem (as do several other std::stod conversions in there). Several of those get a
const char *
from tinyxml, so I figured this interface was more generic rather than having to wrap those inside strings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right, I forgot I also did another PR to actually fix all of that as well: ros/urdfdom#105