forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstring_utils.cpp
88 lines (73 loc) · 1.84 KB
/
string_utils.cpp
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
#include "openmc/string_utils.h"
#include <algorithm> // for equal
#include <cctype> // for tolower, isspace
namespace openmc {
std::string& strtrim(std::string& s)
{
const char* t = " \t\n\r\f\v";
s.erase(s.find_last_not_of(t) + 1);
s.erase(0, s.find_first_not_of(t));
return s;
}
char* strtrim(char* c_str)
{
std::string std_str;
std_str.assign(c_str);
strtrim(std_str);
int length = std_str.copy(c_str, std_str.size());
c_str[length] = '\0';
return c_str;
}
std::string to_element(const std::string& name)
{
int pos = name.find_first_of("0123456789");
return name.substr(0, pos);
}
void to_lower(std::string& str)
{
for (int i = 0; i < str.size(); i++)
str[i] = std::tolower(str[i]);
}
int word_count(const std::string& str)
{
std::stringstream stream(str);
std::string dum;
int count = 0;
while (stream >> dum) {
count++;
}
return count;
}
vector<std::string> split(const std::string& in)
{
vector<std::string> out;
for (int i = 0; i < in.size();) {
// Increment i until we find a non-whitespace character.
if (std::isspace(in[i])) {
i++;
} else {
// Find the next whitespace character at j.
int j = i + 1;
while (j < in.size() && std::isspace(in[j]) == 0) {
j++;
}
// Push-back everything between i and j.
out.push_back(in.substr(i, j - i));
i = j + 1; // j is whitespace so leapfrog to j+1
}
}
return out;
}
bool ends_with(const std::string& value, const std::string& ending)
{
if (ending.size() > value.size())
return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
bool starts_with(const std::string& value, const std::string& beginning)
{
if (beginning.size() > value.size())
return false;
return std::equal(beginning.begin(), beginning.end(), value.begin());
}
} // namespace openmc