blob: 7bbcc2bdda21ee243c7c5bccbf7a59d471375255 [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/*
Junxiao Shi9c9672e2018-04-18 12:55:08 +00003 * Copyright (c) 2013-2018 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>
Yingdi Yu90e23582014-11-06 14:21:04 -080033#include <boost/functional/hash.hpp>
Junxiao Shiadc334e2017-07-14 20:28:28 +000034#include <boost/range/adaptor/reversed.hpp>
35#include <boost/range/concepts.hpp>
Yingdi Yu90e23582014-11-06 14:21:04 -080036
Alexander Afanasyev15f67312014-07-22 15:11:09 -070037namespace ndn {
38
Junxiao Shic2b8d242014-11-04 08:35:29 -070039BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>));
40BOOST_CONCEPT_ASSERT((WireEncodable<Name>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070041BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070042BOOST_CONCEPT_ASSERT((WireDecodable<Name>));
Junxiao Shiadc334e2017-07-14 20:28:28 +000043BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::iterator>));
44BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_iterator>));
45BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::reverse_iterator>));
46BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_reverse_iterator>));
47BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<Name>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070048static_assert(std::is_base_of<tlv::Error, Name::Error>::value,
49 "Name::Error must inherit from tlv::Error");
50
Junxiao Shia6452ac2015-01-23 11:21:06 -070051const size_t Name::npos = std::numeric_limits<size_t>::max();
52
Junxiao Shi71ff2312017-07-12 13:32:50 +000053// ---- constructors, encoding, decoding ----
54
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080055Name::Name()
Junxiao Shi71ff2312017-07-12 13:32:50 +000056 : m_wire(tlv::Name)
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080057{
58}
59
60Name::Name(const Block& wire)
Junxiao Shiadc334e2017-07-14 20:28:28 +000061 : m_wire(wire)
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080062{
Junxiao Shi71ff2312017-07-12 13:32:50 +000063 m_wire.parse();
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080064}
65
66Name::Name(const char* uri)
Junxiao Shi3188c4032016-07-18 20:53:56 +000067 : Name(std::string(uri))
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080068{
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080069}
70
Junxiao Shi3188c4032016-07-18 20:53:56 +000071Name::Name(std::string uri)
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080072{
Junxiao Shi3188c4032016-07-18 20:53:56 +000073 if (uri.empty())
74 return;
75
76 size_t iColon = uri.find(':');
77 if (iColon != std::string::npos) {
78 // Make sure the colon came before a '/'.
79 size_t iFirstSlash = uri.find('/');
80 if (iFirstSlash == std::string::npos || iColon < iFirstSlash) {
81 // Omit the leading protocol such as ndn:
82 uri.erase(0, iColon + 1);
Junxiao Shi3188c4032016-07-18 20:53:56 +000083 }
84 }
85
86 // Trim the leading slash and possibly the authority.
87 if (uri[0] == '/') {
88 if (uri.size() >= 2 && uri[1] == '/') {
89 // Strip the authority following "//".
90 size_t iAfterAuthority = uri.find('/', 2);
91 if (iAfterAuthority == std::string::npos)
92 // Unusual case: there was only an authority.
93 return;
94 else {
95 uri.erase(0, iAfterAuthority + 1);
Junxiao Shi3188c4032016-07-18 20:53:56 +000096 }
97 }
98 else {
99 uri.erase(0, 1);
Junxiao Shi3188c4032016-07-18 20:53:56 +0000100 }
101 }
102
103 size_t iComponentStart = 0;
104
105 // Unescape the components.
106 while (iComponentStart < uri.size()) {
107 size_t iComponentEnd = uri.find("/", iComponentStart);
108 if (iComponentEnd == std::string::npos)
109 iComponentEnd = uri.size();
110
111 append(Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd));
112 iComponentStart = iComponentEnd + 1;
113 }
Alexander Afanasyevc89efb42015-02-10 18:26:42 -0800114}
115
Junxiao Shi71ff2312017-07-12 13:32:50 +0000116std::string
117Name::toUri() const
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700118{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000119 std::ostringstream os;
120 os << *this;
121 return os.str();
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -0700122}
123
Alexander Afanasyev74633892015-02-08 18:08:46 -0800124template<encoding::Tag TAG>
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700125size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800126Name::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700127{
128 size_t totalLength = 0;
Junxiao Shiadc334e2017-07-14 20:28:28 +0000129 for (const Component& comp : *this | boost::adaptors::reversed) {
130 totalLength += comp.wireEncode(encoder);
Junxiao Shi71ff2312017-07-12 13:32:50 +0000131 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700132
133 totalLength += encoder.prependVarNumber(totalLength);
134 totalLength += encoder.prependVarNumber(tlv::Name);
135 return totalLength;
136}
137
Davide Pesavento88a0d812017-08-19 21:31:42 -0400138NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Name);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700139
140const Block&
141Name::wireEncode() const
142{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000143 if (m_wire.hasWire())
144 return m_wire;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700145
146 EncodingEstimator estimator;
147 size_t estimatedSize = wireEncode(estimator);
148
149 EncodingBuffer buffer(estimatedSize, 0);
150 wireEncode(buffer);
151
Junxiao Shi71ff2312017-07-12 13:32:50 +0000152 m_wire = buffer.block();
153 m_wire.parse();
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700154
Junxiao Shi71ff2312017-07-12 13:32:50 +0000155 return m_wire;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700156}
157
158void
159Name::wireDecode(const Block& wire)
160{
161 if (wire.type() != tlv::Name)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700162 BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name"));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700163
Junxiao Shi71ff2312017-07-12 13:32:50 +0000164 m_wire = wire;
165 m_wire.parse();
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700166}
167
Junxiao Shi71ff2312017-07-12 13:32:50 +0000168Name
169Name::deepCopy() const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700170{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000171 Name copiedName(*this);
172 copiedName.m_wire.resetWire();
173 copiedName.wireEncode(); // "compress" the underlying buffer
174 return copiedName;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700175}
176
Junxiao Shi71ff2312017-07-12 13:32:50 +0000177// ---- accessors ----
178
179const name::Component&
180Name::at(ssize_t i) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700181{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000182 if (i < 0) {
183 i = size() + i;
184 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700185
Junxiao Shi71ff2312017-07-12 13:32:50 +0000186 if (i < 0 || static_cast<size_t>(i) >= size()) {
187 BOOST_THROW_EXCEPTION(Error("Requested component does not exist (out of bounds)"));
188 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700189
Junxiao Shi71ff2312017-07-12 13:32:50 +0000190 return reinterpret_cast<const Component&>(m_wire.elements()[i]);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700191}
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700192
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400193PartialName
194Name::getSubName(ssize_t iStartComponent, size_t nComponents) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700195{
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400196 PartialName result;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700197
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400198 ssize_t iStart = iStartComponent < 0 ? this->size() + iStartComponent : iStartComponent;
Junxiao Shia6452ac2015-01-23 11:21:06 -0700199 size_t iEnd = this->size();
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700200
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400201 iStart = std::max(iStart, static_cast<ssize_t>(0));
202
203 if (nComponents != npos)
204 iEnd = std::min(this->size(), iStart + nComponents);
205
206 for (size_t i = iStart; i < iEnd; ++i)
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700207 result.append(at(i));
208
209 return result;
210}
211
Junxiao Shi71ff2312017-07-12 13:32:50 +0000212// ---- modifiers ----
213
214Name&
215Name::appendVersion()
216{
217 return appendVersion(time::toUnixTimestamp(time::system_clock::now()).count());
218}
219
220Name&
221Name::appendTimestamp()
222{
223 return appendTimestamp(time::system_clock::now());
224}
225
226Name&
227Name::append(const PartialName& name)
228{
229 if (&name == this)
230 // Copying from this name, so need to make a copy first.
231 return append(PartialName(name));
232
233 for (size_t i = 0; i < name.size(); ++i)
234 append(name.at(i));
235
236 return *this;
237}
238
239// ---- algorithms ----
240
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700241Name
242Name::getSuccessor() const
243{
244 if (empty()) {
Junxiao Shi71ff2312017-07-12 13:32:50 +0000245 static uint8_t firstValue[] {0};
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700246 Name firstName;
247 firstName.append(firstValue, 1);
248 return firstName;
249 }
250
251 return getPrefix(-1).append(get(-1).getSuccessor());
252}
253
254bool
Junxiao Shi71ff2312017-07-12 13:32:50 +0000255Name::isPrefixOf(const Name& other) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700256{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000257 // This name is longer than the name we are checking against.
258 if (size() > other.size())
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700259 return false;
260
Junxiao Shi71ff2312017-07-12 13:32:50 +0000261 // Check if at least one of given components doesn't match.
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700262 for (size_t i = 0; i < size(); ++i) {
Junxiao Shi71ff2312017-07-12 13:32:50 +0000263 if (get(i) != other.get(i))
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700264 return false;
265 }
266
267 return true;
268}
269
270bool
Junxiao Shi71ff2312017-07-12 13:32:50 +0000271Name::equals(const Name& other) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700272{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000273 if (size() != other.size())
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700274 return false;
275
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700276 for (size_t i = 0; i < size(); ++i) {
Junxiao Shi71ff2312017-07-12 13:32:50 +0000277 if (get(i) != other.get(i))
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700278 return false;
279 }
280
281 return true;
282}
283
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700284int
Junxiao Shia6452ac2015-01-23 11:21:06 -0700285Name::compare(size_t pos1, size_t count1, const Name& other, size_t pos2, size_t count2) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700286{
Junxiao Shia6452ac2015-01-23 11:21:06 -0700287 count1 = std::min(count1, this->size() - pos1);
288 count2 = std::min(count2, other.size() - pos2);
289 size_t count = std::min(count1, count2);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700290
Junxiao Shia6452ac2015-01-23 11:21:06 -0700291 for (size_t i = 0; i < count; ++i) {
Junxiao Shi71ff2312017-07-12 13:32:50 +0000292 int comp = get(pos1 + i).compare(other.get(pos2 + i));
Junxiao Shia6452ac2015-01-23 11:21:06 -0700293 if (comp != 0) { // i-th component differs
294 return comp;
295 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700296 }
Junxiao Shia6452ac2015-01-23 11:21:06 -0700297 // [pos1, pos1+count) of this Name equals [pos2, pos2+count) of other Name
Joao Pereiraaa8fd162015-06-05 16:35:15 -0400298 return count1 - count2;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700299}
300
Junxiao Shi71ff2312017-07-12 13:32:50 +0000301// ---- stream operators ----
302
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700303std::ostream&
304operator<<(std::ostream& os, const Name& name)
305{
Junxiao Shi71ff2312017-07-12 13:32:50 +0000306 if (name.empty()) {
307 os << "/";
308 }
309 else {
310 for (const auto& component : name) {
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700311 os << "/";
Junxiao Shi71ff2312017-07-12 13:32:50 +0000312 component.toUri(os);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700313 }
Junxiao Shi71ff2312017-07-12 13:32:50 +0000314 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700315 return os;
316}
317
318std::istream&
319operator>>(std::istream& is, Name& name)
320{
321 std::string inputString;
322 is >> inputString;
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800323 name = Name(inputString);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700324
325 return is;
326}
327
328} // namespace ndn
Yingdi Yu90e23582014-11-06 14:21:04 -0800329
330namespace std {
Junxiao Shi71ff2312017-07-12 13:32:50 +0000331
Yingdi Yu90e23582014-11-06 14:21:04 -0800332size_t
333hash<ndn::Name>::operator()(const ndn::Name& name) const
334{
335 return boost::hash_range(name.wireEncode().wire(),
336 name.wireEncode().wire() + name.wireEncode().size());
337}
338
339} // namespace std