From 14d8a91f7349baefa83cabc96113a12b9d9848a9 Mon Sep 17 00:00:00 2001 From: Jett Date: Thu, 19 Nov 2015 00:17:36 -0600 Subject: [PATCH] Replace sprintf with hex function, this fixes #149 --- src/json.hpp | 16 ++++++++++++---- src/json.hpp.re2c | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 1e3cd11f..f985a9bf 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -4677,11 +4677,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 { diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 84559240..833cf4c7 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -4677,11 +4677,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 {