blob: d2c1afe2cdb0bc024d4b94aaeb6409fcaca5264a [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 Afanasyev848c61a2014-01-03 13:52:04 -080075 Component(const char *string)
76 : ConstBufferPtr (new Buffer(string, ::strlen(string)))
77 {
78 }
79
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080080 const Buffer&
81 getValue() const { return **this; }
Jeff Thompson6653b0b2013-09-23 12:32:39 -070082
83 /**
84 * Write this component value to result, escaping characters according to the NDN URI Scheme.
85 * This also adds "..." to a value with zero or more ".".
Jeff Thompson6653b0b2013-09-23 12:32:39 -070086 * @param result the string stream to write to.
87 */
88 void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080089 toEscapedString(std::ostream& result) const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070090 {
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080091 Name::toEscapedString(**this, result);
Jeff Thompson6653b0b2013-09-23 12:32:39 -070092 }
93
94 /**
95 * Convert this component value by escaping characters according to the NDN URI Scheme.
96 * This also adds "..." to a value with zero or more ".".
97 * @return The escaped string.
98 */
99 std::string
Jeff Thompsond0159d72013-09-23 13:34:15 -0700100 toEscapedString() const
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700101 {
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800102 return Name::toEscapedString(**this);
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700103 }
Jeff Thompson9bdb3b22013-09-12 12:42:13 -0700104
Jeff Thompson50b37912013-10-08 13:39:33 -0700105 /**
106 * Interpret this name component as a network-ordered number and return an integer.
107 * @return The integer number.
108 */
109 uint64_t
Jeff Thompson25b4e612013-10-10 16:03:24 -0700110 toNumber() const;
Jeff Thompson27cae532013-10-08 12:52:41 -0700111
Jeff Thompson50b37912013-10-08 13:39:33 -0700112 /**
113 * Interpret this name component as a network-ordered number with a marker and return an integer.
114 * @param marker The required first byte of the component.
115 * @return The integer number.
116 * @throw runtime_error If the first byte of the component does not equal the marker.
117 */
118 uint64_t
119 toNumberWithMarker(uint8_t marker) const;
120
121 /**
122 * Interpret this name component as a segment number according to NDN name conventions (a network-ordered number
123 * where the first byte is the marker 0x00).
124 * @return The integer segment number.
125 * @throw runtime_error If the first byte of the component is not the expected marker.
126 */
127 uint64_t
128 toSegment() const
129 {
130 return toNumberWithMarker(0x00);
131 }
132
133 /**
134 * @deprecated Use toSegment.
135 */
136 uint64_t
137 toSeqNum() const
138 {
139 return toSegment();
140 }
141
142 /**
143 * Interpret this name component as a version number according to NDN name conventions (a network-ordered number
144 * where the first byte is the marker 0xFD). Note that this returns the exact number from the component
145 * without converting it to a time representation.
146 * @return The integer segment number.
147 * @throw runtime_error If the first byte of the component is not the expected marker.
148 */
149 uint64_t
150 toVersion() const
151 {
152 return toNumberWithMarker(0xFD);
153 }
Jeff Thompson27cae532013-10-08 12:52:41 -0700154
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700155 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700156 * Create a component whose value is the network-ordered encoding of the number.
157 * Note: if the number is zero, the result is empty.
158 * @param number The number to be encoded.
159 * @return The component value.
160 */
161 static Component
162 fromNumber(uint64_t number);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700163
164 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700165 * Create a component whose value is the marker appended with the network-ordered encoding of the number.
166 * Note: if the number is zero, no bytes are used for the number - the result will have only the marker.
167 * @param number The number to be encoded.
168 * @param marker The marker to use as the first byte of the component.
169 * @return The component value.
Jeff Thompson8aac1992013-08-12 17:26:02 -0700170 */
Jeff Thompsond129ac12013-10-11 14:30:12 -0700171 static Component
172 fromNumberWithMarker(uint64_t number, uint8_t marker);
Jeff Thompsona98000c2013-12-16 14:40:09 -0800173
174 /**
175 * Check if this is the same component as other.
176 * @param other The other Component to compare with.
177 * @return true if the components are equal, otherwise false.
178 */
179 bool
180 equals(const Component& other) const
181 {
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800182 return **this == *other;
183 }
184
185 bool
186 empty() const
187 {
188 return !*this || (*this)->empty();
Jeff Thompsona98000c2013-12-16 14:40:09 -0800189 }
190
191 /**
192 * Check if this is the same component as other.
193 * @param other The other Component to compare with.
194 * @return true if the components are equal, otherwise false.
195 */
196 bool
197 operator == (const Component& other) const { return equals(other); }
198
199 /**
200 * Check if this is not the same component as other.
201 * @param other The other Component to compare with.
202 * @return true if the components are not equal, otherwise false.
203 */
204 bool
205 operator != (const Component& other) const { return !equals(other); }
206
207 /**
208 * Compare this to the other Component using NDN canonical ordering.
209 * @param other The other Component to compare with.
210 * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or
211 * 1 if *this comes after other in the canonical ordering.
212 *
213 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
214 */
215 int
216 compare(const Component& other) const;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700217
Jeff Thompsona98000c2013-12-16 14:40:09 -0800218 /**
219 * Return true if this is less than or equal to the other Component in the NDN canonical ordering.
220 * @param other The other Component to compare with.
221 *
222 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
223 */
224 bool
225 operator <= (const Component& other) const { return compare(other) <= 0; }
226
227 /**
228 * Return true if this is less than the other Component in the NDN canonical ordering.
229 * @param other The other Component to compare with.
230 *
231 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
232 */
233 bool
234 operator < (const Component& other) const { return compare(other) < 0; }
235
236 /**
237 * Return true if this is less than or equal to the other Component in the NDN canonical ordering.
238 * @param other The other Component to compare with.
239 *
240 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
241 */
242 bool
243 operator >= (const Component& other) const { return compare(other) >= 0; }
244
245 /**
246 * Return true if this is greater than the other Component in the NDN canonical ordering.
247 * @param other The other Component to compare with.
248 *
249 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
250 */
251 bool
252 operator > (const Component& other) const { return compare(other) > 0; }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800253 };
Jeff Thompsona98000c2013-12-16 14:40:09 -0800254
Jeff Thompson443398d2013-07-02 19:45:46 -0700255 /**
256 * Create a new Name with no components.
257 */
Jeff Thompson016ed642013-07-02 14:39:06 -0700258 Name() {
259 }
Jeff Thompson443398d2013-07-02 19:45:46 -0700260
261 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700262 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700263 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700264 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700265 Name(const std::vector<Component>& components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700266 : components_(components)
267 {
268 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800269
270 Name(const Block &name)
271 {
272 for (Block::element_const_iterator i = name.getAll().begin();
273 i != name.getAll().end();
274 ++i)
275 {
276 append(Component(i->value_begin(), i->value_end()));
277 }
278 }
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700279
280 /**
Jeff Thompson443398d2013-07-02 19:45:46 -0700281 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700282 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -0700283 */
Jeff Thompson3549ef32013-09-25 14:05:17 -0700284 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -0700285 {
286 set(uri);
287 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700288
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700289 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -0700290 * Parse the uri according to the NDN URI Scheme and create the name with the components.
291 * @param uri The URI string.
292 */
293 Name(const std::string& uri)
294 {
295 set(uri.c_str());
296 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700297
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800298 const Block &
299 wireEncode() const;
Alexander Afanasyev848c61a2014-01-03 13:52:04 -0800300
301 void
302 wireDecode(const Block &wire);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700303
304 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700305 * Parse the uri according to the NDN URI Scheme and set the name with the components.
Jeff Thompson7781b392013-12-17 11:45:59 -0800306 * @param uri The null-terminated URI string.
Jeff Thompson67515bd2013-08-15 17:43:22 -0700307 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700308 void
309 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700310
311 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800312 * Parse the uri according to the NDN URI Scheme and set the name with the components.
313 * @param uri The URI string.
314 */
315 void
316 set(const std::string& uri) { set(uri.c_str()); }
317
318 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700319 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700320 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700321 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700322 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700323 append(const uint8_t *value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700324 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700325 components_.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700326 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700327 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700328
329 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700330 * Append a new component, copying from value.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700331 * @return This name so that you can chain calls to append.
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700332 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700333 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800334 append(const Buffer& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700335 {
336 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700337 return *this;
Jeff Thompson0f743452013-09-12 14:23:18 -0700338 }
339
Jeff Thompson26b0d792013-09-23 16:19:01 -0700340 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800341 append(const ConstBufferPtr &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700342 {
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700343 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700344 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700345 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700346
Jeff Thompson21eb7212013-09-26 09:05:40 -0700347 Name&
348 append(const Component &value)
349 {
350 components_.push_back(value);
351 return *this;
352 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800353
354 Name&
355 append(const Block &value)
356 {
357 components_.push_back(Component(value.begin(), value.end()));
358 return *this;
359 }
Jeff Thompson21eb7212013-09-26 09:05:40 -0700360
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700361 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700362 * Append the components of the given name to this name.
363 * @param name The Name with components to append.
364 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700365 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700366 Name&
367 append(const Name& name);
368
369 /**
370 * @deprecated Use append.
371 */
372 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700373 appendComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700374 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700375 return append(value, valueLength);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700376 }
377
378 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700379 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700380 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700381 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800382 appendComponent(const Buffer& value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700383 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700384 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700385 }
386
387 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700388 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700389 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700390 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800391 appendComponent(const ConstBufferPtr &value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700392 {
393 return append(value);
394 }
395
396 /**
397 * @deprecated Use append.
398 */
399 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700400 addComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700401 {
402 return append(value, valueLength);
403 }
404
405 /**
406 * @deprecated Use append.
407 */
408 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800409 addComponent(const Buffer& value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700410 {
411 return append(value);
412 }
413
414 /**
415 * @deprecated Use append.
416 */
417 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800418 addComponent(const ConstBufferPtr &value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700419 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700420 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700421 }
422
423 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700424 * Clear all the components.
425 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700426 void
427 clear() {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700428 components_.clear();
429 }
430
431 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700432 * @deprecated use size().
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700433 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700434 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700435 getComponentCount() const { return size(); }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700436
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700437 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700438 * @deprecated Use get(i).
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700439 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700440 const Component&
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700441 getComponent(size_t i) const { return get(i); }
Jeff Thompson443398d2013-07-02 19:45:46 -0700442
Jeff Thompsone6063512013-07-01 15:11:28 -0700443 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700444 * Get a new name, constructed as a subset of components.
445 * @param iStartComponent The index if the first component to get.
446 * @param nComponents The number of components starting at iStartComponent.
447 * @return A new name.
448 */
449 Name
450 getSubName(size_t iStartComponent, size_t nComponents) const;
451
452 /**
453 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
454 * @param iStartComponent The index if the first component to get.
455 * @return A new name.
456 */
457 Name
458 getSubName(size_t iStartComponent) const;
459
460 /**
461 * Return a new Name with the first nComponents components of this Name.
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800462 * @param nComponents The number of prefix components. If nComponents is -N then return the prefix up
463 * to name.size() - N. For example getPrefix(-1) returns the name without the final component.
Jeff Thompsond0159d72013-09-23 13:34:15 -0700464 * @return A new Name.
465 */
466 Name
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800467 getPrefix(int nComponents) const
Jeff Thompsond0159d72013-09-23 13:34:15 -0700468 {
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800469 if (nComponents < 0)
470 return getSubName(0, components_.size() + nComponents);
471 else
472 return getSubName(0, nComponents);
Jeff Thompsond0159d72013-09-23 13:34:15 -0700473 }
474
475 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700476 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700477 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700478 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700479 std::string
480 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700481
Jeff Thompson21844fc2013-08-08 14:52:51 -0700482 /**
Jeff Thompson8aac1992013-08-12 17:26:02 -0700483 * Append a component with the encoded segment number.
484 * @param segment The segment number.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700485 * @return This name so that you can chain calls to append.
486 */
487 Name&
Jeff Thompsond129ac12013-10-11 14:30:12 -0700488 appendSegment(uint64_t segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700489 {
Jeff Thompsond129ac12013-10-11 14:30:12 -0700490 components_.push_back(Component::fromNumberWithMarker(segment, 0x00));
491 return *this;
492 }
493
494 /**
495 * Append a component with the encoded version number.
496 * Note that this encodes the exact value of version without converting from a time representation.
497 * @param version The version number.
498 * @return This name so that you can chain calls to append.
499 */
500 Name&
501 appendVersion(uint64_t version)
502 {
503 components_.push_back(Component::fromNumberWithMarker(version, 0xFD));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700504 return *this;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700505 }
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700506
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700507 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700508 * Check if this name has the same component count and components as the given name.
509 * @param name The Name to check.
510 * @return true if the names are equal, otherwise false.
511 */
512 bool
513 equals(const Name& name) const;
514
515 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700516 * Check if the N components of this name are the same as the first N components of the given name.
517 * @param name The Name to check.
518 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
519 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700520 bool
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800521 isPrefixOf(const Name& name) const;
522
523 bool
524 match(const Name& name) const
525 {
526 return isPrefixOf(name);
527 }
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700528
529 /**
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700530 * 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 -0800531 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
532 * which means the component should be skipped in a URI name.
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700533 * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
534 * @param beginOffset The offset in escapedString of the beginning of the portion to decode.
535 * @param endOffset The offset in escapedString of the end of the portion to decode.
536 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
537 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800538 static Component
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700539 fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
540
541 /**
542 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
Jeff Thompson08493592013-11-07 17:44:41 -0800543 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
544 * which means the component should be skipped in a URI name.
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700545 * @param escapedString The null-terminated escaped string.
546 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
547 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800548 static Component
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700549 fromEscapedString(const char *escapedString);
550
551 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800552 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
553 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
554 * which means the component should be skipped in a URI name.
555 * @param escapedString The escaped string.
556 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
557 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800558 static Component
Jeff Thompson7781b392013-12-17 11:45:59 -0800559 fromEscapedString(const std::string& escapedString) { return fromEscapedString(escapedString.c_str()); }
560
561 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700562 * Write the value to result, escaping characters according to the NDN URI Scheme.
563 * This also adds "..." to a value with zero or more ".".
564 * @param value the buffer with the value to escape
565 * @param result the string stream to write to.
566 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700567 static void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800568 toEscapedString(const std::vector<uint8_t>& value, std::ostream& result);
Jeff Thompson21844fc2013-08-08 14:52:51 -0700569
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700570 /**
571 * Convert the value by escaping characters according to the NDN URI Scheme.
572 * This also adds "..." to a value with zero or more ".".
573 * @param value the buffer with the value to escape
574 * @return The escaped string.
575 */
576 static std::string
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700577 toEscapedString(const std::vector<uint8_t>& value);
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700578
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700579 //
580 // vector equivalent interface.
581 //
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800582
583 /**
584 * @brief Check if name is emtpy
585 */
586 bool
587 empty() const { return components_.empty(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700588
589 /**
590 * Get the number of components.
591 * @return The number of components.
592 */
593 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700594 size() const { return components_.size(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700595
596 /**
597 * Get the component at the given index.
598 * @param i The index of the component, starting from 0.
599 * @return The name component at the index.
600 */
601 const Component&
Alexander Afanasyev8f9aa8b2013-12-30 15:58:57 -0800602 get(ssize_t i) const
603 {
604 if (i >= 0)
605 return components_[i];
606 else
607 return components_[size() + i];
608 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700609
610
611 const Component&
612 operator [] (int i) const
613 {
614 return get(i);
615 }
616
617 /**
618 * Append the component
619 * @param component The component of type T.
620 */
621 template<class T> void
622 push_back(const T &component)
623 {
624 append(component);
625 }
626
Jeff Thompson91737f52013-10-04 11:07:24 -0700627 /**
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 equal, otherwise false.
631 */
632 bool
633 operator == (const Name &name) const { return equals(name); }
634
635 /**
636 * Check if this name has the same component count and components as the given name.
637 * @param name The Name to check.
638 * @return true if the names are not equal, otherwise false.
639 */
640 bool
641 operator != (const Name &name) const { return !equals(name); }
642
Jeff Thompson82568ad2013-12-17 15:17:40 -0800643 /**
644 * Compare two names for "less than" using breadth first. If the first components of each name are not equal,
645 * this returns true if the first comes before the second using the NDN canonical ordering for name components.
646 * If they are equal, this compares the second components of each name, etc. If both names are the same up to
647 * the size of the shorter name, this returns true if the first name is shorter than the second. For example, if you
648 * use breadthFirstLess in std::sort, it gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names
649 * with the prefix /a are next to each other. But it may be also be counter-intuitive because /c comes before /bb
650 * according to NDN canonical ordering since it is shorter. Note: We don't define this directly as the Name
651 * less than operation because there are other valid ways to sort names.
652 * @param name1 The first name to compare.
653 * @param name2 The second name to compare.
654 * @return True if the first name is less than the second using breadth first comparison.
655 */
656 static bool
657 breadthFirstLess(const Name& name1, const Name& name2);
658
659 /**
660 * Name::BreadthFirstLess is a function object which calls breadthFirstLess, for use as the "less" operator in map, etc.
661 * For example, you can use Name as the key type in a map as follows: map<Name, int, Name::BreadthFirstLess>.
662 */
663 struct BreadthFirstLess {
664 bool operator() (const Name& name1, const Name& name2) const { return breadthFirstLess(name1, name2); }
665 };
666
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700667 //
668 // Iterator interface to name components.
669 //
670 typedef std::vector<Component>::iterator iterator;
671 typedef std::vector<Component>::const_iterator const_iterator;
672 typedef std::vector<Component>::reverse_iterator reverse_iterator;
673 typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator;
674 typedef std::vector<Component>::reference reference;
675 typedef std::vector<Component>::const_reference const_reference;
676
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800677 typedef Component value_type;
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700678
679 /**
680 * Begin iterator (const).
681 */
682 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700683 begin() const { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700684
685 /**
686 * Begin iterator.
687 */
688 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700689 begin() { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700690
691 /**
692 * End iterator (const).
693 */
694 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700695 end() const { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700696
697 /**
698 * End iterator.
699 */
700 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700701 end() { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700702
703 /**
704 * Reverse begin iterator (const).
705 */
706 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700707 rbegin() const { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700708
709 /**
710 * Reverse begin iterator.
711 */
712 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700713 rbegin() { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700714
715 /**
716 * Reverse end iterator (const).
717 */
718 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700719 rend() const { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700720
721 /**
722 * Reverse end iterator.
723 */
724 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700725 rend() { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700726
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700727private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700728 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700729
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800730 mutable Block wire_;
731};
732
733std::ostream &
734operator << (std::ostream &os, const Name &name);
735
736inline std::string
737Name::toUri() const
Jeff Thompson49e321a2013-10-04 17:35:59 -0700738{
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800739 std::ostringstream os;
740 os << *this;
741 return os.str();
Jeff Thompson49e321a2013-10-04 17:35:59 -0700742}
743
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700744}
745
746#endif
747