blob: 8c348fe2a4b927ef4cfcfe32f176791a52370e35 [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 Thompson27b2bf92013-10-12 14:38:13 -070015#include <string.h>
Jeff Thompsonec7789a2013-08-21 11:08:36 -070016#include <sstream>
Jeff Thompson995aba52013-09-12 12:04:52 -070017#include "util/blob.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070018
Jeff Thompson25b4e612013-10-10 16:03:24 -070019struct ndn_NameComponent;
20struct ndn_Name;
21
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070022namespace ndn {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070023
Jeff Thompsonc7d65502013-11-06 17:22:26 -080024/**
25 * A Name holds an array of Name::Component and represents an NDN name.
26 */
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070027class Name {
28public:
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070029 /**
Jeff Thompsonc7d65502013-11-06 17:22:26 -080030 * A Name::Component holds a read-only name component value.
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070031 */
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070032 class Component {
33 public:
34 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070035 * Create a new Name::Component with a null value.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070036 */
37 Component()
38 {
39 }
40
41 /**
42 * Create a new Name::Component, copying the given value.
43 * @param value The value byte array.
44 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070045 Component(const std::vector<uint8_t>& value)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070046 : value_(value)
47 {
48 }
49
50 /**
51 * Create a new Name::Component, copying the given value.
52 * @param value Pointer to the value byte array.
53 * @param valueLen Length of value.
54 */
Jeff Thompson97223af2013-09-24 17:01:27 -070055 Component(const uint8_t *value, size_t valueLen)
Jeff Thompson995aba52013-09-12 12:04:52 -070056 : value_(value, valueLen)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070057 {
58 }
Jeff Thompson0f743452013-09-12 14:23:18 -070059
60 /**
61 * Create a new Name::Component, taking another pointer to the Blob value.
62 * @param value A blob with a pointer to an immutable array. The pointer is copied.
63 */
64 Component(const Blob &value)
65 : value_(value)
66 {
67 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070068
69 /**
70 * Set the componentStruct to point to this component, without copying any memory.
71 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
72 * @param componentStruct The C ndn_NameComponent struct to receive the pointer.
73 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070074 void
Jeff Thompson25b4e612013-10-10 16:03:24 -070075 get(struct ndn_NameComponent& componentStruct) const;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070076
Jeff Thompson0050abe2013-09-17 12:50:25 -070077 const Blob&
78 getValue() const { return value_; }
Jeff Thompson6653b0b2013-09-23 12:32:39 -070079
80 /**
81 * Write this component value to result, escaping characters according to the NDN URI Scheme.
82 * This also adds "..." to a value with zero or more ".".
Jeff Thompson6653b0b2013-09-23 12:32:39 -070083 * @param result the string stream to write to.
84 */
85 void
Jeff Thompsond0159d72013-09-23 13:34:15 -070086 toEscapedString(std::ostringstream& result) const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070087 {
88 Name::toEscapedString(*value_, result);
89 }
90
91 /**
92 * Convert this component value by escaping characters according to the NDN URI Scheme.
93 * This also adds "..." to a value with zero or more ".".
94 * @return The escaped string.
95 */
96 std::string
Jeff Thompsond0159d72013-09-23 13:34:15 -070097 toEscapedString() const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070098 {
99 return Name::toEscapedString(*value_);
100 }
Jeff Thompson9bdb3b22013-09-12 12:42:13 -0700101
Jeff Thompson50b37912013-10-08 13:39:33 -0700102 /**
103 * Interpret this name component as a network-ordered number and return an integer.
104 * @return The integer number.
105 */
106 uint64_t
Jeff Thompson25b4e612013-10-10 16:03:24 -0700107 toNumber() const;
Jeff Thompson27cae532013-10-08 12:52:41 -0700108
Jeff Thompson50b37912013-10-08 13:39:33 -0700109 /**
110 * Interpret this name component as a network-ordered number with a marker and return an integer.
111 * @param marker The required first byte of the component.
112 * @return The integer number.
113 * @throw runtime_error If the first byte of the component does not equal the marker.
114 */
115 uint64_t
116 toNumberWithMarker(uint8_t marker) const;
117
118 /**
119 * Interpret this name component as a segment number according to NDN name conventions (a network-ordered number
120 * where the first byte is the marker 0x00).
121 * @return The integer segment number.
122 * @throw runtime_error If the first byte of the component is not the expected marker.
123 */
124 uint64_t
125 toSegment() const
126 {
127 return toNumberWithMarker(0x00);
128 }
129
130 /**
131 * @deprecated Use toSegment.
132 */
133 uint64_t
134 toSeqNum() const
135 {
136 return toSegment();
137 }
138
139 /**
140 * Interpret this name component as a version number according to NDN name conventions (a network-ordered number
141 * where the first byte is the marker 0xFD). Note that this returns the exact number from the component
142 * without converting it to a time representation.
143 * @return The integer segment number.
144 * @throw runtime_error If the first byte of the component is not the expected marker.
145 */
146 uint64_t
147 toVersion() const
148 {
149 return toNumberWithMarker(0xFD);
150 }
Jeff Thompson27cae532013-10-08 12:52:41 -0700151
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700152 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700153 * Create a component whose value is the network-ordered encoding of the number.
154 * Note: if the number is zero, the result is empty.
155 * @param number The number to be encoded.
156 * @return The component value.
157 */
158 static Component
159 fromNumber(uint64_t number);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700160
161 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700162 * Create a component whose value is the marker appended with the network-ordered encoding of the number.
163 * Note: if the number is zero, no bytes are used for the number - the result will have only the marker.
164 * @param number The number to be encoded.
165 * @param marker The marker to use as the first byte of the component.
166 * @return The component value.
Jeff Thompson8aac1992013-08-12 17:26:02 -0700167 */
Jeff Thompsond129ac12013-10-11 14:30:12 -0700168 static Component
169 fromNumberWithMarker(uint64_t number, uint8_t marker);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700170
171 private:
Jeff Thompson995aba52013-09-12 12:04:52 -0700172 Blob value_;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700173 };
174
Jeff Thompson443398d2013-07-02 19:45:46 -0700175 /**
176 * Create a new Name with no components.
177 */
Jeff Thompson016ed642013-07-02 14:39:06 -0700178 Name() {
179 }
Jeff Thompson443398d2013-07-02 19:45:46 -0700180
181 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700182 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700183 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700184 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700185 Name(const std::vector<Component>& components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700186 : components_(components)
187 {
188 }
189
190 /**
Jeff Thompson443398d2013-07-02 19:45:46 -0700191 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700192 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -0700193 */
Jeff Thompson3549ef32013-09-25 14:05:17 -0700194 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -0700195 {
196 set(uri);
197 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700198
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700199 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -0700200 * Parse the uri according to the NDN URI Scheme and create the name with the components.
201 * @param uri The URI string.
202 */
203 Name(const std::string& uri)
204 {
205 set(uri.c_str());
206 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700207
Jeff Thompson3549ef32013-09-25 14:05:17 -0700208 /**
Jeff Thompson016ed642013-07-02 14:39:06 -0700209 * Set the nameStruct to point to the components in this name, without copying any memory.
210 * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700211 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -0700212 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700213 void
214 get(struct ndn_Name& nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700215
216 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700217 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700218 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700219 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700220 void
221 set(const struct ndn_Name& nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700222
223 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700224 * Parse the uri according to the NDN URI Scheme and set the name with the components.
225 * @param uri The URI string.
226 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700227 void
228 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700229
230 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700231 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700232 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700233 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700234 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700235 append(const uint8_t *value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700236 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700237 components_.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700238 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700239 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700240
241 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700242 * Append a new component, copying from value.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700243 * @return This name so that you can chain calls to append.
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700244 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700245 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700246 append(const std::vector<uint8_t>& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700247 {
248 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700249 return *this;
Jeff Thompson0f743452013-09-12 14:23:18 -0700250 }
251
Jeff Thompson26b0d792013-09-23 16:19:01 -0700252 Name&
253 append(const Blob &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700254 {
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700255 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700256 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700257 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700258
Jeff Thompson21eb7212013-09-26 09:05:40 -0700259 Name&
260 append(const Component &value)
261 {
262 components_.push_back(value);
263 return *this;
264 }
265
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700266 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700267 * Append the components of the given name to this name.
268 * @param name The Name with components to append.
269 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700270 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700271 Name&
272 append(const Name& name);
273
274 /**
275 * @deprecated Use append.
276 */
277 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700278 appendComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700279 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700280 return append(value, valueLength);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700281 }
282
283 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700284 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700285 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700286 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700287 appendComponent(const std::vector<uint8_t>& value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700288 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700289 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700290 }
291
292 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700293 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700294 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700295 Name&
296 appendComponent(const Blob &value)
297 {
298 return append(value);
299 }
300
301 /**
302 * @deprecated Use append.
303 */
304 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700305 addComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700306 {
307 return append(value, valueLength);
308 }
309
310 /**
311 * @deprecated Use append.
312 */
313 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700314 addComponent(const std::vector<uint8_t>& value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700315 {
316 return append(value);
317 }
318
319 /**
320 * @deprecated Use append.
321 */
322 Name&
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700323 addComponent(const Blob &value)
324 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700325 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700326 }
327
328 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700329 * Clear all the components.
330 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700331 void
332 clear() {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700333 components_.clear();
334 }
335
336 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700337 * @deprecated use size().
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700338 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700339 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700340 getComponentCount() const { return size(); }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700341
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700342 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700343 * @deprecated Use get(i).
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700344 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700345 const Component&
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700346 getComponent(size_t i) const { return get(i); }
Jeff Thompson443398d2013-07-02 19:45:46 -0700347
Jeff Thompsone6063512013-07-01 15:11:28 -0700348 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700349 * Get a new name, constructed as a subset of components.
350 * @param iStartComponent The index if the first component to get.
351 * @param nComponents The number of components starting at iStartComponent.
352 * @return A new name.
353 */
354 Name
355 getSubName(size_t iStartComponent, size_t nComponents) const;
356
357 /**
358 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
359 * @param iStartComponent The index if the first component to get.
360 * @return A new name.
361 */
362 Name
363 getSubName(size_t iStartComponent) const;
364
365 /**
366 * Return a new Name with the first nComponents components of this Name.
367 * @param nComponents The number of prefix components.
368 * @return A new Name.
369 */
370 Name
371 getPrefix(size_t nComponents) const
372 {
373 return getSubName(0, nComponents);
374 }
375
376 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700377 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700378 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700379 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700380 std::string
381 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700382
Jeff Thompson21844fc2013-08-08 14:52:51 -0700383 /**
384 * @deprecated Use toUri().
385 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700386 std::string
387 to_uri() const
Jeff Thompson21844fc2013-08-08 14:52:51 -0700388 {
389 return toUri();
390 }
Jeff Thompson26b0d792013-09-23 16:19:01 -0700391
Jeff Thompson8aac1992013-08-12 17:26:02 -0700392 /**
393 * Append a component with the encoded segment number.
394 * @param segment The segment number.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700395 * @return This name so that you can chain calls to append.
396 */
397 Name&
Jeff Thompsond129ac12013-10-11 14:30:12 -0700398 appendSegment(uint64_t segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700399 {
Jeff Thompsond129ac12013-10-11 14:30:12 -0700400 components_.push_back(Component::fromNumberWithMarker(segment, 0x00));
401 return *this;
402 }
403
404 /**
405 * Append a component with the encoded version number.
406 * Note that this encodes the exact value of version without converting from a time representation.
407 * @param version The version number.
408 * @return This name so that you can chain calls to append.
409 */
410 Name&
411 appendVersion(uint64_t version)
412 {
413 components_.push_back(Component::fromNumberWithMarker(version, 0xFD));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700414 return *this;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700415 }
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700416
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700417 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700418 * Check if this name has the same component count and components as the given name.
419 * @param name The Name to check.
420 * @return true if the names are equal, otherwise false.
421 */
422 bool
423 equals(const Name& name) const;
424
425 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700426 * Check if the N components of this name are the same as the first N components of the given name.
427 * @param name The Name to check.
428 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
429 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700430 bool
431 match(const Name& name) const;
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700432
433 /**
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700434 * 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 -0800435 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
436 * which means the component should be skipped in a URI name.
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700437 * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
438 * @param beginOffset The offset in escapedString of the beginning of the portion to decode.
439 * @param endOffset The offset in escapedString of the end of the portion to decode.
440 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
441 */
442 static Blob
443 fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
444
445 /**
446 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
Jeff Thompson08493592013-11-07 17:44:41 -0800447 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
448 * which means the component should be skipped in a URI name.
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700449 * @param escapedString The null-terminated escaped string.
450 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
451 */
452 static Blob
453 fromEscapedString(const char *escapedString);
454
455 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700456 * Write the value to result, escaping characters according to the NDN URI Scheme.
457 * This also adds "..." to a value with zero or more ".".
458 * @param value the buffer with the value to escape
459 * @param result the string stream to write to.
460 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700461 static void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700462 toEscapedString(const std::vector<uint8_t>& value, std::ostringstream& result);
Jeff Thompson21844fc2013-08-08 14:52:51 -0700463
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700464 /**
465 * Convert the value by escaping characters according to the NDN URI Scheme.
466 * This also adds "..." to a value with zero or more ".".
467 * @param value the buffer with the value to escape
468 * @return The escaped string.
469 */
470 static std::string
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700471 toEscapedString(const std::vector<uint8_t>& value);
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700472
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700473 //
474 // vector equivalent interface.
475 //
476
477 /**
478 * Get the number of components.
479 * @return The number of components.
480 */
481 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700482 size() const { return components_.size(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700483
484 /**
485 * Get the component at the given index.
486 * @param i The index of the component, starting from 0.
487 * @return The name component at the index.
488 */
489 const Component&
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700490 get(size_t i) const { return components_[i]; }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700491
492
493 const Component&
494 operator [] (int i) const
495 {
496 return get(i);
497 }
498
499 /**
500 * Append the component
501 * @param component The component of type T.
502 */
503 template<class T> void
504 push_back(const T &component)
505 {
506 append(component);
507 }
508
Jeff Thompson91737f52013-10-04 11:07:24 -0700509 /**
510 * Check if this name has the same component count and components as the given name.
511 * @param name The Name to check.
512 * @return true if the names are equal, otherwise false.
513 */
514 bool
515 operator == (const Name &name) const { return equals(name); }
516
517 /**
518 * Check if this name has the same component count and components as the given name.
519 * @param name The Name to check.
520 * @return true if the names are not equal, otherwise false.
521 */
522 bool
523 operator != (const Name &name) const { return !equals(name); }
524
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700525 //
526 // Iterator interface to name components.
527 //
528 typedef std::vector<Component>::iterator iterator;
529 typedef std::vector<Component>::const_iterator const_iterator;
530 typedef std::vector<Component>::reverse_iterator reverse_iterator;
531 typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator;
532 typedef std::vector<Component>::reference reference;
533 typedef std::vector<Component>::const_reference const_reference;
534
535 typedef Component partial_type;
536
537 /**
538 * Begin iterator (const).
539 */
540 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700541 begin() const { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700542
543 /**
544 * Begin iterator.
545 */
546 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700547 begin() { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700548
549 /**
550 * End iterator (const).
551 */
552 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700553 end() const { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700554
555 /**
556 * End iterator.
557 */
558 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700559 end() { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700560
561 /**
562 * Reverse begin iterator (const).
563 */
564 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700565 rbegin() const { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700566
567 /**
568 * Reverse begin iterator.
569 */
570 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700571 rbegin() { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700572
573 /**
574 * Reverse end iterator (const).
575 */
576 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700577 rend() const { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700578
579 /**
580 * Reverse end iterator.
581 */
582 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700583 rend() { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700584
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700585private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700586 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700587};
588
Jeff Thompson49e321a2013-10-04 17:35:59 -0700589inline std::ostream&
590operator << (std::ostream& os, const Name& name)
591{
592 os << name.toUri();
593 return os;
594}
595
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700596}
597
598#endif
599