[libc-commits] [libc] [WIP] Basic structures for strftime (PR #111305)
via libc-commits
libc-commits at lists.llvm.org
Sun Oct 6 10:20:29 PDT 2024
================
@@ -0,0 +1,186 @@
+//===-- Format specifier converter for printf -------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDIO_STRFTIME_CORE_CONVERTER_H
+#define LLVM_LIBC_SRC_STDIO_STRFTIME_CORE_CONVERTER_H
+
+#include "src/__support/CPP/string.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/integer_to_string.h"
+#include "src/__support/macros/config.h"
+#include "src/math/log10.h"
+#include "src/stdio/printf_core/writer.h"
+#include "src/stdio/strftime_core/core_structs.h"
+#include "src/stdio/strftime_core/time_internal_def.h"
+#include <time.h>
+
+namespace LIBC_NAMESPACE_DECL {
+namespace strftime_core {
+
+namespace details {
+
+LIBC_INLINE cpp::optional<cpp::string_view>
+num_to_strview(uintmax_t num, cpp::span<char> bufref) {
+ return IntegerToString<uintmax_t>::format_to(bufref, num);
+}
+
+template<int width>
+LIBC_INLINE int write_num(uintmax_t num,
+ printf_core::Writer *writer) {
+ cpp::array<char, width> buf;
+ return writer->write(*num_to_strview(num, buf));
+}
+
+template <int width, char padding>
+LIBC_INLINE int write_num_with_padding(uintmax_t num,
+ printf_core::Writer *writer) {
+ cpp::array<char, width> buf;
+ auto digits = log10(num) + 1;
+ auto padding_needed = width - digits;
+ int char_written = 0;
+ for (int _ = 0; _ < padding_needed; _++) {
+ char_written += writer->write(padding);
+ }
+ char_written += writer->write(*num_to_strview(num, buf));
+ return char_written;
+}
+
+} // namespace details
+
+LIBC_INLINE int convert_weekday(printf_core::Writer *writer,
+ const FormatSection &to_conv) {
+ return writer->write(day_names[to_conv.time->tm_wday]);
+}
+
+LIBC_INLINE int convert_zero_padded_day_of_year(printf_core::Writer *writer,
+ const FormatSection &to_conv) {
+ return details::write_num_with_padding<3, '0'>(to_conv.time->tm_yday + 1, writer);
+}
+
+LIBC_INLINE int convert_zero_padded_day_of_month(printf_core::Writer *writer,
+ const FormatSection &to_conv) {
+ return details::write_num_with_padding<2, '0'>(to_conv.time->tm_mday, writer);
+}
+
+LIBC_INLINE int convert_space_padded_day_of_month(printf_core::Writer *writer,
+ const FormatSection &to_conv) {
+ return details::write_num_with_padding<2, ' '>(to_conv.time->tm_mday, writer);
+}
+
+LIBC_INLINE int convert_decimal_weekday(printf_core::Writer *writer,
+ const FormatSection &to_conv) {
+ return details::write_num<1>(to_conv.time->tm_wday == 0 ? 7 : to_conv.time->tm_wday, writer);
----------------
graphite-app[bot] wrote:
The conversion for `%w` should return 0 for Sunday, not 7. The condition can be simplified to:
```cpp
return details::write_num<1>(to_conv.time->tm_wday, writer);
```
This change aligns with the POSIX specification for `strftime`, where Sunday is represented by 0.
*Spotted by [Graphite Reviewer](https://app.graphite.dev/graphite-reviewer/?org=llvm&ref=ai-review-comment)*<i class='graphite__hidden'><br /><br />Is this helpful? React 👍 or 👎 to let us know.</i>
https://github.com/llvm/llvm-project/pull/111305
More information about the libc-commits
mailing list