1 //===-- Format specifier converter implmentation 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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/stdio/printf_core/converter.h"
10 
11 #include "src/stdio/printf_core/core_structs.h"
12 #include "src/stdio/printf_core/writer.h"
13 
14 // This option allows for replacing all of the conversion functions with custom
15 // replacements. This allows conversions to be replaced at compile time.
16 #ifndef LLVM_LIBC_PRINTF_CONV_ATLAS
17 #include "src/stdio/printf_core/converter_atlas.h"
18 #else
19 #include LLVM_LIBC_PRINTF_CONV_ATLAS
20 #endif
21 
22 #include <stddef.h>
23 
24 namespace __llvm_libc {
25 namespace printf_core {
26 
27 int convert(Writer *writer, const FormatSection &to_conv) {
28   if (!to_conv.has_conv)
29     return writer->write(to_conv.raw_string, to_conv.raw_len);
30 
31   switch (to_conv.conv_name) {
32   case '%':
33     return writer->write("%", 1);
34   case 'c':
35     return convert_char(writer, to_conv);
36   case 's':
37     return convert_string(writer, to_conv);
38   case 'd':
39   case 'i':
40   case 'u':
41     return convert_int(writer, to_conv);
42   case 'o':
43     return convert_oct(writer, to_conv);
44   case 'x':
45   case 'X':
46     return convert_hex(writer, to_conv);
47   // TODO(michaelrj): add a flag to disable float point values here
48   case 'f':
49   case 'F':
50     // return convert_float_decimal(writer, to_conv);
51   case 'e':
52   case 'E':
53     // return convert_float_dec_exp(writer, to_conv);
54   case 'a':
55   case 'A':
56     // return convert_float_hex_exp(writer, to_conv);
57   case 'g':
58   case 'G':
59     // return convert_float_mixed(writer, to_conv);
60 #ifndef LLVM_LIBC_PRINTF_DISABLE_WRITE_INT
61   case 'n':
62     return convert_write_int(writer, to_conv);
63 #endif // LLVM_LIBC_PRINTF_DISABLE_WRITE_INT
64   case 'p':
65     return convert_pointer(writer, to_conv);
66   default:
67     return writer->write(to_conv.raw_string, to_conv.raw_len);
68   }
69   return -1;
70 }
71 
72 } // namespace printf_core
73 } // namespace __llvm_libc
74