Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
StringUtils.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright © 2014-2024 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // C++ includes
21 #include <functional>
22 #include <sstream>
23 #include <string>
24 #include <vector>
25 #include <variant>
26 
27 namespace Reaktoro {
28 namespace internal {
29 
30 template<typename T>
31 auto operator<<(std::ostream& out, std::vector<T> const& values) -> std::ostream&
32 {
33  for(auto i = 0; i < values.size(); ++i)
34  out << ((i == 0) ? "" : ", ") << values[i];
35  return out;
36 }
37 
38 template <typename Arg>
39 auto stringfy(std::ostringstream& ss, std::string const& sep, Arg const& item) -> void
40 {
41  ss << item;
42 }
43 
44 template <typename Arg, typename... Args>
45 auto stringfy(std::ostringstream& ss, std::string const& sep, Arg const& item, Args... items) -> void
46 {
47  ss << item << sep;
48  stringfy(ss, sep, items...);
49 }
50 
51 } // namespace internal
52 
54 template <typename... Args>
55 auto stringfy(std::string const& sep, Args... items) -> std::string
56 {
57  std::ostringstream ss;
58  internal::stringfy(ss, sep, items...);
59  return ss.str();
60 }
61 
63 template <typename... Args>
64 auto stringfy(std::variant<Args...> const& var) -> std::string
65 {
66  std::ostringstream ss;
67  std::visit([&](auto arg){ ss << arg; }, var);
68  return ss.str();
69 }
70 
72 template <typename... Args>
73 auto str(Args... items) -> std::string
74 {
75  return stringfy("", items...);
76 }
77 
79 auto precision(int precision) -> void;
80 
82 auto precision() -> int;
83 
87 auto strfix(double num, int precision = -1) -> std::string;
88 
92 auto strsci(double num, int precision = -1) -> std::string;
93 
95 auto replace(std::string original, std::string substr, std::string newsubstr) -> std::string;
96 
98 auto lowercase(std::string str) -> std::string;
99 
101 auto uppercase(std::string str) -> std::string;
102 
104 auto trimleft(std::string str) -> std::string;
105 
107 auto trimright(std::string str) -> std::string;
108 
110 auto trim(std::string str) -> std::string;
111 
113 auto split(std::string const& str, std::string const& delims, std::function<std::string(std::string)> transform) -> std::vector<std::string>;
114 
116 auto split(std::string const& str, std::string const& delims = " ") -> std::vector<std::string>;
117 
119 auto join(std::vector<std::string> const& strs, std::string sep = " ") -> std::string;
120 
122 auto tofloat(std::string const& str) -> double;
123 
125 auto makeunique(std::vector<std::string> words, std::string suffix) -> std::vector<std::string>;
126 
128 auto strlength(std::string const& str) -> std::size_t;
129 
131 auto strlength(const char* str) -> std::size_t;
132 
134 template<typename SubStr, typename... SubStrs>
135 auto startswith(std::string const& str, SubStr substr, SubStrs... substrs)
136 {
137  auto const aux = str.find(substr) == 0;
138  if constexpr(sizeof...(SubStrs) > 0)
139  return aux || startswith(str, substrs...);
140  return aux;
141 }
142 
144 template<typename SubStr, typename... SubStrs>
145 auto endswith(std::string const& str, SubStr substr, SubStrs... substrs)
146 {
147  auto const aux = str.rfind(substr) == str.size() - strlength(substr);
148  if constexpr(sizeof...(SubStrs) > 0)
149  return aux || endswith(str, substrs...);
150  return aux;
151 }
152 
153 } // namespace Reaktoro
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
auto trimright(std::string str) -> std::string
Trim the string from end (taken from http://stackoverflow.com/questions/216823/whats-the-best-way-to-...
auto split(std::string const &str, std::string const &delims, std::function< std::string(std::string)> transform) -> std::vector< std::string >
Split the string on every occurrence of the specified delimiters and apply a transform function.
auto strlength(std::string const &str) -> std::size_t
Return the length of the string.
auto tofloat(std::string const &str) -> double
Convert the string into a floating point number.
auto strsci(double num, int precision=-1) -> std::string
Return a string representation for a number in scientific format.
auto replace(std::string original, std::string substr, std::string newsubstr) -> std::string
Return a new string where substr occurrences are replaced by newsubstr.
auto makeunique(std::vector< std::string > words, std::string suffix) -> std::vector< std::string >
Return a list of words with duplicate names converted to unique ones.
auto stringfy(std::string const &sep, Args... items) -> std::string
Concatenate the arguments into a string using a given separator string.
Definition: StringUtils.hpp:55
auto lowercase(std::string str) -> std::string
Return a string with lower case characters.
auto precision(int precision) -> void
Set the global precision used when converting floating-point numbers to string.
auto trimleft(std::string str) -> std::string
Trim the string from start (taken from http://stackoverflow.com/questions/216823/whats-the-best-way-t...
auto strfix(double num, int precision=-1) -> std::string
Return a string representation for a number in fixed format.
auto transform(const Container &c, Result &res, const Function &f)
Apply a function f on every item in container c and store in res.
Definition: Algorithms.hpp:116
auto startswith(std::string const &str, SubStr substr, SubStrs... substrs)
Returns true if string str starts with substr, or any other given sub string in substrs.
Definition: StringUtils.hpp:135
auto uppercase(std::string str) -> std::string
Return a string with upper case characters.
auto endswith(std::string const &str, SubStr substr, SubStrs... substrs)
Returns true if string str ends with substr, or any other given sub string in substrs.
Definition: StringUtils.hpp:145
auto operator<<(std::ostream &out, AggregateState option) -> std::ostream &
Output an AggregateState value.
auto str(Args... items) -> std::string
Concatenate the arguments into a string without any separator string.
Definition: StringUtils.hpp:73
auto join(std::vector< std::string > const &strs, std::string sep=" ") -> std::string
Join several strings into one.
auto trim(std::string str) -> std::string
Trim the string from both ends (taken from http://stackoverflow.com/questions/216823/whats-the-best-w...