blob: 6dc735f908396468193cc824e605c57b988c8496 [file] [log] [blame]
Alexander Afanasyev15f67312014-07-22 15:11:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi71ff2312017-07-12 13:32:50 +00002/*
Davide Pesaventodd461432017-01-28 21:47:26 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyev15f67312014-07-22 15:11:09 -07004 *
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 Afanasyev15f67312014-07-22 15:11:09 -070028#include "encoding/block.hpp"
29#include "encoding/encoding-buffer.hpp"
Junxiao Shi71ff2312017-07-12 13:32:50 +000030#include "util/time.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070031
Junxiao Shi71ff2312017-07-12 13:32:50 +000032#include <sstream>
Davide Pesaventodd461432017-01-28 21:47:26 -050033#include <boost/algorithm/string/trim.hpp>
Yingdi Yu90e23582014-11-06 14:21:04 -080034#include <boost/functional/hash.hpp>
Junxiao Shiadc334e2017-07-14 20:28:28 +000035#include <boost/range/adaptor/reversed.hpp>
36#include <boost/range/concepts.hpp>
Yingdi Yu90e23582014-11-06 14:21:04 -080037
Alexander Afanasyev15f67312014-07-22 15:11:09 -070038namespace ndn {
39
Junxiao Shic2b8d242014-11-04 08:35:29 -070040BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>));
41BOOST_CONCEPT_ASSERT((WireEncodable<Name>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070042BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070043BOOST_CONCEPT_ASSERT((WireDecodable<Name>));
Junxiao Shiadc334e2017-07-14 20:28:28 +000044BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::iterator>));
45BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_iterator>));
46BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::reverse_iterator>));
47BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_reverse_iterator>));
48BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<Name>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070049static_assert(std::is_base_of<tlv::Error, Name::Error>::value,
50 "Name::Error must inherit from tlv::Error");
51
Junxiao Shia6452ac2015-01-23 11:21:06 -070052const size_t Name::npos = std::numeric_limits<size_t>::max();
53
Junxiao Shi71ff2312017-07-12 13:32:50 +000054// ---- constructors, encoding, decoding ----
55
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080056Name::Name()
Junxiao Shi71ff2312017-07-12 13:32:50 +000057 : m_wire(tlv::Name)
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080058{
59}
60
61Name::Name(const Block& wire)
Junxiao Shiadc334e2017-07-14 20:28:28 +000062 : m_wire(wire)
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080063{
Junxiao Shi71ff2312017-07-12 13:32:50 +000064 m_wire.parse();
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080065}
66
67Name::Name(const char* uri)
Junxiao Shi3188c4032016-07-18 20:53:56 +000068 : Name(std::string(uri))
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080069{
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080070}
71
Junxiao Shi3188c4032016-07-18 20:53:56 +000072Name::Name(std::string uri)
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080073{
Davide Pesaventodd461432017-01-28 21:47:26 -050074 boost::algorithm::trim(uri);
Junxiao Shi3188c4032016-07-18 20:53:56 +000075 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 Pesaventodd461432017-01-28 21:47:26 -050085 boost::algorithm::trim(uri);
Junxiao Shi3188c4032016-07-18 20:53:56 +000086 }
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 Pesaventodd461432017-01-28 21:47:26 -050099 boost::algorithm::trim(uri);
Junxiao Shi3188c4032016-07-18 20:53:56 +0000100 }
101 }
102 else {
103 uri.erase(0, 1);
Davide Pesaventodd461432017-01-28 21:47:26 -0500104 boost::algorithm::trim(uri);
Junxiao Shi3188c4032016-07-18 20:53:56 +0000105 }
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 Afanasyevc89efb42015-02-10 18:26:42 -0800119}
120
Junxiao Shi71ff2312017-07-12 13:32:50 +0000121std::string
122Name::toUri() const
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700123{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000124 std::ostringstream os;
125 os << *this;
126 return os.str();
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700127}
128
Alexander Afanasyev74633892015-02-08 18:08:46 -0800129template<encoding::Tag TAG>
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700130size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800131Name::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700132{
133 size_t totalLength = 0;
Junxiao Shiadc334e2017-07-14 20:28:28 +0000134 for (const Component& comp : *this | boost::adaptors::reversed) {
135 totalLength += comp.wireEncode(encoder);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000136 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700137
138 totalLength += encoder.prependVarNumber(totalLength);
139 totalLength += encoder.prependVarNumber(tlv::Name);
140 return totalLength;
141}
142
143template size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700144Name::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700145
146template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800147Name::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700148
149const Block&
150Name::wireEncode() const
151{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000152 if (m_wire.hasWire())
153 return m_wire;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700154
155 EncodingEstimator estimator;
156 size_t estimatedSize = wireEncode(estimator);
157
158 EncodingBuffer buffer(estimatedSize, 0);
159 wireEncode(buffer);
160
Junxiao Shi71ff2312017-07-12 13:32:50 +0000161 m_wire = buffer.block();
162 m_wire.parse();
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700163
Junxiao Shi71ff2312017-07-12 13:32:50 +0000164 return m_wire;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700165}
166
167void
168Name::wireDecode(const Block& wire)
169{
170 if (wire.type() != tlv::Name)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700171 BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name"));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700172
Junxiao Shi71ff2312017-07-12 13:32:50 +0000173 m_wire = wire;
174 m_wire.parse();
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700175}
176
Junxiao Shi71ff2312017-07-12 13:32:50 +0000177Name
178Name::deepCopy() const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700179{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000180 Name copiedName(*this);
181 copiedName.m_wire.resetWire();
182 copiedName.wireEncode(); // "compress" the underlying buffer
183 return copiedName;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700184}
185
Junxiao Shi71ff2312017-07-12 13:32:50 +0000186// ---- accessors ----
187
188const name::Component&
189Name::at(ssize_t i) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700190{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000191 if (i < 0) {
192 i = size() + i;
193 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700194
Junxiao Shi71ff2312017-07-12 13:32:50 +0000195 if (i < 0 || static_cast<size_t>(i) >= size()) {
196 BOOST_THROW_EXCEPTION(Error("Requested component does not exist (out of bounds)"));
197 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700198
Junxiao Shi71ff2312017-07-12 13:32:50 +0000199 return reinterpret_cast<const Component&>(m_wire.elements()[i]);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700200}
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700201
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400202PartialName
203Name::getSubName(ssize_t iStartComponent, size_t nComponents) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700204{
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400205 PartialName result;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700206
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400207 ssize_t iStart = iStartComponent < 0 ? this->size() + iStartComponent : iStartComponent;
Junxiao Shia6452ac2015-01-23 11:21:06 -0700208 size_t iEnd = this->size();
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700209
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400210 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 Afanasyev15f67312014-07-22 15:11:09 -0700216 result.append(at(i));
217
218 return result;
219}
220
Junxiao Shi71ff2312017-07-12 13:32:50 +0000221// ---- modifiers ----
222
223Name&
224Name::appendVersion()
225{
226 return appendVersion(time::toUnixTimestamp(time::system_clock::now()).count());
227}
228
229Name&
230Name::appendTimestamp()
231{
232 return appendTimestamp(time::system_clock::now());
233}
234
235Name&
236Name::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 Afanasyev15f67312014-07-22 15:11:09 -0700250Name
251Name::getSuccessor() const
252{
253 if (empty()) {
Junxiao Shi71ff2312017-07-12 13:32:50 +0000254 static uint8_t firstValue[] {0};
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700255 Name firstName;
256 firstName.append(firstValue, 1);
257 return firstName;
258 }
259
260 return getPrefix(-1).append(get(-1).getSuccessor());
261}
262
263bool
Junxiao Shi71ff2312017-07-12 13:32:50 +0000264Name::isPrefixOf(const Name& other) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700265{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000266 // This name is longer than the name we are checking against.
267 if (size() > other.size())
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700268 return false;
269
Junxiao Shi71ff2312017-07-12 13:32:50 +0000270 // Check if at least one of given components doesn't match.
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700271 for (size_t i = 0; i < size(); ++i) {
Junxiao Shi71ff2312017-07-12 13:32:50 +0000272 if (get(i) != other.get(i))
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700273 return false;
274 }
275
276 return true;
277}
278
279bool
Junxiao Shi71ff2312017-07-12 13:32:50 +0000280Name::equals(const Name& other) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700281{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000282 if (size() != other.size())
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700283 return false;
284
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700285 for (size_t i = 0; i < size(); ++i) {
Junxiao Shi71ff2312017-07-12 13:32:50 +0000286 if (get(i) != other.get(i))
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700287 return false;
288 }
289
290 return true;
291}
292
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700293int
Junxiao Shia6452ac2015-01-23 11:21:06 -0700294Name::compare(size_t pos1, size_t count1, const Name& other, size_t pos2, size_t count2) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700295{
Junxiao Shia6452ac2015-01-23 11:21:06 -0700296 count1 = std::min(count1, this->size() - pos1);
297 count2 = std::min(count2, other.size() - pos2);
298 size_t count = std::min(count1, count2);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700299
Junxiao Shia6452ac2015-01-23 11:21:06 -0700300 for (size_t i = 0; i < count; ++i) {
Junxiao Shi71ff2312017-07-12 13:32:50 +0000301 int comp = get(pos1 + i).compare(other.get(pos2 + i));
Junxiao Shia6452ac2015-01-23 11:21:06 -0700302 if (comp != 0) { // i-th component differs
303 return comp;
304 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700305 }
Junxiao Shia6452ac2015-01-23 11:21:06 -0700306 // [pos1, pos1+count) of this Name equals [pos2, pos2+count) of other Name
Joao Pereiraaa8fd162015-06-05 16:35:15 -0400307 return count1 - count2;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700308}
309
Junxiao Shi71ff2312017-07-12 13:32:50 +0000310// ---- stream operators ----
311
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700312std::ostream&
313operator<<(std::ostream& os, const Name& name)
314{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000315 if (name.empty()) {
316 os << "/";
317 }
318 else {
319 for (const auto& component : name) {
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700320 os << "/";
Junxiao Shi71ff2312017-07-12 13:32:50 +0000321 component.toUri(os);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700322 }
Junxiao Shi71ff2312017-07-12 13:32:50 +0000323 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700324 return os;
325}
326
327std::istream&
328operator>>(std::istream& is, Name& name)
329{
330 std::string inputString;
331 is >> inputString;
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800332 name = Name(inputString);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700333
334 return is;
335}
336
337} // namespace ndn
Yingdi Yu90e23582014-11-06 14:21:04 -0800338
339namespace std {
Junxiao Shi71ff2312017-07-12 13:32:50 +0000340
Yingdi Yu90e23582014-11-06 14:21:04 -0800341size_t
342hash<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