[libc-commits] [libc] [WIP] Basic structures for strftime (PR #111305)
via libc-commits
libc-commits at lists.llvm.org
Sun Oct 6 10:23:18 PDT 2024
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 54a49658990e827173f3a3198331df7cbe50b0c0 2f0647568467e3ac66b550aefca931b6e89d5e78 --extensions cpp,h -- libc/src/stdio/strftime_core/converter.cpp libc/src/stdio/strftime_core/converter.h libc/src/stdio/strftime_core/core_structs.h libc/src/stdio/strftime_core/parser.h libc/src/stdio/strftime_core/time_internal_def.h libc/test/src/stdio/strftime_core/converter_test.cpp libc/test/src/stdio/strftime_core/parser_test.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libc/src/stdio/strftime_core/converter.cpp b/libc/src/stdio/strftime_core/converter.cpp
index 9046f07848..d8451a246b 100644
--- a/libc/src/stdio/strftime_core/converter.cpp
+++ b/libc/src/stdio/strftime_core/converter.cpp
@@ -29,9 +29,8 @@ 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) {
+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));
}
@@ -42,7 +41,7 @@ LIBC_INLINE int write_num_with_padding(uintmax_t num,
cpp::array<char, width> buf;
auto digits = log10(num) + 1;
auto padding_needed = width - digits;
- int char_written = 0;
+ int char_written = 0;
for (int _ = 0; _ < padding_needed; _++) {
char_written += writer->write(padding);
}
@@ -53,32 +52,35 @@ LIBC_INLINE int write_num_with_padding(uintmax_t num,
} // namespace details
LIBC_INLINE int convert_weekday(printf_core::Writer *writer,
- const FormatSection &to_conv) {
+ 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);
+ 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) {
+ 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) {
+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);
+ const FormatSection &to_conv) {
+ return details::write_num<1>(
+ to_conv.time->tm_wday == 0 ? 7 : to_conv.time->tm_wday, writer);
}
LIBC_INLINE int convert_decimal_weekday_iso(printf_core::Writer *writer,
- const FormatSection &to_conv) {
+ const FormatSection &to_conv) {
return details::write_num<1>(to_conv.time->tm_wday, writer);
}
@@ -99,7 +101,7 @@ LIBC_INLINE int convert_week_number_monday(printf_core::Writer *writer,
}
LIBC_INLINE int convert_full_month(printf_core::Writer *writer,
- const FormatSection &to_conv) {
+ const FormatSection &to_conv) {
return writer->write(month_names[to_conv.time->tm_mon]);
}
@@ -110,28 +112,33 @@ LIBC_INLINE int convert_abbreviated_month(printf_core::Writer *writer,
LIBC_INLINE int convert_zero_padded_month(printf_core::Writer *writer,
const FormatSection &to_conv) {
- return details::write_num_with_padding<2, '0'>(to_conv.time->tm_mon + 1, writer);
+ return details::write_num_with_padding<2, '0'>(to_conv.time->tm_mon + 1,
+ writer);
}
LIBC_INLINE int convert_full_year(printf_core::Writer *writer,
const FormatSection &to_conv) {
- return details::write_num_with_padding<4, '0'>(to_conv.time->tm_year + 1900, writer);
+ return details::write_num_with_padding<4, '0'>(to_conv.time->tm_year + 1900,
+ writer);
}
LIBC_INLINE int convert_two_digit_year(printf_core::Writer *writer,
const FormatSection &to_conv) {
- return details::write_num_with_padding<2, '0'>((to_conv.time->tm_year + 1900) % 100, writer);
+ return details::write_num_with_padding<2, '0'>(
+ (to_conv.time->tm_year + 1900) % 100, writer);
}
LIBC_INLINE int convert_century(printf_core::Writer *writer,
const FormatSection &to_conv) {
- return details::write_num_with_padding<2, '0'>((to_conv.time->tm_year + 1900) / 100, writer);
+ return details::write_num_with_padding<2, '0'>(
+ (to_conv.time->tm_year + 1900) / 100, writer);
}
LIBC_INLINE int convert_iso_year(printf_core::Writer *writer,
const FormatSection &to_conv) {
// TODO
- return details::write_num_with_padding<4, '0'>(to_conv.time->tm_year + 1900, writer);
+ return details::write_num_with_padding<4, '0'>(to_conv.time->tm_year + 1900,
+ writer);
}
int convert(printf_core::Writer *writer, const FormatSection &to_conv) {
@@ -167,16 +174,15 @@ int convert(printf_core::Writer *writer, const FormatSection &to_conv) {
case 'm':
return convert_zero_padded_month(writer, to_conv);
// year
- case 'Y': // Full year (e.g., 2024)
+ case 'Y': // Full year (e.g., 2024)
return convert_full_year(writer, to_conv);
- case 'y': // Two-digit year (e.g., 24 for 2024)
+ case 'y': // Two-digit year (e.g., 24 for 2024)
return convert_two_digit_year(writer, to_conv);
- case 'C': // Century (e.g., 20 for 2024)
+ case 'C': // Century (e.g., 20 for 2024)
return convert_century(writer, to_conv);
- case 'G': // ISO 8601 year
+ case 'G': // ISO 8601 year
// TODO
return convert_iso_year(writer, to_conv);
-
}
return 0;
}
diff --git a/libc/src/stdio/strftime_core/converter.h b/libc/src/stdio/strftime_core/converter.h
index f192046266..57801b8e8e 100644
--- a/libc/src/stdio/strftime_core/converter.h
+++ b/libc/src/stdio/strftime_core/converter.h
@@ -10,8 +10,8 @@
#define LLVM_LIBC_SRC_STDIO_STRFTIME_CORE_CONVERTER_H
#include "src/__support/macros/config.h"
-#include "src/stdio/strftime_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
+#include "src/stdio/strftime_core/core_structs.h"
#include <stddef.h>
diff --git a/libc/src/stdio/strftime_core/core_structs.h b/libc/src/stdio/strftime_core/core_structs.h
index f8176e6d02..404902bcfb 100644
--- a/libc/src/stdio/strftime_core/core_structs.h
+++ b/libc/src/stdio/strftime_core/core_structs.h
@@ -22,23 +22,23 @@ namespace LIBC_NAMESPACE_DECL {
namespace strftime_core {
struct tm {
- int tm_sec; /* seconds after the minute [0-60] */
- int tm_min; /* minutes after the hour [0-59] */
- int tm_hour; /* hours since midnight [0-23] */
- int tm_mday; /* day of the month [1-31] */
- int tm_mon; /* months since January [0-11] */
- int tm_year; /* years since 1900 */
- int tm_wday; /* days since Sunday [0-6] */
- int tm_yday; /* days since January 1 [0-365] */
- int tm_isdst; /* Daylight Savings Time flag */
- long tm_gmtoff; /* offset from UTC in seconds */
- char *tm_zone; /* timezone abbreviation */
+ int tm_sec; /* seconds after the minute [0-60] */
+ int tm_min; /* minutes after the hour [0-59] */
+ int tm_hour; /* hours since midnight [0-23] */
+ int tm_mday; /* day of the month [1-31] */
+ int tm_mon; /* months since January [0-11] */
+ int tm_year; /* years since 1900 */
+ int tm_wday; /* days since Sunday [0-6] */
+ int tm_yday; /* days since January 1 [0-365] */
+ int tm_isdst; /* Daylight Savings Time flag */
+ long tm_gmtoff; /* offset from UTC in seconds */
+ char *tm_zone; /* timezone abbreviation */
};
struct FormatSection {
- bool has_conv {false};
- bool isE {false};
- bool isO {false};
+ bool has_conv{false};
+ bool isE{false};
+ bool isO{false};
cpp::string_view raw_string;
char conv_name;
const struct tm *time;
diff --git a/libc/src/stdio/strftime_core/parser.h b/libc/src/stdio/strftime_core/parser.h
index 37a1e9019a..046f5b1095 100644
--- a/libc/src/stdio/strftime_core/parser.h
+++ b/libc/src/stdio/strftime_core/parser.h
@@ -109,15 +109,14 @@ public:
section.has_conv = false;
break;
}
- // If the end of the format section is on the '\0'. This means we need to
- // not advance the cur_pos.
- if (str[cur_pos] != '\0')
- ++cur_pos;
+ // If the end of the format section is on the '\0'. This means we need to
+ // not advance the cur_pos.
+ if (str[cur_pos] != '\0')
+ ++cur_pos;
}
section.raw_string = {str + starting_pos, cur_pos - starting_pos};
return section;
-}
-
+ }
};
} // namespace strftime_core
diff --git a/libc/src/stdio/strftime_core/time_internal_def.h b/libc/src/stdio/strftime_core/time_internal_def.h
index e26035c5a3..731d17fb51 100644
--- a/libc/src/stdio/strftime_core/time_internal_def.h
+++ b/libc/src/stdio/strftime_core/time_internal_def.h
@@ -17,24 +17,19 @@ static constexpr int NUM_MONTHS = 12;
static constexpr int YEAR_BASE = 1900;
static constexpr cpp::array<cpp::string_view, NUM_DAYS> day_names = {
- "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
-};
+ "Sunday", "Monday", "Tuesday", "Wednesday",
+ "Thursday", "Friday", "Saturday"};
static constexpr cpp::array<cpp::string_view, NUM_MONTHS> month_names = {
- "January", "February", "March", "April", "May", "June",
- "July", "August", "September", "October", "November", "December"
-};
+ "January", "February", "March", "April", "May", "June",
+ "July", "August", "September", "October", "November", "December"};
-static constexpr cpp::array<cpp::string_view, NUM_DAYS> abbreviated_day_names = {
- "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
-};
+static constexpr cpp::array<cpp::string_view, NUM_DAYS> abbreviated_day_names =
+ {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
-static constexpr cpp::array<cpp::string_view, NUM_MONTHS> abbreviated_month_names = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-};
+static constexpr cpp::array<cpp::string_view, NUM_MONTHS>
+ abbreviated_month_names = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
-
-
-
-}}
+} // namespace strftime_core
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/stdio/strftime_core/converter_test.cpp b/libc/test/src/stdio/strftime_core/converter_test.cpp
index b4aa277afb..7110702431 100644
--- a/libc/test/src/stdio/strftime_core/converter_test.cpp
+++ b/libc/test/src/stdio/strftime_core/converter_test.cpp
@@ -6,9 +6,9 @@
//
//===----------------------------------------------------------------------===//
+#include "src/stdio/printf_core/writer.h"
#include "src/stdio/strftime_core/converter.h"
#include "src/stdio/strftime_core/core_structs.h"
-#include "src/stdio/printf_core/writer.h"
#include "test/UnitTest/Test.h"
@@ -70,7 +70,7 @@ TEST_F(LlvmLibcStrftimeConverterTest, WeekdayConversion) {
TEST_F(LlvmLibcStrftimeConverterTest, AbbreviatedMonthNameConversion) {
LIBC_NAMESPACE::strftime_core::FormatSection simple_conv;
LIBC_NAMESPACE::strftime_core::tm time;
- time.tm_mon = 4; // May
+ time.tm_mon = 4; // May
simple_conv.has_conv = true;
simple_conv.raw_string = "%b";
simple_conv.conv_name = 'b';
@@ -87,7 +87,7 @@ TEST_F(LlvmLibcStrftimeConverterTest, AbbreviatedMonthNameConversion) {
TEST_F(LlvmLibcStrftimeConverterTest, CenturyConversion) {
LIBC_NAMESPACE::strftime_core::FormatSection simple_conv;
LIBC_NAMESPACE::strftime_core::tm time;
- time.tm_year = 122; // Represents 2022
+ time.tm_year = 122; // Represents 2022
simple_conv.has_conv = true;
simple_conv.raw_string = "%C";
simple_conv.conv_name = 'C';
@@ -138,7 +138,7 @@ TEST_F(LlvmLibcStrftimeConverterTest, DayOfMonthSpacePaddedConversion) {
TEST_F(LlvmLibcStrftimeConverterTest, FullMonthNameConversion) {
LIBC_NAMESPACE::strftime_core::FormatSection simple_conv;
LIBC_NAMESPACE::strftime_core::tm time;
- time.tm_mon = 4; // May
+ time.tm_mon = 4; // May
simple_conv.has_conv = true;
simple_conv.raw_string = "%B";
simple_conv.conv_name = 'B';
@@ -206,7 +206,7 @@ TEST_F(LlvmLibcStrftimeConverterTest, MinuteConversion) {
TEST_F(LlvmLibcStrftimeConverterTest, AMPMConversion) {
LIBC_NAMESPACE::strftime_core::FormatSection simple_conv;
LIBC_NAMESPACE::strftime_core::tm time;
- time.tm_hour = 14; // 2 PM
+ time.tm_hour = 14; // 2 PM
simple_conv.has_conv = true;
simple_conv.raw_string = "%p";
simple_conv.conv_name = 'p';
@@ -240,7 +240,7 @@ TEST_F(LlvmLibcStrftimeConverterTest, SecondsConversion) {
TEST_F(LlvmLibcStrftimeConverterTest, FullYearConversion) {
LIBC_NAMESPACE::strftime_core::FormatSection simple_conv;
LIBC_NAMESPACE::strftime_core::tm time;
- time.tm_year = 122; // Represents 2022 (1900 + 122)
+ time.tm_year = 122; // Represents 2022 (1900 + 122)
simple_conv.has_conv = true;
simple_conv.raw_string = "%Y";
simple_conv.conv_name = 'Y';
@@ -257,7 +257,7 @@ TEST_F(LlvmLibcStrftimeConverterTest, FullYearConversion) {
TEST_F(LlvmLibcStrftimeConverterTest, TwoDigitYearConversion) {
LIBC_NAMESPACE::strftime_core::FormatSection simple_conv;
LIBC_NAMESPACE::strftime_core::tm time;
- time.tm_year = 122; // Represents 2022 (1900 + 122)
+ time.tm_year = 122; // Represents 2022 (1900 + 122)
simple_conv.has_conv = true;
simple_conv.raw_string = "%y";
simple_conv.conv_name = 'y';
``````````
</details>
https://github.com/llvm/llvm-project/pull/111305
More information about the libc-commits
mailing list