Skip to content
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

replace hand-rolled lexicalCast #4473

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ripple/beast/core/LexicalCast.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ struct LexicalCast<Out, std::string>
auto first = in.data();
auto last = in.data() + in.size();

if (first != last && *first == '+')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Have to check the next one is digit. See @HowardHinnant comment.

Copy link
Contributor

@nbougalis nbougalis May 26, 2023

Choose a reason for hiding this comment

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

I don't think so - the semantics seem correct to me. If it's a plus and the next character is not a digit, then std::from_chars will return some kind of failure and the check, which ensures that (a) not failure is returned; and (b) that the whole string was parsed and nothing was left over will work correctly.

Check out this example. As you can see, the only cases where the semantics don't match is when you have a numeric value prefixed with a + sign. The "new" code does what we want, and the old one just fails.

++first;

auto ret = std::from_chars(first, last, out);

return ret.ec == std::errc() && ret.ptr == last;
Expand Down
2 changes: 2 additions & 0 deletions src/test/beast/LexicalCast_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,15 @@ class LexicalCast_test : public unit_test::suite

expect(lexicalCastChecked(out, "-0"), "0");
expect(lexicalCastChecked(out, "0"), "0");
expect(lexicalCastChecked(out, "+0"), "0");
}

{
std::uint32_t out;

expect(!lexicalCastChecked(out, "-0"), "0");
expect(lexicalCastChecked(out, "0"), "0");
expect(lexicalCastChecked(out, "+0"), "0");
}
}

Expand Down