MySQL 8.4.0
Source Code Documentation
hexify.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25#ifndef MYSQL_HARNESS_HEXIFY_INCLUDED
26#define MYSQL_HARNESS_HEXIFY_INCLUDED
27
28#include <algorithm> // copy
29#include <array>
30#include <charconv>
31#include <cstdint>
32#include <iterator> // back_inserter
33#include <string>
34#include <utility> // exchange
35
36namespace mysql_harness {
37
38/**
39 * hexdump into a string.
40 *
41 * converts the contents of continous container (has .data() and .size())
42 * as hex values in rows of 16 bytes.
43 *
44 * @param buf a container
45 * @return string containing the hexdump
46 */
47template <class T>
48inline std::string hexify(const T &buf) {
49 std::string out;
50
51 for (auto cur = reinterpret_cast<const uint8_t *>(buf.data()),
52 end = cur + buf.size();
53 cur != end;) {
54 size_t col{};
55 std::array<char, 16L * 3> hexline{
56 '.', '.', ' ', '.', '.', ' ', '.', '.', ' ', '.', '.', ' ',
57 '.', '.', ' ', '.', '.', ' ', '.', '.', ' ', '.', '.', ' ',
58 '.', '.', ' ', '.', '.', ' ', '.', '.', ' ', '.', '.', ' ',
59 '.', '.', ' ', '.', '.', ' ', '.', '.', ' ', '.', '.', ' '};
60 std::array<char, 16> printable;
61
62 for (auto *hexline_pos = hexline.data(); cur != end && col < 16;
63 ++cur, ++col, hexline_pos += 3) {
64 const auto ch = *cur;
65
66 const auto res = std::to_chars(hexline_pos, hexline_pos + 2, ch, 16);
67 if (res.ptr == hexline_pos + 1) {
68 // insert a leading zero, if only one digit was produced.
69 hexline_pos[1] = std::exchange(hexline_pos[0], '0');
70 }
71
72 printable[col] = std::isprint(ch) ? static_cast<char>(ch) : '.';
73 }
74
75 std::copy(hexline.begin(), hexline.end(), std::back_inserter(out));
76 out.append(" "); // separator between hexline and printable
77
78 // only append the chars actually used.
79 for (size_t ndx = 0; ndx < col; ++ndx) {
80 out.push_back(printable[ndx]);
81 }
82 out.append("\n");
83 }
84
85 return out;
86}
87} // namespace mysql_harness
88
89#endif
void copy(Shards< COUNT > &dst, const Shards< COUNT > &src) noexcept
Copy the counters, overwrite destination.
Definition: ut0counter.h:354
Definition: buf0block_hint.cc:30
Definition: common.h:42
std::string hexify(const T &buf)
hexdump into a string.
Definition: hexify.h:48