[libc-commits] [libc] 4d4e18f - [libc][NFC] reuse variable in printf string conv

Michael Jones via libc-commits libc-commits at lists.llvm.org
Tue Jul 18 16:31:01 PDT 2023


Author: Michael Jones
Date: 2023-07-18T16:30:57-07:00
New Revision: 4d4e18f4ac675bf60b06f6518e56145e18d28026

URL: https://github.com/llvm/llvm-project/commit/4d4e18f4ac675bf60b06f6518e56145e18d28026
DIFF: https://github.com/llvm/llvm-project/commit/4d4e18f4ac675bf60b06f6518e56145e18d28026.diff

LOG: [libc][NFC] reuse variable in printf string conv

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libc/src/stdio/printf_core/char_converter.h b/libc/src/stdio/printf_core/char_converter.h
index f75bc811d14b6d..ffd2422c822f7e 100644
--- a/libc/src/stdio/printf_core/char_converter.h
+++ b/libc/src/stdio/printf_core/char_converter.h
@@ -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;

diff  --git a/libc/src/stdio/printf_core/string_converter.h b/libc/src/stdio/printf_core/string_converter.h
index 59e98e20c526a9..137bac6e76bea9 100644
--- a/libc/src/stdio/printf_core/string_converter.h
+++ b/libc/src/stdio/printf_core/string_converter.h
@@ -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;
 }


        


More information about the libc-commits mailing list