Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | * |
| 21 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
| 22 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 23 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 24 | */ |
| 25 | |
| 26 | #include "name.hpp" |
| 27 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 28 | #include "encoding/block.hpp" |
| 29 | #include "encoding/encoding-buffer.hpp" |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 30 | #include "util/time.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 31 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 32 | #include <sstream> |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 33 | #include <boost/algorithm/string/trim.hpp> |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 34 | #include <boost/functional/hash.hpp> |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame^] | 35 | #include <boost/range/adaptor/reversed.hpp> |
| 36 | #include <boost/range/concepts.hpp> |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 38 | namespace ndn { |
| 39 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 40 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>)); |
| 41 | BOOST_CONCEPT_ASSERT((WireEncodable<Name>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 42 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 43 | BOOST_CONCEPT_ASSERT((WireDecodable<Name>)); |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame^] | 44 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::iterator>)); |
| 45 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_iterator>)); |
| 46 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::reverse_iterator>)); |
| 47 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_reverse_iterator>)); |
| 48 | BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<Name>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 49 | static_assert(std::is_base_of<tlv::Error, Name::Error>::value, |
| 50 | "Name::Error must inherit from tlv::Error"); |
| 51 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 52 | const size_t Name::npos = std::numeric_limits<size_t>::max(); |
| 53 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 54 | // ---- constructors, encoding, decoding ---- |
| 55 | |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 56 | Name::Name() |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 57 | : m_wire(tlv::Name) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 58 | { |
| 59 | } |
| 60 | |
| 61 | Name::Name(const Block& wire) |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame^] | 62 | : m_wire(wire) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 63 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 64 | m_wire.parse(); |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | Name::Name(const char* uri) |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 68 | : Name(std::string(uri)) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 69 | { |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 72 | Name::Name(std::string uri) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 73 | { |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 74 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 75 | if (uri.empty()) |
| 76 | return; |
| 77 | |
| 78 | size_t iColon = uri.find(':'); |
| 79 | if (iColon != std::string::npos) { |
| 80 | // Make sure the colon came before a '/'. |
| 81 | size_t iFirstSlash = uri.find('/'); |
| 82 | if (iFirstSlash == std::string::npos || iColon < iFirstSlash) { |
| 83 | // Omit the leading protocol such as ndn: |
| 84 | uri.erase(0, iColon + 1); |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 85 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | // Trim the leading slash and possibly the authority. |
| 90 | if (uri[0] == '/') { |
| 91 | if (uri.size() >= 2 && uri[1] == '/') { |
| 92 | // Strip the authority following "//". |
| 93 | size_t iAfterAuthority = uri.find('/', 2); |
| 94 | if (iAfterAuthority == std::string::npos) |
| 95 | // Unusual case: there was only an authority. |
| 96 | return; |
| 97 | else { |
| 98 | uri.erase(0, iAfterAuthority + 1); |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 99 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | else { |
| 103 | uri.erase(0, 1); |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 104 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | size_t iComponentStart = 0; |
| 109 | |
| 110 | // Unescape the components. |
| 111 | while (iComponentStart < uri.size()) { |
| 112 | size_t iComponentEnd = uri.find("/", iComponentStart); |
| 113 | if (iComponentEnd == std::string::npos) |
| 114 | iComponentEnd = uri.size(); |
| 115 | |
| 116 | append(Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd)); |
| 117 | iComponentStart = iComponentEnd + 1; |
| 118 | } |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 121 | std::string |
| 122 | Name::toUri() const |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 123 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 124 | std::ostringstream os; |
| 125 | os << *this; |
| 126 | return os.str(); |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 129 | template<encoding::Tag TAG> |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 130 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 131 | Name::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 132 | { |
| 133 | size_t totalLength = 0; |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame^] | 134 | for (const Component& comp : *this | boost::adaptors::reversed) { |
| 135 | totalLength += comp.wireEncode(encoder); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 136 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 137 | |
| 138 | totalLength += encoder.prependVarNumber(totalLength); |
| 139 | totalLength += encoder.prependVarNumber(tlv::Name); |
| 140 | return totalLength; |
| 141 | } |
| 142 | |
| 143 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 144 | Name::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 145 | |
| 146 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 147 | Name::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 148 | |
| 149 | const Block& |
| 150 | Name::wireEncode() const |
| 151 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 152 | if (m_wire.hasWire()) |
| 153 | return m_wire; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 154 | |
| 155 | EncodingEstimator estimator; |
| 156 | size_t estimatedSize = wireEncode(estimator); |
| 157 | |
| 158 | EncodingBuffer buffer(estimatedSize, 0); |
| 159 | wireEncode(buffer); |
| 160 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 161 | m_wire = buffer.block(); |
| 162 | m_wire.parse(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 163 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 164 | return m_wire; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void |
| 168 | Name::wireDecode(const Block& wire) |
| 169 | { |
| 170 | if (wire.type() != tlv::Name) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 171 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name")); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 172 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 173 | m_wire = wire; |
| 174 | m_wire.parse(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 177 | Name |
| 178 | Name::deepCopy() const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 179 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 180 | Name copiedName(*this); |
| 181 | copiedName.m_wire.resetWire(); |
| 182 | copiedName.wireEncode(); // "compress" the underlying buffer |
| 183 | return copiedName; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 186 | // ---- accessors ---- |
| 187 | |
| 188 | const name::Component& |
| 189 | Name::at(ssize_t i) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 190 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 191 | if (i < 0) { |
| 192 | i = size() + i; |
| 193 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 194 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 195 | if (i < 0 || static_cast<size_t>(i) >= size()) { |
| 196 | BOOST_THROW_EXCEPTION(Error("Requested component does not exist (out of bounds)")); |
| 197 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 198 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 199 | return reinterpret_cast<const Component&>(m_wire.elements()[i]); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 200 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 201 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 202 | PartialName |
| 203 | Name::getSubName(ssize_t iStartComponent, size_t nComponents) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 204 | { |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 205 | PartialName result; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 206 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 207 | ssize_t iStart = iStartComponent < 0 ? this->size() + iStartComponent : iStartComponent; |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 208 | size_t iEnd = this->size(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 209 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 210 | iStart = std::max(iStart, static_cast<ssize_t>(0)); |
| 211 | |
| 212 | if (nComponents != npos) |
| 213 | iEnd = std::min(this->size(), iStart + nComponents); |
| 214 | |
| 215 | for (size_t i = iStart; i < iEnd; ++i) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 216 | result.append(at(i)); |
| 217 | |
| 218 | return result; |
| 219 | } |
| 220 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 221 | // ---- modifiers ---- |
| 222 | |
| 223 | Name& |
| 224 | Name::appendVersion() |
| 225 | { |
| 226 | return appendVersion(time::toUnixTimestamp(time::system_clock::now()).count()); |
| 227 | } |
| 228 | |
| 229 | Name& |
| 230 | Name::appendTimestamp() |
| 231 | { |
| 232 | return appendTimestamp(time::system_clock::now()); |
| 233 | } |
| 234 | |
| 235 | Name& |
| 236 | Name::append(const PartialName& name) |
| 237 | { |
| 238 | if (&name == this) |
| 239 | // Copying from this name, so need to make a copy first. |
| 240 | return append(PartialName(name)); |
| 241 | |
| 242 | for (size_t i = 0; i < name.size(); ++i) |
| 243 | append(name.at(i)); |
| 244 | |
| 245 | return *this; |
| 246 | } |
| 247 | |
| 248 | // ---- algorithms ---- |
| 249 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 250 | Name |
| 251 | Name::getSuccessor() const |
| 252 | { |
| 253 | if (empty()) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 254 | static uint8_t firstValue[] {0}; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 255 | Name firstName; |
| 256 | firstName.append(firstValue, 1); |
| 257 | return firstName; |
| 258 | } |
| 259 | |
| 260 | return getPrefix(-1).append(get(-1).getSuccessor()); |
| 261 | } |
| 262 | |
| 263 | bool |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 264 | Name::isPrefixOf(const Name& other) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 265 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 266 | // This name is longer than the name we are checking against. |
| 267 | if (size() > other.size()) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 268 | return false; |
| 269 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 270 | // Check if at least one of given components doesn't match. |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 271 | for (size_t i = 0; i < size(); ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 272 | if (get(i) != other.get(i)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 273 | return false; |
| 274 | } |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | bool |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 280 | Name::equals(const Name& other) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 281 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 282 | if (size() != other.size()) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 283 | return false; |
| 284 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 285 | for (size_t i = 0; i < size(); ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 286 | if (get(i) != other.get(i)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 287 | return false; |
| 288 | } |
| 289 | |
| 290 | return true; |
| 291 | } |
| 292 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 293 | int |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 294 | Name::compare(size_t pos1, size_t count1, const Name& other, size_t pos2, size_t count2) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 295 | { |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 296 | count1 = std::min(count1, this->size() - pos1); |
| 297 | count2 = std::min(count2, other.size() - pos2); |
| 298 | size_t count = std::min(count1, count2); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 299 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 300 | for (size_t i = 0; i < count; ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 301 | int comp = get(pos1 + i).compare(other.get(pos2 + i)); |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 302 | if (comp != 0) { // i-th component differs |
| 303 | return comp; |
| 304 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 305 | } |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 306 | // [pos1, pos1+count) of this Name equals [pos2, pos2+count) of other Name |
Joao Pereira | aa8fd16 | 2015-06-05 16:35:15 -0400 | [diff] [blame] | 307 | return count1 - count2; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 310 | // ---- stream operators ---- |
| 311 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 312 | std::ostream& |
| 313 | operator<<(std::ostream& os, const Name& name) |
| 314 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 315 | if (name.empty()) { |
| 316 | os << "/"; |
| 317 | } |
| 318 | else { |
| 319 | for (const auto& component : name) { |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 320 | os << "/"; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 321 | component.toUri(os); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 322 | } |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 323 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 324 | return os; |
| 325 | } |
| 326 | |
| 327 | std::istream& |
| 328 | operator>>(std::istream& is, Name& name) |
| 329 | { |
| 330 | std::string inputString; |
| 331 | is >> inputString; |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 332 | name = Name(inputString); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 333 | |
| 334 | return is; |
| 335 | } |
| 336 | |
| 337 | } // namespace ndn |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 338 | |
| 339 | namespace std { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 340 | |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 341 | size_t |
| 342 | hash<ndn::Name>::operator()(const ndn::Name& name) const |
| 343 | { |
| 344 | return boost::hash_range(name.wireEncode().wire(), |
| 345 | name.wireEncode().wire() + name.wireEncode().size()); |
| 346 | } |
| 347 | |
| 348 | } // namespace std |