From 09bd1fafb131020a81e6519f65e9ba58d7a09abd Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 27 Oct 2020 11:42:09 -0400 Subject: Improve efficiency of number to string conversion --- libqpdf/QUtil.cc | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 366365f1..5442b5ff 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -267,16 +267,26 @@ int_to_string_base_internal(T num, int base, int length) throw std::logic_error( "int_to_string_base called with unsupported base"); } - std::ostringstream buf; - buf.imbue(std::locale::classic()); - buf << std::setbase(base) << std::nouppercase << num; + std::string cvt; + if (base == 10) + { + // Use the more efficient std::to_string when possible + cvt = std::to_string(num); + } + else + { + std::ostringstream buf; + buf.imbue(std::locale::classic()); + buf << std::setbase(base) << std::nouppercase << num; + cvt = buf.str(); + } std::string result; - int str_length = QIntC::to_int(buf.str().length()); + int str_length = QIntC::to_int(cvt.length()); if ((length > 0) && (str_length < length)) { result.append(QIntC::to_size(length - str_length), '0'); } - result += buf.str(); + result += cvt; if ((length < 0) && (str_length < -length)) { result.append(QIntC::to_size(-length - str_length), ' '); -- cgit v1.2.3-54-g00ecf