From 342b76d2783b8931f29b87c67be27286e7970d93 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 1 Jan 2014 10:00:55 -0800 Subject: [PATCH] Implement pad for wide strings. --- format.h | 20 +++++++++++++------- format_test.cc | 7 +++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/format.h b/format.h index 84155d39..ff65ddd9 100644 --- a/format.h +++ b/format.h @@ -504,9 +504,15 @@ DEFINE_INT_FORMATTERS(unsigned long long) \endrst */ -inline StrFormatSpec pad( - const char *str, unsigned width, wchar_t fill = ' ') { - return StrFormatSpec(str, AlignSpec(width, fill)); +template +inline StrFormatSpec pad( + const Char *str, unsigned width, Char fill = ' ') { + return StrFormatSpec(str, AlignSpec(width, fill)); +} + +inline StrFormatSpec pad( + const wchar_t *str, unsigned width, char fill = ' ') { + return StrFormatSpec(str, AlignSpec(width, fill)); } template @@ -712,10 +718,10 @@ class BasicWriter { template BasicWriter &operator<<(const IntFormatSpec &spec); - template - BasicWriter &operator<<(const StrFormatSpec &spec) { - const char *s = spec.str(); - FormatString(s, std::strlen(s), spec); + template + BasicWriter &operator<<(const StrFormatSpec &spec) { + const StringChar *s = spec.str(); + FormatString(s, std::char_traits::length(s), spec); return *this; } diff --git a/format_test.cc b/format_test.cc index d0ec4025..a841f548 100644 --- a/format_test.cc +++ b/format_test.cc @@ -56,6 +56,7 @@ using fmt::Format; using fmt::FormatError; using fmt::StringRef; using fmt::Writer; +using fmt::WWriter; using fmt::pad; #define FORMAT_TEST_THROW_(statement, expected_exception, message, fail) \ @@ -447,6 +448,12 @@ TEST(WriterTest, PadString) { EXPECT_EQ("test******", str(Writer() << pad("test", 10, '*'))); } +TEST(WriterTest, PadWString) { + EXPECT_EQ(L"test ", str(WWriter() << pad(L"test", 8))); + EXPECT_EQ(L"test******", str(WWriter() << pad(L"test", 10, '*'))); + EXPECT_EQ(L"test******", str(WWriter() << pad(L"test", 10, L'*'))); +} + TEST(WriterTest, NoConflictWithIOManip) { using namespace std; using namespace fmt;