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

remove stringstream dependency #1117

Merged
merged 3 commits into from
Jun 1, 2018
Merged

remove stringstream dependency #1117

merged 3 commits into from
Jun 1, 2018

Conversation

TinyTinni
Copy link
Contributor

Hi,
this PR replaces the stringstream conversions with snprintf ones and removes all stringstream dependencies.
snprintf is already use in the project.

Issue reference: #1111

@@ -6,12 +6,10 @@
#include <cmath> // ldexp
#include <cstddef> // size_t
#include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
#include <cstdio> // snprinft
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: snprintf

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed, thanks

return ss.str();
char cr[3];
snprintf(cr, 3, "%.2X", current);
return std::string{cr};
Copy link
Contributor

Choose a reason for hiding this comment

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

This might be better for NRVO purposes if you do it like this (untested, just speculation)

std::string cr{"  "};
snprintf(&cr[0], 3, "%.2X", current);
return cr;

Copy link
Contributor

Choose a reason for hiding this comment

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

Except that C++11 made that undefined, and the non-const data() doesn't exist until C++17, so never mind.

Copy link
Contributor Author

@TinyTinni TinyTinni May 31, 2018

Choose a reason for hiding this comment

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

Even with non-const .data() it is undefined.

The pointer is such that the range [data(); data() + size()) is valid

The main problem is the null-termination of snprintf, which always results in a string with an +1 increased stringlength compared to the the string created before.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fine, size() is 2, so cr[0] to cr[2] is valid, and that's exactly what snprintf with a size of 3 will write to, including the nul terminator. In any case, we can't use it, as we can only require C++11.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see. Scrolling down to the return value, the definition interval is closed

data() + i == &operator for every i in [0, size()]. | (since C++11)

while in the brief, the definition interval is only an open one which excludes [size()].

TinyTinni added 2 commits May 31, 2018 15:32
should fix the GCC unittest
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling ecadcdb on TinyTinni:develop into 8efbf8d on nlohmann:develop.

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

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

Looks good to me.

ss << std::setw(2) << std::uppercase << std::setfill('0') << std::hex << static_cast<int>(byte);
JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + ss.str()));
std::string sn(3, '\0');
snprintf(&sn[0], sn.size(), "%.2X", byte);

Choose a reason for hiding this comment

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

How about changing this to:
snprintf(&sn[0], sn.size(), "%.2X", static_cast<uint8_t>(s[i]));

In order to avoid "‘%.2X’ directive output may be truncated writing between 2 and 8 bytes into a region of size 3 [-Wformat-truncation=]" (GCC 7.3.0).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see the difference.
byte is defined as
const auto byte = static_cast<uint8_t>(s[i]);
So it is 1 byte and the same, what you wrote.
Maybe a false positive in GCC?

Choose a reason for hiding this comment

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

I suppose it might be a false positive since there is no difference.
Maybe someone with the same GCC version can confirm this warning.

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

Successfully merging this pull request may close these issues.

5 participants