AK: Fix bug where "%s" with field width would print too many characters
I introduced this while implementing "%.*s", oops.
This commit is contained in:
parent
a87544fe8b
commit
23a54636ea
Notes:
sideshowbarker
2024-07-19 09:12:48 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/23a54636eae
1 changed files with 9 additions and 6 deletions
|
@ -28,6 +28,7 @@
|
|||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/LogStream.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/Types.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
|
@ -228,20 +229,22 @@ template<typename PutChFunc>
|
|||
}
|
||||
|
||||
template<typename PutChFunc>
|
||||
[[gnu::always_inline]] inline int print_string(PutChFunc putch, char*& bufptr, const char* str, bool leftPad, u32 field_width, bool dot)
|
||||
[[gnu::always_inline]] inline int print_string(PutChFunc putch, char*& bufptr, const char* str, bool left_pad, size_t field_width, bool dot)
|
||||
{
|
||||
size_t len = strlen(str);
|
||||
if (!dot && (!field_width || field_width < len))
|
||||
field_width = len;
|
||||
if (!leftPad && field_width > len) {
|
||||
for (unsigned i = 0; i < field_width - len; ++i)
|
||||
size_t pad_amount = field_width > len ? field_width - len : 0;
|
||||
|
||||
if (!left_pad) {
|
||||
for (size_t i = 0; i < pad_amount; ++i)
|
||||
putch(bufptr, ' ');
|
||||
}
|
||||
for (unsigned i = 0; i < field_width; ++i) {
|
||||
for (size_t i = 0; i < min(len, field_width); ++i) {
|
||||
putch(bufptr, str[i]);
|
||||
}
|
||||
if (leftPad && field_width > len) {
|
||||
for (unsigned i = 0; i < field_width - len; ++i)
|
||||
if (left_pad) {
|
||||
for (size_t i = 0; i < pad_amount; ++i)
|
||||
putch(bufptr, ' ');
|
||||
}
|
||||
return field_width;
|
||||
|
|
Loading…
Add table
Reference in a new issue