113 lines · c
1//===-- Format specifier converter for printf -------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_H10#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_H11 12#include "src/__support/macros/config.h"13#include "src/stdio/printf_core/core_structs.h"14#include "src/stdio/printf_core/printf_config.h"15#include "src/stdio/printf_core/strerror_converter.h"16#include "src/stdio/printf_core/writer.h"17 18// This option allows for replacing all of the conversion functions with custom19// replacements. This allows conversions to be replaced at compile time.20#ifndef LIBC_COPT_PRINTF_CONV_ATLAS21#include "src/stdio/printf_core/converter_atlas.h"22#else23#include LIBC_COPT_PRINTF_CONV_ATLAS24#endif25 26#include <stddef.h>27 28namespace LIBC_NAMESPACE_DECL {29namespace printf_core {30 31// convert will call a conversion function to convert the FormatSection into32// its string representation, and then that will write the result to the33// writer.34template <WriteMode write_mode>35int convert(Writer<write_mode> *writer, const FormatSection &to_conv) {36 if (!to_conv.has_conv)37 return writer->write(to_conv.raw_string);38 39#if !defined(LIBC_COPT_PRINTF_DISABLE_FLOAT) && \40 defined(LIBC_COPT_PRINTF_HEX_LONG_DOUBLE)41 if (to_conv.length_modifier == LengthModifier::L) {42 switch (to_conv.conv_name) {43 case 'f':44 case 'F':45 case 'e':46 case 'E':47 case 'g':48 case 'G':49 return convert_float_hex_exp(writer, to_conv);50 default:51 break;52 }53 }54#endif // LIBC_COPT_PRINTF_DISABLE_FLOAT55 56 switch (to_conv.conv_name) {57 case '%':58 return writer->write("%");59 case 'c':60 return convert_char(writer, to_conv);61 case 's':62 return convert_string(writer, to_conv);63 case 'd':64 case 'i':65 case 'u':66 case 'o':67 case 'x':68 case 'X':69 case 'b':70 case 'B':71 return convert_int(writer, to_conv);72#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT73 case 'f':74 case 'F':75 return convert_float_decimal(writer, to_conv);76 case 'e':77 case 'E':78 return convert_float_dec_exp(writer, to_conv);79 case 'a':80 case 'A':81 return convert_float_hex_exp(writer, to_conv);82 case 'g':83 case 'G':84 return convert_float_dec_auto(writer, to_conv);85#endif // LIBC_COPT_PRINTF_DISABLE_FLOAT86#ifdef LIBC_INTERNAL_PRINTF_HAS_FIXED_POINT87 case 'r':88 case 'R':89 case 'k':90 case 'K':91 return convert_fixed(writer, to_conv);92#endif // LIBC_INTERNAL_PRINTF_HAS_FIXED_POINT93#ifndef LIBC_COPT_PRINTF_DISABLE_STRERROR94 case 'm':95 return convert_strerror(writer, to_conv);96#endif // LIBC_COPT_PRINTF_DISABLE_STRERROR97#ifndef LIBC_COPT_PRINTF_DISABLE_WRITE_INT98 case 'n':99 return convert_write_int(writer, to_conv);100#endif // LIBC_COPT_PRINTF_DISABLE_WRITE_INT101 case 'p':102 return convert_pointer(writer, to_conv);103 default:104 return writer->write(to_conv.raw_string);105 }106 return -1;107}108 109} // namespace printf_core110} // namespace LIBC_NAMESPACE_DECL111 112#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_H113