[svn] commit: r378 - in /branches/jinmei-dnsmessageapi/src/lib/dns/cpp: name.cc name.h name_unittest.cc
BIND 10 source code commits
bind10-changes at lists.isc.org
Thu Dec 17 23:58:42 UTC 2009
Author: jinmei
Date: Thu Dec 17 23:58:42 2009
New Revision: 378
Log:
added downcase() methods
Modified:
branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name.cc
branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name.h
branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name_unittest.cc
Modified: branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name.cc
==============================================================================
--- branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name.cc (original)
+++ branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name.cc Thu Dec 17 23:58:42 2009
@@ -613,6 +613,44 @@
return (retname);
}
+
+Name&
+Name::downcase()
+{
+ unsigned int nlen = length_;
+ unsigned int labels = labels_;
+ unsigned int pos = 0;
+
+ while (labels > 0 && nlen > 0) {
+ --labels;
+ --nlen;
+
+ // we assume a valid name, and do abort() if the assumption fails
+ // rather than throwing an exception.
+ unsigned int count = ndata_.at(pos++);
+ assert(count <= Name::MAX_LABELLEN);
+ assert(nlen >= count);
+
+ while (count > 0) {
+ ndata_.at(pos) =
+ maptolower[static_cast<unsigned char>(ndata_.at(pos))];
+ ++pos;
+ --nlen;
+ --count;
+ }
+ }
+
+ return (*this);
+}
+
+Name
+Name::downcase() const
+{
+ Name newname = *this;
+
+ return (newname.downcase());
+}
+
}
}
Modified: branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name.h
==============================================================================
--- branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name.h (original)
+++ branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name.h Thu Dec 17 23:58:42 2009
@@ -402,6 +402,32 @@
/// \param suffix a Name object to be appended to the Name.
/// \return a new Name object concatenating \c suffix to \c this Name.
Name concatenate(const Name& suffix) const;
+
+ /// \brief Downcase all upper case alphabet characters in the name.
+ ///
+ /// We have two different versions of the \c downcase() method. This
+ /// version modifies \this name class object. Since this method doesn't
+ /// create a new object, it should be faster than the other version, and
+ /// it's exception free. If the caller can allow the original object to be
+ /// modified, this version would therefore be preferred.
+ ///
+ /// \return A reference to the calling object with being downcased.
+ Name& downcase();
+
+ /// \brief Downcase all upper case alphabet characters in a newly created
+ /// copy of the name.
+ ///
+ /// We have two different versions of the \c downcase() method. This
+ /// version makes a copy of the calling object and downcase the upper case
+ /// characters of the copy. The calling object will be intact. This
+ /// version will be slower than the other version due to the additional
+ /// overhead, and may throw an exception if memory allocation fails.
+ /// However, if the original object cannot be modified this version must be
+ /// the choice.
+ ///
+ /// \return A new \c Name class object where upper case characters of the
+ /// calling object are downcased.
+ Name downcase() const;
//@}
///
Modified: branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name_unittest.cc
==============================================================================
--- branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name_unittest.cc (original)
+++ branches/jinmei-dnsmessageapi/src/lib/dns/cpp/name_unittest.cc Thu Dec 17 23:58:42 2009
@@ -16,6 +16,8 @@
#include <vector>
#include <string>
+#include <sstream>
+#include <iomanip>
#include <stdexcept>
#include "buffer.h"
@@ -35,16 +37,26 @@
namespace {
class NameTest : public ::testing::Test {
protected:
- NameTest() : example_name("www.example.com") {}
+ NameTest() : example_name("www.example.com"),
+ example_name_upper("WWW.EXAMPLE.COM"),
+ buffer_actual(0), buffer_expected(0)
+ {}
+
Name example_name;
+ Name example_name_upper;
+ OutputBuffer buffer_actual, buffer_expected;
static const size_t MAX_WIRE = Name::MAX_WIRE;
static const size_t MAX_LABELS = Name::MAX_LABELS;
//
// helper methods
//
- Name nameFactoryFromWire(const char* datafile, size_t position,
- bool downcase = false);
+ static Name nameFactoryFromWire(const char* datafile, size_t position,
+ bool downcase = false);
+ // construct a name including all non-upper-case-alphabet characters.
+ static Name nameFactoryLowerCase();
+ void compareInWireFormat(const Name& name_actual,
+ const Name& name_expected);
};
Name
@@ -58,6 +70,47 @@
buffer.setPosition(position);
return (Name(buffer, downcase));
+}
+
+Name
+NameTest::nameFactoryLowerCase()
+{
+ std::string lowercase_namestr;
+ lowercase_namestr.reserve(Name::MAX_WIRE);
+
+ unsigned int ch = 0;
+ unsigned int labelcount = 0;
+ do {
+ if (ch < 'A' || ch > 'Z') {
+ std::ostringstream ss;
+ ss.setf(std::ios_base::right, std::ios_base::adjustfield);
+ ss.width(3);
+ ss << std::setfill('0') << ch;
+ lowercase_namestr += '\\' + ss.str();
+
+ if (++labelcount == Name::MAX_LABELLEN) {
+ lowercase_namestr.push_back('.');
+ labelcount = 0;
+ }
+ }
+ } while (++ch <= Name::MAX_WIRE);
+
+ return (Name(lowercase_namestr));
+}
+
+void
+NameTest::compareInWireFormat(const Name& name_actual,
+ const Name& name_expected)
+{
+ buffer_actual.clear();
+ buffer_expected.clear();
+
+ name_actual.toWire(buffer_actual);
+ name_expected.toWire(buffer_expected);
+
+ EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
+ buffer_actual.getData(), buffer_actual.getLength(),
+ buffer_expected.getData(), buffer_expected.getLength());
}
TEST_F(NameTest, fromText)
@@ -336,4 +389,20 @@
EXPECT_THROW(example_name.split(1, 0), isc::dns::OutOfRange);
EXPECT_THROW(example_name.split(2, 3), isc::dns::OutOfRange);
}
-}
+
+TEST_F(NameTest, downcase)
+{
+ compareInWireFormat(example_name_upper.downcase(), example_name);
+ compareInWireFormat(nameFactoryLowerCase().downcase(),
+ nameFactoryLowerCase());
+}
+
+TEST_F(NameTest, copyAndDowncase)
+{
+ Name name_lowercased = example_name_upper.downcase();
+ compareInWireFormat(name_lowercased, example_name);
+
+ name_lowercased = nameFactoryLowerCase().downcase();
+ compareInWireFormat(name_lowercased, nameFactoryLowerCase());
+}
+}
More information about the bind10-changes
mailing list