| 68 | } |
| 69 | |
| 70 | void string_split(vector<string> &tokens, |
| 71 | const string &str, |
| 72 | const string &separators, |
| 73 | bool skip_empty_tokens) |
| 74 | { |
| 75 | size_t token_start = 0; |
| 76 | size_t token_length = 0; |
| 77 | for (size_t i = 0; i < str.size(); ++i) { |
| 78 | const char ch = str[i]; |
| 79 | if (separators.find(ch) == string::npos) { |
| 80 | /* Current character is not a separator, |
| 81 | * append it to token by increasing token length. |
| 82 | */ |
| 83 | ++token_length; |
| 84 | } |
| 85 | else { |
| 86 | /* Current character is a separator, |
| 87 | * append current token to the list. |
| 88 | */ |
| 89 | if (!skip_empty_tokens || token_length > 0) { |
| 90 | const string token = str.substr(token_start, token_length); |
| 91 | tokens.push_back(token); |
| 92 | } |
| 93 | token_start = i + 1; |
| 94 | token_length = 0; |
| 95 | } |
| 96 | } |
| 97 | /* Append token from the tail of the string if exists. */ |
| 98 | if (token_length) { |
| 99 | const string token = str.substr(token_start, token_length); |
| 100 | tokens.push_back(token); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | bool string_startswith(const string_view s, const string_view start) |
| 105 | { |