diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2019-08-20 20:51:16 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2019-08-20 20:51:16 +0000 |
commit | 1147845301c03308e3419b89c28c77bb6917fe04 (patch) | |
tree | 225f45e462607f9595a5e5f418a9533ab50e83bb /src/charconv.cpp | |
parent | b7332b04df5d50c92640c74cfeb138ecb7e3f7ae (diff) |
Notes
Diffstat (limited to 'src/charconv.cpp')
-rw-r--r-- | src/charconv.cpp | 167 |
1 files changed, 47 insertions, 120 deletions
diff --git a/src/charconv.cpp b/src/charconv.cpp index ec241db74471..d3035883a78f 100644 --- a/src/charconv.cpp +++ b/src/charconv.cpp @@ -1,9 +1,8 @@ //===------------------------- charconv.cpp -------------------------------===// // -// The LLVM Compiler Infrastructure -// -// This file is dual licensed under the MIT and the University of Illinois Open -// Source Licenses. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// @@ -61,48 +60,50 @@ append4(char* buffer, T i) return append2(append2(buffer, (i) / 100), (i) % 100); } -char* -__u32toa(uint32_t value, char* buffer) +template <typename T> +inline _LIBCPP_INLINE_VISIBILITY char* +append2_no_zeros(char* buffer, T v) +{ + if (v < 10) + return append1(buffer, v); + else + return append2(buffer, v); +} + +template <typename T> +inline _LIBCPP_INLINE_VISIBILITY char* +append4_no_zeros(char* buffer, T v) +{ + if (v < 100) + return append2_no_zeros(buffer, v); + else if (v < 1000) + return append3(buffer, v); + else + return append4(buffer, v); +} + +template <typename T> +inline _LIBCPP_INLINE_VISIBILITY char* +append8_no_zeros(char* buffer, T v) { - if (value < 10000) + if (v < 10000) { - if (value < 100) - { - if (value < 10) - buffer = append1(buffer, value); - else - buffer = append2(buffer, value); - } - else - { - if (value < 1000) - buffer = append3(buffer, value); - else - buffer = append4(buffer, value); - } + buffer = append4_no_zeros(buffer, v); } - else if (value < 100000000) + else + { + buffer = append4_no_zeros(buffer, v / 10000); + buffer = append4(buffer, v % 10000); + } + return buffer; +} + +char* +__u32toa(uint32_t value, char* buffer) +{ + if (value < 100000000) { - // value = bbbbcccc - const uint32_t b = value / 10000; - const uint32_t c = value % 10000; - - if (value < 1000000) - { - if (value < 100000) - buffer = append1(buffer, b); - else - buffer = append2(buffer, b); - } - else - { - if (value < 10000000) - buffer = append3(buffer, b); - else - buffer = append4(buffer, b); - } - - buffer = append4(buffer, c); + buffer = append8_no_zeros(buffer, value); } else { @@ -110,11 +111,7 @@ __u32toa(uint32_t value, char* buffer) const uint32_t a = value / 100000000; // 1 to 42 value %= 100000000; - if (a < 10) - buffer = append1(buffer, a); - else - buffer = append2(buffer, a); - + buffer = append2_no_zeros(buffer, a); buffer = append4(buffer, value / 10000); buffer = append4(buffer, value % 10000); } @@ -128,71 +125,14 @@ __u64toa(uint64_t value, char* buffer) if (value < 100000000) { uint32_t v = static_cast<uint32_t>(value); - if (v < 10000) - { - if (v < 100) - { - if (v < 10) - buffer = append1(buffer, v); - else - buffer = append2(buffer, v); - } - else - { - if (v < 1000) - buffer = append3(buffer, v); - else - buffer = append4(buffer, v); - } - } - else - { - // value = bbbbcccc - const uint32_t b = v / 10000; - const uint32_t c = v % 10000; - - if (v < 1000000) - { - if (v < 100000) - buffer = append1(buffer, b); - else - buffer = append2(buffer, b); - } - else - { - if (v < 10000000) - buffer = append3(buffer, b); - else - buffer = append4(buffer, b); - } - - buffer = append4(buffer, c); - } + buffer = append8_no_zeros(buffer, v); } else if (value < 10000000000000000) { const uint32_t v0 = static_cast<uint32_t>(value / 100000000); const uint32_t v1 = static_cast<uint32_t>(value % 100000000); - const uint32_t b0 = v0 / 10000; - const uint32_t c0 = v0 % 10000; - - if (v0 < 1000000) - { - if (v0 < 100000) - buffer = append1(buffer, b0); - else - buffer = append2(buffer, b0); - } - else - { - if (v0 < 10000000) - buffer = append3(buffer, b0); - else - buffer = append4(buffer, b0); - } - - buffer = append4(buffer, c0); + buffer = append8_no_zeros(buffer, v0); buffer = append4(buffer, v1 / 10000); buffer = append4(buffer, v1 % 10000); } @@ -202,20 +142,7 @@ __u64toa(uint64_t value, char* buffer) static_cast<uint32_t>(value / 10000000000000000); // 1 to 1844 value %= 10000000000000000; - if (a < 100) - { - if (a < 10) - buffer = append1(buffer, a); - else - buffer = append2(buffer, a); - } - else - { - if (a < 1000) - buffer = append3(buffer, a); - else - buffer = append4(buffer, a); - } + buffer = append4_no_zeros(buffer, a); const uint32_t v0 = static_cast<uint32_t>(value / 100000000); const uint32_t v1 = static_cast<uint32_t>(value % 100000000); |