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 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame^] | 143 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Name); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 144 | |
| 145 | const Block& |
| 146 | Name::wireEncode() const |
| 147 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 148 | if (m_wire.hasWire()) |
| 149 | return m_wire; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 150 | |
| 151 | EncodingEstimator estimator; |
| 152 | size_t estimatedSize = wireEncode(estimator); |
| 153 | |
| 154 | EncodingBuffer buffer(estimatedSize, 0); |
| 155 | wireEncode(buffer); |
| 156 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 157 | m_wire = buffer.block(); |
| 158 | m_wire.parse(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 159 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 160 | return m_wire; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
| 164 | Name::wireDecode(const Block& wire) |
| 165 | { |
| 166 | if (wire.type() != tlv::Name) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 167 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name")); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 168 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 169 | m_wire = wire; |
| 170 | m_wire.parse(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 173 | Name |
| 174 | Name::deepCopy() const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 175 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 176 | Name copiedName(*this); |
| 177 | copiedName.m_wire.resetWire(); |
| 178 | copiedName.wireEncode(); // "compress" the underlying buffer |
| 179 | return copiedName; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 182 | // ---- accessors ---- |
| 183 | |
| 184 | const name::Component& |
| 185 | Name::at(ssize_t i) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 186 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 187 | if (i < 0) { |
| 188 | i = size() + i; |
| 189 | } |
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 || static_cast<size_t>(i) >= size()) { |
| 192 | BOOST_THROW_EXCEPTION(Error("Requested component does not exist (out of bounds)")); |
| 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 | return reinterpret_cast<const Component&>(m_wire.elements()[i]); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 196 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 197 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 198 | PartialName |
| 199 | Name::getSubName(ssize_t iStartComponent, size_t nComponents) const |
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 | PartialName result; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 202 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 203 | ssize_t iStart = iStartComponent < 0 ? this->size() + iStartComponent : iStartComponent; |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 204 | size_t iEnd = this->size(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 205 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 206 | iStart = std::max(iStart, static_cast<ssize_t>(0)); |
| 207 | |
| 208 | if (nComponents != npos) |
| 209 | iEnd = std::min(this->size(), iStart + nComponents); |
| 210 | |
| 211 | for (size_t i = iStart; i < iEnd; ++i) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 212 | result.append(at(i)); |
| 213 | |
| 214 | return result; |
| 215 | } |
| 216 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 217 | // ---- modifiers ---- |
| 218 | |
| 219 | Name& |
| 220 | Name::appendVersion() |
| 221 | { |
| 222 | return appendVersion(time::toUnixTimestamp(time::system_clock::now()).count()); |
| 223 | } |
| 224 | |
| 225 | Name& |
| 226 | Name::appendTimestamp() |
| 227 | { |
| 228 | return appendTimestamp(time::system_clock::now()); |
| 229 | } |
| 230 | |
| 231 | Name& |
| 232 | Name::append(const PartialName& name) |
| 233 | { |
| 234 | if (&name == this) |
| 235 | // Copying from this name, so need to make a copy first. |
| 236 | return append(PartialName(name)); |
| 237 | |
| 238 | for (size_t i = 0; i < name.size(); ++i) |
| 239 | append(name.at(i)); |
| 240 | |
| 241 | return *this; |
| 242 | } |
| 243 | |
| 244 | // ---- algorithms ---- |
| 245 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 246 | Name |
| 247 | Name::getSuccessor() const |
| 248 | { |
| 249 | if (empty()) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 250 | static uint8_t firstValue[] {0}; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 251 | Name firstName; |
| 252 | firstName.append(firstValue, 1); |
| 253 | return firstName; |
| 254 | } |
| 255 | |
| 256 | return getPrefix(-1).append(get(-1).getSuccessor()); |
| 257 | } |
| 258 | |
| 259 | bool |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 260 | Name::isPrefixOf(const Name& other) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 261 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 262 | // This name is longer than the name we are checking against. |
| 263 | if (size() > other.size()) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 264 | return false; |
| 265 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 266 | // Check if at least one of given components doesn't match. |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 267 | for (size_t i = 0; i < size(); ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 268 | if (get(i) != other.get(i)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | bool |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 276 | Name::equals(const Name& other) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 277 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 278 | if (size() != other.size()) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 279 | return false; |
| 280 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 281 | for (size_t i = 0; i < size(); ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 282 | if (get(i) != other.get(i)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | |
| 286 | return true; |
| 287 | } |
| 288 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 289 | int |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 290 | 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] | 291 | { |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 292 | count1 = std::min(count1, this->size() - pos1); |
| 293 | count2 = std::min(count2, other.size() - pos2); |
| 294 | size_t count = std::min(count1, count2); |
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 | for (size_t i = 0; i < count; ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 297 | int comp = get(pos1 + i).compare(other.get(pos2 + i)); |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 298 | if (comp != 0) { // i-th component differs |
| 299 | return comp; |
| 300 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 301 | } |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 302 | // [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] | 303 | return count1 - count2; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 306 | // ---- stream operators ---- |
| 307 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 308 | std::ostream& |
| 309 | operator<<(std::ostream& os, const Name& name) |
| 310 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 311 | if (name.empty()) { |
| 312 | os << "/"; |
| 313 | } |
| 314 | else { |
| 315 | for (const auto& component : name) { |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 316 | os << "/"; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 317 | component.toUri(os); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 318 | } |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 319 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 320 | return os; |
| 321 | } |
| 322 | |
| 323 | std::istream& |
| 324 | operator>>(std::istream& is, Name& name) |
| 325 | { |
| 326 | std::string inputString; |
| 327 | is >> inputString; |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 328 | name = Name(inputString); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 329 | |
| 330 | return is; |
| 331 | } |
| 332 | |
| 333 | } // namespace ndn |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 334 | |
| 335 | namespace std { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 336 | |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 337 | size_t |
| 338 | hash<ndn::Name>::operator()(const ndn::Name& name) const |
| 339 | { |
| 340 | return boost::hash_range(name.wireEncode().wire(), |
| 341 | name.wireEncode().wire() + name.wireEncode().size()); |
| 342 | } |
| 343 | |
| 344 | } // namespace std |