Commit 75bdb47d authored by zhujiashun's avatar zhujiashun

Make parameter of ctor in QueryRemover be a pointer to avoid an extra copy

parent 557daeff
......@@ -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) {
......@@ -463,9 +463,9 @@ 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;
......@@ -473,24 +473,24 @@ void QueryRemover::remove_current_key_and_value() {
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;
}
......
......@@ -265,7 +265,7 @@ private:
// 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(); }
......@@ -285,7 +285,7 @@ public:
std::string modified_query();
private:
const std::string _query;
const std::string* _query;
QuerySplitter _qs;
std::string _modified_query;
size_t _iterated_len;
......
......@@ -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");
......@@ -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");
......@@ -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;
......@@ -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;
......@@ -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&&");
......@@ -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(), "");
......@@ -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(), "");
......@@ -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");
......@@ -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");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment