Split a string with specified delimiter character and escape character. https://rosettacode.org/wiki/Tokenize_a_string_with_escaping#C.2B.2B
| 1884 | // Split a string with specified delimiter character and escape character. |
| 1885 | // https://rosettacode.org/wiki/Tokenize_a_string_with_escaping#C.2B.2B |
| 1886 | static void SplitString(const std::string &s, char delim, char escape, |
| 1887 | std::vector<std::string> &elems) { |
| 1888 | std::string token; |
| 1889 | |
| 1890 | bool escaping = false; |
| 1891 | for (size_t i = 0; i < s.size(); ++i) { |
| 1892 | char ch = s[i]; |
| 1893 | if (escaping) { |
| 1894 | escaping = false; |
| 1895 | } else if (ch == escape) { |
| 1896 | escaping = true; |
| 1897 | continue; |
| 1898 | } else if (ch == delim) { |
| 1899 | if (!token.empty()) { |
| 1900 | elems.push_back(token); |
| 1901 | } |
| 1902 | token.clear(); |
| 1903 | continue; |
| 1904 | } |
| 1905 | token += ch; |
| 1906 | } |
| 1907 | |
| 1908 | elems.push_back(token); |
| 1909 | } |
| 1910 | |
| 1911 | static std::string JoinPath(const std::string &dir, |
| 1912 | const std::string &filename) { |