BIND 10 trac2383, updated. 67500d3bd8ae816e5ad73cf475d49f34546db639 [2383] Extract name parser to separate function

BIND 10 source code commits bind10-changes at lists.isc.org
Tue Oct 30 10:13:55 UTC 2012


The branch, trac2383 has been updated
       via  67500d3bd8ae816e5ad73cf475d49f34546db639 (commit)
      from  7187c1bb9c1b8a1d01843b8b3328454a9bce4d4d (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 67500d3bd8ae816e5ad73cf475d49f34546db639
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Tue Oct 30 11:13:15 2012 +0100

    [2383] Extract name parser to separate function
    
    This way it can be reused by the about to be new constructor.

-----------------------------------------------------------------------

Summary of changes:
 src/lib/dns/name.cc                |   38 ++++++++++++++++++++++++++----------
 src/lib/dns/tests/name_unittest.cc |    7 +++++++
 2 files changed, 35 insertions(+), 10 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/lib/dns/name.cc b/src/lib/dns/name.cc
index 5ea339d..36d9d6d 100644
--- a/src/lib/dns/name.cc
+++ b/src/lib/dns/name.cc
@@ -129,9 +129,15 @@ typedef enum {
     // parser where @ is used to mean an origin name.
     ft_at
 } ft_state;
-}
 
-Name::Name(const std::string &namestring, bool downcase) {
+// The parser of name from a string. It is a template, because
+// some parameters are used with two different types, while others
+// are private type aliases.
+template<class String, class Iterator, class Offsets, class Data>
+void
+stringParse(const String& namestring, Iterator s, Iterator send,
+            bool downcase, Offsets& offsets, Data& ndata)
+{
     //
     // Initialize things to make the compiler happy; they're not required.
     //
@@ -142,17 +148,13 @@ Name::Name(const std::string &namestring, bool downcase) {
     //
     // Set up the state machine.
     //
-    std::string::const_iterator s = namestring.begin();
-    std::string::const_iterator send = namestring.end();
     bool done = false;
     bool is_root = false;
     ft_state state = ft_init;
 
-    NameOffsets offsets;
+    // Prepare the output buffers.
     offsets.reserve(Name::MAX_LABELS);
     offsets.push_back(0);
-
-    NameString ndata;
     ndata.reserve(Name::MAX_WIRE);
 
     // should we refactor this code using, e.g, the state pattern?  Probably
@@ -212,7 +214,7 @@ Name::Name(const std::string &namestring, bool downcase) {
             } else if (c == '\\') {
                 state = ft_escape;
             } else {
-                if (++count > MAX_LABELLEN) {
+                if (++count > Name::MAX_LABELLEN) {
                     isc_throw(TooLongLabel,
                               "label is too long in " << namestring);
                 }
@@ -230,7 +232,7 @@ Name::Name(const std::string &namestring, bool downcase) {
             // FALLTHROUGH
         case ft_escape:
             if (!isdigit(c & 0xff)) {
-                if (++count > MAX_LABELLEN) {
+                if (++count > Name::MAX_LABELLEN) {
                     isc_throw(TooLongLabel,
                               "label is too long in " << namestring);
                 }
@@ -257,7 +259,7 @@ Name::Name(const std::string &namestring, bool downcase) {
                               "escaped decimal is too large in "
                               << namestring);
                 }
-                if (++count > MAX_LABELLEN) {
+                if (++count > Name::MAX_LABELLEN) {
                     isc_throw(TooLongLabel,
                               "label is too long in " << namestring);
                 }
@@ -291,7 +293,23 @@ Name::Name(const std::string &namestring, bool downcase) {
             ndata.push_back('\0');
         }
     }
+}
+
+}
+
+Name::Name(const std::string &namestring, bool downcase) {
+    // Prepare inputs for the parser
+    std::string::const_iterator s = namestring.begin();
+    std::string::const_iterator send = namestring.end();
+
+    // Prepare outputs
+    NameOffsets offsets;
+    NameString ndata;
+
+    // To the parsing
+    stringParse(namestring, s, send, downcase, offsets, ndata);
 
+    // And get the output
     labelcount_ = offsets.size();
     assert(labelcount_ > 0 && labelcount_ <= Name::MAX_LABELS);
     ndata_.assign(ndata.data(), ndata.size());
diff --git a/src/lib/dns/tests/name_unittest.cc b/src/lib/dns/tests/name_unittest.cc
index 4df529c..51e0e99 100644
--- a/src/lib/dns/tests/name_unittest.cc
+++ b/src/lib/dns/tests/name_unittest.cc
@@ -157,11 +157,14 @@ checkBadTextName(const string& txt) {
     // NameParserException.
     EXPECT_THROW(Name(txt, false), ExceptionType);
     EXPECT_THROW(Name(txt, false), NameParserException);
+#if 0
+    // TODO: Enable once the new constructor exists.
     // The same is thrown when constructing by the master-file constructor
     EXPECT_THROW(Name(txt.c_str(), txt.length(), &Name::ROOT_NAME()),
                  ExceptionType);
     EXPECT_THROW(Name(txt.c_str(), txt.length(), &Name::ROOT_NAME()),
                  NameParserException);
+#endif
 }
 
 TEST_F(NameTest, fromText) {
@@ -236,6 +239,9 @@ TEST_F(NameTest, fromText) {
     EXPECT_EQ(Name::MAX_LABELS, maxlabels.getLabelCount());
 }
 
+#if 0
+// TODO: Enable once the constructor exists, there's a need to test the changes
+// on the rest while we prepare it.
 // Check the @ syntax is accepted and it just copies the origin.
 TEST_F(NameTest, copyOrigin) {
     EXPECT_EQ(origin_name, Name("@", 1, &origin_name));
@@ -293,6 +299,7 @@ TEST_F(NameTest, combinedTooLong) {
     EXPECT_NO_THROW(Name(max_labels_str, strlen(max_labels_str),
                          &Name::ROOT_NAME()));
 }
+#endif
 
 TEST_F(NameTest, fromWire) {
     //



More information about the bind10-changes mailing list