Skip to content

Commit 62cb469

Browse files
authored
feat: convert const& to value(std::string_view) (#1260)
1 parent 560ac27 commit 62cb469

File tree

8 files changed

+23
-23
lines changed

8 files changed

+23
-23
lines changed

include/cpr/callback.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class HeaderCallback {
3434
public:
3535
HeaderCallback() = default;
3636
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
37-
HeaderCallback(std::function<bool(const std::string_view& header, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
38-
bool operator()(const std::string_view& header) const {
37+
HeaderCallback(std::function<bool(std::string_view header, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
38+
bool operator()(std::string_view header) const {
3939
if(!callback)
4040
{
4141
return true;
@@ -44,15 +44,15 @@ class HeaderCallback {
4444
}
4545

4646
intptr_t userdata{};
47-
std::function<bool(const std::string_view& header, intptr_t userdata)> callback;
47+
std::function<bool(std::string_view header, intptr_t userdata)> callback;
4848
};
4949

5050
class WriteCallback {
5151
public:
5252
WriteCallback() = default;
5353
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
54-
WriteCallback(std::function<bool(const std::string_view& data, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
55-
bool operator()(const std::string_view& data) const {
54+
WriteCallback(std::function<bool(std::string_view data, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
55+
bool operator()(std::string_view data) const {
5656
if(!callback)
5757
{
5858
return true;
@@ -61,7 +61,7 @@ class WriteCallback {
6161
}
6262

6363
intptr_t userdata{};
64-
std::function<bool(const std::string_view& data, intptr_t userdata)> callback;
64+
std::function<bool(std::string_view data, intptr_t userdata)> callback;
6565
};
6666

6767
class ProgressCallback {
@@ -94,17 +94,17 @@ class DebugCallback {
9494
};
9595
DebugCallback() = default;
9696
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
97-
DebugCallback(std::function<void(InfoType type, std::string data, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
98-
void operator()(InfoType type, std::string data) const {
97+
DebugCallback(std::function<void(InfoType type, std::string_view data, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
98+
void operator()(InfoType type, std::string_view data) const {
9999
if(!callback)
100100
{
101101
return;
102102
}
103-
callback(type, std::move(data), userdata);
103+
callback(type, data, userdata);
104104
}
105105

106106
intptr_t userdata{};
107-
std::function<void(InfoType type, std::string data, intptr_t userdata)> callback;
107+
std::function<void(InfoType type, std::string_view data, intptr_t userdata)> callback;
108108
};
109109

110110
/**

test/async_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using namespace cpr;
1414

1515
static HttpServer* server = new HttpServer();
1616

17-
bool write_data(const std::string_view& /*data*/, intptr_t /*userdata*/) {
17+
bool write_data(std::string_view /*data*/, intptr_t /*userdata*/) {
1818
return true;
1919
}
2020

test/callback_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -857,27 +857,27 @@ TEST(CallbackDataTests, CallbackReadFunctionChunkedTest) {
857857

858858
TEST(CallbackDataTests, CallbackHeaderFunctionCancelTest) {
859859
Url url{server->GetBaseUrl() + "/url_post.html"};
860-
Response response = Post(url, HeaderCallback{[](const std::string_view& /*header*/, intptr_t /*userdata*/) -> bool { return false; }});
860+
Response response = Post(url, HeaderCallback{[](const std::string_view /*header*/, intptr_t /*userdata*/) -> bool { return false; }});
861861
EXPECT_TRUE((response.error.code == ErrorCode::ABORTED_BY_CALLBACK) || (response.error.code == ErrorCode::WRITE_ERROR));
862862
}
863863

864864
TEST(CallbackDataTests, CallbackHeaderFunctionTextTest) {
865865
Url url{server->GetBaseUrl() + "/url_post.html"};
866866
std::vector<std::string> expected_headers{"HTTP/1.1 201 Created\r\n", "Content-Type: application/json\r\n", "\r\n"};
867867
std::set<std::string> response_headers;
868-
Post(url, HeaderCallback{[&response_headers](const std::string_view& header, intptr_t /*userdata*/) -> bool {
868+
Post(url, HeaderCallback{[&response_headers](const std::string_view header, intptr_t /*userdata*/) -> bool {
869869
response_headers.insert(std::string{header});
870870
return true;
871871
}});
872-
for (std::string& header : expected_headers) {
872+
for (const std::string& header : expected_headers) {
873873
std::cout << header << '\n';
874874
EXPECT_TRUE(response_headers.count(header));
875875
}
876876
}
877877

878878
TEST(CallbackDataTests, CallbackWriteFunctionCancelTest) {
879879
Url url{server->GetBaseUrl() + "/url_post.html"};
880-
Response response = Post(url, WriteCallback{[](const std::string_view& /*header*/, intptr_t /*userdata*/) -> bool { return false; }});
880+
Response response = Post(url, WriteCallback{[](const std::string_view /*header*/, intptr_t /*userdata*/) -> bool { return false; }});
881881
EXPECT_TRUE((response.error.code == ErrorCode::ABORTED_BY_CALLBACK) || (response.error.code == ErrorCode::WRITE_ERROR));
882882
}
883883

@@ -888,7 +888,7 @@ TEST(CallbackDataTests, CallbackWriteFunctionTextTest) {
888888
" \"x\": 5\n"
889889
"}"};
890890
std::string response_text;
891-
Post(url, Payload{{"x", "5"}}, WriteCallback{[&response_text](const std::string_view& header, intptr_t /*userdata*/) -> bool {
891+
Post(url, Payload{{"x", "5"}}, WriteCallback{[&response_text](const std::string_view header, intptr_t /*userdata*/) -> bool {
892892
response_text.append(header);
893893
return true;
894894
}});
@@ -919,7 +919,7 @@ TEST(CallbackDataTests, CallbackDebugFunctionTextTest) {
919919
Url url{server->GetBaseUrl() + "/url_post.html"};
920920
Body body{"x=5"};
921921
std::string debug_body;
922-
Response response = Post(url, body, DebugCallback{[&](DebugCallback::InfoType type, const std::string& data, intptr_t /*userdata*/) {
922+
Response response = Post(url, body, DebugCallback{[&](DebugCallback::InfoType type, std::string_view data, intptr_t /*userdata*/) {
923923
if (type == DebugCallback::InfoType::DATA_OUT) {
924924
debug_body = data;
925925
}

test/download_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
static cpr::HttpServer* server = new cpr::HttpServer();
1717

18-
bool write_data(const std::string_view& /*data*/, intptr_t /*userdata*/) {
18+
bool write_data(std::string_view /*data*/, intptr_t /*userdata*/) {
1919
return true;
2020
}
2121

@@ -123,7 +123,7 @@ TEST(DownloadTests, RangeTestMultipleRangesOption) {
123123
EXPECT_EQ(download_size, response.downloaded_bytes);
124124
}
125125

126-
bool real_write_data(const std::string_view& data, intptr_t userdata) {
126+
bool real_write_data(std::string_view data, intptr_t userdata) {
127127
// NOLINTNEXTLINE (cppcoreguidelines-pro-type-reinterpret-cast)
128128
std::string* dst = reinterpret_cast<std::string*>(userdata);
129129
*dst += data;

test/interceptor_multi_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class ChangeRequestMethodToDeleteInterceptorMulti : public InterceptorMulti {
122122
}
123123
};
124124

125-
bool write_data(const std::string_view& /*data*/, intptr_t /*userdata*/) {
125+
bool write_data(std::string_view /*data*/, intptr_t /*userdata*/) {
126126
return true;
127127
}
128128

test/interceptor_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class ChangeRequestMethodToDeleteInterceptor : public Interceptor {
9494
}
9595
};
9696

97-
bool write_data(const std::string_view& /*data*/, intptr_t /*userdata*/) {
97+
bool write_data(std::string_view /*data*/, intptr_t /*userdata*/) {
9898
return true;
9999
}
100100

test/multiperform_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ using namespace cpr;
1515

1616
static HttpServer* server = new HttpServer();
1717

18-
bool write_data(const std::string_view& /*data*/, intptr_t /*userdata*/) {
18+
bool write_data(std::string_view /*data*/, intptr_t /*userdata*/) {
1919
return true;
2020
}
2121

test/session_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static HttpServer* server = new HttpServer();
2222
std::chrono::milliseconds sleep_time{50};
2323
std::chrono::seconds zero{0};
2424

25-
bool write_data(const std::string_view& /*data*/, intptr_t /*userdata*/) {
25+
bool write_data(std::string_view /*data*/, intptr_t /*userdata*/) {
2626
return true;
2727
}
2828

0 commit comments

Comments
 (0)