blob: 00f6d2104c4c3fefebad02ee47c17873b20a5812 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonec39fbd2013-10-04 10:56:23 -07005 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
6 * @author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07007 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07008 */
9
10#ifndef NDN_NAME_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070011#define NDN_NAME_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070012
13#include <vector>
Jeff Thompson443398d2013-07-02 19:45:46 -070014#include <string>
Jeff Thompsonec7789a2013-08-21 11:08:36 -070015#include <sstream>
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080016#include <string.h>
17#include "encoding/block.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070018
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070019namespace ndn {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070020
Jeff Thompsonc7d65502013-11-06 17:22:26 -080021/**
22 * A Name holds an array of Name::Component and represents an NDN name.
23 */
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070024class Name {
25public:
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070026 /**
Jeff Thompsonc7d65502013-11-06 17:22:26 -080027 * A Name::Component holds a read-only name component value.
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070028 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080029 class Component : private ConstBufferPtr
30 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070031 public:
32 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070033 * Create a new Name::Component with a null value.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070034 */
35 Component()
36 {
37 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080038
39 // copy constructor OK
40
41 /**
42 * Create a new Name::Component, taking another pointer to the Blob value.
43 * @param value A blob with a pointer to an immutable array. The pointer is copied.
44 */
45 Component(const ConstBufferPtr &buffer)
46 : ConstBufferPtr (buffer)
47 {
48 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070049
50 /**
51 * Create a new Name::Component, copying the given value.
52 * @param value The value byte array.
53 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080054 Component(const Buffer& value)
55 : ConstBufferPtr (new Buffer(value))
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070056 {
57 }
58
59 /**
60 * Create a new Name::Component, copying the given value.
61 * @param value Pointer to the value byte array.
62 * @param valueLen Length of value.
63 */
Jeff Thompson97223af2013-09-24 17:01:27 -070064 Component(const uint8_t *value, size_t valueLen)
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080065 : ConstBufferPtr (new Buffer(value, valueLen))
66 {
67 }
68
69 template<class InputIterator>
70 Component(InputIterator begin, InputIterator end)
71 : ConstBufferPtr (new Buffer(begin, end))
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070072 {
73 }
Jeff Thompson0f743452013-09-12 14:23:18 -070074
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080075 const Buffer&
76 getValue() const { return **this; }
Jeff Thompson6653b0b2013-09-23 12:32:39 -070077
78 /**
79 * Write this component value to result, escaping characters according to the NDN URI Scheme.
80 * This also adds "..." to a value with zero or more ".".
Jeff Thompson6653b0b2013-09-23 12:32:39 -070081 * @param result the string stream to write to.
82 */
83 void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080084 toEscapedString(std::ostream& result) const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070085 {
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080086 Name::toEscapedString(**this, result);
Jeff Thompson6653b0b2013-09-23 12:32:39 -070087 }
88
89 /**
90 * Convert this component value by escaping characters according to the NDN URI Scheme.
91 * This also adds "..." to a value with zero or more ".".
92 * @return The escaped string.
93 */
94 std::string
Jeff Thompsond0159d72013-09-23 13:34:15 -070095 toEscapedString() const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070096 {
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080097 return Name::toEscapedString(**this);
Jeff Thompson6653b0b2013-09-23 12:32:39 -070098 }
Jeff Thompson9bdb3b22013-09-12 12:42:13 -070099
Jeff Thompson50b37912013-10-08 13:39:33 -0700100 /**
101 * Interpret this name component as a network-ordered number and return an integer.
102 * @return The integer number.
103 */
104 uint64_t
Jeff Thompson25b4e612013-10-10 16:03:24 -0700105 toNumber() const;
Jeff Thompson27cae532013-10-08 12:52:41 -0700106
Jeff Thompson50b37912013-10-08 13:39:33 -0700107 /**
108 * Interpret this name component as a network-ordered number with a marker and return an integer.
109 * @param marker The required first byte of the component.
110 * @return The integer number.
111 * @throw runtime_error If the first byte of the component does not equal the marker.
112 */
113 uint64_t
114 toNumberWithMarker(uint8_t marker) const;
115
116 /**
117 * Interpret this name component as a segment number according to NDN name conventions (a network-ordered number
118 * where the first byte is the marker 0x00).
119 * @return The integer segment number.
120 * @throw runtime_error If the first byte of the component is not the expected marker.
121 */
122 uint64_t
123 toSegment() const
124 {
125 return toNumberWithMarker(0x00);
126 }
127
128 /**
129 * @deprecated Use toSegment.
130 */
131 uint64_t
132 toSeqNum() const
133 {
134 return toSegment();
135 }
136
137 /**
138 * Interpret this name component as a version number according to NDN name conventions (a network-ordered number
139 * where the first byte is the marker 0xFD). Note that this returns the exact number from the component
140 * without converting it to a time representation.
141 * @return The integer segment number.
142 * @throw runtime_error If the first byte of the component is not the expected marker.
143 */
144 uint64_t
145 toVersion() const
146 {
147 return toNumberWithMarker(0xFD);
148 }
Jeff Thompson27cae532013-10-08 12:52:41 -0700149
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700150 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700151 * Create a component whose value is the network-ordered encoding of the number.
152 * Note: if the number is zero, the result is empty.
153 * @param number The number to be encoded.
154 * @return The component value.
155 */
156 static Component
157 fromNumber(uint64_t number);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700158
159 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700160 * Create a component whose value is the marker appended with the network-ordered encoding of the number.
161 * Note: if the number is zero, no bytes are used for the number - the result will have only the marker.
162 * @param number The number to be encoded.
163 * @param marker The marker to use as the first byte of the component.
164 * @return The component value.
Jeff Thompson8aac1992013-08-12 17:26:02 -0700165 */
Jeff Thompsond129ac12013-10-11 14:30:12 -0700166 static Component
167 fromNumberWithMarker(uint64_t number, uint8_t marker);
Jeff Thompsona98000c2013-12-16 14:40:09 -0800168
169 /**
170 * Check if this is the same component as other.
171 * @param other The other Component to compare with.
172 * @return true if the components are equal, otherwise false.
173 */
174 bool
175 equals(const Component& other) const
176 {
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800177 return **this == *other;
178 }
179
180 bool
181 empty() const
182 {
183 return !*this || (*this)->empty();
Jeff Thompsona98000c2013-12-16 14:40:09 -0800184 }
185
186 /**
187 * Check if this is the same component as other.
188 * @param other The other Component to compare with.
189 * @return true if the components are equal, otherwise false.
190 */
191 bool
192 operator == (const Component& other) const { return equals(other); }
193
194 /**
195 * Check if this is not the same component as other.
196 * @param other The other Component to compare with.
197 * @return true if the components are not equal, otherwise false.
198 */
199 bool
200 operator != (const Component& other) const { return !equals(other); }
201
202 /**
203 * Compare this to the other Component using NDN canonical ordering.
204 * @param other The other Component to compare with.
205 * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or
206 * 1 if *this comes after other in the canonical ordering.
207 *
208 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
209 */
210 int
211 compare(const Component& other) const;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700212
Jeff Thompsona98000c2013-12-16 14:40:09 -0800213 /**
214 * Return true if this is less than or equal to the other Component in the NDN canonical ordering.
215 * @param other The other Component to compare with.
216 *
217 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
218 */
219 bool
220 operator <= (const Component& other) const { return compare(other) <= 0; }
221
222 /**
223 * Return true if this is less than the other Component in the NDN canonical ordering.
224 * @param other The other Component to compare with.
225 *
226 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
227 */
228 bool
229 operator < (const Component& other) const { return compare(other) < 0; }
230
231 /**
232 * Return true if this is less than or equal to the other Component in the NDN canonical ordering.
233 * @param other The other Component to compare with.
234 *
235 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
236 */
237 bool
238 operator >= (const Component& other) const { return compare(other) >= 0; }
239
240 /**
241 * Return true if this is greater than the other Component in the NDN canonical ordering.
242 * @param other The other Component to compare with.
243 *
244 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
245 */
246 bool
247 operator > (const Component& other) const { return compare(other) > 0; }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800248 };
Jeff Thompsona98000c2013-12-16 14:40:09 -0800249
Jeff Thompson443398d2013-07-02 19:45:46 -0700250 /**
251 * Create a new Name with no components.
252 */
Jeff Thompson016ed642013-07-02 14:39:06 -0700253 Name() {
254 }
Jeff Thompson443398d2013-07-02 19:45:46 -0700255
256 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700257 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700258 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700259 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700260 Name(const std::vector<Component>& components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700261 : components_(components)
262 {
263 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800264
265 Name(const Block &name)
266 {
267 for (Block::element_const_iterator i = name.getAll().begin();
268 i != name.getAll().end();
269 ++i)
270 {
271 append(Component(i->value_begin(), i->value_end()));
272 }
273 }
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700274
275 /**
Jeff Thompson443398d2013-07-02 19:45:46 -0700276 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700277 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -0700278 */
Jeff Thompson3549ef32013-09-25 14:05:17 -0700279 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -0700280 {
281 set(uri);
282 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700283
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700284 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -0700285 * Parse the uri according to the NDN URI Scheme and create the name with the components.
286 * @param uri The URI string.
287 */
288 Name(const std::string& uri)
289 {
290 set(uri.c_str());
291 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700292
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800293 const Block &
294 wireEncode() const;
Jeff Thompsonb468c312013-07-01 17:50:14 -0700295
296 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700297 * Parse the uri according to the NDN URI Scheme and set the name with the components.
Jeff Thompson7781b392013-12-17 11:45:59 -0800298 * @param uri The null-terminated URI string.
Jeff Thompson67515bd2013-08-15 17:43:22 -0700299 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700300 void
301 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700302
303 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800304 * Parse the uri according to the NDN URI Scheme and set the name with the components.
305 * @param uri The URI string.
306 */
307 void
308 set(const std::string& uri) { set(uri.c_str()); }
309
310 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700311 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700312 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700313 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700314 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700315 append(const uint8_t *value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700316 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700317 components_.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700318 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700319 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700320
321 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700322 * Append a new component, copying from value.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700323 * @return This name so that you can chain calls to append.
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700324 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700325 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800326 append(const Buffer& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700327 {
328 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700329 return *this;
Jeff Thompson0f743452013-09-12 14:23:18 -0700330 }
331
Jeff Thompson26b0d792013-09-23 16:19:01 -0700332 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800333 append(const ConstBufferPtr &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700334 {
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700335 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700336 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700337 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700338
Jeff Thompson21eb7212013-09-26 09:05:40 -0700339 Name&
340 append(const Component &value)
341 {
342 components_.push_back(value);
343 return *this;
344 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800345
346 Name&
347 append(const Block &value)
348 {
349 components_.push_back(Component(value.begin(), value.end()));
350 return *this;
351 }
Jeff Thompson21eb7212013-09-26 09:05:40 -0700352
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700353 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700354 * Append the components of the given name to this name.
355 * @param name The Name with components to append.
356 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700357 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700358 Name&
359 append(const Name& name);
360
361 /**
362 * @deprecated Use append.
363 */
364 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700365 appendComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700366 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700367 return append(value, valueLength);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700368 }
369
370 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700371 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700372 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700373 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800374 appendComponent(const Buffer& value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700375 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700376 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700377 }
378
379 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700380 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700381 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700382 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800383 appendComponent(const ConstBufferPtr &value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700384 {
385 return append(value);
386 }
387
388 /**
389 * @deprecated Use append.
390 */
391 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700392 addComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700393 {
394 return append(value, valueLength);
395 }
396
397 /**
398 * @deprecated Use append.
399 */
400 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800401 addComponent(const Buffer& value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700402 {
403 return append(value);
404 }
405
406 /**
407 * @deprecated Use append.
408 */
409 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800410 addComponent(const ConstBufferPtr &value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700411 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700412 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700413 }
414
415 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700416 * Clear all the components.
417 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700418 void
419 clear() {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700420 components_.clear();
421 }
422
423 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700424 * @deprecated use size().
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700425 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700426 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700427 getComponentCount() const { return size(); }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700428
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700429 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700430 * @deprecated Use get(i).
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700431 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700432 const Component&
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700433 getComponent(size_t i) const { return get(i); }
Jeff Thompson443398d2013-07-02 19:45:46 -0700434
Jeff Thompsone6063512013-07-01 15:11:28 -0700435 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700436 * Get a new name, constructed as a subset of components.
437 * @param iStartComponent The index if the first component to get.
438 * @param nComponents The number of components starting at iStartComponent.
439 * @return A new name.
440 */
441 Name
442 getSubName(size_t iStartComponent, size_t nComponents) const;
443
444 /**
445 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
446 * @param iStartComponent The index if the first component to get.
447 * @return A new name.
448 */
449 Name
450 getSubName(size_t iStartComponent) const;
451
452 /**
453 * Return a new Name with the first nComponents components of this Name.
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800454 * @param nComponents The number of prefix components. If nComponents is -N then return the prefix up
455 * to name.size() - N. For example getPrefix(-1) returns the name without the final component.
Jeff Thompsond0159d72013-09-23 13:34:15 -0700456 * @return A new Name.
457 */
458 Name
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800459 getPrefix(int nComponents) const
Jeff Thompsond0159d72013-09-23 13:34:15 -0700460 {
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800461 if (nComponents < 0)
462 return getSubName(0, components_.size() + nComponents);
463 else
464 return getSubName(0, nComponents);
Jeff Thompsond0159d72013-09-23 13:34:15 -0700465 }
466
467 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700468 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700469 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700470 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700471 std::string
472 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700473
Jeff Thompson21844fc2013-08-08 14:52:51 -0700474 /**
Jeff Thompson8aac1992013-08-12 17:26:02 -0700475 * Append a component with the encoded segment number.
476 * @param segment The segment number.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700477 * @return This name so that you can chain calls to append.
478 */
479 Name&
Jeff Thompsond129ac12013-10-11 14:30:12 -0700480 appendSegment(uint64_t segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700481 {
Jeff Thompsond129ac12013-10-11 14:30:12 -0700482 components_.push_back(Component::fromNumberWithMarker(segment, 0x00));
483 return *this;
484 }
485
486 /**
487 * Append a component with the encoded version number.
488 * Note that this encodes the exact value of version without converting from a time representation.
489 * @param version The version number.
490 * @return This name so that you can chain calls to append.
491 */
492 Name&
493 appendVersion(uint64_t version)
494 {
495 components_.push_back(Component::fromNumberWithMarker(version, 0xFD));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700496 return *this;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700497 }
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700498
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700499 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700500 * Check if this name has the same component count and components as the given name.
501 * @param name The Name to check.
502 * @return true if the names are equal, otherwise false.
503 */
504 bool
505 equals(const Name& name) const;
506
507 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700508 * Check if the N components of this name are the same as the first N components of the given name.
509 * @param name The Name to check.
510 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
511 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700512 bool
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800513 isPrefixOf(const Name& name) const;
514
515 bool
516 match(const Name& name) const
517 {
518 return isPrefixOf(name);
519 }
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700520
521 /**
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700522 * Make a Blob value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme.
Jeff Thompson08493592013-11-07 17:44:41 -0800523 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
524 * which means the component should be skipped in a URI name.
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700525 * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
526 * @param beginOffset The offset in escapedString of the beginning of the portion to decode.
527 * @param endOffset The offset in escapedString of the end of the portion to decode.
528 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
529 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800530 static Component
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700531 fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
532
533 /**
534 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
Jeff Thompson08493592013-11-07 17:44:41 -0800535 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
536 * which means the component should be skipped in a URI name.
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700537 * @param escapedString The null-terminated escaped string.
538 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
539 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800540 static Component
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700541 fromEscapedString(const char *escapedString);
542
543 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800544 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
545 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
546 * which means the component should be skipped in a URI name.
547 * @param escapedString The escaped string.
548 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
549 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800550 static Component
Jeff Thompson7781b392013-12-17 11:45:59 -0800551 fromEscapedString(const std::string& escapedString) { return fromEscapedString(escapedString.c_str()); }
552
553 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700554 * Write the value to result, escaping characters according to the NDN URI Scheme.
555 * This also adds "..." to a value with zero or more ".".
556 * @param value the buffer with the value to escape
557 * @param result the string stream to write to.
558 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700559 static void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800560 toEscapedString(const std::vector<uint8_t>& value, std::ostream& result);
Jeff Thompson21844fc2013-08-08 14:52:51 -0700561
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700562 /**
563 * Convert the value by escaping characters according to the NDN URI Scheme.
564 * This also adds "..." to a value with zero or more ".".
565 * @param value the buffer with the value to escape
566 * @return The escaped string.
567 */
568 static std::string
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700569 toEscapedString(const std::vector<uint8_t>& value);
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700570
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700571 //
572 // vector equivalent interface.
573 //
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800574
575 /**
576 * @brief Check if name is emtpy
577 */
578 bool
579 empty() const { return components_.empty(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700580
581 /**
582 * Get the number of components.
583 * @return The number of components.
584 */
585 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700586 size() const { return components_.size(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700587
588 /**
589 * Get the component at the given index.
590 * @param i The index of the component, starting from 0.
591 * @return The name component at the index.
592 */
593 const Component&
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800594 get(ssize_t i) const
595 {
596 if (i >= 0)
597 return components_[i];
598 else
599 return components_[size() + i];
600 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700601
602
603 const Component&
604 operator [] (int i) const
605 {
606 return get(i);
607 }
608
609 /**
610 * Append the component
611 * @param component The component of type T.
612 */
613 template<class T> void
614 push_back(const T &component)
615 {
616 append(component);
617 }
618
Jeff Thompson91737f52013-10-04 11:07:24 -0700619 /**
620 * Check if this name has the same component count and components as the given name.
621 * @param name The Name to check.
622 * @return true if the names are equal, otherwise false.
623 */
624 bool
625 operator == (const Name &name) const { return equals(name); }
626
627 /**
628 * Check if this name has the same component count and components as the given name.
629 * @param name The Name to check.
630 * @return true if the names are not equal, otherwise false.
631 */
632 bool
633 operator != (const Name &name) const { return !equals(name); }
634
Jeff Thompson82568ad2013-12-17 15:17:40 -0800635 /**
636 * Compare two names for "less than" using breadth first. If the first components of each name are not equal,
637 * this returns true if the first comes before the second using the NDN canonical ordering for name components.
638 * If they are equal, this compares the second components of each name, etc. If both names are the same up to
639 * the size of the shorter name, this returns true if the first name is shorter than the second. For example, if you
640 * use breadthFirstLess in std::sort, it gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names
641 * with the prefix /a are next to each other. But it may be also be counter-intuitive because /c comes before /bb
642 * according to NDN canonical ordering since it is shorter. Note: We don't define this directly as the Name
643 * less than operation because there are other valid ways to sort names.
644 * @param name1 The first name to compare.
645 * @param name2 The second name to compare.
646 * @return True if the first name is less than the second using breadth first comparison.
647 */
648 static bool
649 breadthFirstLess(const Name& name1, const Name& name2);
650
651 /**
652 * Name::BreadthFirstLess is a function object which calls breadthFirstLess, for use as the "less" operator in map, etc.
653 * For example, you can use Name as the key type in a map as follows: map<Name, int, Name::BreadthFirstLess>.
654 */
655 struct BreadthFirstLess {
656 bool operator() (const Name& name1, const Name& name2) const { return breadthFirstLess(name1, name2); }
657 };
658
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700659 //
660 // Iterator interface to name components.
661 //
662 typedef std::vector<Component>::iterator iterator;
663 typedef std::vector<Component>::const_iterator const_iterator;
664 typedef std::vector<Component>::reverse_iterator reverse_iterator;
665 typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator;
666 typedef std::vector<Component>::reference reference;
667 typedef std::vector<Component>::const_reference const_reference;
668
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800669 typedef Component value_type;
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700670
671 /**
672 * Begin iterator (const).
673 */
674 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700675 begin() const { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700676
677 /**
678 * Begin iterator.
679 */
680 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700681 begin() { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700682
683 /**
684 * End iterator (const).
685 */
686 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700687 end() const { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700688
689 /**
690 * End iterator.
691 */
692 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700693 end() { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700694
695 /**
696 * Reverse begin iterator (const).
697 */
698 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700699 rbegin() const { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700700
701 /**
702 * Reverse begin iterator.
703 */
704 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700705 rbegin() { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700706
707 /**
708 * Reverse end iterator (const).
709 */
710 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700711 rend() const { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700712
713 /**
714 * Reverse end iterator.
715 */
716 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700717 rend() { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700718
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700719private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700720 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700721
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800722 mutable Block wire_;
723};
724
725std::ostream &
726operator << (std::ostream &os, const Name &name);
727
728inline std::string
729Name::toUri() const
Jeff Thompson49e321a2013-10-04 17:35:59 -0700730{
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800731 std::ostringstream os;
732 os << *this;
733 return os.str();
Jeff Thompson49e321a2013-10-04 17:35:59 -0700734}
735
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700736}
737
738#endif
739