25#include <fmt/format.h>
26#include <fmt/printf.h>
27#include <fmt/ranges.h>
53 template<
class It,
typename... Tformat>
55 const It &iter,
u32 len,
u32 column_count, fmt::format_string<Tformat...> fmt) {
59 for (
u32 i = 0; i < len; i++) {
61 if (i % column_count == 0) {
63 accum += shambase::format(
"{:8} : ", i);
65 accum += shambase::format(
"\n{:8} : ", i);
69 accum += shambase::format(fmt, iter[i]);
86 return sham::format(
"{:.2f} {}B", res.value, res.prefix);
96 std::ofstream myfile(filename);
110 inline void replace_all(std::string &inout, std::string_view what, std::string_view with) {
111 for (std::string::size_type pos{};
112 inout.npos != (pos = inout.find(what.data(), pos, what.length()));
113 pos += with.length()) {
114 inout.replace(pos, what.length(), with.data(), with.length());
125 std::string out = in;
145 throw std::invalid_argument(
"max len should be above 4");
147 if (s.size() > max_len) {
148 return s.substr(0, max_len - 5) +
" ...";
168 throw std::invalid_argument(
"max len should be above 4");
170 if (s.size() > max_len) {
171 return "... " + s.substr(s.size() - (max_len - 4), s.size());
186 return (str.find(what) != std::string::npos);
201 if (len > str.size()) {
203 "the string is too short to be shortened"
205 + shambase::format(
"{} : {} \n {} : {}",
"str", str,
"len", len));
207 return str.substr(0, str.size() - len);
217 inline std::vector<std::string>
split_str(std::string s, std::string delimiter) {
218 std::vector<std::string> ret;
221 while ((pos = s.find(delimiter)) != std::string::npos) {
222 std::string substr = s.substr(0, pos);
223 if (substr.size() > 0)
224 ret.push_back(substr);
225 s.erase(0, pos + delimiter.length());
std::uint32_t u32
32 bit unsigned integer
This header file contains utility functions related to exception handling in the code.
Convert raw numeric values to human-readable SI-formatted pairs.
human_readable_t to_human_readable(double value)
Convert a raw value to a human-readable scaled form with an SI prefix.
namespace for basic c++ utilities
std::string format_array(const It &iter, u32 len, u32 column_count, fmt::format_string< Tformat... > fmt)
Format an array of elements into a string.
std::string trunc_str_start(std::string s, u32 max_len)
Truncate a string to a specified length, adding an ellipsis at the start if necessary.
void write_string_to_file(std::string filename, std::string s)
dump a string to a file
std::string shorten_string(std::string str, u32 len)
Shortens a string by removing the last specified number of characters.
std::string readable_sizeof(double size)
given a sizeof value return a readble string Example : readable_sizeof(1e9) -> "1....
std::string trunc_str(std::string s, u32 max_len)
Truncate a string to a specified length, adding an ellipsis if necessary.
void replace_all(std::string &inout, std::string_view what, std::string_view with)
replace all occurence of a search string with another
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
std::string increase_indent(std::string in, std::string delim="\n ")
Increase indentation of a string.
std::vector< std::string > split_str(std::string s, std::string delimiter)
Splits a string into a vector of substrings according to a delimiter.
bool contain_substr(std::string str, std::string what)
Check if a substring is present in a given string.