Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libc/src/time/strftime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ LLVM_LIBC_FUNCTION(size_t, strftime,
printf_core::WriteMode::FILL_BUFF_AND_DROP_OVERFLOW>::value>
wb(buffer, (buffsz > 0 ? buffsz - 1 : 0));
printf_core::Writer writer(wb);
int ret = strftime_core::strftime_main(&writer, format, timeptr);
auto ret = strftime_core::strftime_main(&writer, format, timeptr);
if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer.
wb.buff[wb.buff_cur] = '\0';
return (ret < 0 || static_cast<size_t>(ret) >= buffsz) ? 0 : ret;
return (!ret.has_value() || ret.value() >= buffsz) ? 0 : ret.value();
}

} // namespace LIBC_NAMESPACE_DECL
1 change: 1 addition & 0 deletions libc/src/time/strftime_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ add_header_library(
.core_structs
.parser
.converter
libc.src.__support.error_or
libc.src.stdio.printf_core.writer
libc.hdr.types.struct_tm
)
10 changes: 5 additions & 5 deletions libc/src/time/strftime_core/strftime_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_LIBC_SRC_STDIO_STRFTIME_CORE_STRFTIME_MAIN_H

#include "hdr/types/struct_tm.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/writer.h"
#include "src/time/strftime_core/converter.h"
Expand All @@ -20,8 +21,8 @@ namespace LIBC_NAMESPACE_DECL {
namespace strftime_core {

template <printf_core::WriteMode write_mode>
int strftime_main(printf_core::Writer<write_mode> *writer,
const char *__restrict str, const tm *timeptr) {
ErrorOr<size_t> strftime_main(printf_core::Writer<write_mode> *writer,
const char *__restrict str, const tm *timeptr) {
Parser parser(str);
int result = 0;
for (strftime_core::FormatSection cur_section = parser.get_next_section();
Expand All @@ -33,11 +34,10 @@ int strftime_main(printf_core::Writer<write_mode> *writer,
result = writer->write(cur_section.raw_string);

if (result < 0)
return result;
return Error(-result);
}

// TODO: Use ErrorOr<size_t>
return static_cast<int>(writer->get_chars_written());
return writer->get_chars_written();
}

} // namespace strftime_core
Expand Down
4 changes: 2 additions & 2 deletions libc/src/time/strftime_l.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ LLVM_LIBC_FUNCTION(size_t, strftime_l,
printf_core::WriteMode::FILL_BUFF_AND_DROP_OVERFLOW>::value>
wb(buffer, (buffsz > 0 ? buffsz - 1 : 0));
printf_core::Writer writer(wb);
int ret = strftime_core::strftime_main(&writer, format, timeptr);
auto ret = strftime_core::strftime_main(&writer, format, timeptr);
if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer.
wb.buff[wb.buff_cur] = '\0';
return (ret < 0 || static_cast<size_t>(ret) >= buffsz) ? 0 : ret;
return (!ret.has_value() || ret.value() >= buffsz) ? 0 : ret.value();
}

} // namespace LIBC_NAMESPACE_DECL
Loading