Skip to content

Commit 9c4f8fd

Browse files
committed
Refactor strftime_main to return ErrorOr<size_t>
1 parent 77b9301 commit 9c4f8fd

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

libc/src/time/strftime.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ LLVM_LIBC_FUNCTION(size_t, strftime,
2323
printf_core::WriteMode::FILL_BUFF_AND_DROP_OVERFLOW>::value>
2424
wb(buffer, (buffsz > 0 ? buffsz - 1 : 0));
2525
printf_core::Writer writer(wb);
26-
int ret = strftime_core::strftime_main(&writer, format, timeptr);
26+
auto ret = strftime_core::strftime_main(&writer, format, timeptr);
2727
if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer.
2828
wb.buff[wb.buff_cur] = '\0';
29-
return (ret < 0 || static_cast<size_t>(ret) >= buffsz) ? 0 : ret;
29+
return (!ret.has_value() || ret.value() >= buffsz) ? 0 : ret.value();
30+
;
3031
}
3132

3233
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/strftime_core/strftime_main.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_LIBC_SRC_STDIO_STRFTIME_CORE_STRFTIME_MAIN_H
1111

1212
#include "hdr/types/struct_tm.h"
13+
#include "src/__support/error_or.h"
1314
#include "src/__support/macros/config.h"
1415
#include "src/stdio/printf_core/writer.h"
1516
#include "src/time/strftime_core/converter.h"
@@ -20,8 +21,8 @@ namespace LIBC_NAMESPACE_DECL {
2021
namespace strftime_core {
2122

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

3536
if (result < 0)
36-
return result;
37+
return Error(-result);
3738
}
3839

39-
// TODO: Use ErrorOr<size_t>
40-
return static_cast<int>(writer->get_chars_written());
40+
return writer->get_chars_written();
4141
}
4242

4343
} // namespace strftime_core

libc/src/time/strftime_l.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ LLVM_LIBC_FUNCTION(size_t, strftime_l,
2626
printf_core::WriteMode::FILL_BUFF_AND_DROP_OVERFLOW>::value>
2727
wb(buffer, (buffsz > 0 ? buffsz - 1 : 0));
2828
printf_core::Writer writer(wb);
29-
int ret = strftime_core::strftime_main(&writer, format, timeptr);
29+
auto ret = strftime_core::strftime_main(&writer, format, timeptr);
3030
if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer.
3131
wb.buff[wb.buff_cur] = '\0';
32-
return (ret < 0 || static_cast<size_t>(ret) >= buffsz) ? 0 : ret;
32+
return (!ret.has_value() || ret.value() >= buffsz) ? 0 : ret.value();
3333
}
3434

3535
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)