|
| 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLVM_LIBC_SRC_STDIO_STRFTIME_CORE_CONVERTER_H |
| 10 | +#define LLVM_LIBC_SRC_STDIO_STRFTIME_CORE_CONVERTER_H |
| 11 | + |
| 12 | +#include "src/__support/CPP/string.h" |
| 13 | +#include "src/__support/CPP/string_view.h" |
| 14 | +#include "src/__support/integer_to_string.h" |
| 15 | +#include "src/__support/macros/config.h" |
| 16 | +#include "src/math/log10.h" |
| 17 | +#include "src/stdio/printf_core/writer.h" |
| 18 | +#include "src/stdio/strftime_core/core_structs.h" |
| 19 | +#include "src/stdio/strftime_core/time_internal_def.h" |
| 20 | +#include <time.h> |
| 21 | + |
| 22 | +namespace LIBC_NAMESPACE_DECL { |
| 23 | +namespace strftime_core { |
| 24 | + |
| 25 | +namespace details { |
| 26 | + |
| 27 | +LIBC_INLINE cpp::optional<cpp::string_view> |
| 28 | +num_to_strview(uintmax_t num, cpp::span<char> bufref) { |
| 29 | + return IntegerToString<uintmax_t>::format_to(bufref, num); |
| 30 | +} |
| 31 | + |
| 32 | +template<int width> |
| 33 | +LIBC_INLINE int write_num(uintmax_t num, |
| 34 | + printf_core::Writer *writer) { |
| 35 | + cpp::array<char, width> buf; |
| 36 | + return writer->write(*num_to_strview(num, buf)); |
| 37 | +} |
| 38 | + |
| 39 | +template <int width, char padding> |
| 40 | +LIBC_INLINE int write_num_with_padding(uintmax_t num, |
| 41 | + printf_core::Writer *writer) { |
| 42 | + cpp::array<char, width> buf; |
| 43 | + auto digits = log10(num) + 1; |
| 44 | + auto padding_needed = width - digits; |
| 45 | + int char_written = 0; |
| 46 | + for (int _ = 0; _ < padding_needed; _++) { |
| 47 | + char_written += writer->write(padding); |
| 48 | + } |
| 49 | + char_written += writer->write(*num_to_strview(num, buf)); |
| 50 | + return char_written; |
| 51 | +} |
| 52 | + |
| 53 | +} // namespace details |
| 54 | + |
| 55 | +LIBC_INLINE int convert_weekday(printf_core::Writer *writer, |
| 56 | + const FormatSection &to_conv) { |
| 57 | + return writer->write(day_names[to_conv.time->tm_wday]); |
| 58 | +} |
| 59 | + |
| 60 | +LIBC_INLINE int convert_zero_padded_day_of_year(printf_core::Writer *writer, |
| 61 | + const FormatSection &to_conv) { |
| 62 | + return details::write_num_with_padding<3, '0'>(to_conv.time->tm_yday + 1, writer); |
| 63 | +} |
| 64 | + |
| 65 | +LIBC_INLINE int convert_zero_padded_day_of_month(printf_core::Writer *writer, |
| 66 | + const FormatSection &to_conv) { |
| 67 | + return details::write_num_with_padding<2, '0'>(to_conv.time->tm_mday, writer); |
| 68 | +} |
| 69 | + |
| 70 | +LIBC_INLINE int convert_space_padded_day_of_month(printf_core::Writer *writer, |
| 71 | + const FormatSection &to_conv) { |
| 72 | + return details::write_num_with_padding<2, ' '>(to_conv.time->tm_mday, writer); |
| 73 | +} |
| 74 | + |
| 75 | +LIBC_INLINE int convert_decimal_weekday(printf_core::Writer *writer, |
| 76 | + const FormatSection &to_conv) { |
| 77 | + return details::write_num<1>(to_conv.time->tm_wday == 0 ? 7 : to_conv.time->tm_wday, writer); |
| 78 | +} |
| 79 | + |
| 80 | +LIBC_INLINE int convert_decimal_weekday_iso(printf_core::Writer *writer, |
| 81 | + const FormatSection &to_conv) { |
| 82 | + return details::write_num<1>(to_conv.time->tm_wday, writer); |
| 83 | +} |
| 84 | + |
| 85 | +LIBC_INLINE int convert_week_number_sunday(printf_core::Writer *writer, |
| 86 | + const FormatSection &to_conv) { |
| 87 | + int wday = to_conv.time->tm_wday; |
| 88 | + int yday = to_conv.time->tm_yday; |
| 89 | + int week = (yday - wday + 7) / 7; |
| 90 | + return details::write_num_with_padding<2, '0'>(week, writer); |
| 91 | +} |
| 92 | + |
| 93 | +LIBC_INLINE int convert_week_number_monday(printf_core::Writer *writer, |
| 94 | + const FormatSection &to_conv) { |
| 95 | + int wday = (to_conv.time->tm_wday + 6) % 7; |
| 96 | + int yday = to_conv.time->tm_yday; |
| 97 | + int week = (yday - wday + 7) / 7; |
| 98 | + return details::write_num_with_padding<2, '0'>(week, writer); |
| 99 | +} |
| 100 | + |
| 101 | +LIBC_INLINE int convert_full_month(printf_core::Writer *writer, |
| 102 | + const FormatSection &to_conv) { |
| 103 | + return writer->write(month_names[to_conv.time->tm_mon]); |
| 104 | +} |
| 105 | + |
| 106 | +LIBC_INLINE int convert_abbreviated_month(printf_core::Writer *writer, |
| 107 | + const FormatSection &to_conv) { |
| 108 | + return writer->write(abbreviated_month_names[to_conv.time->tm_mon]); |
| 109 | +} |
| 110 | + |
| 111 | +LIBC_INLINE int convert_zero_padded_month(printf_core::Writer *writer, |
| 112 | + const FormatSection &to_conv) { |
| 113 | + return details::write_num_with_padding<2, '0'>(to_conv.time->tm_mon + 1, writer); |
| 114 | +} |
| 115 | + |
| 116 | +LIBC_INLINE int convert_full_year(printf_core::Writer *writer, |
| 117 | + const FormatSection &to_conv) { |
| 118 | + return details::write_num_with_padding<4, '0'>(to_conv.time->tm_year + 1900, writer); |
| 119 | +} |
| 120 | + |
| 121 | +LIBC_INLINE int convert_two_digit_year(printf_core::Writer *writer, |
| 122 | + const FormatSection &to_conv) { |
| 123 | + return details::write_num_with_padding<2, '0'>((to_conv.time->tm_year + 1900) % 100, writer); |
| 124 | +} |
| 125 | + |
| 126 | +LIBC_INLINE int convert_century(printf_core::Writer *writer, |
| 127 | + const FormatSection &to_conv) { |
| 128 | + return details::write_num_with_padding<2, '0'>((to_conv.time->tm_year + 1900) / 100, writer); |
| 129 | +} |
| 130 | + |
| 131 | +LIBC_INLINE int convert_iso_year(printf_core::Writer *writer, |
| 132 | + const FormatSection &to_conv) { |
| 133 | + // TODO |
| 134 | + return details::write_num_with_padding<4, '0'>(to_conv.time->tm_year + 1900, writer); |
| 135 | +} |
| 136 | + |
| 137 | +int convert(printf_core::Writer *writer, const FormatSection &to_conv) { |
| 138 | + if (!to_conv.has_conv) |
| 139 | + return writer->write(to_conv.raw_string); |
| 140 | + switch (to_conv.conv_name) { |
| 141 | + // day of the week |
| 142 | + case 'a': |
| 143 | + return convert_weekday(writer, to_conv); |
| 144 | + case 'w': |
| 145 | + return convert_decimal_weekday(writer, to_conv); |
| 146 | + case 'u': |
| 147 | + return convert_decimal_weekday_iso(writer, to_conv); |
| 148 | + // day of the year/ month |
| 149 | + case 'j': |
| 150 | + return convert_zero_padded_day_of_year(writer, to_conv); |
| 151 | + case 'd': |
| 152 | + return convert_zero_padded_day_of_month(writer, to_conv); |
| 153 | + case 'e': |
| 154 | + return convert_space_padded_day_of_month(writer, to_conv); |
| 155 | + // week |
| 156 | + case 'U': |
| 157 | + return convert_week_number_sunday(writer, to_conv); |
| 158 | + case 'W': |
| 159 | + return convert_week_number_monday(writer, to_conv); |
| 160 | + case 'V': // TODO: ISO 8061 |
| 161 | + // month |
| 162 | + case 'B': |
| 163 | + return convert_full_month(writer, to_conv); |
| 164 | + case 'b': |
| 165 | + case 'h': |
| 166 | + return convert_abbreviated_month(writer, to_conv); |
| 167 | + case 'm': |
| 168 | + return convert_zero_padded_month(writer, to_conv); |
| 169 | + // year |
| 170 | + case 'Y': // Full year (e.g., 2024) |
| 171 | + return convert_full_year(writer, to_conv); |
| 172 | + case 'y': // Two-digit year (e.g., 24 for 2024) |
| 173 | + return convert_two_digit_year(writer, to_conv); |
| 174 | + case 'C': // Century (e.g., 20 for 2024) |
| 175 | + return convert_century(writer, to_conv); |
| 176 | + case 'G': // ISO 8601 year |
| 177 | + // TODO |
| 178 | + return convert_iso_year(writer, to_conv); |
| 179 | + |
| 180 | + } |
| 181 | + return 0; |
| 182 | +} |
| 183 | + |
| 184 | +} // namespace strftime_core |
| 185 | +} // namespace LIBC_NAMESPACE_DECL |
| 186 | +#endif |
0 commit comments