dynarmic/fmt/string.h
MerryMage 097203968f Squashed 'externals/fmt/' changes from 39834389..135ab5cf
135ab5cf Update version
93d95f17 Fix markup
4f15c72f Fix markup
e9b19414 Automatically add package to release
c3d1f604 Fix markup
c96062bf Update changelog and version number
f9c97de4 Add note about errno to the documentation
62df6f27 CMakeLists: Use GNUInstallDirs to set install location
493586cb Fix overflow check
1d751bc6 fix warning in header: signed/unsigned comparison
11415bce Update usage.rst
9982dd01 Fix for warning C5030 in VS2015
42e88c4f Silenced MSVC 2017 constant if expression warning
7a9c1ba1 FMT_VARIADIC_CONST - Support for const variadic methods (#591)
324415c0 Use allocator_traits if available.
5f39721c Fix a warning
ca96acbe Add examples
708d9509 fix(Clang CodeGen): remove warnings
9328a074 Fix handling of fixed enums in clang (#580)
2c077dd4 Enable stream exceptions (#581)
933a33a7 Added MSVC checking for support for string_view.
bef89db6 Fix a bogus -Wduplicated-branches gcc warning (#573)
2a619d96 Make format work with C++17 std::string_view (#571)
e051de37 Use less version 2.6.1 and sudo to fix npm install issues on travis
5de459bf Suppress Clang's warning on zero as a null pointer
16589534 Make ArgMap::init not explicitly instantiated (#563)
3e75d3e0 Fix handling of types convertible to int
89654cd1 to_wstring added
37eb419a Fix noreturn attribute detection (#555)
14d85349 Explicitly cast range length to std::size_t to prevent conversion warnings
c2201ce0 Accept wide chars as integers to prevent conversion warning
6efbccb3 Add one more CMake warning fix
032c8380 Fix a segfault in test on glibc 2.26 #551, take 2
6655e804 Fix a segfault in test on glibc 2.26 #551
d16c4d20 Suppress warning about missing noreturn attribute (#549)
9c56a8ce Make format_arg() accept class hierarchies
ca0e3830 Update README.rst
81790d72 Update format.h to remove C4574 error on MSVC 14.2
30283443 Fix undefined behavior in UDL macro
4045d7fe Fix warning about missing ' character
89c3bc58 Remove warning C4668 in MSVC for FMT_GCC_VERSION and FMT_HAS_GXX_CXX11
4af9421f Adding OpenSpace to the list of projects
1a398b54 Fixed CMake CMP0048 warning.
589ccc16 Bump version
c3817046 Add an error on broken includes
16bdd842 Update scripts
b492316d Update version list
91f4ce02 Automatically update version in release script (#431)

git-subtree-dir: externals/fmt
git-subtree-split: 135ab5cf71ed731fc9fa0653051e7d4884a3652f
2020-04-22 20:41:46 +01:00

148 lines
3.4 KiB
C++

/*
Formatting library for C++ - string utilities
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
#ifdef FMT_INCLUDE
# error "Add the fmt's parent directory and not fmt itself to includes."
#endif
#ifndef FMT_STRING_H_
#define FMT_STRING_H_
#include "format.h"
namespace fmt {
namespace internal {
// A buffer that stores data in ``std::basic_string``.
template <typename Char, typename Allocator = std::allocator<Char> >
class StringBuffer : public Buffer<Char> {
public:
typedef std::basic_string<Char, std::char_traits<Char>, Allocator> StringType;
private:
StringType data_;
protected:
virtual void grow(std::size_t size) FMT_OVERRIDE {
data_.resize(size);
this->ptr_ = &data_[0];
this->capacity_ = size;
}
public:
explicit StringBuffer(const Allocator &allocator = Allocator())
: data_(allocator) {}
// Moves the data to ``str`` clearing the buffer.
void move_to(StringType &str) {
data_.resize(this->size_);
str.swap(data_);
this->capacity_ = this->size_ = 0;
this->ptr_ = FMT_NULL;
}
};
} // namespace internal
/**
\rst
This class template provides operations for formatting and writing data
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:
+---------------+----------------------------+
| Type | Definition |
+===============+============================+
| StringWriter | BasicStringWriter<char> |
+---------------+----------------------------+
| WStringWriter | BasicStringWriter<wchar_t> |
+---------------+----------------------------+
**Example**::
StringWriter out;
out << "The answer is " << 42 << "\n";
This will write the following output to the ``out`` object:
.. code-block:: none
The answer is 42
The output can be moved to a ``std::basic_string`` with ``out.move_to()``.
\endrst
*/
template <typename Char, typename Allocator = std::allocator<Char> >
class BasicStringWriter : public BasicWriter<Char> {
private:
internal::StringBuffer<Char, Allocator> buffer_;
public:
/**
\rst
Constructs a :class:`fmt::BasicStringWriter` object.
\endrst
*/
explicit BasicStringWriter(const Allocator &allocator = Allocator())
: BasicWriter<Char>(buffer_), buffer_(allocator) {}
/**
\rst
Moves the buffer content to *str* clearing the buffer.
\endrst
*/
void move_to(std::basic_string<Char, std::char_traits<Char>, Allocator> &str) {
buffer_.move_to(str);
}
};
typedef BasicStringWriter<char> StringWriter;
typedef BasicStringWriter<wchar_t> WStringWriter;
/**
\rst
Converts *value* to ``std::string`` using the default format for type *T*.
**Example**::
#include "fmt/string.h"
std::string answer = fmt::to_string(42);
\endrst
*/
template <typename T>
std::string to_string(const T &value) {
fmt::MemoryWriter w;
w << value;
return w.str();
}
/**
\rst
Converts *value* to ``std::wstring`` using the default format for type *T*.
**Example**::
#include "fmt/string.h"
std::wstring answer = fmt::to_wstring(42);
\endrst
*/
template <typename T>
std::wstring to_wstring(const T &value) {
fmt::WMemoryWriter w;
w << value;
return w.str();
}
}
#endif // FMT_STRING_H_