Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Avoiding default atof behavior for integers and floats #373

Merged
merged 11 commits into from
Mar 14, 2018

Conversation

shantanuchhabra
Copy link
Collaborator

Redirected from unity_sarray to go to flex_int and flex_float and added logic in flexible_type_detail to deal with invalid strings like "2hello", "INFiltrate", etc. Built and tested locally.

Copy link
Contributor

@znation znation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment; otherwise looks great!

long long int converted = std::strtoll(t.c_str(), &end, 10);
if (*end != '\0') {
// was not at end of element so throw error
throw std::runtime_error("Invalid conversion: String contains more characters than just integer");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put the value into the error message, so the user has context on why it failed:

"Invalid conversion: \"" + t + "\" cannot be interpreted as an integer."

Same for the float conversion below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great idea, on it!

@TobyRoseman
Copy link
Collaborator

@shantanuchhabra - a unit test would be great. I would recommend creating a simple unit test. Verify that it fails without your change. Then verify that it passes with your change.

@shantanuchhabra
Copy link
Collaborator Author

Changed the error message and also added a unit test!

long long int converted = std::strtoll(t.c_str(), &end, 10);
if (*end != '\0') {
// was not at end of element so throw error
throw std::runtime_error("Invalid conversion: " + t + "cannot be interpreted as an integer");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably want a space before "cannot"

double converted = std::strtod(t.c_str(), &end);
if (*end != '\0') {
// was not at end of element so throw error
throw std::runtime_error("Invalid conversion: " + t + "cannot be interpreted as a float");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as previous comment

… unity_sarray and hence gets silenced by the error message in unity_sarray on line 1336/1340?
@ylow
Copy link
Collaborator

ylow commented Mar 14, 2018

Hi, sorry. My review came late. Can we revert?

shantanuchhabra added a commit that referenced this pull request Mar 14, 2018
inline FLEX_ALWAYS_INLINE_FLATTEN flex_int operator()(const flex_string& t) const {
char *end;
long long int converted = std::strtoll(t.c_str(), &end, 10);
if (*end != '\0') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rather this not use (*end) == '\0' as a comparison. The null character could occur anywhere.
Use string.data(), and check for position of the pointer instead of comparing the value at the pointer.

inline FLEX_ALWAYS_INLINE_FLATTEN flex_float operator()(const flex_string& t) const { return std::atof(t.c_str()); }
inline FLEX_ALWAYS_INLINE_FLATTEN flex_float operator()(const flex_string& t) const {
char *end;
double converted = std::strtod(t.c_str(), &end);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

@@ -1300,9 +1300,9 @@ std::shared_ptr<unity_sarray_base> unity_sarray::lazy_astype(flex_type_enum dtyp
flexible_type ret;
try {
if (dtype == flex_type_enum::INTEGER) {
ret = std::stoll(f.get<flex_string>());
f.to<flex_int>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to<...> returns. It does not cast inplace. See the flexible_type documentation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course - not sure how I missed that. How does this pass unit tests?

@znation
Copy link
Contributor

znation commented Mar 14, 2018

@shantanuchhabra Let's address @ylow's comments. You can either start fresh (I'll git revert this PR change) or you can put up a 2nd PR to address the comments - let me know which you'd prefer.

@shantanuchhabra
Copy link
Collaborator Author

I'll create a second PR!

@shantanuchhabra
Copy link
Collaborator Author

Please see #376 :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants