[llvm] 609c287 - raw_ostream: Simplify code a bit. NFCI.

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 26 05:07:30 PDT 2020


Author: Benjamin Kramer
Date: 2020-04-26T14:07:05+02:00
New Revision: 609c2873e7f1b8bd3209bd8613a7b4b489bd1119

URL: https://github.com/llvm/llvm-project/commit/609c2873e7f1b8bd3209bd8613a7b4b489bd1119
DIFF: https://github.com/llvm/llvm-project/commit/609c2873e7f1b8bd3209bd8613a7b4b489bd1119.diff

LOG: raw_ostream: Simplify code a bit. NFCI.

Added: 
    

Modified: 
    llvm/lib/Support/raw_ostream.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp
index fa69fe3c7154..7cac834f1010 100644
--- a/llvm/lib/Support/raw_ostream.cpp
+++ b/llvm/lib/Support/raw_ostream.cpp
@@ -343,36 +343,33 @@ raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
 }
 
 raw_ostream &raw_ostream::operator<<(const formatv_object_base &Obj) {
-  SmallString<128> S;
   Obj.format(*this);
   return *this;
 }
 
 raw_ostream &raw_ostream::operator<<(const FormattedString &FS) {
-  if (FS.Str.size() >= FS.Width || FS.Justify == FormattedString::JustifyNone) {
-    this->operator<<(FS.Str);
-    return *this;
-  }
-  const size_t Difference = FS.Width - FS.Str.size();
-  switch (FS.Justify) {
-  case FormattedString::JustifyLeft:
-    this->operator<<(FS.Str);
-    this->indent(Difference);
-    break;
-  case FormattedString::JustifyRight:
-    this->indent(Difference);
-    this->operator<<(FS.Str);
-    break;
-  case FormattedString::JustifyCenter: {
-    int PadAmount = Difference / 2;
-    this->indent(PadAmount);
-    this->operator<<(FS.Str);
-    this->indent(Difference - PadAmount);
-    break;
-  }
-  default:
-    llvm_unreachable("Bad Justification");
+  unsigned LeftIndent = 0;
+  unsigned RightIndent = 0;
+  const ssize_t Difference = FS.Width - FS.Str.size();
+  if (Difference > 0) {
+    switch (FS.Justify) {
+    case FormattedString::JustifyNone:
+      break;
+    case FormattedString::JustifyLeft:
+      RightIndent = Difference;
+      break;
+    case FormattedString::JustifyRight:
+      LeftIndent = Difference;
+      break;
+    case FormattedString::JustifyCenter:
+      LeftIndent = Difference / 2;
+      RightIndent = Difference - LeftIndent;
+      break;
+    }
   }
+  indent(LeftIndent);
+  (*this) << FS.Str;
+  indent(RightIndent);
   return *this;
 }
 


        


More information about the llvm-commits mailing list