-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
739624e
Use stringstream instead of stod to work around locale issues.
clalancette 678ab93
Use imbue to always use the "classic" locale.
clalancette c5f6b9f
Make a strToDouble facility in the utils.h header.
clalancette caf2e8c
Add some additional includes.
clalancette 567ba15
Add in an actual error message to the strToDouble throw.
clalancette File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,9 @@ | |
#ifndef URDF_INTERFACE_UTILS_H | ||
#define URDF_INTERFACE_UTILS_H | ||
|
||
#include <locale> | ||
#include <sstream> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
|
||
|
@@ -62,6 +65,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("Failed converting string to double"); | ||
} | ||
|
||
return out; | ||
} | ||
|
||
} | ||
|
||
#endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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