Skip to content

Commit

Permalink
Make parameter of ctor in QueryRemover be a pointer to avoid an extra…
Browse files Browse the repository at this point in the history
… copy
  • Loading branch information
zhujiashun committed Sep 20, 2017
1 parent 557daef commit 75bdb47
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
20 changes: 10 additions & 10 deletions src/brpc/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ void QuerySplitter::split() {
_is_split = true;
}

QueryRemover::QueryRemover(const std::string& str)
QueryRemover::QueryRemover(const std::string* str)
: _query(str)
, _qs(_query.data(), _query.data() + _query.size())
, _qs(str->data(), str->data() + str->size())
, _iterated_len(0)
, _removed_current_key_value(false)
, _ever_removed(false) {
Expand Down Expand Up @@ -463,34 +463,34 @@ void QueryRemover::remove_current_key_and_value() {
_removed_current_key_value = true;
if (!_ever_removed) {
_ever_removed = true;
size_t offset = key().data() - _query.data();
size_t len = offset - ((offset > 0 && _query[offset - 1] == '&')? 1: 0);
_modified_query.append(_query.data(), len);
size_t offset = key().data() - _query->data();
size_t len = offset - ((offset > 0 && (*_query)[offset - 1] == '&')? 1: 0);
_modified_query.append(_query->data(), len);
_iterated_len += len;
}
return;
}

std::string QueryRemover::modified_query() {
if (!_ever_removed) {
return _query;
return *_query;
}
size_t offset = key().data() - _query.data();
size_t offset = key().data() - _query->data();
// find out where the remaining string starts
if (_removed_current_key_value) {
size_t size = key_and_value().length();
while (offset + size < _query.size() && _query[offset + size] == '&') {
while (offset + size < _query->size() && (*_query)[offset + size] == '&') {
// ingore unnecessary '&'
size += 1;
}
offset += size;
}
_modified_query.resize(_iterated_len);
if (offset < _query.size()) {
if (offset < _query->size()) {
if (!_modified_query.empty()) {
_modified_query.push_back('&');
}
_modified_query.append(_query, offset, std::string::npos);
_modified_query.append(*_query, offset, std::string::npos);
}
return _modified_query;
}
Expand Down
4 changes: 2 additions & 2 deletions src/brpc/uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class QuerySplitter {
// query.
class QueryRemover {
public:
QueryRemover(const std::string& str);
QueryRemover(const std::string* str);

const butil::StringPiece& key() { return _qs.key();}
const butil::StringPiece& value() { return _qs.value(); }
Expand All @@ -285,7 +285,7 @@ class QueryRemover {
std::string modified_query();

private:
const std::string _query;
const std::string* _query;
QuerySplitter _qs;
std::string _modified_query;
size_t _iterated_len;
Expand Down
18 changes: 9 additions & 9 deletions test/brpc_uri_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ TEST(URITest, copy_and_assign) {

TEST(URITest, query_remover_sanity) {
std::string query = "key1=value1&key2=value2&key3=value3";
brpc::QueryRemover qr(query);
brpc::QueryRemover qr(&query);
ASSERT_TRUE(qr);
ASSERT_EQ(qr.key(), "key1");
ASSERT_EQ(qr.value(), "value1");
Expand All @@ -345,7 +345,7 @@ TEST(URITest, query_remover_sanity) {

TEST(URITest, query_remover_remove_current_key_and_value) {
std::string query = "key1=value1&key2=value2&key3=value3";
brpc::QueryRemover qr(query);
brpc::QueryRemover qr(&query);
ASSERT_TRUE(qr);
qr.remove_current_key_and_value();
ASSERT_EQ(qr.modified_query(), "key2=value2&key3=value3");
Expand All @@ -366,7 +366,7 @@ TEST(URITest, query_remover_remove_current_key_and_value) {
TEST(URITest, query_remover_random_remove) {
std::string query = "key1=value1&key2=value2&key3=value3&key4=value4"
"&key5=value5&key6=value6";
brpc::QueryRemover qr(query);
brpc::QueryRemover qr(&query);
ASSERT_TRUE(qr);
++qr;
++qr;
Expand All @@ -381,7 +381,7 @@ TEST(URITest, query_remover_random_remove) {
TEST(URITest, query_remover_onekey_remove) {
std::string query = "key1=value1&key2=value2&key3=value3&key4=value4"
"&key5=value5&key6=value6";
brpc::QueryRemover qr(query);
brpc::QueryRemover qr(&query);
ASSERT_TRUE(qr);
++qr;
++qr;
Expand All @@ -399,7 +399,7 @@ TEST(URITest, query_remover_onekey_remove) {

TEST(URITest, query_remover_consecutive_ampersand) {
std::string query = "key1=value1&&&key2=value2&key3=value3&&";
brpc::QueryRemover qr(query);
brpc::QueryRemover qr(&query);
ASSERT_TRUE(qr);
qr.remove_current_key_and_value();
ASSERT_EQ(qr.modified_query(), "key2=value2&key3=value3&&");
Expand All @@ -415,7 +415,7 @@ TEST(URITest, query_remover_consecutive_ampersand) {

TEST(URITest, query_remover_only_equality) {
std::string query ="key1=&&key2&=&key3=value3";
brpc::QueryRemover qr(query);
brpc::QueryRemover qr(&query);
ASSERT_TRUE(qr);
ASSERT_EQ(qr.key(), "key1");
ASSERT_EQ(qr.value(), "");
Expand All @@ -436,7 +436,7 @@ TEST(URITest, query_remover_only_equality) {

TEST(URITest, query_remover_only_one_key) {
std::string query = "key1";
brpc::QueryRemover qr(query);
brpc::QueryRemover qr(&query);
ASSERT_TRUE(qr);
ASSERT_EQ(qr.key(), "key1");
ASSERT_EQ(qr.value(), "");
Expand All @@ -448,7 +448,7 @@ TEST(URITest, query_remover_only_one_key) {

TEST(URITest, query_remover_no_modify) {
std::string query = "key1=value1&key2=value2&key3=value3";
brpc::QueryRemover qr(query);
brpc::QueryRemover qr(&query);
ASSERT_TRUE(qr);
ASSERT_EQ(qr.key(), "key1");
ASSERT_EQ(qr.value(), "value1");
Expand All @@ -461,7 +461,7 @@ TEST(URITest, query_remover_no_modify) {

TEST(URITest, query_remover_key_value_not_changed_after_modified_query) {
std::string query = "key1=value1&key2=value2&key3=value3";
brpc::QueryRemover qr(query);
brpc::QueryRemover qr(&query);
ASSERT_TRUE(qr);
++qr;
ASSERT_EQ(qr.key(), "key2");
Expand Down

0 comments on commit 75bdb47

Please sign in to comment.