Skip to content

Commit

Permalink
utils関数にtrimを追加 (#135)
Browse files Browse the repository at this point in the history
* feat: Trim関数をutilに

* fix: 既存の処理でtrimを使用するように
  • Loading branch information
hayashi-ay authored Jul 29, 2022
1 parent 8a71902 commit 586b149
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
16 changes: 1 addition & 15 deletions srcs/Http/HTTPRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,6 @@ void HTTPRequest::Parse(std::string str) {
}
}

// utilにあってもいいかも
// OWSをtrimする
static std::string ltrim(const std::string &s) {
size_t start = s.find_first_not_of(" \t");
return (start == std::string::npos) ? "" : s.substr(start);
}

static std::string rtrim(const std::string &s) {
size_t end = s.find_last_not_of(" \t");
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}

static std::string trim(const std::string &s) { return rtrim(ltrim(s)); }

void HTTPRequest::ParseRequestLine(std::string line) {
this->logging_.Debug("ParseRequestLine");
std::vector<std::string> items = Split(line, " ");
Expand Down Expand Up @@ -169,7 +155,7 @@ void HTTPRequest::ParseHeader(std::string str) {
throw std::runtime_error("header format invalid");
}
std::string header = lines[i].substr(0, found);
std::string value = trim(lines[i].substr(found + 1));
std::string value = Trim(lines[i].substr(found + 1), " \t");
if (value.empty()) {
// TODO(hayashi-ay): 対応するエラーを定義する
throw std::runtime_error("header format invalid");
Expand Down
12 changes: 12 additions & 0 deletions srcs/Utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ bool IsInteger(std::string str) {

int ToInteger(std::string str) { return std::atoi(str.c_str()); }

std::string LeftTrim(const std::string& s, const std::string& chars) {
size_t start = s.find_first_not_of(chars);
return (start == std::string::npos) ? "" : s.substr(start);
}
std::string RightTrim(const std::string& s, const std::string& chars) {
size_t end = s.find_last_not_of(chars);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
std::string Trim(const std::string& s, const std::string& chars) {
return RightTrim(LeftTrim(s, chars), chars);
}

bool StartsWith(const std::string& s, const std::string& prefix) {
return s.find(prefix, 0) == 0;
}
Expand Down
5 changes: 4 additions & 1 deletion srcs/Utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ std::vector<std::string> Split(std::string const str, std::string const delim);
std::string Join(std::vector<std::string> strs, std::string separator);

bool IsInteger(std::string str);

int ToInteger(std::string str);

std::string LeftTrim(const std::string& s, const std::string& chars);
std::string RightTrim(const std::string& s, const std::string& chars);
std::string Trim(const std::string& s, const std::string& chars);

bool IsDigit(const char c);

bool IsAlpha(const char c);
Expand Down
9 changes: 9 additions & 0 deletions tests/unit_test/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ TEST(ToInteger, normal) {
ASSERT_EQ(2147483647, ToInteger("2147483647"));
ASSERT_EQ(-2147483648, ToInteger("-2147483648"));
}

TEST(Trim, normal) {
std::string str = " \t42 \t";
std::string chars = " \t";

ASSERT_EQ("42 \t", LeftTrim(str, chars));
ASSERT_EQ(" \t42", RightTrim(str, chars));
ASSERT_EQ("42", Trim(str, chars));
}

0 comments on commit 586b149

Please sign in to comment.