Add fmt::bytes

This commit is contained in:
Victor Zverovich 2019-12-24 10:11:47 -08:00
parent dea7fde8b7
commit f219dcd59b
3 changed files with 27 additions and 4 deletions

View File

@ -1513,8 +1513,8 @@ FMT_API void vprint_mojibake(std::FILE*, string_view, format_args);
/** /**
\rst \rst
Prints formatted data to the file *f*. For wide format strings *f* should be Prints formatted data to the file *f*. Strings are assumed to be
in wide-oriented mode set via ``fwide(f, 1)``. Unicode-encoded unless the ``FMT_UNICODE`` macro is set to 0.
**Example**:: **Example**::
@ -1535,8 +1535,9 @@ inline void print(std::FILE* f, const S& format_str, Args&&... args) {
/** /**
\rst \rst
Formats ``args`` according to specifications in ``format_str`` and writes the Formats ``args`` according to specifications in ``format_str`` and writes
output to ``stdout``. the output to ``stdout``. Strings are assumed to be Unicode-encoded unless
the ``FMT_UNICODE`` macro is set to 0.
**Example**:: **Example**::

View File

@ -3112,6 +3112,22 @@ template <typename T> inline const void* ptr(const std::shared_ptr<T>& p) {
return p.get(); return p.get();
} }
class bytes {
private:
string_view data_;
friend struct formatter<bytes>;
public:
explicit bytes(string_view data) : data_(data) {}
};
template <> struct formatter<bytes> : formatter<string_view> {
template <typename FormatContext>
auto format(bytes b, FormatContext& ctx) -> decltype(ctx.out()) {
return formatter<string_view>::format(b.data_, ctx);
}
};
template <typename It, typename Char> struct arg_join : internal::view { template <typename It, typename Char> struct arg_join : internal::view {
It begin; It begin;
It end; It end;

View File

@ -1766,6 +1766,12 @@ TEST(FormatTest, Dynamic) {
EXPECT_EQ("42 and abc1 and 1.5", result); EXPECT_EQ("42 and abc1 and 1.5", result);
} }
TEST(FormatTest, Bytes) {
auto s = fmt::format("{:10}", fmt::bytes("ёжик"));
EXPECT_EQ("ёжик ", s);
EXPECT_EQ(10, s.size());
}
TEST(FormatTest, JoinArg) { TEST(FormatTest, JoinArg) {
using fmt::join; using fmt::join;
int v1[3] = {1, 2, 3}; int v1[3] = {1, 2, 3};