]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/pretty_binary.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
9 static std::string
pretty_binary_string(const S
& bin
)
14 pretty
.reserve(bin
.length() * 3);
15 auto printable
= [](unsigned char c
) -> bool {
16 return (c
>= 32) && (c
<= 126);
18 auto append_hex
= [&](unsigned char c
) {
19 static const char hex
[16] = {'0', '1', '2', '3',
23 pretty
.push_back(hex
[c
/ 16]);
24 pretty
.push_back(hex
[c
% 16]);
27 bool strmode
= printable(bin
[0]);
29 pretty
.push_back('\'');
31 pretty
.push_back('0');
32 pretty
.push_back('x');
34 for (size_t i
= 0; i
< bin
.length(); ++i
) {
35 // change mode from hex to str if following 3 characters are printable
37 if (!printable(bin
[i
])) {
38 pretty
.push_back('\'');
39 pretty
.push_back('0');
40 pretty
.push_back('x');
44 if (i
+ 2 < bin
.length() &&
46 printable(bin
[i
+ 1]) &&
47 printable(bin
[i
+ 2])) {
48 pretty
.push_back('\'');
54 pretty
.push_back('\'');
55 pretty
.push_back(bin
[i
]);
62 pretty
.push_back('\'');
67 std::string
pretty_binary_string_reverse(const std::string
& pretty
);