correct extract_line to avoid duplicate with erase or substr, and to resolve pbm of returning trailing delim

This commit is contained in:
hugogogo
2022-08-11 17:33:42 +02:00
parent ff77dfd298
commit ad2b5a629a
3 changed files with 91 additions and 55 deletions

View File

@@ -30,25 +30,27 @@ std::vector<std::string> split(std::string input, char delimiter)
}
std::vector<std::string>
split_trim(std::string input, std::string delim = "\n", char ctrim = '\0')
split_trim(std::string input, std::string delim, char ctrim)
{
std::vector<std::string> split_str;
std::string tmp;
int start = 0;
int end;
size_t start = 0;
size_t end = 0;
size_t len = 0;
end = input.find(delim);
while (end != -1)
while (end != std::string::npos)
{
tmp = input.substr(start, end - start);
end = input.find(delim, start);
len = end - start;
if (end == std::string::npos)
len = end;
tmp = input.substr(start, len);
if (ctrim != '\0')
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;
}
@@ -70,6 +72,19 @@ std::string trim(std::string str, char del)
return str;
}
//// trim a set of char
//std::string trim(std::string str, std::string del)
//{
// std::string new_str;
//
// while (new_str.compare(str) != 0)
// {
// for (size_t i = 0; i < del.size(); i++)
// trim(str, del[i]);
// }
// return str;
//}
std::string itos(int n)
{
std::stringstream strs;
@@ -160,7 +175,11 @@ int path_is_valid(std::string path)
}
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr)
void
replace_all_substr(
std::string &str,
const std::string &ori_substr,
const std::string &new_substr)
{
if (ori_substr.empty())
return;
@@ -184,13 +203,13 @@ std::string str_tolower(std::string str)
// identify a line in a string, by delim (ex. '\n')
// delete this line from the string
// and return the deleted line
// if delim is empty, the extraction begin exactly at pos, and end at npos
std::string
extract_line(std::string & str, size_t pos = 0, std::string delim = "")
extract_line(std::string & str, size_t pos, std::string delim)
{
std::string del_str;
size_t begin;
size_t end;
size_t len;
begin = str.rfind(delim, pos);
if (begin == std::string::npos)
@@ -199,38 +218,23 @@ std::string
begin += delim.size();
end = str.find(delim, pos);
len = end;
if (end != std::string::npos)
end += delim.size();
if (delim.empty())
end = std::string::npos;
len = end - begin;
del_str = str.substr(begin, end - begin);
str.erase(begin, end - begin);
del_str = str.substr(begin, len);
str.erase(begin, len);
return del_str;
}
// get a line in a string, by delim
// same as extract, except it doesn't delete it
std::string get_line(std::string str, size_t pos = 0, std::string delim = "")
std::string get_line(std::string str, size_t pos, std::string delim)
{
std::string ret_str;
size_t begin;
size_t end;
std::string ret;
begin = str.rfind(delim, pos);
if (begin == std::string::npos)
begin = 0;
else
begin += delim.size();
end = str.find(delim, pos);
if (end != std::string::npos)
end += delim.size();
if (delim.empty())
end = std::string::npos;
ret_str = str.substr(begin, end - begin);
return ret_str;
ret = ::extract_line(str, pos, delim);
return ret;
}
size_t
@@ -271,6 +275,24 @@ size_t
return err;
}
// DEBUG
void print_special(std::string str)
{
char c;
for (size_t i = 0; i < str.size(); i++)
{
c = str[i];
if (c == '\r')
std::cout << "\\r";
else if (c == '\n')
std::cout << "\\n" << "\n";
else
std::cout << c;
fflush(stdout);
}
}
bool operator==(const listen_socket& lhs, int fd)
{ return lhs.fd == fd; }