Skip to content

Commit

Permalink
Merge pull request #153 from whackashoe/remove_sprintf
Browse files Browse the repository at this point in the history
Replace sprintf with hex function, this fixes #149
  • Loading branch information
nlohmann committed Dec 7, 2015
2 parents 6f8e36a + 14d8a91 commit f36f316
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4696,11 +4696,19 @@ class basic_json
{
if (c >= 0x00 and c <= 0x1f)
{
// convert a number 0..15 to its hex representation (0..f)
auto hexify = [](const char v) -> char
{
return (v < 10) ? ('0' + v) : ('a' + v - 10);
};

// print character c as \uxxxx
sprintf(&result[pos + 1], "u%04x", int(c));
pos += 6;
// overwrite trailing null character
result[pos] = '\\';
for(const char m : { 'u', '0', '0', hexify(c >> 4), hexify(c & 0x0f) })
{
result[++pos] = m;
}

++pos;
}
else
{
Expand Down
16 changes: 12 additions & 4 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -4696,11 +4696,19 @@ class basic_json
{
if (c >= 0x00 and c <= 0x1f)
{
// convert a number 0..15 to its hex representation (0..f)
auto hexify = [](const char v) -> char
{
return (v < 10) ? ('0' + v) : ('a' + v - 10);
};

// print character c as \uxxxx
sprintf(&result[pos + 1], "u%04x", int(c));
pos += 6;
// overwrite trailing null character
result[pos] = '\\';
for(const char m : { 'u', '0', '0', hexify(c >> 4), hexify(c & 0x0f) })
{
result[++pos] = m;
}

++pos;
}
else
{
Expand Down

0 comments on commit f36f316

Please sign in to comment.