-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Changes from 1 commit
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 |
---|---|---|
|
@@ -6,12 +6,10 @@ | |
#include <cmath> // ldexp | ||
#include <cstddef> // size_t | ||
#include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t | ||
#include <cstdio> // snprinft | ||
#include <cstring> // memcpy | ||
#include <iomanip> // setw, setfill | ||
#include <ios> // hex | ||
#include <iterator> // back_inserter | ||
#include <limits> // numeric_limits | ||
#include <sstream> // stringstream | ||
#include <string> // char_traits, string | ||
#include <utility> // make_pair, move | ||
|
||
|
@@ -1665,9 +1663,9 @@ class binary_reader | |
*/ | ||
std::string get_token_string() const | ||
{ | ||
std::stringstream ss; | ||
ss << std::setw(2) << std::uppercase << std::setfill('0') << std::hex << current; | ||
return ss.str(); | ||
char cr[3]; | ||
snprintf(cr, 3, "%.2X", current); | ||
return std::string{cr}; | ||
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. This might be better for NRVO purposes if you do it like this (untested, just speculation)
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. Except that C++11 made that undefined, and the non-const 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. Even with non-const
The main problem is the null-termination of 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. That's fine, 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. |
||
} | ||
|
||
private: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,8 @@ | |
#include <cstddef> // size_t, ptrdiff_t | ||
#include <cstdint> // uint8_t | ||
#include <cstdio> // snprintf | ||
#include <iomanip> // setfill | ||
#include <iterator> // next | ||
#include <limits> // numeric_limits | ||
#include <string> // string | ||
#include <sstream> // stringstream | ||
#include <type_traits> // is_same | ||
|
||
#include <nlohmann/detail/exceptions.hpp> | ||
|
@@ -389,9 +386,9 @@ class serializer | |
|
||
case UTF8_REJECT: // decode found invalid UTF-8 byte | ||
{ | ||
std::stringstream ss; | ||
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); | ||
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. How about changing this to: 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). 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. I don't see the difference. 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. I suppose it might be a false positive since there is no difference. |
||
JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn)); | ||
} | ||
|
||
default: // decode found yet incomplete multi-byte code point | ||
|
@@ -417,9 +414,9 @@ class serializer | |
else | ||
{ | ||
// we finish reading, but do not accept: string was incomplete | ||
std::stringstream ss; | ||
ss << std::setw(2) << std::uppercase << std::setfill('0') << std::hex << static_cast<int>(static_cast<uint8_t>(s.back())); | ||
JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + ss.str())); | ||
std::string sn(3,'\0'); | ||
snprintf(&sn[0], sn.size(), "%.2X", s.back()); | ||
JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn)); | ||
} | ||
} | ||
|
||
|
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.
typo: snprintf
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.
fixed, thanks