Coverage Report

Created: 2025-10-10 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brpc/src/butil/binary_printer.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
// Date: Thu Nov 22 13:57:56 CST 2012
19
20
#ifndef BUTIL_BINARY_PRINTER_H
21
#define BUTIL_BINARY_PRINTER_H
22
23
#include "butil/strings/string_piece.h"
24
25
namespace butil {
26
class IOBuf;
27
28
// Print binary content within max length.
29
// The printing format is optimized for humans and may change in future.
30
31
class ToPrintable {
32
public:
33
    static const size_t DEFAULT_MAX_LENGTH = 64;
34
    
35
    ToPrintable(const IOBuf& b, size_t max_length = DEFAULT_MAX_LENGTH)
36
0
        : _iobuf(&b), _max_length(max_length) {}
37
38
    ToPrintable(const StringPiece& str, size_t max_length = DEFAULT_MAX_LENGTH)
39
0
        : _iobuf(NULL), _str(str), _max_length(max_length) {}
40
41
    ToPrintable(const void* data, size_t n, size_t max_length = DEFAULT_MAX_LENGTH)
42
0
        : _iobuf(NULL), _str((const char*)data, n), _max_length(max_length) {}
43
    
44
    void Print(std::ostream& os) const;
45
46
private:
47
    const IOBuf* _iobuf;
48
    StringPiece _str;
49
    size_t _max_length;
50
};
51
52
// Keep old name for compatibility.
53
typedef ToPrintable PrintedAsBinary;
54
55
0
inline std::ostream& operator<<(std::ostream& os, const ToPrintable& p) {
56
0
    p.Print(os);
57
0
    return os;
58
0
}
59
60
// Convert binary data to a printable string.
61
std::string ToPrintableString(const IOBuf& data,
62
                              size_t max_length = ToPrintable::DEFAULT_MAX_LENGTH);
63
std::string ToPrintableString(const StringPiece& data,
64
                              size_t max_length = ToPrintable::DEFAULT_MAX_LENGTH);
65
std::string ToPrintableString(const void* data, size_t n,
66
                              size_t max_length = ToPrintable::DEFAULT_MAX_LENGTH);
67
68
} // namespace butil
69
70
#endif  // BUTIL_BINARY_PRINTER_H