Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: base/strings/string_number_conversions.cc

Issue 1359253003: Optimise IntToString by avoiding string allocation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/strings/string_number_conversions.h" 5 #include "base/strings/string_number_conversions.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <wctype.h> 10 #include <wctype.h>
11 11
12 #include <limits> 12 #include <limits>
13 13
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/numerics/safe_conversions.h" 15 #include "base/numerics/safe_conversions.h"
16 #include "base/numerics/safe_math.h" 16 #include "base/numerics/safe_math.h"
17 #include "base/scoped_clear_errno.h" 17 #include "base/scoped_clear_errno.h"
18 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
19 #include "base/third_party/dmg_fp/dmg_fp.h" 19 #include "base/third_party/dmg_fp/dmg_fp.h"
20 20
21 namespace base { 21 namespace base {
22 22
23 namespace { 23 namespace {
24 24
25 template <typename STR, typename INT> 25 template <typename STR, typename INT>
26 struct IntToStringT { 26 struct IntToStringT {
27 static STR IntToString(INT value) { 27 static STR IntToString(INT value) {
28 // log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4. 28 // log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4.
29 // So round up to allocate 3 output characters per byte, plus 1 for '-'. 29 // So round up to allocate 3 output characters per byte, plus 1 for '-'.
30 const int kOutputBufSize = 30 const size_t kOutputBufSize =
31 3 * sizeof(INT) + std::numeric_limits<INT>::is_signed; 31 3 * sizeof(INT) + std::numeric_limits<INT>::is_signed;
32 32
33 // Allocate the whole string right away, we will right back to front, and 33 // Create the string in a temporary buffer, write it back to front, and
34 // then return the substr of what we ended up using. 34 // then return the substr of what we ended up using.
35 STR outbuf(kOutputBufSize, 0); 35 typedef typename STR::value_type CHR;
Mark Mentovai 2015/09/25 15:35:28 https://chromium-cpp.appspot.com/: use “using CHR
Adam Rice 2015/09/25 17:35:36 Done.
36 CHR outbuf[kOutputBufSize];
36 37
37 // The ValueOrDie call below can never fail, because UnsignedAbs is valid 38 // The ValueOrDie call below can never fail, because UnsignedAbs is valid
38 // for all valid inputs. 39 // for all valid inputs.
39 auto res = CheckedNumeric<INT>(value).UnsignedAbs().ValueOrDie(); 40 auto res = CheckedNumeric<INT>(value).UnsignedAbs().ValueOrDie();
40 41
41 typename STR::iterator it(outbuf.end()); 42 CHR* i = outbuf + kOutputBufSize;
Mark Mentovai 2015/09/25 15:35:27 You can save outbuf + kOutputBufSize in an “end” v
Adam Rice 2015/09/25 17:35:36 Done.
42 do { 43 do {
43 --it; 44 --i;
44 DCHECK(it != outbuf.begin()); 45 DCHECK(i != outbuf);
45 *it = static_cast<typename STR::value_type>((res % 10) + '0'); 46 *i = static_cast<CHR>((res % 10) + '0');
46 res /= 10; 47 res /= 10;
47 } while (res != 0); 48 } while (res != 0);
48 if (IsValueNegative(value)) { 49 if (IsValueNegative(value)) {
49 --it; 50 --i;
50 DCHECK(it != outbuf.begin()); 51 DCHECK(i != outbuf);
51 *it = static_cast<typename STR::value_type>('-'); 52 *i = static_cast<CHR>('-');
52 } 53 }
53 return STR(it, outbuf.end()); 54 return STR(i, outbuf + kOutputBufSize);
54 } 55 }
55 }; 56 };
56 57
57 // Utility to convert a character to a digit in a given base 58 // Utility to convert a character to a digit in a given base
58 template<typename CHAR, int BASE, bool BASE_LTE_10> class BaseCharToDigit { 59 template<typename CHAR, int BASE, bool BASE_LTE_10> class BaseCharToDigit {
59 }; 60 };
60 61
61 // Faster specialization for bases <= 10 62 // Faster specialization for bases <= 10
62 template<typename CHAR, int BASE> class BaseCharToDigit<CHAR, BASE, true> { 63 template<typename CHAR, int BASE> class BaseCharToDigit<CHAR, BASE, true> {
63 public: 64 public:
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 bool HexStringToUInt64(const StringPiece& input, uint64* output) { 477 bool HexStringToUInt64(const StringPiece& input, uint64* output) {
477 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( 478 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke(
478 input.begin(), input.end(), output); 479 input.begin(), input.end(), output);
479 } 480 }
480 481
481 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { 482 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) {
482 return HexStringToBytesT(input, output); 483 return HexStringToBytesT(input, output);
483 } 484 }
484 485
485 } // namespace base 486 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698