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
18
23#include <fmt/ranges.h>
24#include <string_view>
25#include <array>
26#include <fstream>
27#include <vector>
28
29namespace shambase {
30
49 template<class It, typename... Tformat>
50 inline std::string format_array(
51 const It &iter, u32 len, u32 column_count, fmt::format_string<Tformat...> fmt) {
52
53 std::string accum;
54
55 for (u32 i = 0; i < len; i++) {
56
57 if (i % column_count == 0) {
58 if (i == 0) {
59 accum += sham::format("{:8} : ", i);
60 } else {
61 accum += sham::format("\n{:8} : ", i);
62 }
63 }
64
65 accum += sham::format(fmt, iter[i]);
66 }
67
68 return accum;
69 }
70
80 inline std::string readable_sizeof(double size) {
81 auto res = sham::to_human_readable<false>(size);
82 return sham::format("{:.2f} {}B", res.value, res.prefix);
83 }
84
91 inline void write_string_to_file(std::string filename, std::string s) {
92 std::ofstream myfile(filename);
93 myfile << s;
94 myfile.close();
95 }
96
106 inline void replace_all(std::string &inout, std::string_view what, std::string_view with) {
107 for (std::string::size_type pos{};
108 inout.npos != (pos = inout.find(what.data(), pos, what.length()));
109 pos += with.length()) {
110 inout.replace(pos, what.length(), with.data(), with.length());
111 }
112 }
113
120 inline std::string increase_indent(std::string in, std::string delim = "\n ") {
121 std::string out = in;
122 replace_all(out, "\n", delim);
123 return " " + out;
124 }
125
138 inline std::string trunc_str(std::string s, u32 max_len) {
139
140 if (max_len < 5)
141 throw std::invalid_argument("max len should be above 4");
142
143 if (s.size() > max_len) {
144 return s.substr(0, max_len - 5) + " ...";
145 } else {
146 return s;
147 }
148 }
149
162 inline std::string trunc_str_start(std::string s, u32 max_len) {
163 if (max_len < 5)
164 throw std::invalid_argument("max len should be above 4");
165
166 if (s.size() > max_len) {
167 return "... " + s.substr(s.size() - (max_len - 4), s.size());
168 } else {
169 return s;
170 }
171 }
172
181 inline bool contain_substr(std::string str, std::string what) {
182 return (str.find(what) != std::string::npos);
183 }
184
196 inline std::string shorten_string(std::string str, u32 len) {
197 if (len > str.size()) {
199 "the string is too short to be shortened"
200 "\n args : "
201 + sham::format("{} : {} \n {} : {}", "str", str, "len", len));
202 }
203 return str.substr(0, str.size() - len);
204 }
205
213 inline std::vector<std::string> split_str(std::string s, std::string delimiter) {
214 std::vector<std::string> ret;
215
216 size_t pos = 0;
217 while ((pos = s.find(delimiter)) != std::string::npos) {
218 std::string substr = s.substr(0, pos);
219 if (substr.size() > 0)
220 ret.push_back(substr);
221 s.erase(0, pos + delimiter.length());
222 }
223 if (s.size() > 0)
224 ret.push_back(s);
225
226 return ret;
227 }
228
229} // namespace shambase
std::uint32_t u32
32 bit unsigned integer
This header file contains utility functions related to exception handling in the code.
Core formatting functions: format, vformat, and format_printf.
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.
Definition string.hpp:50
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:162
void write_string_to_file(std::string filename, std::string s)
dump a string to a file
Definition string.hpp:91
std::string shorten_string(std::string str, u32 len)
Shortens a string by removing the last specified number of characters.
Definition string.hpp:196
std::string readable_sizeof(double size)
given a sizeof value return a readble string Example : readable_sizeof(1e9) -> "1....
Definition string.hpp:80
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:138
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:106
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.
Definition string.hpp:120
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:213
bool contain_substr(std::string str, std::string what)
Check if a substring is present in a given string.
Definition string.hpp:181