Skip to content
Permalink
Browse files

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

  • Loading branch information...
xicilion committed Jul 1, 2018
1 parent 78cd60e commit 958a835b5d492a45605ea450e222f9e4241b87cf
Showing with 37 additions and 22 deletions.
  1. +2 −2 fibjs/include/HttpUploadCollection.h
  2. +35 −20 fibjs/src/http/HttpUploadCollection.cpp
@@ -39,8 +39,8 @@ class HttpUploadCollection : public HttpCollection_base {
void parse(exlib::string& str, const char* boundary);

private:
QuickArray<exlib::string> m_names;
QuickArray<VariantEx> m_values;
typedef std::pair<exlib::string, VariantEx> pair;
QuickArray<pair> m_map;
int32_t m_count;
};

@@ -220,7 +220,7 @@ result_t HttpUploadCollection::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;
}
@@ -233,10 +233,14 @@ result_t HttpUploadCollection::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;
}
@@ -249,17 +253,20 @@ result_t HttpUploadCollection::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;
}

result_t HttpUploadCollection::add(exlib::string name, Variant value)
{
m_names[m_count] = name;
m_values[m_count] = value;
m_map[m_count] = pair(name, value);
m_count++;

return 0;
@@ -285,24 +292,30 @@ result_t HttpUploadCollection::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())) {
m_values[i] = value;
{
pair &_pair = m_map[i];

if (!qstricmp(_pair.first.c_str(), name.c_str())) {
_pair.second = value;
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
@@ -331,14 +344,16 @@ result_t HttpUploadCollection::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;

@@ -358,7 +373,7 @@ result_t HttpUploadCollection::_named_enumerator(v8::Local<v8::Array>& retVal)

retVal = v8::Array::New(isolate->m_isolate);
for (i = 0; i < m_count; i++)
retVal->Set(i, isolate->NewString(m_names[i]));
retVal->Set(i, isolate->NewString(m_map[i].first));

return 0;
}

0 comments on commit 958a835

Please sign in to comment.
You can’t perform that action at this time.