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

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: Changes proposed by mark. 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 using CHR = typename STR::value_type;
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* end = outbuf + kOutputBufSize;
43 CHR* i = end;
42 do { 44 do {
43 --it; 45 --i;
44 DCHECK(it != outbuf.begin()); 46 DCHECK(i != outbuf);
45 *it = static_cast<typename STR::value_type>((res % 10) + '0'); 47 *i = static_cast<CHR>((res % 10) + '0');
46 res /= 10; 48 res /= 10;
47 } while (res != 0); 49 } while (res != 0);
48 if (IsValueNegative(value)) { 50 if (IsValueNegative(value)) {
49 --it; 51 --i;
50 DCHECK(it != outbuf.begin()); 52 DCHECK(i != outbuf);
51 *it = static_cast<typename STR::value_type>('-'); 53 *i = static_cast<CHR>('-');
52 } 54 }
53 return STR(it, outbuf.end()); 55 return STR(i, end);
54 } 56 }
55 }; 57 };
56 58
57 // Utility to convert a character to a digit in a given base 59 // Utility to convert a character to a digit in a given base
58 template<typename CHAR, int BASE, bool BASE_LTE_10> class BaseCharToDigit { 60 template<typename CHAR, int BASE, bool BASE_LTE_10> class BaseCharToDigit {
59 }; 61 };
60 62
61 // Faster specialization for bases <= 10 63 // Faster specialization for bases <= 10
62 template<typename CHAR, int BASE> class BaseCharToDigit<CHAR, BASE, true> { 64 template<typename CHAR, int BASE> class BaseCharToDigit<CHAR, BASE, true> {
63 public: 65 public:
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 bool HexStringToUInt64(const StringPiece& input, uint64* output) { 478 bool HexStringToUInt64(const StringPiece& input, uint64* output) {
477 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( 479 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke(
478 input.begin(), input.end(), output); 480 input.begin(), input.end(), output);
479 } 481 }
480 482
481 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { 483 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) {
482 return HexStringToBytesT(input, output); 484 return HexStringToBytesT(input, output);
483 } 485 }
484 486
485 } // namespace base 487 } // 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