[libc-commits] [libc] [libc] Implement strftime (PR #111305)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Tue Nov 12 11:29:09 PST 2024
================
@@ -0,0 +1,208 @@
+//===-- Format specifier converter for printf -------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See htto_conv.times://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDIO_STRFTIME_CORE_NUM_CONVERTER_H
+#define LLVM_LIBC_SRC_STDIO_STRFTIME_CORE_NUM_CONVERTER_H
+
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/integer_to_string.h"
+#include "src/__support/macros/config.h"
+#include "src/stdio/printf_core/writer.h"
+#include "src/time/strftime_core/core_structs.h"
+#include "src/time/strftime_core/time_internal_def.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 <typename T> int count_digits(T num) {
+ if (num == 0)
+ return 1;
+ int digits = 0;
+ while (num > 0) {
+ num /= 10;
+ digits++;
+ }
+ return digits;
+}
+
+LIBC_INLINE int write_num_with_padding(int width, char padding, uintmax_t num,
+ printf_core::Writer *writer) {
+ cpp::array<char, IntegerToString<uintmax_t>::buffer_size()> buf;
+ int digits = count_digits(num);
+ int padding_needed = width - digits;
+
+ for (int _ = 0; _ < padding_needed; _++) {
----------------
michaelrj-google wrote:
instead of this you should use the `write(char new_char, size_t length)` function that printf writers have for specifically this purpose. See: https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/printf_core/writer.h#L134
https://github.com/llvm/llvm-project/pull/111305
More information about the libc-commits
mailing list