Skip to content

Commit

Permalink
Modify variable names and simplify code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Barenboim committed Nov 8, 2024
1 parent c111301 commit 64a91d1
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/util/StringUtil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/

#include <ctype.h>
#include <string>
#include <vector>
#include <algorithm>
#include "StringUtil.h"

Expand Down Expand Up @@ -85,10 +87,10 @@ void StringUtil::url_decode(std::string& str)
std::string StringUtil::url_encode(const std::string& str)
{
const char *cur = str.c_str();
const char *ed = cur + str.size();
const char *end = cur + str.size();
std::string res;

while (cur < ed)
while (cur < end)
{
if (*cur == ' ')
res += '+';
Expand All @@ -113,10 +115,10 @@ std::string StringUtil::url_encode(const std::string& str)
std::string StringUtil::url_encode_component(const std::string& str)
{
const char *cur = str.c_str();
const char *ed = cur + str.size();
const char *end = cur + str.size();
std::string res;

while (cur < ed)
while (cur < end)
{
if (*cur == ' ')
res += '+';
Expand All @@ -139,41 +141,41 @@ std::string StringUtil::url_encode_component(const std::string& str)

std::vector<std::string> StringUtil::split(const std::string& str, char sep)
{
std::string::const_iterator start = str.begin();
std::string::const_iterator cur = str.begin();
std::string::const_iterator end = str.end();
std::string::const_iterator next = find(start, end, sep);
std::string::const_iterator next = find(cur, end, sep);
std::vector<std::string> res;

while (next != end)
{
res.emplace_back(start, next);
start = next + 1;
next = std::find(start, end, sep);
res.emplace_back(cur, next);
cur = next + 1;
next = std::find(cur, end, sep);
}

res.emplace_back(start, next);
res.emplace_back(cur, next);
return res;
}

std::vector<std::string> StringUtil::split_filter_empty(const std::string& str,
char sep)
{
std::vector<std::string> res;
std::string::const_iterator start = str.begin();
std::string::const_iterator cur = str.begin();
std::string::const_iterator end = str.end();
std::string::const_iterator next = find(start, end, sep);
std::string::const_iterator next = find(cur, end, sep);

while (next != end)
{
if (start < next)
res.emplace_back(start, next);
if (cur < next)
res.emplace_back(cur, next);

start = next + 1;
next = find(start, end, sep);
cur = next + 1;
next = find(cur, end, sep);
}

if (start < next)
res.emplace_back(start, next);
if (cur < next)
res.emplace_back(cur, next);

return res;
}
Expand All @@ -184,27 +186,27 @@ std::string StringUtil::strip(const std::string& str)

if (!str.empty())
{
const char *st = str.c_str();
const char *ed = st + str.size() - 1;
const char *cur = str.c_str();
const char *end = cur + str.size();

while (st <= ed)
while (cur < end)
{
if (!isspace(*st))
if (!isspace(*cur))
break;

st++;
cur++;
}

while (ed >= st)
while (end > cur)
{
if (!isspace(*ed))
if (!isspace(*(end - 1)))
break;

ed--;
end--;
}

if (ed >= st)
res.assign(st, ed - st + 1);
if (end > cur)
res.assign(cur, end - cur);
}

return res;
Expand Down

0 comments on commit 64a91d1

Please sign in to comment.