-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStrings.cpp
130 lines (100 loc) · 2.75 KB
/
Strings.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
#include <memory>
#include <vector>
#define _CRT_SECURE_NO_DEPRECATE
#include "Strings.h"
#include "Run.h"
#include <string>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <sstream>
using namespace runexe;
using namespace std;
string Strings::format(const char *format, va_list ap) {
char *msg = new char[MAX_FORMAT_LENGTH];
vsprintf(msg, format, ap);
string result = msg;
delete[] msg;
return result;
}
string Strings::format(const char *format, ...) {
va_list ap;
va_start(ap, format);
string result = Strings::format(format, ap);
va_end(ap);
return result;
}
string Strings::trim(const string &s) {
int first = 0, last = (int) s.length();
while (first < last) {
if ((unsigned char) s[first] <= ' ') {
++first;
continue;
}
if ((unsigned char) s[last - 1] <= ' ') {
--last;
continue;
}
break;
}
return s.substr(first, last - first);
}
bool Strings::checkIntegerIdentialToString(const string &s, long long x) {
return s == integerToString(x);
}
bool Strings::checkDoubleIdentialToString(const string &s, double x) {
return s == doubleToString(x);
}
string Strings::integerToString(long long x) {
stringstream stream;
stream << x;
return stream.str();
}
string Strings::doubleToString(double x) {
stringstream stream;
stream << x;
return stream.str();
}
int Strings::parseInt(const string &s) {
int result = atoi(s.c_str());
if (!checkIntegerIdentialToString(s, result))
crash("expected int but received '" + s + "'");
return result;
}
double Strings::parseDouble(const string &s) {
double result = atof(s.c_str());
if (!checkDoubleIdentialToString(s, result))
crash("expected double but received '" + s + "'");
return result;
}
long long Strings::parseInt64(const string &s) {
stringstream ss;
ss << s;
long long result;
ss >> result;
if (!checkIntegerIdentialToString(s, result))
crash("expected int64 but received '" + s + "'");
return result;
}
vector<string> Strings::tokenizeCommandLine(const string &commandLine) {
vector<string> result;
bool inQuotes = false;
string buffer;
for (size_t i = 0; i < commandLine.length(); i++) {
if (commandLine[i] == '\"') {
inQuotes = !inQuotes;
continue;
}
if (!inQuotes && commandLine[i] <= ' ') {
if (!buffer.empty() || (i > 0 && commandLine[i - 1] == '\"')) {
result.push_back(buffer);
buffer = "";
}
continue;
}
buffer.append(1, commandLine[i]);
}
if (!buffer.empty())
result.push_back(buffer);
return result;
}