From d6badca6106ce0a9986df0f5877e6e63f8d85643 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Mon, 30 Mar 2015 13:01:16 +0200 Subject: [PATCH] use lamda function to do complex initialization with a (wanted) side-effect --- util/string_util.hpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/util/string_util.hpp b/util/string_util.hpp index e65290d09..cb67fa9db 100644 --- a/util/string_util.hpp +++ b/util/string_util.hpp @@ -41,12 +41,18 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // work with negative values to prevent overflowing when taking -value template static inline char *printInt(char *buffer, int value) { - bool minus = true; - if (value >= 0) - { - minus = false; - value = -value; - } + static_assert(length > 0, "length must be positive"); + static_assert(precision > 0, "precision must be positive"); + + const bool minus = [&value]{ + if (value >= 0) + { + value = -value; + return false; + } + return true; + }(); + buffer += length - 1; for (int i = 0; i < precision; ++i) { @@ -56,6 +62,7 @@ template static inline char *printInt(char *buffer, } *buffer = '.'; --buffer; + for (int i = precision + 1; i < length; ++i) { *buffer = '0' - (value % 10); @@ -66,6 +73,7 @@ template static inline char *printInt(char *buffer, } --buffer; } + if (minus) { --buffer; @@ -128,10 +136,10 @@ inline std::string escape_JSON(const std::string &input) inline std::size_t URIDecode(const std::string &input, std::string &output) { - auto src_iter = input.begin(); + auto src_iter = std::begin(input); output.resize(input.size() + 1); std::size_t decoded_length = 0; - for (decoded_length = 0; src_iter != input.end(); ++decoded_length) + for (decoded_length = 0; src_iter != std::end(input); ++decoded_length) { if (src_iter[0] == '%' && src_iter[1] && src_iter[2] && isxdigit(src_iter[1]) && isxdigit(src_iter[2]))