blob: 5cd65a5a98cef556864192bdede9e67e1493c2fd [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 Thompson9c41dfe2013-06-27 12:10:25 -070024class Name {
25public:
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070026 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070027 * A Name::Component is holds a read-only name component value.
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070028 */
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070029 class Component {
30 public:
31 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070032 * Create a new Name::Component with a null value.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070033 */
34 Component()
35 {
36 }
37
38 /**
39 * Create a new Name::Component, copying the given value.
40 * @param value The value byte array.
41 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070042 Component(const std::vector<uint8_t>& value)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070043 : value_(value)
44 {
45 }
46
47 /**
48 * Create a new Name::Component, copying the given value.
49 * @param value Pointer to the value byte array.
50 * @param valueLen Length of value.
51 */
Jeff Thompson97223af2013-09-24 17:01:27 -070052 Component(const uint8_t *value, size_t valueLen)
Jeff Thompson995aba52013-09-12 12:04:52 -070053 : value_(value, valueLen)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070054 {
55 }
Jeff Thompson0f743452013-09-12 14:23:18 -070056
57 /**
58 * Create a new Name::Component, taking another pointer to the Blob value.
59 * @param value A blob with a pointer to an immutable array. The pointer is copied.
60 */
61 Component(const Blob &value)
62 : value_(value)
63 {
64 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070065
66 /**
67 * Set the componentStruct to point to this component, without copying any memory.
68 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
69 * @param componentStruct The C ndn_NameComponent struct to receive the pointer.
70 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070071 void
Jeff Thompson25b4e612013-10-10 16:03:24 -070072 get(struct ndn_NameComponent& componentStruct) const;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070073
Jeff Thompson0050abe2013-09-17 12:50:25 -070074 const Blob&
75 getValue() const { return value_; }
Jeff Thompson6653b0b2013-09-23 12:32:39 -070076
77 /**
78 * Write this component value to result, escaping characters according to the NDN URI Scheme.
79 * This also adds "..." to a value with zero or more ".".
80 * @param value the buffer with the value to escape
81 * @param result the string stream to write to.
82 */
83 void
Jeff Thompsond0159d72013-09-23 13:34:15 -070084 toEscapedString(std::ostringstream& result) const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070085 {
86 Name::toEscapedString(*value_, result);
87 }
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 {
97 return Name::toEscapedString(*value_);
98 }
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 Thompson46411c92013-09-13 19:31:25 -0700151 * Make a component value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme.
152 * If the escaped string is "", "." or ".." then return a Blob with a null pointer, which means this component value was not changed, and
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700153 * the component should be skipped in a URI name.
154 * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
155 * @param beginOffset The offset in escapedString of the beginning of the portion to decode.
156 * @param endOffset The offset in escapedString of the end of the portion to decode.
Jeff Thompsond129ac12013-10-11 14:30:12 -0700157 * @return The component value. If the escapedString is not a valid escaped component, then the component value is a null pointer.
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700158 */
Jeff Thompsond129ac12013-10-11 14:30:12 -0700159 static Component
160 fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
161
162 /**
163 * Create a component whose value is the network-ordered encoding of the number.
164 * Note: if the number is zero, the result is empty.
165 * @param number The number to be encoded.
166 * @return The component value.
167 */
168 static Component
169 fromNumber(uint64_t number);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700170
171 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700172 * Create a component whose value is the marker appended with the network-ordered encoding of the number.
173 * Note: if the number is zero, no bytes are used for the number - the result will have only the marker.
174 * @param number The number to be encoded.
175 * @param marker The marker to use as the first byte of the component.
176 * @return The component value.
Jeff Thompson8aac1992013-08-12 17:26:02 -0700177 */
Jeff Thompsond129ac12013-10-11 14:30:12 -0700178 static Component
179 fromNumberWithMarker(uint64_t number, uint8_t marker);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700180
181 private:
Jeff Thompson995aba52013-09-12 12:04:52 -0700182 Blob value_;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700183 };
184
Jeff Thompson443398d2013-07-02 19:45:46 -0700185 /**
186 * Create a new Name with no components.
187 */
Jeff Thompson016ed642013-07-02 14:39:06 -0700188 Name() {
189 }
Jeff Thompson443398d2013-07-02 19:45:46 -0700190
191 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700192 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700193 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700194 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700195 Name(const std::vector<Component>& components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700196 : components_(components)
197 {
198 }
199
200 /**
Jeff Thompson443398d2013-07-02 19:45:46 -0700201 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700202 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -0700203 */
Jeff Thompson3549ef32013-09-25 14:05:17 -0700204 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -0700205 {
206 set(uri);
207 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700208
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700209 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -0700210 * Parse the uri according to the NDN URI Scheme and create the name with the components.
211 * @param uri The URI string.
212 */
213 Name(const std::string& uri)
214 {
215 set(uri.c_str());
216 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700217
Jeff Thompson3549ef32013-09-25 14:05:17 -0700218 /**
Jeff Thompson016ed642013-07-02 14:39:06 -0700219 * Set the nameStruct to point to the components in this name, without copying any memory.
220 * 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 -0700221 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -0700222 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700223 void
224 get(struct ndn_Name& nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700225
226 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700227 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700228 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700229 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700230 void
231 set(const struct ndn_Name& nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700232
233 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700234 * Parse the uri according to the NDN URI Scheme and set the name with the components.
235 * @param uri The URI string.
236 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700237 void
238 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700239
240 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700241 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700242 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700243 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700244 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700245 append(const uint8_t *value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700246 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700247 components_.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700248 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700249 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700250
251 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700252 * Append a new component, copying from value.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700253 * @return This name so that you can chain calls to append.
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700254 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700255 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700256 append(const std::vector<uint8_t>& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700257 {
258 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700259 return *this;
Jeff Thompson0f743452013-09-12 14:23:18 -0700260 }
261
Jeff Thompson26b0d792013-09-23 16:19:01 -0700262 Name&
263 append(const Blob &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700264 {
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700265 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700266 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700267 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700268
Jeff Thompson21eb7212013-09-26 09:05:40 -0700269 Name&
270 append(const Component &value)
271 {
272 components_.push_back(value);
273 return *this;
274 }
Jeff Thompson16c56cc2013-10-11 14:51:52 -0700275
276 /**
277 * Parse the escaped string according to the NDN URI Scheme and append a component to this name.
278 * If escapedString has "/", then the value of the single appended component has "/" in it.
279 * (If you need to append a URI which is split into separate components, then call append(Name(uri)).)
280 * @param escapedString The escaped string representing a single component.
281 * @return This name so that you can chain calls to append.
282 */
283 Name&
284 append(const char* escapedString)
285 {
286 components_.push_back(Component::fromEscapedString(escapedString, 0, ::strlen(escapedString)));
287 return *this;
288 }
Jeff Thompson21eb7212013-09-26 09:05:40 -0700289
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700290 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700291 * Append the components of the given name to this name.
292 * @param name The Name with components to append.
293 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700294 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700295 Name&
296 append(const Name& name);
297
298 /**
299 * @deprecated Use append.
300 */
301 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700302 appendComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700303 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700304 return append(value, valueLength);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700305 }
306
307 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700308 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700309 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700310 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700311 appendComponent(const std::vector<uint8_t>& value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700312 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700313 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700314 }
315
316 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700317 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700318 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700319 Name&
320 appendComponent(const Blob &value)
321 {
322 return append(value);
323 }
324
325 /**
326 * @deprecated Use append.
327 */
328 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700329 addComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700330 {
331 return append(value, valueLength);
332 }
333
334 /**
335 * @deprecated Use append.
336 */
337 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700338 addComponent(const std::vector<uint8_t>& value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700339 {
340 return append(value);
341 }
342
343 /**
344 * @deprecated Use append.
345 */
346 Name&
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700347 addComponent(const Blob &value)
348 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700349 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700350 }
351
352 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700353 * Clear all the components.
354 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700355 void
356 clear() {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700357 components_.clear();
358 }
359
360 /**
361 * Get the number of components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700362 * @return The number of components.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700363 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700364 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -0700365 getComponentCount() const {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700366 return components_.size();
367 }
368
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700369 /**
370 * Get the component at the given index.
371 * @param i The index of the component, starting from 0.
372 * @return The name component at the index.
373 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700374 const Component&
Jeff Thompson97223af2013-09-24 17:01:27 -0700375 getComponent(size_t i) const { return components_[i]; }
Jeff Thompson443398d2013-07-02 19:45:46 -0700376
Jeff Thompsone6063512013-07-01 15:11:28 -0700377 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700378 * Get a new name, constructed as a subset of components.
379 * @param iStartComponent The index if the first component to get.
380 * @param nComponents The number of components starting at iStartComponent.
381 * @return A new name.
382 */
383 Name
384 getSubName(size_t iStartComponent, size_t nComponents) const;
385
386 /**
387 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
388 * @param iStartComponent The index if the first component to get.
389 * @return A new name.
390 */
391 Name
392 getSubName(size_t iStartComponent) const;
393
394 /**
395 * Return a new Name with the first nComponents components of this Name.
396 * @param nComponents The number of prefix components.
397 * @return A new Name.
398 */
399 Name
400 getPrefix(size_t nComponents) const
401 {
402 return getSubName(0, nComponents);
403 }
404
405 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700406 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700407 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700408 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700409 std::string
410 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700411
Jeff Thompson21844fc2013-08-08 14:52:51 -0700412 /**
413 * @deprecated Use toUri().
414 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700415 std::string
416 to_uri() const
Jeff Thompson21844fc2013-08-08 14:52:51 -0700417 {
418 return toUri();
419 }
Jeff Thompson26b0d792013-09-23 16:19:01 -0700420
Jeff Thompson8aac1992013-08-12 17:26:02 -0700421 /**
422 * Append a component with the encoded segment number.
423 * @param segment The segment number.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700424 * @return This name so that you can chain calls to append.
425 */
426 Name&
Jeff Thompsond129ac12013-10-11 14:30:12 -0700427 appendSegment(uint64_t segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700428 {
Jeff Thompsond129ac12013-10-11 14:30:12 -0700429 components_.push_back(Component::fromNumberWithMarker(segment, 0x00));
430 return *this;
431 }
432
433 /**
434 * Append a component with the encoded version number.
435 * Note that this encodes the exact value of version without converting from a time representation.
436 * @param version The version number.
437 * @return This name so that you can chain calls to append.
438 */
439 Name&
440 appendVersion(uint64_t version)
441 {
442 components_.push_back(Component::fromNumberWithMarker(version, 0xFD));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700443 return *this;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700444 }
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700445
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700446 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700447 * Check if this name has the same component count and components as the given name.
448 * @param name The Name to check.
449 * @return true if the names are equal, otherwise false.
450 */
451 bool
452 equals(const Name& name) const;
453
454 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700455 * Check if the N components of this name are the same as the first N components of the given name.
456 * @param name The Name to check.
457 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
458 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700459 bool
460 match(const Name& name) const;
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700461
462 /**
463 * Write the value to result, escaping characters according to the NDN URI Scheme.
464 * This also adds "..." to a value with zero or more ".".
465 * @param value the buffer with the value to escape
466 * @param result the string stream to write to.
467 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700468 static void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700469 toEscapedString(const std::vector<uint8_t>& value, std::ostringstream& result);
Jeff Thompson21844fc2013-08-08 14:52:51 -0700470
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700471 /**
472 * Convert the value by escaping characters according to the NDN URI Scheme.
473 * This also adds "..." to a value with zero or more ".".
474 * @param value the buffer with the value to escape
475 * @return The escaped string.
476 */
477 static std::string
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700478 toEscapedString(const std::vector<uint8_t>& value);
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700479
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700480 //
481 // vector equivalent interface.
482 //
483
484 /**
485 * Get the number of components.
486 * @return The number of components.
487 */
488 size_t
489 size() const {
490 return getComponentCount();
491 }
492
493 /**
494 * Get the component at the given index.
495 * @param i The index of the component, starting from 0.
496 * @return The name component at the index.
497 */
498 const Component&
499 get(size_t i) const { return getComponent(i); }
500
501
502 const Component&
503 operator [] (int i) const
504 {
505 return get(i);
506 }
507
508 /**
509 * Append the component
510 * @param component The component of type T.
511 */
512 template<class T> void
513 push_back(const T &component)
514 {
515 append(component);
516 }
517
Jeff Thompson91737f52013-10-04 11:07:24 -0700518 /**
519 * Check if this name has the same component count and components as the given name.
520 * @param name The Name to check.
521 * @return true if the names are equal, otherwise false.
522 */
523 bool
524 operator == (const Name &name) const { return equals(name); }
525
526 /**
527 * Check if this name has the same component count and components as the given name.
528 * @param name The Name to check.
529 * @return true if the names are not equal, otherwise false.
530 */
531 bool
532 operator != (const Name &name) const { return !equals(name); }
533
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700534 //
535 // Iterator interface to name components.
536 //
537 typedef std::vector<Component>::iterator iterator;
538 typedef std::vector<Component>::const_iterator const_iterator;
539 typedef std::vector<Component>::reverse_iterator reverse_iterator;
540 typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator;
541 typedef std::vector<Component>::reference reference;
542 typedef std::vector<Component>::const_reference const_reference;
543
544 typedef Component partial_type;
545
546 /**
547 * Begin iterator (const).
548 */
549 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700550 begin() const { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700551
552 /**
553 * Begin iterator.
554 */
555 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700556 begin() { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700557
558 /**
559 * End iterator (const).
560 */
561 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700562 end() const { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700563
564 /**
565 * End iterator.
566 */
567 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700568 end() { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700569
570 /**
571 * Reverse begin iterator (const).
572 */
573 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700574 rbegin() const { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700575
576 /**
577 * Reverse begin iterator.
578 */
579 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700580 rbegin() { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700581
582 /**
583 * Reverse end iterator (const).
584 */
585 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700586 rend() const { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700587
588 /**
589 * Reverse end iterator.
590 */
591 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700592 rend() { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700593
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700594private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700595 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700596};
597
Jeff Thompson49e321a2013-10-04 17:35:59 -0700598inline std::ostream&
599operator << (std::ostream& os, const Name& name)
600{
601 os << name.toUri();
602 return os;
603}
604
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700605}
606
607#endif
608