Browse Source

Make string::split(s, "") throw Exception.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
d53802e1e6
2 changed files with 3 additions and 10 deletions
  1. +1
    -1
      include/string.hpp
  2. +2
    -9
      src/string.cpp

+ 1
- 1
include/string.hpp View File

@@ -74,7 +74,7 @@ Examples:
split("abc", "+") // {"abc"}
split("a++c", "+") // {"a", "", "c"}
split("", "+") // {}
split("abc", "") // {"a", "b", "c"}
split("abc", "") // throws rack::Exception
*/
std::vector<std::string> split(const std::string& s, const std::string& seperator);



+ 2
- 9
src/string.cpp View File

@@ -206,20 +206,13 @@ bool CaseInsensitiveCompare::operator()(const std::string& a, const std::string&


std::vector<std::string> split(const std::string& s, const std::string& separator) {
if (separator.empty())
throw Exception("split(): separator cannot be empty string");
// Special case of empty string
if (s == "")
return {};

std::vector<std::string> v;
// Special case of empty separator
if (separator == "") {
for (char c : s) {
std::string token(1, c);
v.push_back(token);
}
return v;
}

size_t sepLen = separator.size();
size_t start = 0;
size_t end;


Loading…
Cancel
Save