forked from drogonframework/drogon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use drogon::OStringStream instead of std::stringstream in text templa…
…te (drogonframework#322)
- Loading branch information
Showing
6 changed files
with
212 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/** | ||
* | ||
* OStringStream.h | ||
* An Tao | ||
* | ||
* Copyright 2018, An Tao. All rights reserved. | ||
* https://github.com/an-tao/drogon | ||
* Use of this source code is governed by a MIT license | ||
* that can be found in the License file. | ||
* | ||
* Drogon | ||
* | ||
*/ | ||
|
||
#pragma once | ||
#include <string> | ||
#include <sstream> | ||
#include <drogon/utils/string_view.h> | ||
|
||
namespace drogon | ||
{ | ||
namespace internal | ||
{ | ||
template <typename T> | ||
struct CanConvertToString | ||
{ | ||
using Type = std::remove_reference_t<std::remove_cv_t<T>>; | ||
|
||
private: | ||
using yes = std::true_type; | ||
using no = std::false_type; | ||
|
||
template <typename U> | ||
static auto test(int) -> decltype(std::to_string(U{}), yes()); | ||
|
||
template <typename> | ||
static no test(...); | ||
|
||
public: | ||
static constexpr bool value = | ||
std::is_same<decltype(test<Type>(0)), yes>::value; | ||
}; | ||
} // namespace internal | ||
class OStringStream | ||
{ | ||
public: | ||
OStringStream() = default; | ||
void reserve(size_t size) | ||
{ | ||
buffer_.reserve(size); | ||
} | ||
template <typename T> | ||
std::enable_if_t<!internal::CanConvertToString<T>::value, OStringStream&> | ||
operator<<(T&& value) | ||
{ | ||
std::stringstream ss; | ||
ss << std::forward<T>(value); | ||
buffer_.append(ss.str()); | ||
return *this; | ||
} | ||
template <typename T> | ||
std::enable_if_t<internal::CanConvertToString<T>::value, OStringStream&> | ||
operator<<(T&& value) | ||
{ | ||
buffer_.append(std::to_string(std::forward<T>(value))); | ||
return *this; | ||
} | ||
template <int N> | ||
OStringStream& operator<<(const char (&buf)[N]) | ||
{ | ||
buffer_.append(buf, N - 1); | ||
return *this; | ||
} | ||
OStringStream& operator<<(const string_view& str) | ||
{ | ||
buffer_.append(str.data(), str.length()); | ||
return *this; | ||
} | ||
OStringStream& operator<<(string_view&& str) | ||
{ | ||
buffer_.append(str.data(), str.length()); | ||
return *this; | ||
} | ||
|
||
OStringStream& operator<<(const std::string& str) | ||
{ | ||
buffer_.append(str); | ||
return *this; | ||
} | ||
OStringStream& operator<<(std::string&& str) | ||
{ | ||
buffer_.append(std::move(str)); | ||
return *this; | ||
} | ||
|
||
OStringStream& operator<<(const double& d) | ||
{ | ||
std::stringstream ss; | ||
ss << d; | ||
buffer_.append(ss.str()); | ||
return *this; | ||
} | ||
|
||
OStringStream& operator<<(const float& f) | ||
{ | ||
std::stringstream ss; | ||
ss << f; | ||
buffer_.append(ss.str()); | ||
return *this; | ||
} | ||
|
||
OStringStream& operator<<(double&& d) | ||
{ | ||
std::stringstream ss; | ||
ss << d; | ||
buffer_.append(ss.str()); | ||
return *this; | ||
} | ||
|
||
OStringStream& operator<<(float&& f) | ||
{ | ||
std::stringstream ss; | ||
ss << f; | ||
buffer_.append(ss.str()); | ||
return *this; | ||
} | ||
|
||
std::string& str() | ||
{ | ||
return buffer_; | ||
} | ||
const std::string& str() const | ||
{ | ||
return buffer_; | ||
} | ||
|
||
private: | ||
std::string buffer_; | ||
}; | ||
} // namespace drogon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <gtest/gtest.h> | ||
#include <drogon/utils/OStringStream.h> | ||
#include <string> | ||
#include <iostream> | ||
|
||
TEST(OStringStreamTest, interger) | ||
{ | ||
drogon::OStringStream ss; | ||
ss << 12; | ||
ss << 345L; | ||
EXPECT_EQ(ss.str(), "12345"); | ||
} | ||
|
||
TEST(OStringStreamTest, float_number) | ||
{ | ||
drogon::OStringStream ss; | ||
ss << 3.14f; | ||
ss << 3.1416; | ||
EXPECT_EQ(ss.str(), "3.143.1416"); | ||
} | ||
|
||
TEST(OStringStreamTest, literal_string) | ||
{ | ||
drogon::OStringStream ss; | ||
ss << "hello"; | ||
ss << " world"; | ||
EXPECT_EQ(ss.str(), "hello world"); | ||
} | ||
|
||
TEST(OStringStreamTest, string_view) | ||
{ | ||
drogon::OStringStream ss; | ||
ss << drogon::string_view("hello"); | ||
ss << drogon::string_view(" world"); | ||
EXPECT_EQ(ss.str(), "hello world"); | ||
} | ||
|
||
TEST(OStringStreamTest, std_string) | ||
{ | ||
drogon::OStringStream ss; | ||
ss << std::string("hello"); | ||
ss << std::string(" world"); | ||
EXPECT_EQ(ss.str(), "hello world"); | ||
} | ||
|
||
TEST(OStringStreamTest, mix) | ||
{ | ||
drogon::OStringStream ss; | ||
ss << std::string("hello"); | ||
ss << drogon::string_view(" world"); | ||
ss << "!"; | ||
ss << 123; | ||
ss << 3.14f; | ||
|
||
EXPECT_EQ(ss.str(), "hello world!1233.14"); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |