Reaktoro
A unified framework for modeling chemically reactive systems
StringUtils.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright (C) 2014-2018 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 <algorithm>
22 #include <cctype>
23 #include <cstdlib>
24 #include <functional>
25 #include <locale>
26 #include <string>
27 #include <vector>
28 
29 namespace Reaktoro {
30 
32 inline auto lowercase(std::string str) -> std::string
33 {
34  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
35  return str;
36 }
37 
39 inline auto uppercase(std::string str) -> std::string
40 {
41  std::transform(str.begin(), str.end(), str.begin(), ::toupper);
42  return str;
43 }
44 
46 inline auto leftTrim(std::string& str) -> std::string&
47 {
48  str.erase(str.begin(),
49  std::find_if(str.begin(), str.end(), [](int ch){ return !std::isspace(ch); }));
50  return str;
51 }
52 
54 inline auto rightTrim(std::string& str) -> std::string&
55 {
56  str.erase(std::find_if(str.rbegin(), str.rend(),
57  [](int ch){ return !std::isspace(ch); }).base(), str.end());
58  return str;
59 }
60 
62 inline auto trim(std::string& str) -> std::string&
63 {
64  return leftTrim(rightTrim(str));
65 }
66 
68 inline auto split(const std::string& str, const std::string& delims,
69  std::function<std::string&(std::string&)> transform) -> std::vector<std::string>
70 {
71  std::vector<std::string> words;
72  std::size_t start = 0, end = 0;
73  while(end != std::string::npos)
74  {
75  end = str.find_first_of(delims, start);
76  std::string word = str.substr(start, end - start);
77  if(word != "") words.push_back(transform ? transform(word) : word);
78  start = end + 1;
79  }
80  return words;
81 }
82 
84 inline auto split(const std::string& str, const std::string& delims = " ") -> std::vector<std::string>
85 {
86  return split(str, delims, {});
87 }
88 
90 inline auto splitrim(const std::string& str, const std::string& delims = " ") -> std::vector<std::string>
91 {
92  return split(str, delims, trim);
93 }
94 
96 inline auto join(const std::vector<std::string>& strs, std::string delim = " ") -> std::string
97 {
98  std::string res;
99  for(unsigned i = 0; i < strs.size(); ++i)
100  res = res + (i > 0 ? delim : "") + strs[i];
101  return res;
102 }
103 
105 inline auto tofloat(const std::string& str) -> double
106 {
107  return atof(str.c_str());
108 }
109 
111 inline auto tofloats(const std::string& str, const std::string& delims = " ") -> std::vector<double>
112 {
113  std::vector<double> values;
114  for(const std::string& num : split(str, delims))
115  values.push_back(tofloat(num));
116  return values;
117 }
118 
119 } // namespace Reaktoro
120 
121 
auto splitrim(const std::string &str, const std::string &delims=" ") -> std::vector< std::string >
Split the string on every occurrence of the specified delimiters and trim each word.
Definition: StringUtils.hpp:90
auto trim(std::string &str) -> std::string &
Trim the string from both ends (taken from http://stackoverflow.com/questions/216823/whats-the-best-w...
Definition: StringUtils.hpp:62
auto end(const Reaktoro::ReactionEquation &equation) -> decltype(equation.equation().end())
Return end const iterator of a ReactionEquation instance.
Definition: ReactionEquation.hpp:98
auto split(const std::string &str, const std::string &delims, std::function< std::string &(std::string &)> transform) -> std::vector< std::string >
Split the string on every occurrence of the specified delimiters.
Definition: StringUtils.hpp:68
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
auto rightTrim(std::string &str) -> std::string &
Trim the string from end (taken from http://stackoverflow.com/questions/216823/whats-the-best-way-to-...
Definition: StringUtils.hpp:54
auto join(const std::vector< std::string > &strs, std::string delim=" ") -> std::string
Join several strings into one.
Definition: StringUtils.hpp:96
auto tofloats(const std::string &str, const std::string &delims=" ") -> std::vector< double >
Convert the string into a list of floating point numbers.
Definition: StringUtils.hpp:111
auto uppercase(std::string str) -> std::string
Return a string with upper case characters.
Definition: StringUtils.hpp:39
auto lowercase(std::string str) -> std::string
Return a string with lower case characters.
Definition: StringUtils.hpp:32
auto tofloat(const std::string &str) -> double
Convert the string into a floating point number.
Definition: StringUtils.hpp:105
auto leftTrim(std::string &str) -> std::string &
Trim the string from start (taken from http://stackoverflow.com/questions/216823/whats-the-best-way-t...
Definition: StringUtils.hpp:46