bool HttpMessage::ParseHeaders(const StringPiece& data, HttpMessage::ErrorCode* error) { HttpMessage::ErrorCode error_placeholder; if (error == NULL) error = &error_placeholder; StringPiece::size_type pos = data.find_first_of('\n'); if (pos == StringPiece::npos) { pos = data.size(); } std::string first_line = StringTrimRight(data.substr(0, pos), "\r"); if (first_line.empty()) { *error = HttpMessage::ERROR_NO_START_LINE; return false; } if (!ParseStartLine(first_line, error)) return false; int error_code = 0; bool result = m_headers.Parse(data.substr(pos + 1), &error_code); *error = static_cast<HttpMessage::ErrorCode>(error_code); return result; }
TEST(StrUtilTest, Ops) { string str(" Hello World "); StringTrimLeft(&str); EXPECT_EQ("Hello World ", str); StringTrimRight(&str); EXPECT_EQ("Hello World", str); StringToLower(&str); EXPECT_EQ("hello world", str); string str2(" Hello World "); StringTrim(&str2); EXPECT_EQ("Hello World", str2); }
std::string StringTrimRight(const StringPiece& str, const StringPiece& trim_value) { std::string res = str.as_string(); StringTrimRight(&res, trim_value); return res; }
std::string StringTrimRight(const StringPiece& str) { std::string res = str.as_string(); StringTrimRight(&res); return res; }
std::string StringUtil::StringTrim( const std::string& strContent ) { return StringTrimLeft(StringTrimRight(strContent)); }