| 302 | |
| 303 | |
| 304 | std::vector<std::string> split(const std::string& s, const std::string& separator, size_t maxTokens) { |
| 305 | if (separator.empty()) |
| 306 | throw Exception("split(): separator cannot be empty string"); |
| 307 | // Special case of empty string |
| 308 | if (s == "") |
| 309 | return {}; |
| 310 | if (maxTokens == 1) |
| 311 | return {s}; |
| 312 | |
| 313 | std::vector<std::string> v; |
| 314 | size_t sepLen = separator.size(); |
| 315 | size_t start = 0; |
| 316 | size_t end; |
| 317 | while ((end = s.find(separator, start)) != std::string::npos) { |
| 318 | // Add token to vector |
| 319 | std::string token = s.substr(start, end - start); |
| 320 | v.push_back(token); |
| 321 | // Don't include delimiter |
| 322 | start = end + sepLen; |
| 323 | // Stop searching for tokens if we're at the token limit |
| 324 | if (maxTokens == v.size() + 1) |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | v.push_back(s.substr(start)); |
| 329 | return v; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | std::string formatTime(const char* format, double timestamp) { |