-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[libc] Support %lc in printf #169983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[libc] Support %lc in printf #169983
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,11 @@ | |
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "hdr/types/wchar_t.h" | ||
| #include "hdr/wchar_macros.h" | ||
| #include "src/stdio/printf_core/converter.h" | ||
| #include "src/stdio/printf_core/core_structs.h" | ||
| #include "src/stdio/printf_core/writer.h" | ||
|
|
||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| class LlvmLibcPrintfConverterTest : public LIBC_NAMESPACE::testing::Test { | ||
|
|
@@ -255,3 +256,83 @@ TEST_F(LlvmLibcPrintfConverterTest, OctConversion) { | |
| ASSERT_STREQ(str, "1234"); | ||
| ASSERT_EQ(writer.get_chars_written(), size_t{4}); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcPrintfConverterTest, WideCharConversion) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For simplicity most of the functionality testing of the printf conversions is actually done in |
||
| LIBC_NAMESPACE::printf_core::FormatSection section; | ||
| section.has_conv = true; | ||
| section.raw_string = "%lc"; | ||
| section.conv_name = 'c'; | ||
| section.length_modifier = LIBC_NAMESPACE::printf_core::LengthModifier::l; | ||
| section.conv_val_raw = static_cast<wchar_t>(L'€'); | ||
|
|
||
| LIBC_NAMESPACE::printf_core::convert(&writer, section); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should also consider adding end-to-end test of this functionality - e.g. a test of For example, the Parser should accept the arguments of type |
||
|
|
||
| wb.buff[wb.buff_cur] = '\0'; | ||
|
|
||
| ASSERT_STREQ(str, "€"); | ||
| ASSERT_EQ(writer.get_chars_written(), size_t{3}); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcPrintfConverterTest, WideCharConversionLeftJustified) { | ||
| LIBC_NAMESPACE::printf_core::FormatSection left_justified_conv; | ||
| left_justified_conv.has_conv = true; | ||
| left_justified_conv.raw_string = "%-4lc"; | ||
| left_justified_conv.conv_name = 'c'; | ||
| left_justified_conv.length_modifier = | ||
| LIBC_NAMESPACE::printf_core::LengthModifier::l; | ||
| left_justified_conv.flags = | ||
| LIBC_NAMESPACE::printf_core::FormatFlags::LEFT_JUSTIFIED; | ||
| left_justified_conv.min_width = 4; | ||
| left_justified_conv.conv_val_raw = static_cast<wchar_t>(L'€'); | ||
|
|
||
| LIBC_NAMESPACE::printf_core::convert(&writer, left_justified_conv); | ||
| wb.buff[wb.buff_cur] = '\0'; | ||
|
|
||
| ASSERT_STREQ(str, "€ "); | ||
| ASSERT_EQ(writer.get_chars_written(), size_t{6}); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcPrintfConverterTest, WideCharConversionRightJustified) { | ||
| LIBC_NAMESPACE::printf_core::FormatSection right_justified_conv; | ||
| right_justified_conv.has_conv = true; | ||
| right_justified_conv.raw_string = "%4lc"; | ||
| right_justified_conv.conv_name = 'c'; | ||
| right_justified_conv.length_modifier = | ||
| LIBC_NAMESPACE::printf_core::LengthModifier::l; | ||
| right_justified_conv.min_width = 4; | ||
| right_justified_conv.conv_val_raw = static_cast<wchar_t>(L'€'); | ||
|
|
||
| LIBC_NAMESPACE::printf_core::convert(&writer, right_justified_conv); | ||
| wb.buff[wb.buff_cur] = '\0'; | ||
|
|
||
| ASSERT_STREQ(str, " €"); | ||
| ASSERT_EQ(writer.get_chars_written(), size_t{6}); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcPrintfConverterTest, WideCharConversionInvalid) { | ||
| LIBC_NAMESPACE::printf_core::FormatSection section; | ||
| section.has_conv = true; | ||
| section.raw_string = "%lc"; | ||
| section.conv_name = 'c'; | ||
| section.length_modifier = LIBC_NAMESPACE::printf_core::LengthModifier::l; | ||
| // An invalid wide character. | ||
| section.conv_val_raw = static_cast<wchar_t>(0xFFFFFFFF); | ||
|
|
||
| int ret = LIBC_NAMESPACE::printf_core::convert(&writer, section); | ||
|
|
||
| ASSERT_EQ(ret, -1); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcPrintfConverterTest, WideCharWEOFConversion) { | ||
| LIBC_NAMESPACE::printf_core::FormatSection section; | ||
| section.has_conv = true; | ||
| section.raw_string = "%lc"; | ||
| section.conv_name = 'c'; | ||
| section.length_modifier = LIBC_NAMESPACE::printf_core::LengthModifier::l; | ||
| // WEOF value. | ||
| section.conv_val_raw = static_cast<wchar_t>(WEOF); | ||
|
|
||
| int ret = LIBC_NAMESPACE::printf_core::convert(&writer, section); | ||
|
|
||
| ASSERT_EQ(ret, -1); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks good overall, but we'll want to have a compile flag to disable wide characters to save code size. Could you add something similar to
LIBC_COPT_PRINTF_DISABLE_STRERRORthat just disables the wide character portion? You'll also need to guard the includes at the top of this file and the tests.