cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
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 <unordered_map>
28#include <vector>
29
30namespace cepgen {
31 class Limits;
32 class ParametersList;
33 namespace 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&);
107 Modifier operator|(const Modifier&, 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 = 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
151 std::vector<std::string> between(const std::string& str, const std::string& beg, const std::string& end);
152 std::string s(const std::string&, float, bool = true);
153
155 template <class T>
156 inline std::string repr(const std::vector<T>& vec,
157 const std::function<std::string(const T&)>& printer,
158 const std::string& sep = ",") {
159 if (vec.empty())
160 return "{}";
161 return std::accumulate(
162 std::next(vec.begin()), vec.end(), printer(*vec.begin()), [&printer, &sep](std::string str, const T& xv) {
163 return std::move(str) + sep + printer(xv);
164 });
165 }
167 template <class T>
168 inline std::string repr(const std::vector<T>& vec, const std::string& sep = ",") {
169 return repr<T>(vec, [](const T& xv) { return toString(xv); }, sep);
170 }
171 std::string randomString(size_t size);
172 std::string ltrim(const std::string& str);
173 std::string rtrim(const std::string& str);
174 inline std::string trim(const std::string& str) { return ltrim(rtrim(str)); }
175 std::string strip(const std::string&);
176 bool startsWith(const std::string&, const std::string&);
177 bool endsWith(const std::string&, const std::string&);
178 std::string describeError(int errnum);
179
180 } // namespace utils
181} // namespace cepgen
182
183#endif
Validity interval for a variable.
Definition Limits.h:28
std::ostream & operator<<(std::ostream &os, const Drawer::Mode &mode)
Definition Drawer.cpp:37
std::string format(const std::string &fmt, Args... args)
Format a string using a printf style format descriptor.
Definition String.h:61
std::string yesno(bool test)
Human-readable boolean printout Boldify a string for TTY-type output streams.
Definition String.cpp:45
std::string timeAs(const std::string &fmt)
Return the formatted date/time now.
Definition String.cpp:110
std::string describeError(int errnum)
Describe an error code.
Definition String.cpp:373
std::string s(const std::string &word, float num, bool show_number)
Add a trailing "s" when needed.
Definition String.cpp:228
std::string strip(const std::string &str)
Strip all special characters from string.
Definition String.cpp:334
std::string parseSpecialChars(const std::string &str)
Transform all emoji-like special characters into their LaTeX representation.
Definition String.cpp:90
std::vector< K > keys(const std::map< K, T > &coll)
Retrieve all keys from a map.
Definition Collections.h:33
std::string boldify(std::string str)
String implementation of the boldification procedure.
Definition String.cpp:49
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.
Definition String.cpp:351
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:156
std::string toLower(const std::string &str)
Lowercase version of a string.
Definition String.cpp:304
std::string trim(const std::string &str)
Trim leading and trailing spaces.
Definition String.h:174
std::string toString(const std::wstring &str)
Convert a wide characters to a standard characters string.
Definition String.cpp:151
Colour
TTY-type enumeration of colours.
Definition String.h:83
std::string sanitise(const std::string &str)
Replace all unsafe characters to build a computer-readable (and filename-safe) string.
Definition String.cpp:105
std::string toCamelCase(const std::string &in, bool lower)
Convert any case into a camelCase string.
Definition String.cpp:186
std::string ltrim(const std::string &str)
Trim leading spaces.
Definition String.cpp:321
bool endsWith(const std::string &str, const std::string &end)
Check if a string ends with a given token.
Definition String.cpp:367
bool isFloat(const std::string &str)
Check if a string is also a floating point number.
Definition String.cpp:295
bool startsWith(const std::string &str, const std::string &beg)
Check if a string starts with a given token.
Definition String.cpp:365
std::string toUpper(const std::string &str)
Capitalise a string.
Definition String.cpp:297
std::string colourise(const std::string &str, const Colour &col, const Modifier &mod)
Colourise a string for TTY-type output streams.
Definition String.cpp:70
Drawer::Mode operator|(const Drawer::Mode &lhs, const Drawer::Mode::value_t &rhs)
Definition Drawer.cpp:32
std::string randomString(size_t size)
Generate a random string of a given size.
Definition String.cpp:221
size_t replaceAll(std::string &str, const std::string &from, const std::string &to)
Replace all occurrences of a text by another.
Definition String.cpp:118
std::string merge(const std::vector< T > &vec, const std::string &delim)
Merge a collection of a printable type in a single string.
Definition String.cpp:248
std::vector< std::string > split(const std::string &str, char delim, bool trim)
Split a string according to a separation character.
Definition String.cpp:233
std::string demangle(const char *name)
Demangle a type id if possible.
Definition String.cpp:341
std::string rtrim(const std::string &str)
Trim trailing spaces.
Definition String.cpp:327
std::wstring toWstring(const std::string &str)
Convert a standard characters to a wide characters string.
Definition String.cpp:171
bool isInt(const std::string &str)
Check if a string is also an integer.
Definition String.cpp:290
Common namespace for this Monte Carlo generator.