Function to take a string and a list of delimiters and split the string into tokens based on those delimiters This assumes that tokens are also to be split by newlines Enabling tokenCompression merges adjacent delimiters together, preventing empty tokens
| 21 | /// This assumes that tokens are also to be split by newlines |
| 22 | /// Enabling tokenCompression merges adjacent delimiters together, preventing empty tokens |
| 23 | inline std::vector<std::string> StringTokenizer(const std::string& str, |
| 24 | const char* delimiters, |
| 25 | bool tokenCompression = true) |
| 26 | { |
| 27 | std::stringstream stringStream(str); |
| 28 | std::string line; |
| 29 | std::vector<std::string> tokenVector; |
| 30 | while (std::getline(stringStream, line)) |
| 31 | { |
| 32 | std::size_t prev = 0; |
| 33 | std::size_t pos; |
| 34 | while ((pos = line.find_first_of(delimiters, prev)) != std::string::npos) |
| 35 | { |
| 36 | // Ignore adjacent tokens |
| 37 | if (pos > prev) |
| 38 | { |
| 39 | tokenVector.push_back(line.substr(prev, pos - prev)); |
| 40 | } |
| 41 | // Unless token compression is disabled |
| 42 | else if (!tokenCompression) |
| 43 | { |
| 44 | tokenVector.push_back(line.substr(prev, pos - prev)); |
| 45 | } |
| 46 | prev = pos + 1; |
| 47 | } |
| 48 | if (prev < line.length()) |
| 49 | { |
| 50 | tokenVector.push_back(line.substr(prev, std::string::npos)); |
| 51 | } |
| 52 | } |
| 53 | return tokenVector; |
| 54 | } |
| 55 | |
| 56 | // Set of 3 utility functions for trimming std::strings |
| 57 | // Default char set for common whitespace characters |
no test coverage detected