Skip to content

Commit 2022aad

Browse files
committed
replaced old trim function based on std::string by one using string_view
1 parent b367bc7 commit 2022aad

File tree

2 files changed

+10
-42
lines changed

2 files changed

+10
-42
lines changed

‎include/crow/middlewares/cookie_parser.h‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,6 @@ namespace crow
243243

244244
const std::string_view cookies_sv = req.get_header_value("Cookie");
245245

246-
auto trim_sv = [](const std::string_view sv) -> std::string_view {
247-
const size_t first = sv.find_first_not_of(" \t\n\r\f\v"); // same as isspace
248-
if (std::string_view::npos == first) {
249-
return sv.substr(0, 0);
250-
}
251-
const size_t last = sv.find_last_not_of(" \t\n\r\f\v");
252-
return sv.substr(first, (last - first + 1));
253-
};
254-
255246
size_t pos = 0;
256247
while (pos < cookies_sv.size())
257248
{
@@ -261,7 +252,7 @@ namespace crow
261252
}
262253

263254
std::string_view name_sv = cookies_sv.substr(pos, pos_equal - pos);
264-
name_sv = trim_sv(name_sv);
255+
name_sv = utility::trim(name_sv);
265256

266257
pos = pos_equal + 1;
267258
if (pos == cookies_sv.size()) {
@@ -279,7 +270,7 @@ namespace crow
279270
pos = pos_semicolon + 1;
280271
}
281272

282-
value_sv = trim_sv(value_sv);
273+
value_sv = utility::trim(value_sv);
283274

284275
if (!value_sv.empty() && value_sv.front() == '"' && value_sv.back() == '"')
285276
{

‎include/crow/utility.h‎

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -855,41 +855,18 @@ namespace crow
855855
return res;
856856
}
857857

858-
859-
/// Return a copy of the given string with its
858+
/// Return string view of the given string view with its
860859
/// leading and trailing whitespaces removed.
861-
inline static std::string trim(const std::string& v)
862-
{
863-
if (v.empty())
864-
return "";
865-
866-
size_t begin = 0, end = v.length();
867-
868-
size_t i;
869-
for (i = 0; i < v.length(); i++)
870-
{
871-
if (!std::isspace(v[i]))
872-
{
873-
begin = i;
874-
break;
875-
}
860+
inline static std::string_view trim(const std::string_view sv) {
861+
const size_t first = sv.find_first_not_of(" \t\n\r\f\v"); // same as isspace
862+
if (std::string_view::npos == first) {
863+
return sv.substr(0, 0);
876864
}
877-
878-
if (i == v.length())
879-
return "";
880-
881-
for (i = v.length(); i > 0; i--)
882-
{
883-
if (!std::isspace(v[i - 1]))
884-
{
885-
end = i;
886-
break;
887-
}
888-
}
889-
890-
return v.substr(begin, end - begin);
865+
const size_t last = sv.find_last_not_of(" \t\n\r\f\v");
866+
return sv.substr(first, (last - first + 1));
891867
}
892868

869+
893870
/**
894871
* @brief splits a string based on a separator
895872
*/

0 commit comments

Comments
 (0)