<div>I don't necessarily have a strong objection to this aside from the fact that formatv already supports this.</div><div><br></div><div>formatv("{0,=6}", "foo");</div><div>formatv("{0}", fmt_align("foo", AlignStyle::Center, Width));</div><div><br></div><div>Both work</div><div><br><div class="gmail_quote"><div>On Tue, Jul 11, 2017 at 3:05 PM Frederich Munch via Phabricator <<a href="mailto:reviews@reviews.llvm.org">reviews@reviews.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">marsupial created this revision.<br>
<br>
Completes the set.<br>
<br>
<br>
<a href="https://reviews.llvm.org/D35278" rel="noreferrer" target="_blank">https://reviews.llvm.org/D35278</a><br>
<br>
Files:<br>
include/llvm/Support/Format.h<br>
lib/Support/raw_ostream.cpp<br>
unittests/Support/raw_ostream_test.cpp<br>
<br>
<br>
Index: unittests/Support/raw_ostream_test.cpp<br>
===================================================================<br>
--- unittests/Support/raw_ostream_test.cpp<br>
+++ unittests/Support/raw_ostream_test.cpp<br>
@@ -151,6 +151,11 @@<br>
EXPECT_EQ(" xyz", printToString(right_justify("xyz", 6), 6));<br>
EXPECT_EQ("abc", printToString(right_justify("abc", 3), 3));<br>
EXPECT_EQ("big", printToString(right_justify("big", 1), 3));<br>
+ EXPECT_EQ(" on ", printToString(center_justify("on", 9), 9));<br>
+ EXPECT_EQ(" off ", printToString(center_justify("off", 10), 10));<br>
+ EXPECT_EQ("single ", printToString(center_justify("single", 7), 7));<br>
+ EXPECT_EQ("none", printToString(center_justify("none", 1), 4));<br>
+ EXPECT_EQ("none", printToString(center_justify("none", 1), 1));<br>
}<br>
<br>
TEST(raw_ostreamTest, FormatHex) {<br>
Index: lib/Support/raw_ostream.cpp<br>
===================================================================<br>
--- lib/Support/raw_ostream.cpp<br>
+++ lib/Support/raw_ostream.cpp<br>
@@ -326,12 +326,20 @@<br>
}<br>
<br>
raw_ostream &raw_ostream::operator<<(const FormattedString &FS) {<br>
- unsigned Len = FS.Str.size();<br>
- int PadAmount = FS.Width - Len;<br>
- if (FS.RightJustify && (PadAmount > 0))<br>
+ if (FS.Str.size() >= FS.Width || FS.Justify == 0) {<br>
+ this->operator<<(FS.Str);<br>
+ return *this;<br>
+ }<br>
+ const int Difference = FS.Width - FS.Str.size();<br>
+ int PadAmount = FS.Justify == FormattedString::JustifyCenter ? Difference / 2<br>
+ : Difference;<br>
+ if (FS.Justify & FormattedString::JustifyRight) {<br>
this->indent(PadAmount);<br>
+ if (FS.Justify == FormattedString::JustifyCenter)<br>
+ PadAmount += Difference - (PadAmount * 2);<br>
+ }<br>
this->operator<<(FS.Str);<br>
- if (!FS.RightJustify && (PadAmount > 0))<br>
+ if (FS.Justify & FormattedString::JustifyLeft)<br>
this->indent(PadAmount);<br>
return *this;<br>
}<br>
Index: include/llvm/Support/Format.h<br>
===================================================================<br>
--- include/llvm/Support/Format.h<br>
+++ include/llvm/Support/Format.h<br>
@@ -125,30 +125,38 @@<br>
return format_object<Ts...>(Fmt, Vals...);<br>
}<br>
<br>
-/// This is a helper class used for left_justify() and right_justify().<br>
+/// This is a helper class for left_justify, right_justify, and center_justify.<br>
class FormattedString {<br>
StringRef Str;<br>
unsigned Width;<br>
- bool RightJustify;<br>
+ unsigned Justify : 2;<br>
friend class raw_ostream;<br>
<br>
public:<br>
- FormattedString(StringRef S, unsigned W, bool R)<br>
- : Str(S), Width(W), RightJustify(R) { }<br>
+ enum Justification { JustifyLeft = 1, JustifyRight = 2, JustifyCenter = 3 };<br>
+ FormattedString(StringRef S, unsigned W, Justification J)<br>
+ : Str(S), Width(W), Justify(J) { }<br>
};<br>
<br>
/// left_justify - append spaces after string so total output is<br>
/// \p Width characters. If \p Str is larger that \p Width, full string<br>
/// is written with no padding.<br>
inline FormattedString left_justify(StringRef Str, unsigned Width) {<br>
- return FormattedString(Str, Width, false);<br>
+ return FormattedString(Str, Width, FormattedString::JustifyLeft);<br>
}<br>
<br>
/// right_justify - add spaces before string so total output is<br>
/// \p Width characters. If \p Str is larger that \p Width, full string<br>
/// is written with no padding.<br>
inline FormattedString right_justify(StringRef Str, unsigned Width) {<br>
- return FormattedString(Str, Width, true);<br>
+ return FormattedString(Str, Width, FormattedString::JustifyRight);<br>
+}<br>
+<br>
+/// center_justify - add spaces before and after string so total output is<br>
+/// \p Width characters. If \p Str is larger that \p Width, full string<br>
+/// is written with no padding.<br>
+inline FormattedString center_justify(StringRef Str, unsigned Width) {<br>
+ return FormattedString(Str, Width, FormattedString::JustifyCenter);<br>
}<br>
<br>
/// This is a helper class used for format_hex() and format_decimal().<br>
<br>
<br>
</blockquote></div></div>