From db780cb119b604c1998cc91ae73e8c9439c540be Mon Sep 17 00:00:00 2001 From: Andrey Glebov Date: Mon, 26 Dec 2016 19:36:56 +0300 Subject: [PATCH] Add std::basic_string allocator support to StringRef, StringBuffer and relatives (#441) * - added basic_string allocator support to BasicStringRef, BasicCStringRef, BasicWriter::str, StringBuffer and BasicStringWriter * - removed templated str() and to_string() function - code style fixes --- fmt/format.h | 18 ++++++++++++------ fmt/string.h | 29 ++++++++++++++++++----------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/fmt/format.h b/fmt/format.h index d926d4ce..b8713978 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -440,7 +440,8 @@ class BasicFormatter; /** \rst - A string reference. It can be constructed from a C string or ``std::string``. + A string reference. It can be constructed from a C string or + ``std::basic_string``. You can use one of the following typedefs for common character types: @@ -483,10 +484,12 @@ class BasicStringRef { /** \rst - Constructs a string reference from an ``std::string`` object. + Constructs a string reference from a ``std::basic_string`` object. \endrst */ - BasicStringRef(const std::basic_string &s) + template + BasicStringRef( + const std::basic_string, Allocator> &s) : data_(s.c_str()), size_(s.size()) {} /** @@ -539,7 +542,7 @@ typedef BasicStringRef WStringRef; /** \rst A reference to a null terminated string. It can be constructed from a C - string or ``std::string``. + string or ``std::basic_string``. You can use one of the following typedefs for common character types: @@ -572,10 +575,13 @@ class BasicCStringRef { /** \rst - Constructs a string reference from an ``std::string`` object. + Constructs a string reference from a ``std::basic_string`` object. \endrst */ - BasicCStringRef(const std::basic_string &s) : data_(s.c_str()) {} + template + BasicCStringRef( + const std::basic_string, Allocator> &s) + : data_(s.c_str()) {} /** Returns the pointer to a C string. */ const Char *c_str() const { return data_; } diff --git a/fmt/string.h b/fmt/string.h index ba1fc692..ccf46ee1 100644 --- a/fmt/string.h +++ b/fmt/string.h @@ -16,11 +16,14 @@ namespace fmt { namespace internal { -// A buffer that stores data in ``std::string``. -template +// A buffer that stores data in ``std::basic_string``. +template > class StringBuffer : public Buffer { + public: + typedef std::basic_string, Allocator> StringType; + private: - std::basic_string data_; + StringType data_; protected: virtual void grow(std::size_t size) FMT_OVERRIDE { @@ -30,8 +33,11 @@ class StringBuffer : public Buffer { } public: + explicit StringBuffer(const Allocator &allocator = Allocator()) + : data_(allocator) {} + // Moves the data to ``str`` clearing the buffer. - void move_to(std::basic_string &str) { + void move_to(StringType &str) { data_.resize(this->size_); str.swap(data_); this->capacity_ = this->size_ = 0; @@ -43,8 +49,8 @@ class StringBuffer : public Buffer { /** \rst This class template provides operations for formatting and writing data - into a character stream. The output is stored in ``std::string`` that grows - dynamically. + into a character stream. The output is stored in a ``std::basic_string`` + that grows dynamically. You can use one of the following typedefs for common character types and the standard allocator: @@ -68,13 +74,13 @@ class StringBuffer : public Buffer { The answer is 42 - The output can be moved to an ``std::string`` with ``out.move_to()``. + The output can be moved to a ``std::basic_string`` with ``out.move_to()``. \endrst */ -template +template > class BasicStringWriter : public BasicWriter { private: - internal::StringBuffer buffer_; + internal::StringBuffer buffer_; public: /** @@ -82,14 +88,15 @@ class BasicStringWriter : public BasicWriter { Constructs a :class:`fmt::BasicStringWriter` object. \endrst */ - BasicStringWriter() : BasicWriter(buffer_) {} + explicit BasicStringWriter(const Allocator &allocator = Allocator()) + : BasicWriter(buffer_), buffer_(allocator) {} /** \rst Moves the buffer content to *str* clearing the buffer. \endrst */ - void move_to(std::basic_string &str) { + void move_to(std::basic_string, Allocator> &str) { buffer_.move_to(str); } };