23#include <fmt/format.h>
24#include <fmt/printf.h>
25#include <fmt/ranges.h>
36 return fmt::vformat(fmt, args);
37 }
catch (
const std::exception &e) {
40 "format failed : " + std::string(e.what()) +
"\n fmt string : " + std::string(fmt));
47 return fmt::vformat(fmt, args);
48 }
catch (
const std::exception &e) {
51 "format failed : " + std::string(e.what())
52 +
"\n fmt string : " + fmt::to_string(fmt));
65 template<
typename... T>
80 template<
typename... T>
81 inline std::string
format_printf(std::string_view format,
const T &...args) {
83 return fmt::sprintf(format,
args...);
84 }
catch (
const std::exception &e) {
87 "format failed : " + std::string(e.what())
88 +
"\n fmt string : " + std::string(format));
120 accum += shambase::format(
"{:8} : ",
i);
122 accum += shambase::format(
"\n{:8} : ",
i);
143 using namespace std::string_literals;
144 const std::array units{
"B"s,
"kB"s,
"MB"s,
"GB"s,
"TB"s,
"PB"s,
"EB"s,
"ZB"s,
"YB"s};
147 while (size > 1024) {
169 std::ofstream
myfile(filename);
184 for (std::string::size_type pos{};
185 inout.npos != (pos =
inout.find(what.data(), pos, what.length()));
186 pos +=
with.length()) {
187 inout.replace(pos, what.length(),
with.data(),
with.length());
198 std::string
out =
in;
218 throw std::invalid_argument(
"max len should be above 4");
221 return s.substr(0,
max_len - 5) +
" ...";
241 throw std::invalid_argument(
"max len should be above 4");
244 return "... " + s.substr(s.size() - (
max_len - 4), s.size());
259 return (
str.find(what) != std::string::npos);
276 "the string is too short to be shortened"
278 + shambase::format(
"{} : {} \n {} : {}",
"str",
str,
"len",
len));
291 std::vector<std::string>
ret;
294 while ((pos = s.find(
delimiter)) != std::string::npos) {
295 std::string
substr = s.substr(0, pos);
std::uint32_t u32
32 bit unsigned integer
std::int32_t i32
32 bit integer
This header file contains utility functions related to exception handling in the code.
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.
__attribute__((always_inline)) auto vformat(std
format a string using fmtlib style Cheat sheet : https://hackingcpp.com/cpp/libs/fmt....
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(1024*1024*1024) -> "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
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
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.
std::vector< std::string_view > args
Executable argument list (mapped from argv)