Repository navigation
Expand file tree
/
Copy pathStrUtil.cpp
More file actions
172 lines (145 loc) · 4.62 KB
/
Copy pathStrUtil.cpp
File metadata and controls
172 lines (145 loc) · 4.62 KB
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
//-----------------------------------------------------------------------------
// This file is part of the the QParser2 project. You can find the complete
// project here: https://github.com/robdunn4/QSpice/
//-----------------------------------------------------------------------------
#include "StrUtil.h"
#include <sstream>
namespace StrUtil {
int strToInt(const std::string &str) {
try {
std::string trimmed = trim(str);
if (trimmed.empty()) {
throw std::invalid_argument("Empty string");
}
return std::stoi(trimmed);
} catch (const std::exception &e) {
throw std::runtime_error("Cannot convert '" + str +
"' to int: " + e.what());
}
}
long strToLong(const std::string &str) {
try {
std::string trimmed = trim(str);
if (trimmed.empty()) {
throw std::invalid_argument("Empty string");
}
return std::stol(trimmed);
} catch (const std::exception &e) {
throw std::runtime_error("Cannot convert '" + str +
"' to long: " + e.what());
}
}
float strToFloat(const std::string &str) {
try {
std::string trimmed = trim(str);
if (trimmed.empty()) {
throw std::invalid_argument("Empty string");
}
return std::stof(trimmed);
} catch (const std::exception &e) {
throw std::runtime_error("Cannot convert '" + str +
"' to float: " + e.what());
}
}
double strToDouble(const std::string &str) {
try {
std::string trimmed = trim(str);
if (trimmed.empty()) {
throw std::invalid_argument("Empty string");
}
return std::stod(trimmed);
} catch (const std::exception &e) {
throw std::runtime_error("Cannot convert '" + str +
"' to double: " + e.what());
}
}
std::string intToStr(int value) { return std::to_string(value); }
std::string longToStr(long value) { return std::to_string(value); }
std::string floatToStr(float value, int precision) {
std::ostringstream oss;
oss.precision(precision);
oss << std::fixed << value;
return oss.str();
}
std::string doubleToStr(double value, int precision) {
std::ostringstream oss;
oss.precision(precision);
oss << std::fixed << value;
return oss.str();
}
std::string trim(const std::string &str) { return trimLeft(trimRight(str)); }
std::string trimLeft(const std::string &str) {
size_t start = str.find_first_not_of(" \t\n\r\f\v");
return (start == std::string::npos) ? "" : str.substr(start);
}
std::string trimRight(const std::string &str) {
size_t end = str.find_last_not_of(" \t\n\r\f\v");
return (end == std::string::npos) ? "" : str.substr(0, end + 1);
}
// tokenize() -- parses a string into individual strings.
//
// Note: This process is fragile. It is possible for a GUI user to insert
// double-quotes into some fields which may break QSpice. This code probably
// works no better...
//
StrList tokenize(const std::string &input) {
std::istringstream iss(input);
std::string curToken;
StrList tokens;
while (iss.good()) {
iss >> curToken;
if (!curToken.length()) continue;
if (curToken[0] == '"') {
std::string remainder;
getline(iss, remainder);
curToken += remainder;
}
tokens.push_back(curToken);
}
return tokens;
}
std::string forceQuotes(std::string str) {
if (!str.length()) return "\"\"";
if (*str.begin() != '\"' || *(str.end() - 1) != '\"')
return "\"" + str + "\"";
return str;
}
// Split a string with embedded \n or \r\n into a StrList
StrList fromString(const std::string &input) {
StrList lines;
std::istringstream stream(input);
std::string line;
while (std::getline(stream, line)) {
if (!line.empty() && line.back() == '\r') line.pop_back();
lines.push_back(std::move(line));
}
return lines;
}
// Read lines from an istream into a StrList
StrList fromStream(std::istream &input) {
StrList lines;
std::string line;
while (std::getline(input, line)) {
if (!line.empty() && line.back() == '\r') line.pop_back();
lines.push_back(std::move(line));
}
return lines;
}
// Join a StrList into a single string with a given delimiter (default: \n)
std::string toString(const StrList &lines, const std::string &delimiter) {
std::ostringstream stream;
for (size_t i = 0; i < lines.size(); ++i) {
if (i > 0) stream << delimiter;
stream << lines[i];
}
return stream.str();
}
// Write a StrList to an ostream
void toStream(const StrList &lines, std::ostream &output,
const std::string &delimiter) {
for (size_t i = 0; i < lines.size(); ++i) {
if (i > 0) output << delimiter;
output << lines[i];
}
}
} // namespace StrUtil