Skip to content

[lib++][print] Don't pad the ostream output. #128354

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

Merged
merged 1 commit into from
Feb 26, 2025
Merged
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
14 changes: 2 additions & 12 deletions libcxx/include/__ostream/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,11 @@ __vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool _
if (__write_nl)
__o += '\n';

const char* __str = __o.data();
size_t __len = __o.size();

# if _LIBCPP_HAS_EXCEPTIONS
try {
# endif // _LIBCPP_HAS_EXCEPTIONS
typedef ostreambuf_iterator<char> _Ip;
if (std::__pad_and_output(
_Ip(__os),
__str,
(__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
__str + __len,
__os,
__os.fill())
.failed())
if (auto __rdbuf = __os.rdbuf();
!__rdbuf || __rdbuf->sputn(__o.data(), __o.size()) != static_cast<streamsize>(__o.size()))
__os.setstate(ios_base::badbit | ios_base::failbit);

# if _LIBCPP_HAS_EXCEPTIONS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ static void test_write_failure() {
assert(os.fail());
}

// Test the formatting does no padding.
static void test_stream_formatting() {
std::stringstream sstr;
auto test = [&]<class... Args>(std::string_view expected, test_format_string<char, Args...> fmt, Args&&... args) {
Expand All @@ -148,37 +149,20 @@ static void test_stream_formatting() {
test("hello", "{}", "hello");

sstr.width(10);
test(" hello", "{}", "hello");
test("hello", "{}", "hello");
assert(sstr.width() == 10);

sstr.fill('+');

sstr.width(10);
test("+++++hello", "{}", "hello");
test("hello", "{}", "hello");
assert(sstr.width() == 10);

// *** Test embedded NUL character ***
using namespace std::literals;
sstr.width(15);
test("++++hello\0world"sv, "hello{}{}", '\0', "world");

// *** Test Unicode ***
// Streams count code units not code points
// 2-byte code points
sstr.width(5);
test("+++\u00a1", "{}", "\u00a1"); // INVERTED EXCLAMATION MARK
sstr.width(5);
test("+++\u07ff", "{}", "\u07ff"); // NKO TAMAN SIGN

// 3-byte code points
sstr.width(5);
test("++\u0800", "{}", "\u0800"); // SAMARITAN LETTER ALAF
sstr.width(5);
test("++\ufffd", "{}", "\ufffd"); // REPLACEMENT CHARACTER

// 4-byte code points
sstr.width(5);
test("+\U00010000", "{}", "\U00010000"); // LINEAR B SYLLABLE B008 A
sstr.width(5);
test("+\U0010FFFF", "{}", "\U0010FFFF"); // Undefined Character
test("hello\0world"sv, "hello{}{}", '\0', "world");
assert(sstr.width() == 15);
}

int main(int, char**) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ static void test_write_failure() {
assert(os.fail());
}

// Test the formatting does no padding.
static void test_stream_formatting() {
std::stringstream sstr;
auto test = [&]<class... Args>(std::string_view expected, test_format_string<char, Args...> fmt, Args&&... args) {
Expand All @@ -164,37 +165,20 @@ static void test_stream_formatting() {
test("hello", "{}", "hello");

sstr.width(10);
test(" hello", "{}", "hello");
test("hello", "{}", "hello");
assert(sstr.width() == 10);

sstr.fill('+');

sstr.width(10);
test("+++++hello", "{}", "hello");
test("hello", "{}", "hello");
assert(sstr.width() == 10);

// *** Test embedded NUL character ***
using namespace std::literals;
sstr.width(15);
test("++++hello\0world"sv, "hello{}{}", '\0', "world");

// *** Test Unicode ***
// Streams count code units not code points
// 2-byte code points
sstr.width(5);
test("+++\u00a1", "{}", "\u00a1"); // INVERTED EXCLAMATION MARK
sstr.width(5);
test("+++\u07ff", "{}", "\u07ff"); // NKO TAMAN SIGN

// 3-byte code points
sstr.width(5);
test("++\u0800", "{}", "\u0800"); // SAMARITAN LETTER ALAF
sstr.width(5);
test("++\ufffd", "{}", "\ufffd"); // REPLACEMENT CHARACTER

// 4-byte code points
sstr.width(5);
test("+\U00010000", "{}", "\U00010000"); // LINEAR B SYLLABLE B008 A
sstr.width(5);
test("+\U0010FFFF", "{}", "\U0010FFFF"); // Undefined Character
test("hello\0world"sv, "hello{}{}", '\0', "world");
assert(sstr.width() == 15);
}

int main(int, char**) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ static void test_write_failure() {
assert(os.fail());
}

// Test the formatting does no padding.
static void test_stream_formatting() {
std::stringstream sstr;
auto test = [&]<class... Args>(std::string_view expected, test_format_string<char, Args...> fmt, Args&&... args) {
Expand All @@ -164,37 +165,20 @@ static void test_stream_formatting() {
test("hello", "{}", "hello");

sstr.width(10);
test(" hello", "{}", "hello");
test("hello", "{}", "hello");
assert(sstr.width() == 10);

sstr.fill('+');

sstr.width(10);
test("+++++hello", "{}", "hello");
test("hello", "{}", "hello");
assert(sstr.width() == 10);

// *** Test embedded NUL character ***
using namespace std::literals;
sstr.width(15);
test("++++hello\0world"sv, "hello{}{}", '\0', "world");

// *** Test Unicode ***
// Streams count code units not code points
// 2-byte code points
sstr.width(5);
test("+++\u00a1", "{}", "\u00a1"); // INVERTED EXCLAMATION MARK
sstr.width(5);
test("+++\u07ff", "{}", "\u07ff"); // NKO TAMAN SIGN

// 3-byte code points
sstr.width(5);
test("++\u0800", "{}", "\u0800"); // SAMARITAN LETTER ALAF
sstr.width(5);
test("++\ufffd", "{}", "\ufffd"); // REPLACEMENT CHARACTER

// 4-byte code points
sstr.width(5);
test("+\U00010000", "{}", "\U00010000"); // LINEAR B SYLLABLE B008 A
sstr.width(5);
test("+\U0010FFFF", "{}", "\U0010FFFF"); // Undefined Character
test("hello\0world"sv, "hello{}{}", '\0', "world");
assert(sstr.width() == 15);
}

int main(int, char**) {
Expand Down