diff --git a/srcs/Http/HTTPRequest.cpp b/srcs/Http/HTTPRequest.cpp index 7beb6a7e..36c94456 100644 --- a/srcs/Http/HTTPRequest.cpp +++ b/srcs/Http/HTTPRequest.cpp @@ -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 items = Split(line, " "); @@ -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"); diff --git a/srcs/Utils/utils.cpp b/srcs/Utils/utils.cpp index e4175a59..e09280de 100644 --- a/srcs/Utils/utils.cpp +++ b/srcs/Utils/utils.cpp @@ -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; } diff --git a/srcs/Utils/utils.hpp b/srcs/Utils/utils.hpp index d927bd02..150a50e5 100644 --- a/srcs/Utils/utils.hpp +++ b/srcs/Utils/utils.hpp @@ -9,9 +9,12 @@ std::vector Split(std::string const str, std::string const delim); std::string Join(std::vector 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); diff --git a/tests/unit_test/utils.cpp b/tests/unit_test/utils.cpp index 8d4282be..26059043 100644 --- a/tests/unit_test/utils.cpp +++ b/tests/unit_test/utils.cpp @@ -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)); +}