| OLD | NEW |
| 1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2012 Apple Inc. All Rights Reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com> | 3 // found in the LICENSE file. |
| 4 * | |
| 5 * This library is free software; you can redistribute it and/or | |
| 6 * modify it under the terms of the GNU Library General Public | |
| 7 * License as published by the Free Software Foundation; either | |
| 8 * version 2 of the License, or (at your option) any later version. | |
| 9 * | |
| 10 * This library is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 * Library General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU Library General Public License | |
| 16 * along with this library; see the file COPYING.LIB. If not, write to | |
| 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
| 18 * Boston, MA 02110-1301, USA. | |
| 19 * | |
| 20 */ | |
| 21 | 4 |
| 22 #ifndef IntegerToStringConversion_h | 5 #include "platform/wtf/text/IntegerToStringConversion.h" |
| 23 #define IntegerToStringConversion_h | |
| 24 | 6 |
| 25 #include "base/numerics/safe_conversions.h" | 7 // The contents of this header was moved to platform/wtf as part of |
| 26 #include "wtf/StdLibExtras.h" | 8 // WTF migration project. See the following post for details: |
| 27 #include "wtf/text/Unicode.h" | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
| 28 #include <limits> | |
| 29 #include <type_traits> | |
| 30 | |
| 31 namespace WTF { | |
| 32 | |
| 33 // TODO(esprehn): See if we can generalize IntToStringT in | |
| 34 // base/strings/string_number_conversions.cc, and use unsigned type expansion | |
| 35 // optimization here instead of CheckedNumeric::UnsignedAbs(). | |
| 36 template <typename IntegerType> | |
| 37 class IntegerToStringConverter { | |
| 38 public: | |
| 39 static_assert(std::is_integral<IntegerType>::value, | |
| 40 "IntegerType must be a type of integer."); | |
| 41 | |
| 42 explicit IntegerToStringConverter(IntegerType input) { | |
| 43 LChar* end = m_buffer + WTF_ARRAY_LENGTH(m_buffer); | |
| 44 m_begin = end; | |
| 45 | |
| 46 // We need to switch to the unsigned type when negating the value since | |
| 47 // abs(INT_MIN) == INT_MAX + 1. | |
| 48 bool isNegative = base::IsValueNegative(input); | |
| 49 UnsignedIntegerType value = isNegative ? 0u - input : input; | |
| 50 | |
| 51 do { | |
| 52 --m_begin; | |
| 53 DCHECK_NE(m_begin, m_buffer); | |
| 54 *m_begin = static_cast<LChar>((value % 10) + '0'); | |
| 55 value /= 10; | |
| 56 } while (value); | |
| 57 | |
| 58 if (isNegative) { | |
| 59 --m_begin; | |
| 60 DCHECK_NE(m_begin, m_buffer); | |
| 61 *m_begin = static_cast<LChar>('-'); | |
| 62 } | |
| 63 | |
| 64 m_length = static_cast<unsigned>(end - m_begin); | |
| 65 } | |
| 66 | |
| 67 const LChar* characters8() const { return m_begin; } | |
| 68 unsigned length() const { return m_length; } | |
| 69 | |
| 70 private: | |
| 71 using UnsignedIntegerType = typename std::make_unsigned<IntegerType>::type; | |
| 72 static const size_t kBufferSize = 3 * sizeof(UnsignedIntegerType) + | |
| 73 std::numeric_limits<IntegerType>::is_signed; | |
| 74 | |
| 75 LChar m_buffer[kBufferSize]; | |
| 76 LChar* m_begin; | |
| 77 unsigned m_length; | |
| 78 }; | |
| 79 | |
| 80 } // namespace WTF | |
| 81 | |
| 82 #endif // IntegerToStringConversion_h | |
| OLD | NEW |