cepgen is hosted by Hepforge, IPPP Durham
CepGen N/A
Central exclusive processes event generator
String.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2024 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGen_Utils_String_h
20#define CepGen_Utils_String_h
21
22#include <algorithm>
23#include <functional>
24#include <numeric>
25#include <set>
26#include <string>
27#include <vector>
28
29namespace cepgen {
30 class Limits;
31 class ParametersList;
32} // namespace cepgen
33namespace cepgen::utils {
35 template <typename T>
36 inline std::string toString(const T& obj) {
37 return std::to_string(obj);
38 }
40 template <>
41 std::string toString(const Limits&);
43 template <>
44 std::string toString(const ParametersList&);
46 template <>
47 inline std::string toString(const std::string& obj) {
48 return obj;
49 }
50 template <>
51 std::string toString(const double&);
52 template <>
53 std::string toString(const std::wstring&);
54 std::wstring toWstring(const std::string& str);
55 std::string toCamelCase(const std::string&, bool lower = true);
56 bool isInt(const std::string&);
57 bool isFloat(const std::string&);
58
60 template <typename... Args>
61 inline std::string format(const std::string& fmt, Args... args) {
62 // first check how much space is required for output buffer
63 size_t size = snprintf(nullptr, 0, fmt.data(), args...) + 1; // extra space for last '\0'
64 if (size <= 0)
65 return fmt;
66 std::vector<char> buffer(size);
67 snprintf(buffer.data(), size, fmt.data(), args...);
68 return std::string(buffer.data(), buffer.data() + size - 1); // strip last '\0'
69 }
71 template <typename... Args>
72 inline std::string format(const std::wstring& fmt, Args... args) {
73 return format(toString(fmt), args...);
74 }
75 std::string demangle(const char*);
76 std::string timeAs(const std::string& fmt);
77 std::string yesno(bool test);
80 template <typename T>
81 std::string boldify(T str);
83 enum class Colour {
84 none = -1,
85 reset = 0,
86 black = 30,
87 red = 31,
88 green = 32,
89 yellow = 33,
90 blue = 34,
91 magenta = 35,
92 cyan = 36,
93 white = 37
94 };
95 std::ostream& operator<<(std::ostream&, const Colour&);
96 enum struct Modifier : int16_t {
97 none = -1,
98 reset = 0,
99 bold = 1,
100 dimmed = 1 << 1,
101 italic = 1 << 2,
102 underline = 1 << 3,
103 blink = 1 << 4,
104 reverse = 1 << 6
105 };
106 std::ostream& operator<<(std::ostream&, const Modifier&);
109 std::string colourise(const std::string&, const Colour&, const Modifier& = Modifier::none);
111 std::string sanitise(const std::string&);
113 std::string parseSpecialChars(const std::string&);
115 size_t replaceAll(std::string& str, const std::string& from, const std::string& to);
117 std::string replaceAll(const std::string& str, const std::string& from, const std::string& to);
119 std::string replaceAll(const std::string& str, const std::vector<std::pair<std::string, std::string> >& keys);
121 std::vector<std::string> split(const std::string&, char, bool trim_parts = false);
123 template <typename T>
124 inline std::string merge(const T& val, const std::string&) {
125 return toString(val);
126 }
128 template <typename T>
129 std::string merge(const std::vector<T>&, const std::string&);
131 template <typename T>
132 std::string merge(const std::vector<std::vector<T> >&, const std::string&);
134 template <typename T, size_t N>
135 inline std::string merge(const std::array<T, N>& arr, const std::string& delim) {
136 return merge(std::vector<T>(arr.begin(), arr.end()), delim);
137 }
139 inline std::string merge(const std::string& val, const std::string&) { return val; }
141 std::string merge(const ParametersList&, const std::string&);
143 std::string merge(const Limits&, const std::string&);
144
145 std::string toUpper(const std::string&);
146 std::string toLower(const std::string&);
147
152 std::vector<std::string> between(const std::string& str, const std::string& beg, const std::string& end);
153 std::string s(const std::string&, float, bool = true);
154
156 template <class T>
157 inline std::string repr(const std::vector<T>& vec,
158 const std::function<std::string(const T&)>& printer,
159 const std::string& sep = ",") {
160 if (vec.empty())
161 return "{}";
162 return std::accumulate(
163 std::next(vec.begin()), vec.end(), printer(*vec.begin()), [&printer, &sep](std::string str, const T& xv) {
164 return std::move(str) + sep + printer(xv);
165 });
166 }
168 template <class T>
169 inline std::string repr(const std::vector<T>& vec, const std::string& sep = ",") {
170 return repr<T>(vec, [](const T& xv) { return toString(xv); }, sep);
171 }
173 template <class T>
174 inline std::string repr(const std::set<T>& vec, const std::string& sep = ",") {
175 return repr<T>(std::vector(vec.begin(), vec.end()), sep);
176 }
177 std::string randomString(size_t size);
178 std::string ltrim(const std::string& str);
179 std::string rtrim(const std::string& str);
180 inline std::string trim(const std::string& str) { return ltrim(rtrim(str)); }
181 std::string strip(const std::string&);
182 bool startsWith(const std::string&, const std::string&);
183 bool endsWith(const std::string&, const std::string&);
184 std::string describeError(int error_number);
185
186} // namespace cepgen::utils
187
188#endif
Validity interval for a variable.
Definition Limits.h:28
Collection of utilities.
std::string format(const std::string &fmt, Args... args)
Format a string using a printf style format descriptor.
Definition String.h:61
Modifier operator|(const Modifier &, const Modifier &)
std::string yesno(bool test)
Human-readable boolean printout Boldify a string for TTY-type output streams.
std::string timeAs(const std::string &fmt)
Return the formatted date/time now.
std::string s(const std::string &, float, bool=true)
Add a trailing "s" when needed.
std::string strip(const std::string &)
Strip all special characters from string.
std::ostream & operator<<(std::ostream &, const Colour &)
std::string parseSpecialChars(const std::string &)
Transform all emoji-like special characters into their LaTeX representation.
std::vector< K > keys(const std::map< K, T > &coll)
Retrieve all keys from a map.
Definition Collections.h:32
std::vector< std::string > between(const std::string &str, const std::string &beg, const std::string &end)
Get a (list of) substring(s) between two characters chains.
std::string repr(const std::vector< T > &vec, const std::function< std::string(const T &)> &printer, const std::string &sep=",")
Helper to print a vector.
Definition String.h:157
std::string toLower(const std::string &)
Lowercase version of a string.
std::string trim(const std::string &str)
Trim leading and trailing spaces.
Definition String.h:180
std::string merge(const T &val, const std::string &)
Merge a printable type in a single string.
Definition String.h:124
Colour
TTY-type enumeration of colours.
Definition String.h:83
std::string sanitise(const std::string &)
Replace all unsafe characters to build a computer-readable (and filename-safe) string.
std::string toCamelCase(const std::string &, bool lower=true)
Convert any case into a camelCase string.
std::string ltrim(const std::string &str)
Trim leading spaces.
bool endsWith(const std::string &, const std::string &)
Check if a string ends with a given token.
bool isFloat(const std::string &)
Check if a string is also a floating point number.
bool startsWith(const std::string &, const std::string &)
Check if a string starts with a given token.
std::string boldify(T str)
std::string describeError(int error_number)
Describe an error code.
std::string toUpper(const std::string &)
Capitalise a string.
std::string colourise(const std::string &, const Colour &, const Modifier &=Modifier::none)
Colourise a string for TTY-type output streams.
std::string randomString(size_t size)
Generate a random string of a given size.
size_t replaceAll(std::string &str, const std::string &from, const std::string &to)
Replace all occurrences of a text by another.
std::string demangle(const char *)
Demangle a type id if possible.
std::string rtrim(const std::string &str)
Trim trailing spaces.
std::vector< std::string > split(const std::string &, char, bool trim_parts=false)
Split a string according to a separation character.
std::wstring toWstring(const std::string &str)
Convert a standard characters to a wide characters string.
std::string toString(const T &obj)
Transform any type into a string.
Definition String.h:36
bool isInt(const std::string &)
Check if a string is also an integer.
Common namespace for this Monte Carlo generator.
Definition Handler.h:26