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/base.h>
24#include <fmt/core.h>
25#include <fmt/format.h>
26#include <fmt/printf.h>
27#include <fmt/ranges.h>
28#include <string_view>
29#include <array>
30#include <fstream>
31#include <vector>
32
33namespace shambase {
34
53 template<class It, typename... Tformat>
54 inline std::string format_array(
55 const It &iter, u32 len, u32 column_count, fmt::format_string<Tformat...> fmt) {
56
57 std::string accum;
58
59 for (u32 i = 0; i < len; i++) {
60
61 if (i % column_count == 0) {
62 if (i == 0) {
63 accum += shambase::format("{:8} : ", i);
64 } else {
65 accum += shambase::format("\n{:8} : ", i);
66 }
67 }
68
69 accum += shambase::format(fmt, iter[i]);
70 }
71
72 return accum;
73 }
74
84 inline std::string readable_sizeof(double size) {
85 auto res = sham::to_human_readable<false>(size);
86 return sham::format("{:.2f} {}B", res.value, res.prefix);
87 }
88
95 inline void write_string_to_file(std::string filename, std::string s) {
96 std::ofstream myfile(filename);
97 myfile << s;
98 myfile.close();
99 }
100
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());
115 }
116 }
117
124 inline std::string increase_indent(std::string in, std::string delim = "\n ") {
125 std::string out = in;
126 replace_all(out, "\n", delim);
127 return " " + out;
128 }
129
142 inline std::string trunc_str(std::string s, u32 max_len) {
143
144 if (max_len < 5)
145 throw std::invalid_argument("max len should be above 4");
146
147 if (s.size() > max_len) {
148 return s.substr(0, max_len - 5) + " ...";
149 } else {
150 return s;
151 }
152 }
153
166 inline std::string trunc_str_start(std::string s, u32 max_len) {
167 if (max_len < 5)
168 throw std::invalid_argument("max len should be above 4");
169
170 if (s.size() > max_len) {
171 return "... " + s.substr(s.size() - (max_len - 4), s.size());
172 } else {
173 return s;
174 }
175 }
176
185 inline bool contain_substr(std::string str, std::string what) {
186 return (str.find(what) != std::string::npos);
187 }
188
200 inline std::string shorten_string(std::string str, u32 len) {
201 if (len > str.size()) {
203 "the string is too short to be shortened"
204 "\n args : "
205 + shambase::format("{} : {} \n {} : {}", "str", str, "len", len));
206 }
207 return str.substr(0, str.size() - len);
208 }
209
217 inline std::vector<std::string> split_str(std::string s, std::string delimiter) {
218 std::vector<std::string> ret;
219
220 size_t pos = 0;
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());
226 }
227 if (s.size() > 0)
228 ret.push_back(s);
229
230 return ret;
231 }
232
233} // 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:54
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:166
void write_string_to_file(std::string filename, std::string s)
dump a string to a file
Definition string.hpp:95
std::string shorten_string(std::string str, u32 len)
Shortens a string by removing the last specified number of characters.
Definition string.hpp:200
std::string readable_sizeof(double size)
given a sizeof value return a readble string Example : readable_sizeof(1e9) -> "1....
Definition string.hpp:84
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:142
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:110
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:124
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:217
bool contain_substr(std::string str, std::string what)
Check if a substring is present in a given string.
Definition string.hpp:185