1 //===-- Format specifier converter for scanf -------------------*- 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 #ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_CONVERTER_UTILS_H 10 #define LLVM_LIBC_SRC_STDIO_SCANF_CORE_CONVERTER_UTILS_H 11 12 #include "src/__support/ctype_utils.h" 13 #include "src/__support/macros/config.h" 14 #include "src/__support/str_to_float.h" 15 #include "src/stdio/scanf_core/core_structs.h" 16 17 #include <stddef.h> 18 19 namespace LIBC_NAMESPACE_DECL { 20 namespace scanf_core { 21 to_lower(char a)22LIBC_INLINE constexpr char to_lower(char a) { return a | 32; } 23 b36_char_to_int(char input)24LIBC_INLINE constexpr int b36_char_to_int(char input) { 25 if (internal::isdigit(input)) 26 return input - '0'; 27 if (internal::isalpha(input)) 28 return to_lower(input) + 10 - 'a'; 29 return 0; 30 } 31 write_int_with_length(uintmax_t output_val,const FormatSection & to_conv)32LIBC_INLINE void write_int_with_length(uintmax_t output_val, 33 const FormatSection &to_conv) { 34 if ((to_conv.flags & NO_WRITE) != 0) { 35 return; 36 } 37 void *output_ptr = to_conv.output_ptr; 38 // The %p conversion uses this function, and is always void*. 39 if (to_conv.conv_name == 'p') { 40 *reinterpret_cast<void **>(output_ptr) = 41 reinterpret_cast<void *>(output_val); 42 return; 43 } 44 LengthModifier lm = to_conv.length_modifier; 45 switch (lm) { 46 case (LengthModifier::hh): 47 *reinterpret_cast<unsigned char *>(output_ptr) = 48 static_cast<unsigned char>(output_val); 49 break; 50 case (LengthModifier::h): 51 *reinterpret_cast<unsigned short *>(output_ptr) = 52 static_cast<unsigned short>(output_val); 53 break; 54 case (LengthModifier::NONE): 55 *reinterpret_cast<unsigned int *>(output_ptr) = 56 static_cast<unsigned int>(output_val); 57 break; 58 case (LengthModifier::l): 59 *reinterpret_cast<unsigned long *>(output_ptr) = 60 static_cast<unsigned long>(output_val); 61 break; 62 case (LengthModifier::ll): 63 case (LengthModifier::L): 64 *reinterpret_cast<unsigned long long *>(output_ptr) = 65 static_cast<unsigned long long>(output_val); 66 break; 67 case (LengthModifier::j): 68 *reinterpret_cast<uintmax_t *>(output_ptr) = 69 static_cast<uintmax_t>(output_val); 70 break; 71 case (LengthModifier::z): 72 *reinterpret_cast<size_t *>(output_ptr) = static_cast<size_t>(output_val); 73 break; 74 case (LengthModifier::t): 75 *reinterpret_cast<ptrdiff_t *>(output_ptr) = 76 static_cast<ptrdiff_t>(output_val); 77 break; 78 } 79 } 80 write_float_with_length(char * str,const FormatSection & to_conv)81LIBC_INLINE void write_float_with_length(char *str, 82 const FormatSection &to_conv) { 83 if ((to_conv.flags & NO_WRITE) != 0) { 84 return; 85 } 86 87 void *output_ptr = to_conv.output_ptr; 88 89 LengthModifier lm = to_conv.length_modifier; 90 switch (lm) { 91 case (LengthModifier::l): { 92 auto value = internal::strtofloatingpoint<double>(str); 93 *reinterpret_cast<double *>(output_ptr) = value; 94 break; 95 } 96 case (LengthModifier::L): { 97 auto value = internal::strtofloatingpoint<long double>(str); 98 *reinterpret_cast<long double *>(output_ptr) = value; 99 break; 100 } 101 default: { 102 auto value = internal::strtofloatingpoint<float>(str); 103 *reinterpret_cast<float *>(output_ptr) = value; 104 break; 105 } 106 } 107 } 108 109 } // namespace scanf_core 110 } // namespace LIBC_NAMESPACE_DECL 111 112 #endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_CONVERTER_UTILS_H 113