16a22b185SMichael Jones //===-- Format specifier converter implmentation for printf -----*- C++ -*-===//
26a22b185SMichael Jones //
36a22b185SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46a22b185SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
56a22b185SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66a22b185SMichael Jones //
76a22b185SMichael Jones //===----------------------------------------------------------------------===//
86a22b185SMichael Jones 
96a22b185SMichael Jones #include "src/stdio/printf_core/converter.h"
106a22b185SMichael Jones 
116a22b185SMichael Jones #include "src/stdio/printf_core/core_structs.h"
126a22b185SMichael Jones #include "src/stdio/printf_core/writer.h"
136a22b185SMichael Jones 
146a22b185SMichael Jones // This option allows for replacing all of the conversion functions with custom
156a22b185SMichael Jones // replacements. This allows conversions to be replaced at compile time.
166a22b185SMichael Jones #ifndef LLVM_LIBC_PRINTF_CONV_ATLAS
176a22b185SMichael Jones #include "src/stdio/printf_core/converter_atlas.h"
186a22b185SMichael Jones #else
196a22b185SMichael Jones #include LLVM_LIBC_PRINTF_CONV_ATLAS
206a22b185SMichael Jones #endif
216a22b185SMichael Jones 
226a22b185SMichael Jones #include <stddef.h>
236a22b185SMichael Jones 
246a22b185SMichael Jones namespace __llvm_libc {
256a22b185SMichael Jones namespace printf_core {
266a22b185SMichael Jones 
convert(Writer * writer,const FormatSection & to_conv)272e6eccfeSMichael Jones int convert(Writer *writer, const FormatSection &to_conv) {
282e6eccfeSMichael Jones   if (!to_conv.has_conv)
292e6eccfeSMichael Jones     return writer->write(to_conv.raw_string, to_conv.raw_len);
302e6eccfeSMichael Jones 
316a22b185SMichael Jones   switch (to_conv.conv_name) {
326a22b185SMichael Jones   case '%':
332e6eccfeSMichael Jones     return writer->write("%", 1);
346a22b185SMichael Jones   case 'c':
352e6eccfeSMichael Jones     return convert_char(writer, to_conv);
366a22b185SMichael Jones   case 's':
372e6eccfeSMichael Jones     return convert_string(writer, to_conv);
386a22b185SMichael Jones   case 'd':
396a22b185SMichael Jones   case 'i':
406a22b185SMichael Jones   case 'u':
412e6eccfeSMichael Jones     return convert_int(writer, to_conv);
426a22b185SMichael Jones   case 'o':
436ec465abSMichael Jones     return convert_oct(writer, to_conv);
446a22b185SMichael Jones   case 'x':
456a22b185SMichael Jones   case 'X':
46652ecb25SMichael Jones     return convert_hex(writer, to_conv);
47*f9f8693bSMichael Jones #ifndef LLVM_LIBC_PRINTF_DISABLE_FLOAT
48*f9f8693bSMichael Jones   // case 'f':
49*f9f8693bSMichael Jones   // case 'F':
502e6eccfeSMichael Jones   // return convert_float_decimal(writer, to_conv);
51*f9f8693bSMichael Jones   // case 'e':
52*f9f8693bSMichael Jones   // case 'E':
532e6eccfeSMichael Jones   // return convert_float_dec_exp(writer, to_conv);
546a22b185SMichael Jones   case 'a':
556a22b185SMichael Jones   case 'A':
56*f9f8693bSMichael Jones     return convert_float_hex_exp(writer, to_conv);
57*f9f8693bSMichael Jones     // case 'g':
58*f9f8693bSMichael Jones     // case 'G':
592e6eccfeSMichael Jones     // return convert_float_mixed(writer, to_conv);
60*f9f8693bSMichael Jones #endif // LLVM_LIBC_PRINTF_DISABLE_FLOAT
6188b80139SMichael Jones #ifndef LLVM_LIBC_PRINTF_DISABLE_WRITE_INT
626a22b185SMichael Jones   case 'n':
6388b80139SMichael Jones     return convert_write_int(writer, to_conv);
6488b80139SMichael Jones #endif // LLVM_LIBC_PRINTF_DISABLE_WRITE_INT
656a22b185SMichael Jones   case 'p':
66121c6454SMichael Jones     return convert_pointer(writer, to_conv);
676a22b185SMichael Jones   default:
682e6eccfeSMichael Jones     return writer->write(to_conv.raw_string, to_conv.raw_len);
696a22b185SMichael Jones   }
702e6eccfeSMichael Jones   return -1;
716a22b185SMichael Jones }
726a22b185SMichael Jones 
736a22b185SMichael Jones } // namespace printf_core
746a22b185SMichael Jones } // namespace __llvm_libc
75