[PATCH] D35278: Support: Add llvm::center_justify.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 11 15:36:14 PDT 2017


I don't necessarily have a strong objection to this aside from the fact
that formatv already supports this.

formatv("{0,=6}", "foo");
formatv("{0}", fmt_align("foo", AlignStyle::Center, Width));

Both work

On Tue, Jul 11, 2017 at 3:05 PM Frederich Munch via Phabricator <
reviews at reviews.llvm.org> wrote:

> marsupial created this revision.
>
> Completes the set.
>
>
> https://reviews.llvm.org/D35278
>
> Files:
>   include/llvm/Support/Format.h
>   lib/Support/raw_ostream.cpp
>   unittests/Support/raw_ostream_test.cpp
>
>
> Index: unittests/Support/raw_ostream_test.cpp
> ===================================================================
> --- unittests/Support/raw_ostream_test.cpp
> +++ unittests/Support/raw_ostream_test.cpp
> @@ -151,6 +151,11 @@
>    EXPECT_EQ("   xyz", printToString(right_justify("xyz", 6), 6));
>    EXPECT_EQ("abc",    printToString(right_justify("abc", 3), 3));
>    EXPECT_EQ("big",    printToString(right_justify("big", 1), 3));
> +  EXPECT_EQ("   on    ",    printToString(center_justify("on", 9), 9));
> +  EXPECT_EQ("   off    ",    printToString(center_justify("off", 10),
> 10));
> +  EXPECT_EQ("single ",    printToString(center_justify("single", 7), 7));
> +  EXPECT_EQ("none",    printToString(center_justify("none", 1), 4));
> +  EXPECT_EQ("none",    printToString(center_justify("none", 1), 1));
>  }
>
>  TEST(raw_ostreamTest, FormatHex) {
> Index: lib/Support/raw_ostream.cpp
> ===================================================================
> --- lib/Support/raw_ostream.cpp
> +++ lib/Support/raw_ostream.cpp
> @@ -326,12 +326,20 @@
>  }
>
>  raw_ostream &raw_ostream::operator<<(const FormattedString &FS) {
> -  unsigned Len = FS.Str.size();
> -  int PadAmount = FS.Width - Len;
> -  if (FS.RightJustify && (PadAmount > 0))
> +  if (FS.Str.size() >= FS.Width || FS.Justify == 0) {
> +    this->operator<<(FS.Str);
> +    return *this;
> +  }
> +  const int Difference = FS.Width - FS.Str.size();
> +  int PadAmount = FS.Justify == FormattedString::JustifyCenter ?
> Difference / 2
> +                                                               :
> Difference;
> +  if (FS.Justify & FormattedString::JustifyRight) {
>      this->indent(PadAmount);
> +    if (FS.Justify == FormattedString::JustifyCenter)
> +      PadAmount += Difference - (PadAmount * 2);
> +  }
>    this->operator<<(FS.Str);
> -  if (!FS.RightJustify && (PadAmount > 0))
> +  if (FS.Justify & FormattedString::JustifyLeft)
>      this->indent(PadAmount);
>    return *this;
>  }
> Index: include/llvm/Support/Format.h
> ===================================================================
> --- include/llvm/Support/Format.h
> +++ include/llvm/Support/Format.h
> @@ -125,30 +125,38 @@
>    return format_object<Ts...>(Fmt, Vals...);
>  }
>
> -/// This is a helper class used for left_justify() and right_justify().
> +/// This is a helper class for left_justify, right_justify, and
> center_justify.
>  class FormattedString {
>    StringRef Str;
>    unsigned Width;
> -  bool RightJustify;
> +  unsigned Justify : 2;
>    friend class raw_ostream;
>
>  public:
> -    FormattedString(StringRef S, unsigned W, bool R)
> -      : Str(S), Width(W), RightJustify(R) { }
> +    enum Justification { JustifyLeft = 1, JustifyRight = 2, JustifyCenter
> = 3 };
> +    FormattedString(StringRef S, unsigned W, Justification J)
> +      : Str(S), Width(W), Justify(J) { }
>  };
>
>  /// left_justify - append spaces after string so total output is
>  /// \p Width characters.  If \p Str is larger that \p Width, full string
>  /// is written with no padding.
>  inline FormattedString left_justify(StringRef Str, unsigned Width) {
> -  return FormattedString(Str, Width, false);
> +  return FormattedString(Str, Width, FormattedString::JustifyLeft);
>  }
>
>  /// right_justify - add spaces before string so total output is
>  /// \p Width characters.  If \p Str is larger that \p Width, full string
>  /// is written with no padding.
>  inline FormattedString right_justify(StringRef Str, unsigned Width) {
> -  return FormattedString(Str, Width, true);
> +  return FormattedString(Str, Width, FormattedString::JustifyRight);
> +}
> +
> +/// center_justify - add spaces before and after string so total output is
> +/// \p Width characters.  If \p Str is larger that \p Width, full string
> +/// is written with no padding.
> +inline FormattedString center_justify(StringRef Str, unsigned Width) {
> +  return FormattedString(Str, Width, FormattedString::JustifyCenter);
>  }
>
>  /// This is a helper class used for format_hex() and format_decimal().
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170711/c4459e7f/attachment.html>


More information about the llvm-commits mailing list