This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringutils.cpp
142 lines (125 loc) · 4.52 KB
/
stringutils.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
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
// SPDX-FileCopyrightText: 2017 Christian Sailer
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "stringutils.h"
#include <algorithm>
#include <cstring>
#include <ctype.h>
#include <memory>
#include <sstream>
namespace dXstring {
std::vector<std::string> split(const std::string &s, char delim, bool skipEmptyTokens) {
std::vector<std::string> elems;
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
if (skipEmptyTokens && item.empty()) {
continue;
}
elems.push_back(item);
}
return elems;
}
std::string readString(std::istream &stream) {
unsigned int length;
stream.read(reinterpret_cast<char *>(&length), sizeof(length));
if (length == 0) {
return std::string();
}
std::string result(length, '\0');
char *ptr = &result[0];
stream.read(ptr, length);
return result;
}
void writeString(std::ostream &stream, const std::string &s) {
unsigned int length = static_cast<unsigned int>(s.length());
stream.write(reinterpret_cast<char *>(&length), sizeof(unsigned int));
if (length > 0) {
stream.write(s.data(), length);
}
}
std::string formatString(double value, const std::string &format) {
size_t bufferLength = 24 + format.length();
std::vector<char> buffer(bufferLength, '\0');
snprintf(&buffer[0], bufferLength, format.c_str(), value);
return std::string(&buffer[0]);
}
std::string formatString(int value, const std::string &format) {
size_t bufferLength = 24 + format.length();
std::vector<char> buffer(bufferLength, '\0');
snprintf(&buffer[0], bufferLength, format.c_str(), value);
return std::string(&buffer[0]);
}
std::string &toLower(std::string &str) {
std::transform(str.begin(), str.end(), str.begin(), tolower);
return str;
}
// trim from start (in place)
void ltrim(std::string &s, char c) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [&c](int ch) { return ch != c; }));
}
// trim from end (in place)
void rtrim(std::string &s, char c) {
s.erase(std::find_if(s.rbegin(), s.rend(), [&c](int ch) { return ch != c; }).base(),
s.end());
}
void makeInitCaps(std::string &s) {
bool literal = false;
bool reset = true;
for (auto &c : s) {
if (!isalpha(c)) {
if (c == '"') {
literal = !literal;
}
reset = true;
} else {
if (!literal) {
if (reset) {
c = static_cast<char>(toupper(c));
} else {
c = static_cast<char>(tolower(c));
}
}
reset = false;
}
}
}
bool isDouble(const std::string &s) {
// nasty const cast to satisfy the function signature - we will not change the value of
// endPtr
char *endPtr = const_cast<char *>(&s[0]);
strtod(s.c_str(), &endPtr);
return endPtr != &s[0];
}
// handles all three line endings ("\r", "\n" and "\r\n"). taken from:
// https://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf
std::istream &safeGetline(std::istream &is, std::string &t) {
t.clear();
// The characters in the stream are read one-by-one using a std::streambuf.
// That is faster than reading them one-by-one using the std::istream.
// Code that uses streambuf this way must be guarded by a sentry object.
// The sentry object performs various tasks,
// such as thread synchronization and updating the stream state.
std::istream::sentry se(is, true);
std::streambuf *sb = is.rdbuf();
for (;;) {
int c = sb->sbumpc();
switch (c) {
case '\n':
return is;
case '\r':
if (sb->sgetc() == '\n')
sb->sbumpc();
return is;
case std::streambuf::traits_type::eof():
// Also handle the case when the last line has no line ending
if (t.empty())
is.setstate(std::ios::eofbit);
return is;
default:
t += (char)c;
}
}
}
} // namespace dXstring