Skip to content

Commit

Permalink
basic_buffer -> buffer
Browse files Browse the repository at this point in the history
This reduces symbol sizes and gets rid of shadowing warnings.
  • Loading branch information
vitaut committed Apr 8, 2019
1 parent 6e37c20 commit 2808395
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 101 deletions.
31 changes: 14 additions & 17 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,23 @@ FMT_CONSTEXPR typename std::make_unsigned<Int>::type to_unsigned(Int value) {
}

/** A contiguous memory buffer with an optional growing ability. */
template <typename T> class basic_buffer {
template <typename T> class buffer {
private:
basic_buffer(const basic_buffer&) = delete;
void operator=(const basic_buffer&) = delete;
buffer(const buffer&) = delete;
void operator=(const buffer&) = delete;

T* ptr_;
std::size_t size_;
std::size_t capacity_;

protected:
// Don't initialize ptr_ since it is not accessed to save a few cycles.
basic_buffer(std::size_t sz) FMT_NOEXCEPT : size_(sz), capacity_(sz) {}
buffer(std::size_t sz) FMT_NOEXCEPT : size_(sz), capacity_(sz) {}

basic_buffer(T* p = FMT_NULL, std::size_t sz = 0,
std::size_t cap = 0) FMT_NOEXCEPT : ptr_(p),
size_(sz),
capacity_(cap) {}
buffer(T* p = FMT_NULL, std::size_t sz = 0, std::size_t cap = 0) FMT_NOEXCEPT
: ptr_(p),
size_(sz),
capacity_(cap) {}

/** Sets the buffer data and capacity. */
void set(T* buf_data, std::size_t buf_capacity) FMT_NOEXCEPT {
Expand All @@ -272,7 +272,7 @@ template <typename T> class basic_buffer {
typedef T value_type;
typedef const T& const_reference;

virtual ~basic_buffer() {}
virtual ~buffer() {}

T* begin() FMT_NOEXCEPT { return ptr_; }
T* end() FMT_NOEXCEPT { return ptr_ + size_; }
Expand Down Expand Up @@ -317,12 +317,9 @@ template <typename T> class basic_buffer {
const T& operator[](std::size_t index) const { return ptr_[index]; }
};

typedef basic_buffer<char> buffer;
typedef basic_buffer<wchar_t> wbuffer;

// A container-backed buffer.
template <typename Container>
class container_buffer : public basic_buffer<typename Container::value_type> {
class container_buffer : public buffer<typename Container::value_type> {
private:
Container& container_;

Expand All @@ -334,7 +331,7 @@ class container_buffer : public basic_buffer<typename Container::value_type> {

public:
explicit container_buffer(Container& c)
: basic_buffer<typename Container::value_type>(c.size()), container_(c) {}
: buffer<typename Container::value_type>(c.size()), container_(c) {}
};

// Extracts a reference to the container from back_insert_iterator.
Expand Down Expand Up @@ -1141,7 +1138,7 @@ template <typename OutputIt, typename Char> class basic_format_context {

template <typename Char> struct buffer_context {
typedef basic_format_context<
std::back_insert_iterator<internal::basic_buffer<Char>>, Char>
std::back_insert_iterator<internal::buffer<Char>>, Char>
type;
};
typedef buffer_context<char>::type format_context;
Expand Down Expand Up @@ -1381,7 +1378,7 @@ std::basic_string<Char> vformat(

template <typename Char>
typename buffer_context<Char>::type::iterator vformat_to(
internal::basic_buffer<Char>& buf, basic_string_view<Char> format_str,
internal::buffer<Char>& buf, basic_string_view<Char> format_str,
basic_format_args<typename buffer_context<Char>::type> args);
} // namespace internal

Expand Down Expand Up @@ -1415,7 +1412,7 @@ template <typename Char>
struct is_contiguous<std::basic_string<Char>> : std::true_type {};

template <typename Char>
struct is_contiguous<internal::basic_buffer<Char>> : std::true_type {};
struct is_contiguous<internal::buffer<Char>> : std::true_type {};

/** Formats a string and writes the output to ``out``. */
template <typename Container, typename S>
Expand Down
12 changes: 6 additions & 6 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ inline int fmt_snprintf(char* buffer, size_t size, const char* format, ...) {
# define FMT_SWPRINTF swprintf
#endif // defined(_WIN32) && defined(__MINGW32__) && !defined(__NO_ISOCEXT)

typedef void (*FormatFunc)(internal::buffer&, int, string_view);
typedef void (*FormatFunc)(internal::buffer<char>&, int, string_view);

// Portable thread-safe version of strerror.
// Sets buffer to point to a string describing the error code.
Expand Down Expand Up @@ -154,7 +154,7 @@ int safe_strerror(int error_code, char*& buffer,
return dispatcher(error_code, buffer, buffer_size).run();
}

void format_error_code(internal::buffer& out, int error_code,
void format_error_code(internal::buffer<char>& out, int error_code,
string_view message) FMT_NOEXCEPT {
// Report error code making sure that the output fits into
// inline_buffer_size to avoid dynamic memory allocation and potential
Expand Down Expand Up @@ -661,7 +661,7 @@ struct shortest_handler {

template <typename Double, typename std::enable_if<
sizeof(Double) == sizeof(uint64_t), int>::type>
FMT_FUNC bool grisu2_format(Double value, buffer& buf, int precision,
FMT_FUNC bool grisu2_format(Double value, buffer<char>& buf, int precision,
bool fixed, int& exp) {
FMT_ASSERT(value >= 0, "value is negative");
if (value <= 0) { // <= instead of == to silence a warning.
Expand Down Expand Up @@ -713,7 +713,7 @@ FMT_FUNC bool grisu2_format(Double value, buffer& buf, int precision,
}

template <typename Double>
void sprintf_format(Double value, internal::buffer& buf,
void sprintf_format(Double value, internal::buffer<char>& buf,
core_format_specs spec) {
// Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail.
FMT_ASSERT(buf.capacity() != 0, "empty buffer");
Expand Down Expand Up @@ -849,7 +849,7 @@ FMT_FUNC void windows_error::init(int err_code, string_view format_str,
base = std::runtime_error(to_string(buffer));
}

FMT_FUNC void internal::format_windows_error(internal::buffer& out,
FMT_FUNC void internal::format_windows_error(internal::buffer<char>& out,
int error_code,
string_view message) FMT_NOEXCEPT {
FMT_TRY {
Expand Down Expand Up @@ -883,7 +883,7 @@ FMT_FUNC void internal::format_windows_error(internal::buffer& out,

#endif // FMT_USE_WINDOWS_H

FMT_FUNC void format_system_error(internal::buffer& out, int error_code,
FMT_FUNC void format_system_error(internal::buffer<char>& out, int error_code,
string_view message) FMT_NOEXCEPT {
FMT_TRY {
memory_buffer buf;
Expand Down
34 changes: 17 additions & 17 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ class back_insert_range
back_insert_range(typename base::iterator it) : base(it) {}
};

typedef basic_writer<back_insert_range<internal::buffer>> writer;
typedef basic_writer<back_insert_range<internal::wbuffer>> wwriter;
typedef basic_writer<back_insert_range<internal::buffer<char>>> writer;
typedef basic_writer<back_insert_range<internal::buffer<wchar_t>>> wwriter;

/** A formatting error such as invalid format string. */
class format_error : public std::runtime_error {
Expand Down Expand Up @@ -383,7 +383,7 @@ template <typename T> inline T* make_checked(T* p, std::size_t) { return p; }

template <typename T>
template <typename U>
void basic_buffer<T>::append(const U* begin, const U* end) {
void buffer<T>::append(const U* begin, const U* end) {
std::size_t new_size = size_ + internal::to_unsigned(end - begin);
reserve(new_size);
std::uninitialized_copy(begin, end,
Expand Down Expand Up @@ -454,8 +454,7 @@ enum { inline_buffer_size = 500 };
*/
template <typename T, std::size_t SIZE = inline_buffer_size,
typename Allocator = std::allocator<T>>
class basic_memory_buffer : private Allocator,
public internal::basic_buffer<T> {
class basic_memory_buffer : private Allocator, public internal::buffer<T> {
private:
T store_[SIZE];

Expand Down Expand Up @@ -1087,7 +1086,8 @@ class utf16_to_utf8 {
FMT_API int convert(wstring_view s);
};

FMT_API void format_windows_error(fmt::internal::buffer& out, int error_code,
FMT_API void format_windows_error(fmt::internal::buffer<char>& out,
int error_code,
fmt::string_view message) FMT_NOEXCEPT;
#endif

Expand Down Expand Up @@ -1149,10 +1149,10 @@ namespace internal {
// Formats value using Grisu2 algorithm:
// https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf
template <typename Double, FMT_ENABLE_IF(sizeof(Double) == sizeof(uint64_t))>
FMT_API bool grisu2_format(Double value, buffer& buf, int precision, bool fixed,
int& exp);
FMT_API bool grisu2_format(Double value, buffer<char>& buf, int precision,
bool fixed, int& exp);
template <typename Double, FMT_ENABLE_IF(sizeof(Double) != sizeof(uint64_t))>
inline bool grisu2_format(Double, buffer&, int, bool, int&) {
inline bool grisu2_format(Double, buffer<char>&, int, bool, int&) {
return false;
}

Expand Down Expand Up @@ -1242,7 +1242,7 @@ It grisu2_prettify(const char* digits, int size, int exp, It it,
}

template <typename Double>
void sprintf_format(Double, internal::buffer&, core_format_specs);
void sprintf_format(Double, internal::buffer<char>&, core_format_specs);

template <typename Handler>
FMT_CONSTEXPR void handle_int_type_spec(char spec, Handler&& handler) {
Expand Down Expand Up @@ -2405,7 +2405,7 @@ class system_error : public std::runtime_error {
may look like "Unknown error -1" and is platform-dependent.
\endrst
*/
FMT_API void format_system_error(internal::buffer& out, int error_code,
FMT_API void format_system_error(internal::buffer<char>& out, int error_code,
fmt::string_view message) FMT_NOEXCEPT;

/**
Expand Down Expand Up @@ -2654,7 +2654,7 @@ template <typename Range> class basic_writer {

struct double_writer {
char sign;
internal::buffer& buffer;
internal::buffer<char>& buffer;

size_t size() const { return buffer.size() + (sign ? 1 : 0); }
size_t width() const { return size(); }
Expand All @@ -2667,14 +2667,14 @@ template <typename Range> class basic_writer {

class grisu_writer {
private:
internal::buffer& digits_;
internal::buffer<char>& digits_;
size_t size_;
char sign_;
int exp_;
internal::gen_digits_params params_;

public:
grisu_writer(char sign, internal::buffer& digits, int exp,
grisu_writer(char sign, internal::buffer<char>& digits, int exp,
const internal::gen_digits_params& params)
: digits_(digits), sign_(sign), exp_(exp), params_(params) {
int num_digits = static_cast<int>(digits.size());
Expand Down Expand Up @@ -3392,17 +3392,17 @@ std::basic_string<Char> to_string(const basic_memory_buffer<Char, SIZE>& buf) {

template <typename Char>
typename buffer_context<Char>::type::iterator internal::vformat_to(
internal::basic_buffer<Char>& buf, basic_string_view<Char> format_str,
internal::buffer<Char>& buf, basic_string_view<Char> format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
typedef back_insert_range<internal::basic_buffer<Char>> range;
typedef back_insert_range<internal::buffer<Char>> range;
return vformat_to<arg_formatter<range>>(buf, to_string_view(format_str),
args);
}

template <typename S, typename Char = FMT_CHAR(S),
FMT_ENABLE_IF(internal::is_string<S>::value)>
inline typename buffer_context<Char>::type::iterator vformat_to(
internal::basic_buffer<Char>& buf, const S& format_str,
internal::buffer<Char>& buf, const S& format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
return internal::vformat_to(buf, to_string_view(format_str), args);
}
Expand Down
4 changes: 2 additions & 2 deletions include/fmt/locale.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ FMT_BEGIN_NAMESPACE
namespace internal {
template <typename Char>
typename buffer_context<Char>::type::iterator vformat_to(
const std::locale& loc, basic_buffer<Char>& buf,
const std::locale& loc, buffer<Char>& buf,
basic_string_view<Char> format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
typedef back_insert_range<basic_buffer<Char>> range;
typedef back_insert_range<buffer<Char>> range;
return vformat_to<arg_formatter<range>>(buf, to_string_view(format_str), args,
internal::locale_ref(loc));
}
Expand Down
8 changes: 4 additions & 4 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ template <class Char> class formatbuf : public std::basic_streambuf<Char> {
typedef typename std::basic_streambuf<Char>::int_type int_type;
typedef typename std::basic_streambuf<Char>::traits_type traits_type;

basic_buffer<Char>& buffer_;
buffer<Char>& buffer_;

public:
formatbuf(basic_buffer<Char>& buffer) : buffer_(buffer) {}
formatbuf(buffer<Char>& buffer) : buffer_(buffer) {}

protected:
// The put-area is actually always empty. This makes the implementation
Expand Down Expand Up @@ -71,7 +71,7 @@ template <typename T, typename Char> class is_streamable {

// Write the content of buf to os.
template <typename Char>
void write(std::basic_ostream<Char>& os, basic_buffer<Char>& buf) {
void write(std::basic_ostream<Char>& os, buffer<Char>& buf) {
const Char* data = buf.data();
typedef std::make_unsigned<std::streamsize>::type UnsignedStreamSize;
UnsignedStreamSize size = buf.size();
Expand All @@ -86,7 +86,7 @@ void write(std::basic_ostream<Char>& os, basic_buffer<Char>& buf) {
}

template <typename Char, typename T>
void format_value(basic_buffer<Char>& buffer, const T& value) {
void format_value(buffer<Char>& buffer, const T& value) {
internal::formatbuf<Char> format_buf(buffer);
std::basic_ostream<Char> output(&format_buf);
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
Expand Down
6 changes: 3 additions & 3 deletions include/fmt/prepare.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class prepared_format {

std::basic_string<char_type> format(const Args&... args) const {
basic_memory_buffer<char_type> buffer;
typedef back_insert_range<internal::basic_buffer<char_type>> range;
typedef back_insert_range<internal::buffer<char_type>> range;
this->vformat_to(range(buffer), make_args_checked(format_, args...));
return to_string(buffer);
}
Expand All @@ -225,7 +225,7 @@ class prepared_format {
inline std::back_insert_iterator<Container> format_to(
std::back_insert_iterator<Container> out, const Args&... args) const {
internal::container_buffer<Container> buffer(internal::get_container(out));
typedef back_insert_range<internal::basic_buffer<char_type>> range;
typedef back_insert_range<internal::buffer<char_type>> range;
this->vformat_to(range(buffer), make_args_checked(format_, args...));
return out;
}
Expand All @@ -241,7 +241,7 @@ class prepared_format {
template <std::size_t SIZE = inline_buffer_size>
inline typename buffer_context<char_type>::type::iterator format_to(
basic_memory_buffer<char_type, SIZE>& buf, const Args&... args) const {
typedef back_insert_range<internal::basic_buffer<char_type>> range;
typedef back_insert_range<internal::buffer<char_type>> range;
return this->vformat_to(range(buf), make_args_checked(format_, args...));
}

Expand Down
Loading

0 comments on commit 2808395

Please sign in to comment.