Browse Source

[1602] use vector for offsets

Jelte Jansen 13 years ago
parent
commit
3f6b00419f
2 changed files with 14 additions and 25 deletions
  1. 13 21
      src/lib/dns/labelsequence.cc
  2. 1 4
      src/lib/dns/labelsequence.h

+ 13 - 21
src/lib/dns/labelsequence.cc

@@ -15,24 +15,22 @@
 #include <dns/labelsequence.h>
 #include <exceptions/exceptions.h>
 
+#include <iostream>
 namespace isc {
 namespace dns {
 
 LabelSequence::LabelSequence(const Name& name) : name_(name),
-                             first_label_(0) {
-    size_t label_count_ = name.getLabelCount();
-    last_label_ = label_count_ - 1;
-    offsets_ = new size_t[label_count_];
+                             first_label_(0), offsets_(name.getLabelCount()) {
     offsets_[0] = 0;
-    for (size_t i = 1; i < label_count_; ++i) {
+    last_label_ = name.getLabelCount() - 1;
+    // Walk through the wire format data and store all offsets
+    for (size_t i = 1; i < offsets_.size(); ++i) {
+        // Each offset is the previous offset plus the length of the
+        // label plus 1 (for the label length octet)
         offsets_[i] = offsets_[i - 1] + name.at(offsets_[i - 1]) + 1;
     }
 }
 
-LabelSequence::~LabelSequence() {
-    delete[] offsets_;
-}
-
 const char*
 LabelSequence::getData(size_t *len) const {
     *len = offsets_[last_label_] - offsets_[first_label_];
@@ -57,20 +55,14 @@ LabelSequence::equals(const LabelSequence& other, bool case_sensitive) const {
 
 void
 LabelSequence::split(int i) {
+    if (abs(i) > getLabelCount()) {
+        isc_throw(OutOfRange, "Label " << i << " out of range (" <<
+                              getLabelCount() << ")");
+    }
     if (i > 0) {
-        if (i > getLabelCount()) {
-            isc_throw(OutOfRange, "Label " << i << " out of range (" <<
-                                  getLabelCount() << ")");
-        } else {
-            first_label_ += i;
-        }
+        first_label_ += i;
     } else if (i < 0) {
-        if (-i > getLabelCount()) {
-            isc_throw(OutOfRange, "Label " << i << " out of range (" <<
-                                  getLabelCount() << ")");
-        } else {
-            last_label_ += i;
-        }
+        last_label_ += i;
     }
 }
 

+ 1 - 4
src/lib/dns/labelsequence.h

@@ -58,9 +58,6 @@ public:
     /// \param name The Name to construct a LabelSequence for
     LabelSequence(const Name& name);
 
-    /// \brief Destructor
-    ~LabelSequence();
-
     /// \brief Return the wire-format data for this LabelSequence
     ///
     /// The data, is returned as a pointer to the original wireformat
@@ -128,7 +125,7 @@ private:
     const Name& name_;
     size_t first_label_;
     size_t last_label_;
-    size_t* offsets_;
+    std::vector<size_t> offsets_;
 };