aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Richard <[email protected]>2005-12-30 20:32:05 +0000
committerVincent Richard <[email protected]>2005-12-30 20:32:05 +0000
commit9439d60f9690458546a041e8cde5b480da8d1f89 (patch)
treec47e9f394aab0b696206a591cd13ae81b1076967
parentRewritten some code without using iterators. (diff)
downloadvmime-9439d60f9690458546a041e8cde5b480da8d1f89.tar.gz
vmime-9439d60f9690458546a041e8cde5b480da8d1f89.zip
Rewritten code using std:: algorithms (thanks to Pierre THIERRY).
-rw-r--r--src/header.cpp66
-rw-r--r--vmime/header.hpp25
2 files changed, 64 insertions, 27 deletions
diff --git a/src/header.cpp b/src/header.cpp
index 719e2456..d7159e00 100644
--- a/src/header.cpp
+++ b/src/header.cpp
@@ -24,6 +24,8 @@
#include "vmime/header.hpp"
#include "vmime/parserHelpers.hpp"
+#include <algorithm>
+
namespace vmime
{
@@ -143,29 +145,23 @@ header& header::operator=(const header& other)
const bool header::hasField(const string& fieldName) const
{
- const string name = utility::stringUtils::toLower(fieldName);
-
- std::vector <ref <headerField> >::const_iterator pos = m_fields.begin();
- const std::vector <ref <headerField> >::const_iterator end = m_fields.end();
+ std::vector <ref <headerField> >::const_iterator pos =
+ std::find_if(m_fields.begin(), m_fields.end(),
+ fieldHasName(utility::stringUtils::toLower(fieldName)));
- for ( ; pos != end && utility::stringUtils::toLower((*pos)->getName()) != name ; ++pos);
-
- return (pos != end);
+ return (pos != m_fields.end());
}
ref <headerField> header::findField(const string& fieldName) const
{
- const string name = utility::stringUtils::toLower(fieldName);
-
// Find the first field that matches the specified name
- std::vector <ref <headerField> >::const_iterator pos = m_fields.begin();
- const std::vector <ref <headerField> >::const_iterator end = m_fields.end();
-
- for ( ; pos != end && utility::stringUtils::toLower((*pos)->getName()) != name ; ++pos);
+ std::vector <ref <headerField> >::const_iterator pos =
+ std::find_if(m_fields.begin(), m_fields.end(),
+ fieldHasName(utility::stringUtils::toLower(fieldName)));
// No field with this name can be found
- if (pos == end)
+ if (pos == m_fields.end())
{
throw exceptions::no_such_field();
}
@@ -179,21 +175,11 @@ ref <headerField> header::findField(const string& fieldName) const
std::vector <ref <headerField> > header::findAllFields(const string& fieldName)
{
- const string name = utility::stringUtils::toLower(fieldName);
-
std::vector <ref <headerField> > result;
+ std::back_insert_iterator <std::vector <ref <headerField> > > back(result);
- std::vector <ref <headerField> >::const_iterator pos = m_fields.begin();
- const std::vector <ref <headerField> >::const_iterator end = m_fields.end();
-
- for ( ; pos != end ; ++pos)
- {
- // Add the header if it matches the specified type
- if (utility::stringUtils::toLower((*pos)->getName()) == name)
- {
- result.push_back(*pos);
- }
- }
+ std::remove_copy_if(m_fields.begin(), m_fields.end(), back,
+ fieldHasNotName(utility::stringUtils::toLower(fieldName)));
return result;
}
@@ -360,4 +346,30 @@ const std::vector <ref <const component> > header::getChildComponents() const
}
+
+// Field search
+
+
+header::fieldHasName::fieldHasName(const string& name)
+ : m_name(name)
+{
+}
+
+const bool header::fieldHasName::operator() (const ref <const headerField>& field)
+{
+ return utility::stringUtils::toLower(field->getName()) == m_name;
+}
+
+
+header::fieldHasNotName::fieldHasNotName(const string& name)
+ : m_name(name)
+{
+}
+
+const bool header::fieldHasNotName::operator() (const ref <const headerField>& field)
+{
+ return utility::stringUtils::toLower(field->getName()) != m_name;
+}
+
+
} // vmime
diff --git a/vmime/header.hpp b/vmime/header.hpp
index 9f10ed41..3d286ae1 100644
--- a/vmime/header.hpp
+++ b/vmime/header.hpp
@@ -226,6 +226,31 @@ private:
std::vector <ref <headerField> > m_fields;
+
+ class fieldHasName
+ {
+ public:
+
+ fieldHasName(const string& name);
+ const bool operator() (const ref <const headerField>& field);
+
+ private:
+
+ string m_name;
+ };
+
+ class fieldHasNotName
+ {
+ public:
+
+ fieldHasNotName(const string& name);
+ const bool operator() (const ref <const headerField>& field);
+
+ private:
+
+ string m_name;
+ };
+
public:
using component::parse;