| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "mos/misc/kutils.hpp" |
| 4 | |
| 5 | #include "mos/syslog/printk.hpp" |
| 6 | |
| 7 | #include <bits/c++config.h> |
| 8 | #include <mos/string.hpp> |
| 9 | |
| 10 | static const int HEXDUMP_COLS = 16; |
| 11 | |
| 12 | void hexdump(const char *data, const size_t len) |
| 13 | { |
| 14 | if (len) |
| 15 | pr_info(" " PTR_FMT ": " , (ptr_t) data); |
| 16 | |
| 17 | for (size_t i = 0; i < len; i++) |
| 18 | { |
| 19 | pr_cont("%02hhx " , (char) data[i]); |
| 20 | if ((i + 1) % HEXDUMP_COLS == 0) |
| 21 | { |
| 22 | for (size_t j = i - (HEXDUMP_COLS - 1); j <= i; j++) |
| 23 | { |
| 24 | const char c = data[j]; |
| 25 | pr_cont("%c" , c >= 32 && c <= 126 ? c : '.'); |
| 26 | } |
| 27 | |
| 28 | if (i + 1 < len) |
| 29 | pr_info(" " PTR_FMT ": " , (ptr_t) (data + i + 1)); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | if (len % HEXDUMP_COLS != 0) |
| 34 | { |
| 35 | const size_t spaces = (HEXDUMP_COLS - (len % HEXDUMP_COLS)) * 3; |
| 36 | pr_cont("%*c" , (int) spaces, ' '); |
| 37 | for (size_t i = len - (len % HEXDUMP_COLS); i < len; i++) |
| 38 | { |
| 39 | const char c = data[i]; |
| 40 | pr_cont("%c" , c >= 32 && c <= 126 ? c : '.'); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pr_info("" ); |
| 45 | } |
| 46 | |
| 47 | mos::vector<mos::string> split_string(mos::string_view str, char delim) |
| 48 | { |
| 49 | mos::vector<mos::string> result; |
| 50 | size_t start = 0; |
| 51 | size_t end = str.find(c: delim); |
| 52 | |
| 53 | const auto add_substr = [&](mos::string_view str, size_t start, size_t end) |
| 54 | { |
| 55 | if (const auto substr = str.substr(start, end: end - start); !substr.empty()) |
| 56 | result.push_back(value: mos::string(substr)); |
| 57 | }; |
| 58 | while (end != mos::string::npos) |
| 59 | { |
| 60 | add_substr(str, start, end); |
| 61 | start = end + 1; |
| 62 | end = str.find(c: delim, start); |
| 63 | } |
| 64 | |
| 65 | add_substr(str, start, end); |
| 66 | return result; |
| 67 | } |
| 68 | |