[libc][NFC] reuse variable in printf string conv
authorMichael Jones <michaelrj@google.com>
Tue, 11 Jul 2023 22:06:08 +0000 (15:06 -0700)
committerMichael Jones <michaelrj@google.com>
Tue, 18 Jul 2023 23:30:57 +0000 (16:30 -0700)
The amount of spaces to pad with is stored in the variable
padding_spaces, previously the actual write calls used the same formula
to calculate the value. This simplifies and clarifies the values by just
reusing the variable.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D155113

libc/src/stdio/printf_core/char_converter.h
libc/src/stdio/printf_core/string_converter.h

index f75bc81..ffd2422 100644 (file)
@@ -29,7 +29,7 @@ LIBC_INLINE int convert_char(Writer *writer, const FormatSection &to_conv) {
   // If the padding is on the left side, write the spaces first.
   if (padding_spaces > 0 &&
       (to_conv.flags & FormatFlags::LEFT_JUSTIFIED) == 0) {
-    RET_IF_RESULT_NEGATIVE(writer->write(' ', to_conv.min_width - string_len));
+    RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_spaces));
   }
 
   RET_IF_RESULT_NEGATIVE(writer->write(c));
@@ -37,7 +37,7 @@ LIBC_INLINE int convert_char(Writer *writer, const FormatSection &to_conv) {
   // If the padding is on the right side, write the spaces last.
   if (padding_spaces > 0 &&
       (to_conv.flags & FormatFlags::LEFT_JUSTIFIED) != 0) {
-    RET_IF_RESULT_NEGATIVE(writer->write(' ', to_conv.min_width - string_len));
+    RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_spaces));
   }
 
   return WRITE_OK;
index 59e98e2..137bac6 100644 (file)
@@ -39,7 +39,7 @@ LIBC_INLINE int convert_string(Writer *writer, const FormatSection &to_conv) {
   // If the padding is on the left side, write the spaces first.
   if (padding_spaces > 0 &&
       (to_conv.flags & FormatFlags::LEFT_JUSTIFIED) == 0) {
-    RET_IF_RESULT_NEGATIVE(writer->write(' ', to_conv.min_width - string_len));
+    RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_spaces));
   }
 
   RET_IF_RESULT_NEGATIVE(writer->write(
@@ -48,7 +48,7 @@ LIBC_INLINE int convert_string(Writer *writer, const FormatSection &to_conv) {
   // If the padding is on the right side, write the spaces last.
   if (padding_spaces > 0 &&
       (to_conv.flags & FormatFlags::LEFT_JUSTIFIED) != 0) {
-    RET_IF_RESULT_NEGATIVE(writer->write(' ', to_conv.min_width - string_len));
+    RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_spaces));
   }
   return WRITE_OK;
 }