File: string.cpp

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (254 lines) | stat: -rw-r--r-- 6,135 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/***********************************************/
/**
* @file string.cpp
*
* @brief Miscellaneous string functions.
*
* @author Torsten Mayer-Guerr
* @author Sebastian Strasser
* @date 2019-09-04
*
*/
/***********************************************/

#include "base/importStd.h"
#include "base/string.h"
#include <regex>

/***********************************************/

std::string String::upperCase(const std::string &str)
{
  std::string result = str;
  std::transform(result.begin(), result.end(), result.begin(), [](auto c){return std::toupper(c);});
  return result;
}

/***********************************************/

std::string String::lowerCase(const std::string &str)
{
  std::string result = str;
  std::transform(result.begin(), result.end(), result.begin(), [](auto c){return std::tolower(c);});
  return result;
}

/***********************************************/

std::string String::trim(const std::string &str)
{
  try
  {
    auto start = str.find_first_not_of(" \t\r");
    if(start == std::string::npos)
      return "";
    auto end = str.find_last_not_of(" \t\r");
    return str.substr(start, end-start+1);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e);
  }
}

/***********************************************/

std::string String::trimLeft(const std::string &str)
{
  try
  {
    auto start = str.find_first_not_of(" \t\r");
    if(start == std::string::npos)
      return "";
    return str.substr(start);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e);
  }
}

/***********************************************/

std::string String::trimRight(const std::string &str)
{
  try
  {
    auto end = str.find_last_not_of(" \t\r");
    if(end == std::string::npos)
      return "";
    return str.substr(0, end+1);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e);
  }
}

/***********************************************/

Double String::toDouble(const std::string &str)
{
  try
  {
    if(std::all_of(str.begin(), str.end(), ::isspace))
      return 0.;

    std::string str2 = str;
    auto dpos = str.find_first_of("Dd");
    if(dpos != std::string::npos)
      str2[dpos] = 'e';

    return std::stod(str2);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW_EXTRA("cannot read double from string '"+str+"'", e)
  }
}

/***********************************************/

Int String::toInt(const std::string &str)
{
  try
  {
    if(std::all_of(str.begin(), str.end(), ::isspace))
      return 0;

    return std::stoi(str);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW_EXTRA("cannot read integer from string '"+str+"'", e)
  }
}

/***********************************************/

Bool String::contains(const std::string &str, const std::string &test)
{
  return str.size() && (str.find(test) != std::string::npos);
}

/***********************************************/

Bool String::startsWith(const std::string &str, const std::string &test)
{
  return (str.rfind(test, 0) == 0);
}

/***********************************************/

Bool String::endsWith(const std::string &str, const std::string &test)
{
  return (str.size() >= test.size()) && (str.compare(str.size()-test.size(), test.size(), test) == 0);
}

/***********************************************/

std::vector<std::string> String::split(const std::string &str, Char seperator)
{
  try
  {
    std::vector<std::string> parts;
    UInt pos1 = 0;
    UInt pos2 = 0;
    for(;;)
    {
      pos2 = str.find(seperator, pos2);
      parts.push_back(str.substr(pos1, pos2-pos1));
      if(pos2 == std::string::npos)
        break;
      pos1 = ++pos2;
    }
    return parts;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::vector<std::string> String::split(const std::string &str, const std::string &seperators)
{
  try
  {
    std::vector<std::string> parts;
    UInt pos1 = 0;
    UInt pos2 = 0;
    for(;;)
    {
      pos2 = str.find_first_of(seperators, pos2);
      parts.push_back(str.substr(pos1, pos2-pos1));
      if(pos2 == std::string::npos)
        break;
      pos1 = ++pos2;
    }
    return parts;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string String::replaceAll(const std::string &str, const std::string &search, const std::string &substitute)
{
  std::string result = str;
  UInt pos = 0;
  for(;;)
  {
    pos = result.find(search, pos);
    if(pos == std::string::npos)
        break;
    result.replace(pos, search.size(), substitute);
    pos += substitute.size();
  }
  return result;
}

/***********************************************/

std::string String::replaceAll(const std::string &str, const std::vector<std::pair<std::string, std::string>> &substitutes)
{
  std::string result = str;
  for(const auto &substitute : substitutes)
  {
    auto pos = result.find(substitute.first);
    while(pos != std::string::npos)
    {
      result.replace(pos, substitute.first.size(), substitute.second);
      pos = result.find(substitute.first, pos + substitute.second.size());
    }
  }
  return result;
}

/***********************************************/

std::regex String::wildcard2regex(const std::string &pattern)
{
  try
  {
    // escape ., +, \*, \? in regex pattern and
    // replace valid wildcards *, ? with regex equivalents .*, .
    std::string regexPattern = pattern;
    regexPattern = replaceAll(regexPattern, {{"\\*", "<backslash_star>"}, {"\\?", "<backslash_question_mark>"}});  // temp replacement
    regexPattern = replaceAll(regexPattern, {{"\\", "\\\\"}, {"^", "\\^"}, {".", "\\."}, {"$", "\\$"}, {"|", "\\|"}, {"(", "\\("},
                                             {")", "\\)"},   {"[", "\\["}, {"]", "\\]"}, {"+", "\\+"}, {"/", "\\/"}});
    regexPattern = replaceAll(regexPattern, {{"*", ".*"}, {"?", "."}});
    regexPattern = replaceAll(regexPattern, {{"<backslash_star>", "\\*"}, {"<backslash_question_mark>", "\\?"}}); // undo temp replacement
    return std::regex(regexPattern);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/