diff options
author | Jay Berkenbilt <ejb@ql.org> | 2020-10-27 16:42:09 +0100 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2020-10-27 16:57:48 +0100 |
commit | 09bd1fafb131020a81e6519f65e9ba58d7a09abd (patch) | |
tree | cce03274c544a7f89c6b11ddaee72e69879cd0ce /libqpdf | |
parent | bcea54fcaa16a7d5feff0c4cd038fea51d1359ea (diff) | |
download | qpdf-09bd1fafb131020a81e6519f65e9ba58d7a09abd.tar.zst |
Improve efficiency of number to string conversion
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/QUtil.cc | 20 |
1 files changed, 15 insertions, 5 deletions
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), ' '); |