1 /* 2 * Copyright 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <cassert> 20 #include <charconv> 21 #include <limits> 22 #include <string> 23 #include <string_view> 24 #include <type_traits> 25 26 namespace android::ftl { 27 28 enum class Radix { kBin = 2, kDec = 10, kHex = 16 }; 29 30 template <typename T> 31 struct to_chars_length { 32 static_assert(std::is_integral_v<T>); 33 // Maximum binary digits, plus minus sign and radix prefix. 34 static constexpr std::size_t value = std::numeric_limits<std::make_unsigned_t<T>>::digits + 3; 35 }; 36 37 template <typename T> 38 constexpr std::size_t to_chars_length_v = to_chars_length<T>::value; 39 40 template <typename T = std::int64_t> 41 using to_chars_buffer_t = char[to_chars_length_v<T>]; 42 43 // Lightweight (not allocating nor sprintf-based) alternative to std::to_string for integers, with 44 // optional radix. See also ftl::to_string below. 45 // 46 // ftl::to_chars_buffer_t<> buffer; 47 // 48 // assert(ftl::to_chars(buffer, 123u) == "123"); 49 // assert(ftl::to_chars(buffer, -42, ftl::Radix::kBin) == "-0b101010"); 50 // assert(ftl::to_chars(buffer, 0xcafe, ftl::Radix::kHex) == "0xcafe"); 51 // assert(ftl::to_chars(buffer, '*', ftl::Radix::kHex) == "0x2a"); 52 // 53 template <typename T, std::size_t N> 54 std::string_view to_chars(char (&buffer)[N], T v, Radix radix = Radix::kDec) { 55 static_assert(N >= to_chars_length_v<T>); 56 57 auto begin = buffer + 2; 58 const auto [end, err] = std::to_chars(begin, buffer + N, v, static_cast<int>(radix)); 59 assert(err == std::errc()); 60 61 if (radix == Radix::kDec) { 62 // TODO: Replace with {begin, end} in C++20. 63 return {begin, static_cast<std::size_t>(end - begin)}; 64 } 65 66 const auto prefix = radix == Radix::kBin ? 'b' : 'x'; 67 if constexpr (std::is_unsigned_v<T>) { 68 buffer[0] = '0'; 69 buffer[1] = prefix; 70 } else { 71 if (*begin == '-') { 72 *buffer = '-'; 73 } else { 74 --begin; 75 } 76 77 *begin-- = prefix; 78 *begin = '0'; 79 } 80 81 // TODO: Replace with {buffer, end} in C++20. 82 return {buffer, static_cast<std::size_t>(end - buffer)}; 83 } 84 85 // Lightweight (not sprintf-based) alternative to std::to_string for integers, with optional radix. 86 // 87 // assert(ftl::to_string(123u) == "123"); 88 // assert(ftl::to_string(-42, ftl::Radix::kBin) == "-0b101010"); 89 // assert(ftl::to_string(0xcafe, ftl::Radix::kHex) == "0xcafe"); 90 // assert(ftl::to_string('*', ftl::Radix::kHex) == "0x2a"); 91 // 92 template <typename T> 93 inline std::string to_string(T v, Radix radix = Radix::kDec) { 94 to_chars_buffer_t<T> buffer; 95 return std::string(to_chars(buffer, v, radix)); 96 } 97 98 std::string to_string(bool) = delete; 99 std::string to_string(bool, Radix) = delete; 100 101 } // namespace android::ftl 102