MOS Source Code
Loading...
Searching...
No Matches
kutils.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/misc/kutils.hpp"
4
6
7#include <bits/c++config.h>
8#include <mos/string.hpp>
9
10static const int HEXDUMP_COLS = 16;
11
12void 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
48{
50 size_t start = 0;
51 size_t end = str.find(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 - start); !substr.empty())
56 result.push_back(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(delim, start);
63 }
64
65 add_substr(str, start, end);
66 return result;
67}
constexpr size_t find(CharT c, size_t start=0) const
constexpr basic_string_view substr(size_t start, size_t end=-1) const
constexpr bool empty() const
static constexpr auto npos
Definition string.hpp:35
auto push_back(const TItem &value) noexcept
Definition vector.hpp:143
mos::vector< mos::string > split_string(mos::string_view str, char delim)
Definition kutils.cpp:47
void hexdump(const char *data, const size_t len)
Definition kutils.cpp:12
static const int HEXDUMP_COLS
Definition kutils.cpp:10
basic_string_view< char > string_view
mos::basic_string< char > string
Definition string.hpp:395
#define pr_info(fmt,...)
Definition printk.hpp:35
#define pr_cont(fmt,...)
Definition printk.hpp:41
#define PTR_FMT
Definition types.h:29
unsigned long ptr_t
Definition types.h:21