From f43de4a469fed091684b8b27bf6c335de9bb7f6d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 15 Feb 2014 09:24:31 -0800 Subject: [PATCH] Only append terminating '\0' when necessary. --- format.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/format.h b/format.h index 6afee1ec..c4be2b3e 100644 --- a/format.h +++ b/format.h @@ -1287,13 +1287,12 @@ class FormatInt { // Buffer should be large enough to hold all digits (digits10 + 1), // a sign and a null character. enum {BUFFER_SIZE = std::numeric_limits::digits10 + 3}; - char buffer_[BUFFER_SIZE]; + mutable char buffer_[BUFFER_SIZE]; char *str_; // Formats value in reverse and returns the number of digits. char *FormatDecimal(unsigned long long value) { - char *buffer_end = buffer_ + BUFFER_SIZE; - *--buffer_end = '\0'; + char *buffer_end = buffer_ + BUFFER_SIZE - 1; while (value >= 100) { // Integer division is slow so do it for a group of two digits instead // of for every digit. The idea comes from the talk by Alexandrescu @@ -1331,8 +1330,11 @@ class FormatInt { explicit FormatInt(unsigned long value) : str_(FormatDecimal(value)) {} explicit FormatInt(unsigned long long value) : str_(FormatDecimal(value)) {} - const char *c_str() const { return str_; } - std::string str() const { return str_; } + const char *c_str() const { + buffer_[BUFFER_SIZE - 1] = '\0'; + return str_; + } + std::string str() const { return std::string(str_, size()); } std::size_t size() const { return buffer_ - str_ + BUFFER_SIZE - 1; } };