1 #ifndef MARNAV_UTILS_BITSET_STRING_HPP
2 #define MARNAV_UTILS_BITSET_STRING_HPP
4 /// Copyright (c) 2016 Mario Konrad <mario.konrad@gmx.net>
5 /// The code is licensed under the BSD License.
14 /// Renders the bitset as string.
16 /// @param[in] bits The bits to render.
17 /// @return String representing the bitset as continous stream of '0' and '1'.
18 template <class T
> std::string
to_string(const bitset
<T
> & bits
)
21 result
.reserve(bits
.size());
22 for (auto const & b
: bits
)
27 /// Renders the bitset as string, packed by specified number of bits.
29 /// @param[in] bits The bits to render.
30 /// @param[in] pack Number of bits per pack.
31 /// @param[in] delm Delimitter to separate the packs.
32 /// @return String representing the bitset as stream of '0' and '1', separated by
35 std::string
to_string(const bitset
<T
> & bits
, std::size_t pack
, char delm
= ' ')
37 if ((pack
== 0) || (pack
>= bits
.size()))
38 return to_string(bits
);
42 // reserve space, wasted space at most: sizeof(delm) characters
43 result
.reserve(bits
.size() + sizeof(delm
) * (bits
.size() / pack
));
46 auto begin
= bits
.begin();
48 result
+= '0' + *begin
;
50 if (begin
== bits
.end())