Permalink
Browse files

http, refactor: rewrite HttpCollection with std::pair.

  • Loading branch information...
xicilion committed Jul 1, 2018
1 parent 16b20f1 commit 78cd60e5b413cb2f1ca53b298ef76813ef6df53d
Showing with 53 additions and 33 deletions.
  1. +4 −7 fibjs/include/HttpCollection.h
  2. +49 −26 fibjs/src/http/HttpCollection.cpp
@@ -39,15 +39,12 @@ class HttpCollection : public HttpCollection_base {
public:
void add(const char* name, int32_t szName, const char* value, int32_t szValue)
{
m_names[m_count].assign(name, szName);
m_values[m_count].assign(value, szValue);
m_count++;
add(exlib::string(name, szName), exlib::string(value, szValue));
}
void add(exlib::string& name, exlib::string& value)
{
m_names[m_count].assign(name);
m_values[m_count].assign(value);
m_map[m_count] = pair(name, value);
m_count++;
}
@@ -58,8 +55,8 @@ class HttpCollection : public HttpCollection_base {
result_t parseCookie(exlib::string& str);
private:
QuickArray<exlib::string> m_names;
QuickArray<exlib::string> m_values;
typedef std::pair<exlib::string, exlib::string> pair;
QuickArray<pair> m_map;
int32_t m_count;
};
@@ -19,7 +19,10 @@ size_t HttpCollection::size()
int32_t i;
for (i = 0; i < m_count; i++)
sz += m_names[i].length() + m_values[i].length() + 4;
{
pair &_pair = m_map[i];
sz += _pair.first.length() + _pair.second.length() + 4;
}
return sz;
}
@@ -43,8 +46,9 @@ size_t HttpCollection::getData(char* buf, size_t sz)
int32_t i;
for (i = 0; i < m_count; i++) {
exlib::string& n = m_names[i];
exlib::string& v = m_values[i];
pair &_pair = m_map[i];
exlib::string& n = _pair.first;
exlib::string& v = _pair.second;
cp(buf, sz, pos, n.c_str(), n.length());
cp(buf, sz, pos, ": ", 2);
@@ -183,7 +187,7 @@ result_t HttpCollection::has(exlib::string name, bool& retVal)
retVal = false;
for (i = 0; i < m_count; i++)
if (!qstricmp(m_names[i].c_str(), name.c_str())) {
if (!qstricmp(m_map[i].first.c_str(), name.c_str())) {
retVal = true;
break;
}
@@ -196,10 +200,14 @@ result_t HttpCollection::first(exlib::string name, Variant& retVal)
int32_t i;
for (i = 0; i < m_count; i++)
if (!qstricmp(m_names[i].c_str(), name.c_str())) {
retVal = m_values[i];
{
pair &_pair = m_map[i];
if (!qstricmp(_pair.first.c_str(), name.c_str())) {
retVal = _pair.second;
return 0;
}
}
return CALL_RETURN_NULL;
}
@@ -212,8 +220,12 @@ result_t HttpCollection::all(exlib::string name, obj_ptr<NArray>& retVal)
list = new NArray();
for (i = 0; i < m_count; i++)
if (!qstricmp(m_names[i].c_str(), name.c_str()))
list->append(m_values[i]);
{
pair &_pair = m_map[i];
if (!qstricmp(_pair.first.c_str(), name.c_str()))
list->append(_pair.second);
}
retVal = list;
return 0;
@@ -225,8 +237,7 @@ result_t HttpCollection::add(exlib::string name, Variant value)
value.toString(s);
m_names[m_count] = name;
m_values[m_count] = s;
m_map[m_count] = pair(name, s);
m_count++;
return 0;
@@ -252,28 +263,34 @@ result_t HttpCollection::set(exlib::string name, Variant value)
bool bFound = false;
for (i = 0; i < m_count; i++)
if (!qstricmp(m_names[i].c_str(), name.c_str())) {
{
pair &_pair = m_map[i];
if (!qstricmp(_pair.first.c_str(), name.c_str())) {
exlib::string s;
value.toString(s);
m_values[i] = s;
_pair.second = s;
bFound = true;
break;
}
}
if (bFound) {
int32_t p = ++i;
for (; i < m_count; i++)
if (qstricmp(m_names[i].c_str(), name.c_str())) {
if (i != p) {
m_names[p] = m_names[i];
m_values[p] = m_values[i];
}
{
pair &_pair = m_map[i];
if (qstricmp(_pair.first.c_str(), name.c_str())) {
if (i != p)
m_map[p] = _pair;
p++;
}
}
m_count = p;
} else
@@ -302,14 +319,16 @@ result_t HttpCollection::remove(exlib::string name)
int32_t p = 0;
for (i = 0; i < m_count; i++)
if (qstricmp(m_names[i].c_str(), name.c_str())) {
if (i != p) {
m_names[p] = m_names[i];
m_values[p] = m_values[i];
}
{
pair &_pair = m_map[i];
if (qstricmp(_pair.first.c_str(), name.c_str())) {
if (i != p)
m_map[p] = _pair;
p++;
}
}
m_count = p;
@@ -324,9 +343,12 @@ result_t HttpCollection::_named_getter(exlib::string property, Variant& retVal)
v8::Local<v8::Array> a;
for (i = 0; i < m_count; i++)
if (!qstricmp(m_names[i].c_str(), property.c_str())) {
{
pair &_pair = m_map[i];
if (!qstricmp(_pair.first.c_str(), property.c_str())) {
if (n == 0) {
v = m_values[i];
v = _pair.second;
n = 1;
} else {
if (n == 1) {
@@ -336,10 +358,11 @@ result_t HttpCollection::_named_getter(exlib::string property, Variant& retVal)
v = a;
}
Variant t = m_values[i];
Variant t = _pair.second;
a->Set(n++, t);
}
}
}
if (n > 0) {
retVal = v;
@@ -357,7 +380,7 @@ result_t HttpCollection::_named_enumerator(v8::Local<v8::Array>& retVal)
retVal = v8::Array::New(isolate->m_isolate);
for (i = 0, n = 0; i < m_count; i++) {
exlib::string& name = m_names[i];
exlib::string& name = m_map[i].first;
if (name_set.insert(name).second)
retVal->Set(n++, isolate->NewString(name));
}

0 comments on commit 78cd60e

Please sign in to comment.