[libc-commits] [libc] [libc] Support ls in printf (PR #178841)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Wed Feb 4 14:47:12 PST 2026
================
@@ -3582,4 +3582,94 @@ TEST(LlvmLibcSprintfTest, WideCharConversion) {
-1);
ASSERT_ERRNO_EQ(EILSEQ);
}
+
+TEST(LlvmLibcSprintfTest, WideCharStringConversion) {
+ char buff[64];
+ int written;
+
+ // Basic test.
+ written = LIBC_NAMESPACE::sprintf(buff, "%ls", L"Hello, World!");
+ EXPECT_EQ(written, 13);
+ ASSERT_STREQ_LEN(written, buff, "Hello, World!");
+
+ // Left justified.
+ written = LIBC_NAMESPACE::sprintf(buff, "%-10ls", L"Hello, World!");
+ EXPECT_EQ(written, 13);
+ ASSERT_STREQ_LEN(written, buff, "Hello, World!");
+
+ // Precision test.
+ written = LIBC_NAMESPACE::sprintf(buff, "%.5ls", L"Hello, World!");
+ EXPECT_EQ(written, 5);
+ ASSERT_STREQ_LEN(written, buff, "Hello");
+
+ // Test ¢ which is 2 bytes multibyte character.
+ written = LIBC_NAMESPACE::sprintf(buff, "%ls", L"¢");
+ EXPECT_EQ(written, 2);
+ ASSERT_STREQ_LEN(written, buff, "¢");
+
+ // Test € which is 3 bytes multibyte character.
+ written = LIBC_NAMESPACE::sprintf(buff, "%ls", L"€");
+ EXPECT_EQ(written, 3);
+ ASSERT_STREQ_LEN(written, buff, "€");
+
+ // Test 😀 which is 4 bytes multibyte character
+ written = LIBC_NAMESPACE::sprintf(buff, "%ls", L"😀");
+ EXPECT_EQ(written, 4);
+ ASSERT_STREQ_LEN(written, buff, "😀");
+
+ // Test string with multiple multibyte characters.
+ written = LIBC_NAMESPACE::sprintf(buff, "%ls", L"¢€😀");
+ EXPECT_EQ(written, 9);
+ ASSERT_STREQ_LEN(written, buff, "¢€😀");
+
+ // Width with multibyte characters.
+ written = LIBC_NAMESPACE::sprintf(buff, "%2ls", L"¢€😀");
+ EXPECT_EQ(written, 9);
+ ASSERT_STREQ_LEN(written, buff, "¢€😀");
+
+ // Width with multibyte characters and padding.
+ written = LIBC_NAMESPACE::sprintf(buff, "%12ls", L"¢€😀");
+ EXPECT_EQ(written, 12);
+ ASSERT_STREQ_LEN(written, buff, " ¢€😀");
+
+ // Precision with multibyte characters.
+ written = LIBC_NAMESPACE::sprintf(buff, "%.4ls", L"¢€😀");
----------------
michaelrj-google wrote:
make sure you have precision tests with all four lengths of character. Ideally there would be one ensuring the smiley shows up as the leading character when the precision is 4 or 5 but not when the precision is 3.
https://github.com/llvm/llvm-project/pull/178841
More information about the libc-commits
mailing list