1 /*
2 Formatting library for C++ - string utilities
3
4 Copyright (c) 2012 - 2016, Victor Zverovich
5 All rights reserved.
6
7 For the license information refer to format.h.
8 */
9
10 #ifndef FMT_STRING_H_
11 #define FMT_STRING_H_
12
13 #include "format.h"
14
15 namespace fmt {
16
17 namespace internal {
18
19 // A buffer that stores data in ``std::basic_string``.
20 template <typename Char, typename Allocator = std::allocator<Char> >
21 class StringBuffer : public Buffer<Char> {
22 public:
23 typedef std::basic_string<Char, std::char_traits<Char>, Allocator> StringType;
24
25 private:
26 StringType data_;
27
28 protected:
grow(std::size_t size)29 virtual void grow(std::size_t size) FMT_OVERRIDE {
30 data_.resize(size);
31 this->ptr_ = &data_[0];
32 this->capacity_ = size;
33 }
34
35 public:
36 explicit StringBuffer(const Allocator &allocator = Allocator())
data_(allocator)37 : data_(allocator) {}
38
39 // Moves the data to ``str`` clearing the buffer.
move_to(StringType & str)40 void move_to(StringType &str) {
41 data_.resize(this->size_);
42 str.swap(data_);
43 this->capacity_ = this->size_ = 0;
44 this->ptr_ = FMT_NULL;
45 }
46 };
47 } // namespace internal
48
49 /**
50 \rst
51 This class template provides operations for formatting and writing data
52 into a character stream. The output is stored in a ``std::basic_string``
53 that grows dynamically.
54
55 You can use one of the following typedefs for common character types
56 and the standard allocator:
57
58 +---------------+----------------------------+
59 | Type | Definition |
60 +===============+============================+
61 | StringWriter | BasicStringWriter<char> |
62 +---------------+----------------------------+
63 | WStringWriter | BasicStringWriter<wchar_t> |
64 +---------------+----------------------------+
65
66 **Example**::
67
68 StringWriter out;
69 out << "The answer is " << 42 << "\n";
70
71 This will write the following output to the ``out`` object:
72
73 .. code-block:: none
74
75 The answer is 42
76
77 The output can be moved to a ``std::basic_string`` with ``out.move_to()``.
78 \endrst
79 */
80 template <typename Char, typename Allocator = std::allocator<Char> >
81 class BasicStringWriter : public BasicWriter<Char> {
82 private:
83 internal::StringBuffer<Char, Allocator> buffer_;
84
85 public:
86 /**
87 \rst
88 Constructs a :class:`fmt::BasicStringWriter` object.
89 \endrst
90 */
91 explicit BasicStringWriter(const Allocator &allocator = Allocator())
92 : BasicWriter<Char>(buffer_), buffer_(allocator) {}
93
94 /**
95 \rst
96 Moves the buffer content to *str* clearing the buffer.
97 \endrst
98 */
move_to(std::basic_string<Char,std::char_traits<Char>,Allocator> & str)99 void move_to(std::basic_string<Char, std::char_traits<Char>, Allocator> &str) {
100 buffer_.move_to(str);
101 }
102 };
103
104 typedef BasicStringWriter<char> StringWriter;
105 typedef BasicStringWriter<wchar_t> WStringWriter;
106
107 /**
108 \rst
109 Converts *value* to ``std::string`` using the default format for type *T*.
110
111 **Example**::
112
113 #include "fmt/string.h"
114
115 std::string answer = fmt::to_string(42);
116 \endrst
117 */
118 template <typename T>
to_string(const T & value)119 std::string to_string(const T &value) {
120 fmt::MemoryWriter w;
121 w << value;
122 return w.str();
123 }
124 }
125
126 #endif // FMT_STRING_H_
127