//===-- Utilities to convert integral values to string ----------*- C++ -*-===// // // 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 // //===----------------------------------------------------------------------===// // // Converts an integer to a string. // // By default, the string is written as decimal to an internal buffer and // accessed via the 'view' method. // // IntegerToString<int> buffer(42); // cpp::string_view view = buffer.view(); // // The buffer is allocated on the stack and its size is so that the conversion // always succeeds. // // It is also possible to write the data to a preallocated buffer, but this may // fail. // // char buffer[8]; // if (auto maybe_view = IntegerToString<int>::write_to_span(buffer, 42)) { // cpp::string_view view = *maybe_view; // } // // The first template parameter is the type of the integer. // The second template parameter defines how the integer is formatted. // Available default are 'radix::Bin', 'radix::Oct', 'radix::Dec' and // 'radix::Hex'. // // For 'radix::Bin', 'radix::Oct' and 'radix::Hex' the value is always // interpreted as a positive type but 'radix::Dec' will honor negative values. // e.g., // // IntegerToString<int8_t>(-1) // "-1" // IntegerToString<int8_t, radix::Dec>(-1) // "-1" // IntegerToString<int8_t, radix::Bin>(-1) // "11111111" // IntegerToString<int8_t, radix::Oct>(-1) // "377" // IntegerToString<int8_t, radix::Hex>(-1) // "ff" // // Additionnally, the format can be changed by navigating the subtypes: // - WithPrefix : Adds "0b", "0", "0x" for binary, octal and hexadecimal // - WithWidth<XX> : Pad string to XX characters filling leading digits with 0 // - Uppercase : Use uppercase letters (only for HexString) // - WithSign : Prepend '+' for positive values (only for DecString) // // Examples // -------- // IntegerToString<int8_t, radix::Dec::WithWidth<2>::WithSign>(0) : "+00" // IntegerToString<int8_t, radix::Dec::WithWidth<2>::WithSign>(-1) : "-01" // IntegerToString<uint8_t, radix::Hex::WithPrefix::Uppercase>(255) : "0xFF" // IntegerToString<uint8_t, radix::Hex::WithWidth<4>::Uppercase>(255) : "00FF" //===----------------------------------------------------------------------===// #ifndef LLVM_LIBC_SRC___SUPPORT_INTEGER_TO_STRING_H #define LLVM_LIBC_SRC___SUPPORT_INTEGER_TO_STRING_H #include <stdint.h> #include "src/__support/CPP/algorithm.h" // max #include "src/__support/CPP/array.h" #include "src/__support/CPP/bit.h" #include "src/__support/CPP/limits.h" #include "src/__support/CPP/optional.h" #include "src/__support/CPP/span.h" #include "src/__support/CPP/string_view.h" #include "src/__support/CPP/type_traits.h" #include "src/__support/big_int.h" // make_integral_or_big_int_unsigned_t #include "src/__support/common.h" #include "src/__support/macros/config.h" namespace LIBC_NAMESPACE_DECL { namespace details { template <uint8_t base, bool prefix = false, bool force_sign = false, bool is_uppercase = false, size_t min_digits = 1> struct Fmt { … }; // Move this to a separate header since it might be useful elsewhere. template <bool forward> class StringBufferWriterImpl { … }; StringBufferWriter; BackwardStringBufferWriter; } // namespace details namespace radix { Bin; Oct; Dec; Hex; Custom; } // namespace radix // See file header for documentation. template <typename T, typename Fmt = radix::Dec> class IntegerToString { … }; } // namespace LIBC_NAMESPACE_DECL #endif // LLVM_LIBC_SRC___SUPPORT_INTEGER_TO_STRING_H