Commit 09bd1fafb131020a81e6519f65e9ba58d7a09abd
1 parent
bcea54fc
Improve efficiency of number to string conversion
Showing
1 changed file
with
15 additions
and
5 deletions
libqpdf/QUtil.cc
| @@ -267,16 +267,26 @@ int_to_string_base_internal(T num, int base, int length) | @@ -267,16 +267,26 @@ int_to_string_base_internal(T num, int base, int length) | ||
| 267 | throw std::logic_error( | 267 | throw std::logic_error( |
| 268 | "int_to_string_base called with unsupported base"); | 268 | "int_to_string_base called with unsupported base"); |
| 269 | } | 269 | } |
| 270 | - std::ostringstream buf; | ||
| 271 | - buf.imbue(std::locale::classic()); | ||
| 272 | - buf << std::setbase(base) << std::nouppercase << num; | 270 | + std::string cvt; |
| 271 | + if (base == 10) | ||
| 272 | + { | ||
| 273 | + // Use the more efficient std::to_string when possible | ||
| 274 | + cvt = std::to_string(num); | ||
| 275 | + } | ||
| 276 | + else | ||
| 277 | + { | ||
| 278 | + std::ostringstream buf; | ||
| 279 | + buf.imbue(std::locale::classic()); | ||
| 280 | + buf << std::setbase(base) << std::nouppercase << num; | ||
| 281 | + cvt = buf.str(); | ||
| 282 | + } | ||
| 273 | std::string result; | 283 | std::string result; |
| 274 | - int str_length = QIntC::to_int(buf.str().length()); | 284 | + int str_length = QIntC::to_int(cvt.length()); |
| 275 | if ((length > 0) && (str_length < length)) | 285 | if ((length > 0) && (str_length < length)) |
| 276 | { | 286 | { |
| 277 | result.append(QIntC::to_size(length - str_length), '0'); | 287 | result.append(QIntC::to_size(length - str_length), '0'); |
| 278 | } | 288 | } |
| 279 | - result += buf.str(); | 289 | + result += cvt; |
| 280 | if ((length < 0) && (str_length < -length)) | 290 | if ((length < 0) && (str_length < -length)) |
| 281 | { | 291 | { |
| 282 | result.append(QIntC::to_size(-length - str_length), ' '); | 292 | result.append(QIntC::to_size(-length - str_length), ' '); |