fmtlegacy/include/fmt/string.h

95 lines
2.0 KiB
C
Raw Normal View History

2016-05-19 02:54:52 +00:00
/*
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_
#include "fmt/format.h"
2016-05-19 02:54:52 +00:00
namespace fmt {
2016-07-14 14:41:00 +00:00
/**
\rst
2017-02-17 14:23:16 +00:00
This class template represents a character buffer backed by std::string.
2016-07-14 14:41:00 +00:00
You can use one of the following typedefs for common character types
and the standard allocator:
2017-02-17 14:23:16 +00:00
+----------------+------------------------------+
| Type | Definition |
+================+==============================+
| string_buffer | basic_string_buffer<char> |
+----------------+------------------------------+
| wstring_buffer | basic_string_buffer<wchar_t> |
+----------------+------------------------------+
2016-07-14 14:41:00 +00:00
**Example**::
2017-02-17 14:23:16 +00:00
string_buffer out;
format_to(out, "The answer is {}", 42);
2016-07-14 14:41:00 +00:00
This will write the following output to the ``out`` object:
.. code-block:: none
The answer is 42
The output can be moved to an ``std::string`` with ``out.move_to()``.
\endrst
2017-02-17 14:23:16 +00:00
*/template <typename Char>
class basic_string_buffer : public basic_buffer<Char> {
2016-07-14 14:41:00 +00:00
private:
std::basic_string<Char> str_;
2016-07-14 14:41:00 +00:00
2017-02-17 14:23:16 +00:00
protected:
2017-03-12 14:30:20 +00:00
virtual void grow(std::size_t capacity) {
str_.resize(capacity);
this->set(&str_[0], capacity);
2017-02-17 14:23:16 +00:00
}
2016-07-14 14:41:00 +00:00
2017-02-17 14:23:16 +00:00
public:
2016-07-14 14:41:00 +00:00
/**
\rst
Moves the buffer content to *str* clearing the buffer.
\endrst
*/
void move_to(std::basic_string<Char> &str) {
2017-03-12 14:30:20 +00:00
str_.resize(this->size());
str.swap(str_);
2017-03-12 14:30:20 +00:00
this->resize(0);
this->set(0, 0);
2016-07-14 14:41:00 +00:00
}
};
2017-02-17 14:23:16 +00:00
typedef basic_string_buffer<char> string_buffer;
typedef basic_string_buffer<wchar_t> wstring_buffer;
2016-07-14 14:41:00 +00:00
2016-05-19 02:54:52 +00:00
/**
\rst
Converts *value* to ``std::string`` using the default format for type *T*.
**Example**::
2016-05-19 13:39:03 +00:00
#include "fmt/string.h"
2016-05-19 02:54:52 +00:00
std::string answer = fmt::to_string(42);
\endrst
*/
template <typename T>
std::string to_string(const T &value) {
2017-02-17 14:23:16 +00:00
string_buffer buf;
basic_writer<char>(buf).write(value);
std::string str;
buf.move_to(str);
return str;
2016-05-19 02:54:52 +00:00
}
}
#endif // FMT_STRING_H_