correction of trim in case of str only fill with delim char

+ added a new split, that also does trim, to split without counting newlines
This commit is contained in:
hugogogo
2022-08-10 20:01:23 +02:00
parent c7905ebd19
commit 9a379c835d
5 changed files with 59 additions and 37 deletions

View File

@@ -21,21 +21,42 @@ std::vector<std::string> split(std::string input, char delimiter)
return answer;
}
std::string trim(std::string str, char c)
std::vector<std::string>
split_trim(std::string input, std::string delim = "\n", char ctrim = '')
{
std::vector<std::string> split_str;
std::string tmp;
size_t start = 0;
size_t end;
end = input.find(delim);
while (end != -1)
{
tmp = input.substr(start, end - start);
tmp = trim(tmp, ctrim);
if (tmp.size() != 0)
split_str.push_back( tmp );
start = end + delim.size();
end = input.find(delim, start);
}
split_str.push_back( input.substr(start, end - start) );
return split_str;
}
std::string trim(std::string str, char del)
{
size_t pos;
// delete leadings c
pos = str.find_first_not_of(c);
// delete leadings del
pos = str.find_first_not_of(del);
if (pos == std::string::npos)
return str;
pos = str.size();
str = str.substr(pos);
// delete endings c
pos = str.find_last_not_of(c);
if (pos == std::string::npos)
return str;
str = str.substr(0, pos + 1);
// delete trailing del
pos = str.find_last_not_of(del);
if (pos != std::string::npos)
str = str.substr(0, pos + 1);
return str;
}
@@ -177,14 +198,6 @@ std::string extract_line(std::string * str, size_t pos, std::string delim)
return del_str;
}
// transform a str, like a http header, into a map
// with <keys> delim <values>
// and perform an action on keys and values
// action receives address of keys and values, and return bool error :
// bool action(&keys, &values)
//std::map<std:string, std::string>
// str_to_map(str, delim, action = NULL)
bool operator==(const listen_socket& lhs, int fd)
{ return lhs.fd == fd; }