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> |
| 35 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 36 | namespace ndn { |
| 37 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 38 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>)); |
| 39 | BOOST_CONCEPT_ASSERT((WireEncodable<Name>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 40 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 41 | BOOST_CONCEPT_ASSERT((WireDecodable<Name>)); |
| 42 | static_assert(std::is_base_of<tlv::Error, Name::Error>::value, |
| 43 | "Name::Error must inherit from tlv::Error"); |
| 44 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 45 | const size_t Name::npos = std::numeric_limits<size_t>::max(); |
| 46 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 47 | // ---- constructors, encoding, decoding ---- |
| 48 | |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 49 | Name::Name() |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 50 | : m_wire(tlv::Name) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
| 54 | Name::Name(const Block& wire) |
| 55 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 56 | m_wire = wire; |
| 57 | m_wire.parse(); |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | Name::Name(const char* uri) |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 61 | : Name(std::string(uri)) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 62 | { |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 65 | Name::Name(std::string uri) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 66 | { |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 67 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 68 | if (uri.empty()) |
| 69 | return; |
| 70 | |
| 71 | size_t iColon = uri.find(':'); |
| 72 | if (iColon != std::string::npos) { |
| 73 | // Make sure the colon came before a '/'. |
| 74 | size_t iFirstSlash = uri.find('/'); |
| 75 | if (iFirstSlash == std::string::npos || iColon < iFirstSlash) { |
| 76 | // Omit the leading protocol such as ndn: |
| 77 | uri.erase(0, iColon + 1); |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 78 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | // Trim the leading slash and possibly the authority. |
| 83 | if (uri[0] == '/') { |
| 84 | if (uri.size() >= 2 && uri[1] == '/') { |
| 85 | // Strip the authority following "//". |
| 86 | size_t iAfterAuthority = uri.find('/', 2); |
| 87 | if (iAfterAuthority == std::string::npos) |
| 88 | // Unusual case: there was only an authority. |
| 89 | return; |
| 90 | else { |
| 91 | uri.erase(0, iAfterAuthority + 1); |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 92 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | else { |
| 96 | uri.erase(0, 1); |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 97 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | size_t iComponentStart = 0; |
| 102 | |
| 103 | // Unescape the components. |
| 104 | while (iComponentStart < uri.size()) { |
| 105 | size_t iComponentEnd = uri.find("/", iComponentStart); |
| 106 | if (iComponentEnd == std::string::npos) |
| 107 | iComponentEnd = uri.size(); |
| 108 | |
| 109 | append(Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd)); |
| 110 | iComponentStart = iComponentEnd + 1; |
| 111 | } |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 114 | std::string |
| 115 | Name::toUri() const |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 116 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 117 | std::ostringstream os; |
| 118 | os << *this; |
| 119 | return os.str(); |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 122 | template<encoding::Tag TAG> |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 123 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 124 | Name::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 125 | { |
| 126 | size_t totalLength = 0; |
| 127 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 128 | for (const_reverse_iterator i = rbegin(); i != rend(); ++i) { |
| 129 | totalLength += i->wireEncode(encoder); |
| 130 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 131 | |
| 132 | totalLength += encoder.prependVarNumber(totalLength); |
| 133 | totalLength += encoder.prependVarNumber(tlv::Name); |
| 134 | return totalLength; |
| 135 | } |
| 136 | |
| 137 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 138 | Name::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 139 | |
| 140 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 141 | Name::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 142 | |
| 143 | const Block& |
| 144 | Name::wireEncode() const |
| 145 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 146 | if (m_wire.hasWire()) |
| 147 | return m_wire; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 148 | |
| 149 | EncodingEstimator estimator; |
| 150 | size_t estimatedSize = wireEncode(estimator); |
| 151 | |
| 152 | EncodingBuffer buffer(estimatedSize, 0); |
| 153 | wireEncode(buffer); |
| 154 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 155 | m_wire = buffer.block(); |
| 156 | m_wire.parse(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 158 | return m_wire; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void |
| 162 | Name::wireDecode(const Block& wire) |
| 163 | { |
| 164 | if (wire.type() != tlv::Name) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 165 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name")); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 166 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 167 | m_wire = wire; |
| 168 | m_wire.parse(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 171 | Name |
| 172 | Name::deepCopy() const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 173 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 174 | Name copiedName(*this); |
| 175 | copiedName.m_wire.resetWire(); |
| 176 | copiedName.wireEncode(); // "compress" the underlying buffer |
| 177 | return copiedName; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 180 | // ---- accessors ---- |
| 181 | |
| 182 | const name::Component& |
| 183 | Name::at(ssize_t i) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 184 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 185 | if (i < 0) { |
| 186 | i = size() + i; |
| 187 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 188 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 189 | if (i < 0 || static_cast<size_t>(i) >= size()) { |
| 190 | BOOST_THROW_EXCEPTION(Error("Requested component does not exist (out of bounds)")); |
| 191 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 192 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 193 | return reinterpret_cast<const Component&>(m_wire.elements()[i]); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 194 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 195 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 196 | PartialName |
| 197 | Name::getSubName(ssize_t iStartComponent, size_t nComponents) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 198 | { |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 199 | PartialName result; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 200 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 201 | ssize_t iStart = iStartComponent < 0 ? this->size() + iStartComponent : iStartComponent; |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 202 | size_t iEnd = this->size(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 203 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 204 | iStart = std::max(iStart, static_cast<ssize_t>(0)); |
| 205 | |
| 206 | if (nComponents != npos) |
| 207 | iEnd = std::min(this->size(), iStart + nComponents); |
| 208 | |
| 209 | for (size_t i = iStart; i < iEnd; ++i) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 210 | result.append(at(i)); |
| 211 | |
| 212 | return result; |
| 213 | } |
| 214 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 215 | // ---- modifiers ---- |
| 216 | |
| 217 | Name& |
| 218 | Name::appendVersion() |
| 219 | { |
| 220 | return appendVersion(time::toUnixTimestamp(time::system_clock::now()).count()); |
| 221 | } |
| 222 | |
| 223 | Name& |
| 224 | Name::appendTimestamp() |
| 225 | { |
| 226 | return appendTimestamp(time::system_clock::now()); |
| 227 | } |
| 228 | |
| 229 | Name& |
| 230 | Name::append(const PartialName& name) |
| 231 | { |
| 232 | if (&name == this) |
| 233 | // Copying from this name, so need to make a copy first. |
| 234 | return append(PartialName(name)); |
| 235 | |
| 236 | for (size_t i = 0; i < name.size(); ++i) |
| 237 | append(name.at(i)); |
| 238 | |
| 239 | return *this; |
| 240 | } |
| 241 | |
| 242 | // ---- algorithms ---- |
| 243 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 244 | Name |
| 245 | Name::getSuccessor() const |
| 246 | { |
| 247 | if (empty()) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 248 | static uint8_t firstValue[] {0}; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 249 | Name firstName; |
| 250 | firstName.append(firstValue, 1); |
| 251 | return firstName; |
| 252 | } |
| 253 | |
| 254 | return getPrefix(-1).append(get(-1).getSuccessor()); |
| 255 | } |
| 256 | |
| 257 | bool |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 258 | Name::isPrefixOf(const Name& other) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 259 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 260 | // This name is longer than the name we are checking against. |
| 261 | if (size() > other.size()) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 262 | return false; |
| 263 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 264 | // Check if at least one of given components doesn't match. |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 265 | for (size_t i = 0; i < size(); ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 266 | if (get(i) != other.get(i)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 267 | return false; |
| 268 | } |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | bool |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 274 | Name::equals(const Name& other) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 275 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 276 | if (size() != other.size()) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 277 | return false; |
| 278 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 279 | for (size_t i = 0; i < size(); ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 280 | if (get(i) != other.get(i)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
| 284 | return true; |
| 285 | } |
| 286 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 287 | int |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 288 | 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] | 289 | { |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 290 | count1 = std::min(count1, this->size() - pos1); |
| 291 | count2 = std::min(count2, other.size() - pos2); |
| 292 | size_t count = std::min(count1, count2); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 293 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 294 | for (size_t i = 0; i < count; ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 295 | int comp = get(pos1 + i).compare(other.get(pos2 + i)); |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 296 | if (comp != 0) { // i-th component differs |
| 297 | return comp; |
| 298 | } |
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 | // [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] | 301 | return count1 - count2; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 304 | // ---- stream operators ---- |
| 305 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 306 | std::ostream& |
| 307 | operator<<(std::ostream& os, const Name& name) |
| 308 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 309 | if (name.empty()) { |
| 310 | os << "/"; |
| 311 | } |
| 312 | else { |
| 313 | for (const auto& component : name) { |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 314 | os << "/"; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 315 | component.toUri(os); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 316 | } |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 317 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 318 | return os; |
| 319 | } |
| 320 | |
| 321 | std::istream& |
| 322 | operator>>(std::istream& is, Name& name) |
| 323 | { |
| 324 | std::string inputString; |
| 325 | is >> inputString; |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 326 | name = Name(inputString); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 327 | |
| 328 | return is; |
| 329 | } |
| 330 | |
| 331 | } // namespace ndn |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 332 | |
| 333 | namespace std { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame^] | 334 | |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 335 | size_t |
| 336 | hash<ndn::Name>::operator()(const ndn::Name& name) const |
| 337 | { |
| 338 | return boost::hash_range(name.wireEncode().wire(), |
| 339 | name.wireEncode().wire() + name.wireEncode().size()); |
| 340 | } |
| 341 | |
| 342 | } // namespace std |