|
1 | 1 | #include <frp/stdafx.h> |
| 2 | +#include <frp/Random.h> |
2 | 3 | #include <frp/io/File.h> |
3 | 4 |
|
4 | 5 | #ifdef _WIN32 |
|
13 | 14 | #endif |
14 | 15 |
|
15 | 16 | namespace frp { |
| 17 | + static Random GLOBAL_RANDOBJECT; |
| 18 | + |
16 | 19 | void SetThreadPriorityToMaxLevel() noexcept { |
17 | 20 | #ifdef _WIN32 |
18 | 21 | SetThreadPriority(GetCurrentProcess(), THREAD_PRIORITY_TIME_CRITICAL); |
@@ -68,15 +71,6 @@ namespace frp { |
68 | 71 | return true; |
69 | 72 | } |
70 | 73 |
|
71 | | - Char RandomAscii() noexcept { |
72 | | - static const int m_ = 3; |
73 | | - static Byte x_[m_] = { 'a', 'A', '0' }; |
74 | | - static Byte y_[m_] = { 'z', 'Z', '9' }; |
75 | | - |
76 | | - int i_ = abs(RandomNext()) % m_; |
77 | | - return (Char)RandomNext(x_[i_], y_[i_]); |
78 | | - } |
79 | | - |
80 | 74 | int GetHashCode(const char* s, int len) noexcept { |
81 | 75 | if (s == NULL) { |
82 | 76 | return 0; |
@@ -135,45 +129,25 @@ namespace frp { |
135 | 129 | return (int)num; |
136 | 130 | } |
137 | 131 |
|
138 | | - int RandomNext() noexcept { |
139 | | - return RandomNext(0, INT_MAX); |
140 | | - } |
141 | | - |
142 | | - int RandomNext_r(volatile unsigned int* seed) noexcept { |
143 | | - unsigned int next = *seed; |
144 | | - int result; |
145 | | - |
146 | | - next *= 1103515245; |
147 | | - next += 12345; |
148 | | - result = (unsigned int)(next / 65536) % 2048; |
149 | | - |
150 | | - next *= 1103515245; |
151 | | - next += 12345; |
152 | | - result <<= 10; |
153 | | - result ^= (unsigned int)(next / 65536) % 1024; |
| 132 | + Char RandomAscii() noexcept { |
| 133 | + static const int m_ = 3; |
| 134 | + static Byte x_[m_] = { 'a', 'A', '0' }; |
| 135 | + static Byte y_[m_] = { 'z', 'Z', '9' }; |
154 | 136 |
|
155 | | - next *= 1103515245; |
156 | | - next += 12345; |
157 | | - result <<= 10; |
158 | | - result ^= (unsigned int)(next / 65536) % 1024; |
| 137 | + int i_ = abs(GLOBAL_RANDOBJECT.Next()) % m_; |
| 138 | + return (Char)GLOBAL_RANDOBJECT.Next(x_[i_], y_[i_]); |
| 139 | + } |
159 | 140 |
|
160 | | - *seed = next; |
161 | | - return result; |
| 141 | + int RandomNext() noexcept { |
| 142 | + return GLOBAL_RANDOBJECT.Next(0, INT_MAX); |
162 | 143 | } |
163 | 144 |
|
164 | 145 | int RandomNext(int minValue, int maxValue) noexcept { |
165 | | - static volatile unsigned int seed = time(NULL); |
166 | | - |
167 | | - int v = RandomNext_r(&seed); |
168 | | - return v % (maxValue - minValue + 1) + minValue; |
| 146 | + return GLOBAL_RANDOBJECT.Next(minValue, maxValue); |
169 | 147 | } |
170 | 148 |
|
171 | 149 | double RandomNextDouble() noexcept { |
172 | | - double d; |
173 | | - int* p = (int*)&d; |
174 | | - *p++ = RandomNext(); |
175 | | - *p++ = RandomNext(); |
176 | | - return d; |
| 150 | + return GLOBAL_RANDOBJECT.NextDouble(); |
177 | 151 | } |
178 | 152 |
|
179 | 153 | std::string StrFormatByteSize(Int64 size) noexcept { |
@@ -260,45 +234,62 @@ namespace frp { |
260 | 234 | std::string key2 = key1 + " "; |
261 | 235 | key1.append("="); |
262 | 236 |
|
| 237 | + std::string line; |
263 | 238 | for (int i = 1; i < argc; i++) { |
264 | | - std::string line = argv[i]; |
265 | | - if (line.empty()) { |
266 | | - continue; |
267 | | - } |
| 239 | + line.append(RTrim(LTrim<std::string>(argv[i]))); |
| 240 | + line.append(" "); |
| 241 | + } |
| 242 | + if (line.empty()) { |
| 243 | + return ""; |
| 244 | + } |
268 | 245 |
|
269 | | - std::string* key = addressof(key1); |
270 | | - std::size_t L = line.find(*key); |
| 246 | + std::string* key = addressof(key1); |
| 247 | + std::size_t L = line.find(*key); |
| 248 | + if (L == std::string::npos) { |
| 249 | + key = addressof(key2); |
| 250 | + L = line.find(*key); |
271 | 251 | if (L == std::string::npos) { |
272 | | - key = addressof(key2); |
273 | | - L = line.find(*key); |
274 | | - if (L == std::string::npos) { |
275 | | - continue; |
276 | | - } |
| 252 | + return ""; |
277 | 253 | } |
278 | | - elif(L) { |
279 | | - char ch = line[L - 1]; |
280 | | - if (ch != ' ') { |
281 | | - continue; |
282 | | - } |
| 254 | + } |
| 255 | + |
| 256 | + if (L) { |
| 257 | + char ch = line[L - 1]; |
| 258 | + if (ch != ' ') { |
| 259 | + return ""; |
283 | 260 | } |
| 261 | + } |
284 | 262 |
|
285 | | - std::string cmd; |
286 | | - std::size_t M = L + key->size(); |
287 | | - std::size_t R = line.find(' ', L); |
288 | | - if (R == std::string::npos) { |
289 | | - if (M != line.size()) { |
290 | | - cmd = line.substr(M); |
| 263 | + std::string cmd; |
| 264 | + std::size_t M = L + key->size(); |
| 265 | + std::size_t R = line.find(' ', L); |
| 266 | + if (M >= R) { |
| 267 | + R = std::string::npos; |
| 268 | + for (std::size_t I = M, SZ = line.size(); I < SZ; I++) { |
| 269 | + int ch = line[I]; |
| 270 | + if (ch == ' ') { |
| 271 | + R = I; |
| 272 | + L = M; |
| 273 | + break; |
291 | 274 | } |
292 | 275 | } |
293 | | - else { |
294 | | - int S = (int)(R - M); |
295 | | - if (S > 0) { |
296 | | - cmd = line.substr(M, S); |
297 | | - } |
| 276 | + if (!L || L == std::string::npos) { |
| 277 | + return ""; |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + if (R == std::string::npos) { |
| 282 | + if (M != line.size()) { |
| 283 | + cmd = line.substr(M); |
| 284 | + } |
| 285 | + } |
| 286 | + else { |
| 287 | + int S = (int)(R - M); |
| 288 | + if (S > 0) { |
| 289 | + cmd = line.substr(M, S); |
298 | 290 | } |
299 | | - return cmd; |
300 | 291 | } |
301 | | - return ""; |
| 292 | + return cmd; |
302 | 293 | } |
303 | 294 |
|
304 | 295 | std::string GetFullExecutionFilePath() noexcept { |
|
0 commit comments