Commit c0954453 authored by Victor Zverovich's avatar Victor Zverovich

Replace buffer with range

parent c3d6c5fc
This diff is collapsed.
/*
Formatting library for C++
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Formatting library for C++
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#include "fmt/format.h"
#include "fmt/locale.h"
......@@ -348,18 +328,18 @@ FMT_FUNC void windows_error::init(
FMT_FUNC void internal::format_windows_error(
buffer &out, int error_code, string_view message) FMT_NOEXCEPT {
FMT_TRY {
wmemory_buffer buffer;
buffer.resize(INLINE_BUFFER_SIZE);
wmemory_buffer buf;
buf.resize(INLINE_BUFFER_SIZE);
for (;;) {
wchar_t *system_message = &buffer[0];
wchar_t *system_message = &buf[0];
int result = FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
0, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
system_message, static_cast<uint32_t>(buffer.size()), 0);
system_message, static_cast<uint32_t>(buf.size()), 0);
if (result != 0) {
utf16_to_utf8 utf8_message;
if (utf8_message.convert(system_message) == ERROR_SUCCESS) {
basic_writer<char> w(out);
basic_writer<buffer> w(out);
w.write(message);
w.write(": ");
w.write(utf8_message);
......@@ -369,10 +349,10 @@ FMT_FUNC void internal::format_windows_error(
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
break; // Can't get error message, report error code instead.
buffer.resize(buffer.size() * 2);
buf.resize(buf.size() * 2);
}
} FMT_CATCH(...) {}
fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32.
format_error_code(out, error_code, message);
}
#endif // FMT_USE_WINDOWS_H
......@@ -467,8 +447,6 @@ template int internal::char_traits<char>::format_float(
template wchar_t internal::thousands_sep(locale_provider *lp);
template class basic_context<wchar_t>;
template void basic_fixed_buffer<wchar_t>::grow(std::size_t);
template void internal::arg_map<wcontext>::init(const wformat_args &args);
......
This diff is collapsed.
/*
Formatting library for C++ - locale support
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// Formatting library for C++ - locale support
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#include "fmt/format.h"
#include <locale>
......
/*
Formatting library for C++ - std::ostream support
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// Formatting library for C++ - std::ostream support
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#ifndef FMT_OSTREAM_H_
#define FMT_OSTREAM_H_
......@@ -104,12 +102,12 @@ struct formatter<T, Char,
typename std::enable_if<!internal::format_type<T>::value>::type>
: formatter<basic_string_view<Char>, Char> {
void format(basic_buffer<Char> &buf, const T &value,
basic_context<Char> &ctx) {
template <typename Context>
void format(const T &value, Context &ctx) {
basic_memory_buffer<Char> buffer;
internal::format_value(buffer, value);
basic_string_view<Char> str(buffer.data(), buffer.size());
formatter<basic_string_view<Char>, Char>::format(buf, str, ctx);
formatter<basic_string_view<Char>, Char>::format(str, ctx);
}
};
......
/*
A C++ interface to POSIX functions.
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// A C++ interface to POSIX functions.
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
// Disable bogus MSVC warnings.
#ifndef _CRT_SECURE_NO_WARNINGS
......
/*
A C++ interface to POSIX functions.
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// A C++ interface to POSIX functions.
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#ifndef FMT_POSIX_H_
#define FMT_POSIX_H_
......
This diff is collapsed.
/*
Formatting library for C++ - string utilities
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// Formatting library for C++ - string utilities
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#ifndef FMT_STRING_H_
#define FMT_STRING_H_
......
/*
Formatting library for C++ - time formatting
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// Formatting library for C++ - time formatting
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#ifndef FMT_TIME_H_
#define FMT_TIME_H_
......@@ -32,7 +30,8 @@ struct formatter<std::tm> {
return pointer_from(end);
}
void format(buffer &buf, const std::tm &tm, context &) {
void format(const std::tm &tm, context &ctx) {
buffer &buf = ctx.range();
std::size_t start = buf.size();
for (;;) {
std::size_t size = buf.capacity() - start;
......
......@@ -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);
}
};
......
......@@ -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);
}
};
}
......
......@@ -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()));
......
......@@ -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();
}));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment