* @brief Splits the given string by a separator. * * @param[in] str String to be splitted. * @param[in] sep Separator to be used. * @param[in] trimWhitespace If @c true, trims whitespace around the separated * strings. * * For example, * @code * split("; a ; b ; c ;", ';', true) * @endcode * returns * @code * ["", "a", "b", "c", ""] * @endcode */
| 597 | * @endcode |
| 598 | */ |
| 599 | std::vector<std::string> split(const std::string &str, char sep, bool trimWhitespace) { |
| 600 | std::vector<std::string> result; |
| 601 | std::stringstream ss(str); |
| 602 | std::string item; |
| 603 | while (std::getline(ss, item, sep)) { |
| 604 | result.push_back(trimWhitespace ? trim(item) : item); |
| 605 | } |
| 606 | |
| 607 | // If the input string ends with the separator, we have to add another |
| 608 | // empty string to the result. Indeed, the above way throws it away. |
| 609 | if (!str.empty() && str.back() == sep) { |
| 610 | result.push_back(""); |
| 611 | } |
| 612 | |
| 613 | return result; |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * @brief Unifies line ends in the given string to LF. |