Replace buffer with range

This commit is contained in:
Victor Zverovich
2018-01-10 22:41:23 -08:00
parent c3d6c5fc4c
commit c095445394
14 changed files with 544 additions and 555 deletions
+5 -5
View File
@@ -14,18 +14,18 @@ using fmt::printf_arg_formatter;
// A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
class CustomArgFormatter : public fmt::arg_formatter<char> {
class CustomArgFormatter : public fmt::arg_formatter<fmt::buffer> {
public:
CustomArgFormatter(fmt::buffer &buf, fmt::basic_context<char> &ctx,
CustomArgFormatter(fmt::buffer &buf, fmt::basic_context<fmt::buffer> &ctx,
fmt::format_specs &s)
: fmt::arg_formatter<char>(buf, ctx, s) {}
: fmt::arg_formatter<fmt::buffer>(buf, ctx, s) {}
using fmt::arg_formatter<char>::operator();
using fmt::arg_formatter<fmt::buffer>::operator();
void operator()(double value) {
if (round(value * pow(10, spec().precision())) == 0)
value = 0;
fmt::arg_formatter<char>::operator()(value);
fmt::arg_formatter<fmt::buffer>::operator()(value);
}
};
+30 -36
View File
@@ -31,7 +31,6 @@
#include <cmath>
#include <cstring>
#include <memory>
#include <type_traits>
#include <stdint.h>
#include "gmock/gmock.h"
@@ -91,7 +90,7 @@ template <typename Char, typename T>
fmt::basic_memory_buffer<Char> buffer;
fmt::basic_writer<fmt::basic_buffer<Char>> writer(buffer);
writer.write(value);
std::basic_string<Char> actual = writer.str();
std::basic_string<Char> actual = to_string(buffer);
std::basic_string<Char> expected;
std_format(value, expected);
if (expected == actual)
@@ -149,19 +148,11 @@ TEST(WriterTest, NotCopyAssignable) {
EXPECT_FALSE(std::is_copy_assignable<basic_writer<fmt::buffer>>::value);
}
TEST(WriterTest, Ctor) {
memory_buffer buf;
fmt::basic_writer<fmt::buffer> w(buf);
EXPECT_EQ(0u, w.size());
EXPECT_STREQ("", w.c_str());
EXPECT_EQ("", w.str());
}
TEST(WriterTest, Data) {
memory_buffer buf;
fmt::basic_writer<fmt::buffer> w(buf);
w.write(42);
EXPECT_EQ("42", std::string(w.data(), w.size()));
EXPECT_EQ("42", to_string(buf));
}
TEST(WriterTest, WriteInt) {
@@ -224,7 +215,9 @@ TEST(WriterTest, WriteDoubleWithFilledBuffer) {
for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i)
writer.write(' ');
writer.write(1.2);
EXPECT_STREQ("1.2", writer.c_str() + fmt::internal::INLINE_BUFFER_SIZE);
fmt::string_view sv(buf.data(), buf.size());
sv.remove_prefix(fmt::internal::INLINE_BUFFER_SIZE);
EXPECT_EQ("1.2", sv);
}
TEST(WriterTest, WriteChar) {
@@ -239,31 +232,29 @@ TEST(WriterTest, WriteString) {
CHECK_WRITE_CHAR("abc");
CHECK_WRITE_WCHAR("abc");
// The following line shouldn't compile:
//MemoryWriter() << L"abc";
//std::declval<fmt::basic_writer<fmt::buffer>>().write(L"abc");
}
TEST(WriterTest, WriteWideString) {
CHECK_WRITE_WCHAR(L"abc");
// The following line shouldn't compile:
//fmt::WMemoryWriter() << "abc";
//std::declval<fmt::basic_writer<fmt::wbuffer>>().write("abc");
}
template <typename... T>
std::string write_str(T... args) {
memory_buffer buf;
fmt::basic_writer<fmt::buffer> writer(buf);
using namespace fmt;
writer.write(args...);
return writer.str();
return to_string(buf);
}
template <typename... T>
std::wstring write_wstr(T... args) {
wmemory_buffer buf;
fmt::basic_writer<fmt::wbuffer> writer(buf);
using namespace fmt;
writer.write(args...);
return writer.str();
return to_string(buf);
}
TEST(WriterTest, bin) {
@@ -350,17 +341,20 @@ TEST(WriterTest, pad) {
EXPECT_EQ(" 33", write_str(33ll, width=7));
EXPECT_EQ(" 44", write_str(44ull, width=7));
memory_buffer buf;
fmt::basic_writer<fmt::buffer> w(buf);
w.clear();
w.write(42, fmt::width=5, fmt::fill='0');
EXPECT_EQ("00042", w.str());
w.clear();
w << Date(2012, 12, 9);
EXPECT_EQ("2012-12-9", w.str());
w.clear();
w << iso8601(Date(2012, 1, 9));
EXPECT_EQ("2012-01-09", w.str());
EXPECT_EQ("00042", write_str(42, fmt::width=5, fmt::fill='0'));
{
memory_buffer buf;
fmt::basic_writer<fmt::buffer> w(buf);
w << Date(2012, 12, 9);
EXPECT_EQ("2012-12-9", to_string(buf));
}
{
memory_buffer buf;
fmt::basic_writer<fmt::buffer> w(buf);
w << iso8601(Date(2012, 1, 9));
EXPECT_EQ("2012-01-09", to_string(buf));
}
}
TEST(WriterTest, PadString) {
@@ -1228,8 +1222,8 @@ struct formatter<Date> {
return it;
}
void format(buffer &buf, const Date &d, context &) {
format_to(buf, "{}-{}-{}", d.year(), d.month(), d.day());
void format(const Date &d, context &ctx) {
format_to(ctx.range(), "{}-{}-{}", d.year(), d.month(), d.day());
}
};
}
@@ -1245,8 +1239,8 @@ class Answer {};
namespace fmt {
template <>
struct formatter<Answer> : formatter<int> {
void format(fmt::buffer &buf, Answer, fmt::context &ctx) {
formatter<int>::format(buf, 42, ctx);
void format(Answer, fmt::context &ctx) {
formatter<int>::format(42, ctx);
}
};
}
@@ -1535,11 +1529,11 @@ struct variant {
namespace fmt {
template <>
struct formatter<variant> : dynamic_formatter<> {
void format(buffer& buf, variant value, context& ctx) {
void format(variant value, context& ctx) {
if (value.type == variant::INT)
dynamic_formatter::format(buf, 42, ctx);
dynamic_formatter::format(ctx.range(), 42, ctx);
else
dynamic_formatter::format(buf, "foo", ctx);
dynamic_formatter::format(ctx.range(), "foo", ctx);
}
};
}
+3 -3
View File
@@ -58,14 +58,14 @@ TEST(OStreamTest, Enum) {
EXPECT_EQ("0", fmt::format("{}", A));
}
struct TestArgFormatter : fmt::arg_formatter<char> {
struct TestArgFormatter : fmt::arg_formatter<fmt::buffer> {
TestArgFormatter(fmt::buffer &buf, fmt::context &ctx, fmt::format_specs &s)
: fmt::arg_formatter<char>(buf, ctx, s) {}
: fmt::arg_formatter<fmt::buffer>(buf, ctx, s) {}
};
TEST(OStreamTest, CustomArg) {
fmt::memory_buffer buffer;
fmt::context ctx("", fmt::format_args());
fmt::context ctx(buffer, "", fmt::format_args());
fmt::format_specs spec;
TestArgFormatter af(buffer, ctx, spec);
visit(af, fmt::internal::make_arg<fmt::context>(TestEnum()));
+8 -8
View File
@@ -81,9 +81,9 @@ struct formatter<Test, Char> {
return ctx.begin();
}
void format(basic_buffer<Char> &b, Test, basic_context<Char> &) {
void format(Test, basic_context<basic_buffer<Char>> &ctx) {
const Char *test = "test";
b.append(test, test + std::strlen(test));
ctx.range().append(test, test + std::strlen(test));
}
};
}
@@ -442,7 +442,7 @@ struct CustomContext {
return ctx.begin();
}
void format(fmt::buffer &, const T &, CustomContext& ctx) {
void format(const T &, CustomContext& ctx) {
ctx.called = true;
}
};
@@ -456,8 +456,7 @@ TEST(UtilTest, MakeValueWithCustomFormatter) {
::Test t;
fmt::internal::value<CustomContext> arg(t);
CustomContext ctx = {false};
fmt::memory_buffer buffer;
arg.custom.format(buffer, &t, ctx);
arg.custom.format(&t, ctx);
EXPECT_TRUE(ctx.called);
}
@@ -518,7 +517,8 @@ VISIT_TYPE(float, double);
#define CHECK_ARG_(Char, expected, value) { \
testing::StrictMock<MockVisitor<decltype(expected)>> visitor; \
EXPECT_CALL(visitor, visit(expected)); \
fmt::visit(visitor, make_arg<fmt::basic_context<Char>>(value)); \
fmt::visit(visitor, \
make_arg<fmt::basic_context<basic_buffer<Char>>>(value)); \
}
#define CHECK_ARG(value) { \
@@ -596,8 +596,8 @@ TEST(UtilTest, CustomArg) {
testing::StrictMock<visitor> v;
EXPECT_CALL(v, visit(_)).WillOnce(testing::Invoke([&](handle h) {
fmt::memory_buffer buffer;
fmt::context ctx("", fmt::format_args());
h.format(buffer, ctx);
fmt::context ctx(buffer, "", fmt::format_args());
h.format(ctx);
EXPECT_EQ("test", std::string(buffer.data(), buffer.size()));
return visitor::Result();
}));