Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
20#include "exception.hpp"
21#include <fmt/base.h>
22#include <fmt/core.h>
23#include <fmt/format.h>
24#include <fmt/printf.h>
25#include <fmt/ranges.h>
26#include <string_view>
27#include <array>
28#include <fstream>
29#include <vector>
30
31namespace shambase {
32
33 inline __attribute__((always_inline)) auto vformat(std::string_view fmt, fmt::format_args args)
34 -> std::string {
35 try {
36 return fmt::vformat(fmt, args);
37 } catch (const std::exception &e) {
38
40 "format failed : " + std::string(e.what()) + "\n fmt string : " + std::string(fmt));
41 }
42 }
43
44 inline __attribute__((always_inline)) auto vformat(fmt::string_view fmt, fmt::format_args args)
45 -> std::string {
46 try {
47 return fmt::vformat(fmt, args);
48 } catch (const std::exception &e) {
49
51 "format failed : " + std::string(e.what())
52 + "\n fmt string : " + fmt::to_string(fmt));
53 }
54 }
55
65 template<typename... T>
66 inline __attribute__((always_inline)) auto format(fmt::format_string<T...> fmt, T &&...args)
67 -> std::string {
68 return shambase::vformat(fmt, fmt::make_format_args(args...));
69 }
70
80 template<typename... T>
81 inline std::string format_printf(std::string_view format, const T &...args) {
82 try {
83 return fmt::sprintf(format, args...);
84 } catch (const std::exception &e) {
85
87 "format failed : " + std::string(e.what())
88 + "\n fmt string : " + std::string(format));
89 }
90 }
91
110 template<class It, typename... Tformat>
111 inline std::string format_array(
112 const It &iter, u32 len, u32 column_count, fmt::format_string<Tformat...> fmt) {
113
114 std::string accum;
115
116 for (u32 i = 0; i < len; i++) {
117
118 if (i % column_count == 0) {
119 if (i == 0) {
120 accum += shambase::format("{:8} : ", i);
121 } else {
122 accum += shambase::format("\n{:8} : ", i);
123 }
124 }
125
126 accum += shambase::format(fmt, iter[i]);
127 }
128
129 return accum;
130 }
131
139 inline std::string readable_sizeof(double size) {
140
141 i32 i = 0;
142
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};
145
146 if (size >= 0) {
147 while (size > 1024) {
148 size /= 1024;
149 i++;
150 }
151 } else {
152 i = 9;
153 }
154
155 if (i > 8) {
156 return format_printf("%s", "err val");
157 } else {
158 return format_printf("%.2f %s", size, units[i]);
159 }
160 }
161
168 inline void write_string_to_file(std::string filename, std::string s) {
169 std::ofstream myfile(filename);
170 myfile << s;
171 myfile.close();
172 }
173
183 inline void replace_all(std::string &inout, std::string_view what, std::string_view with) {
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());
188 }
189 }
190
197 inline std::string increase_indent(std::string in, std::string delim = "\n ") {
198 std::string out = in;
199 replace_all(out, "\n", delim);
200 return " " + out;
201 }
202
215 inline std::string trunc_str(std::string s, u32 max_len) {
216
217 if (max_len < 5)
218 throw std::invalid_argument("max len should be above 4");
219
220 if (s.size() > max_len) {
221 return s.substr(0, max_len - 5) + " ...";
222 } else {
223 return s;
224 }
225 }
226
239 inline std::string trunc_str_start(std::string s, u32 max_len) {
240 if (max_len < 5)
241 throw std::invalid_argument("max len should be above 4");
242
243 if (s.size() > max_len) {
244 return "... " + s.substr(s.size() - (max_len - 4), s.size());
245 } else {
246 return s;
247 }
248 }
249
258 inline bool contain_substr(std::string str, std::string what) {
259 return (str.find(what) != std::string::npos);
260 }
261
273 inline std::string shorten_string(std::string str, u32 len) {
274 if (len > str.size()) {
276 "the string is too short to be shortened"
277 "\n args : "
278 + shambase::format("{} : {} \n {} : {}", "str", str, "len", len));
279 }
280 return str.substr(0, str.size() - len);
281 }
282
290 inline std::vector<std::string> split_str(std::string s, std::string delimiter) {
291 std::vector<std::string> ret;
292
293 size_t pos = 0;
294 while ((pos = s.find(delimiter)) != std::string::npos) {
295 std::string substr = s.substr(0, pos);
296 if (substr.size() > 0)
297 ret.push_back(substr);
298 s.erase(0, pos + delimiter.length());
299 }
300 if (s.size() > 0)
301 ret.push_back(s);
302
303 return ret;
304 }
305
306} // namespace shambase
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.
Definition string.hpp:111
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.
Definition string.hpp:239
__attribute__((always_inline)) auto vformat(std
format a string using fmtlib style Cheat sheet : https://hackingcpp.com/cpp/libs/fmt....
Definition string.hpp:33
void write_string_to_file(std::string filename, std::string s)
dump a string to a file
Definition string.hpp:168
std::string shorten_string(std::string str, u32 len)
Shortens a string by removing the last specified number of characters.
Definition string.hpp:273
std::string readable_sizeof(double size)
given a sizeof value return a readble string Example : readable_sizeof(1024*1024*1024) -> "1....
Definition string.hpp:139
std::string trunc_str(std::string s, u32 max_len)
Truncate a string to a specified length, adding an ellipsis if necessary.
Definition string.hpp:215
void replace_all(std::string &inout, std::string_view what, std::string_view with)
replace all occurence of a search string with another
Definition string.hpp:183
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.
Definition string.hpp:197
std::vector< std::string > split_str(std::string s, std::string delimiter)
Splits a string into a vector of substrings according to a delimiter.
Definition string.hpp:290
bool contain_substr(std::string str, std::string what)
Check if a substring is present in a given string.
Definition string.hpp:258
std::vector< std::string_view > args
Executable argument list (mapped from argv)
Definition cmdopt.cpp:63