blob: 5e6747131711d6922611bf5a0b5dba3727d00721 [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 ".".
83 * @param value the buffer with the value to escape
84 * @param result the string stream to write to.
85 */
86 void
Jeff Thompsond0159d72013-09-23 13:34:15 -070087 toEscapedString(std::ostringstream& result) const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070088 {
89 Name::toEscapedString(*value_, result);
90 }
91
92 /**
93 * Convert this component value by escaping characters according to the NDN URI Scheme.
94 * This also adds "..." to a value with zero or more ".".
95 * @return The escaped string.
96 */
97 std::string
Jeff Thompsond0159d72013-09-23 13:34:15 -070098 toEscapedString() const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070099 {
100 return Name::toEscapedString(*value_);
101 }
Jeff Thompson9bdb3b22013-09-12 12:42:13 -0700102
Jeff Thompson50b37912013-10-08 13:39:33 -0700103 /**
104 * Interpret this name component as a network-ordered number and return an integer.
105 * @return The integer number.
106 */
107 uint64_t
Jeff Thompson25b4e612013-10-10 16:03:24 -0700108 toNumber() const;
Jeff Thompson27cae532013-10-08 12:52:41 -0700109
Jeff Thompson50b37912013-10-08 13:39:33 -0700110 /**
111 * Interpret this name component as a network-ordered number with a marker and return an integer.
112 * @param marker The required first byte of the component.
113 * @return The integer number.
114 * @throw runtime_error If the first byte of the component does not equal the marker.
115 */
116 uint64_t
117 toNumberWithMarker(uint8_t marker) const;
118
119 /**
120 * Interpret this name component as a segment number according to NDN name conventions (a network-ordered number
121 * where the first byte is the marker 0x00).
122 * @return The integer segment number.
123 * @throw runtime_error If the first byte of the component is not the expected marker.
124 */
125 uint64_t
126 toSegment() const
127 {
128 return toNumberWithMarker(0x00);
129 }
130
131 /**
132 * @deprecated Use toSegment.
133 */
134 uint64_t
135 toSeqNum() const
136 {
137 return toSegment();
138 }
139
140 /**
141 * Interpret this name component as a version number according to NDN name conventions (a network-ordered number
142 * where the first byte is the marker 0xFD). Note that this returns the exact number from the component
143 * without converting it to a time representation.
144 * @return The integer segment number.
145 * @throw runtime_error If the first byte of the component is not the expected marker.
146 */
147 uint64_t
148 toVersion() const
149 {
150 return toNumberWithMarker(0xFD);
151 }
Jeff Thompson27cae532013-10-08 12:52:41 -0700152
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700153 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700154 * Create a component whose value is the network-ordered encoding of the number.
155 * Note: if the number is zero, the result is empty.
156 * @param number The number to be encoded.
157 * @return The component value.
158 */
159 static Component
160 fromNumber(uint64_t number);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700161
162 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700163 * Create a component whose value is the marker appended with the network-ordered encoding of the number.
164 * Note: if the number is zero, no bytes are used for the number - the result will have only the marker.
165 * @param number The number to be encoded.
166 * @param marker The marker to use as the first byte of the component.
167 * @return The component value.
Jeff Thompson8aac1992013-08-12 17:26:02 -0700168 */
Jeff Thompsond129ac12013-10-11 14:30:12 -0700169 static Component
170 fromNumberWithMarker(uint64_t number, uint8_t marker);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700171
172 private:
Jeff Thompson995aba52013-09-12 12:04:52 -0700173 Blob value_;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700174 };
175
Jeff Thompson443398d2013-07-02 19:45:46 -0700176 /**
177 * Create a new Name with no components.
178 */
Jeff Thompson016ed642013-07-02 14:39:06 -0700179 Name() {
180 }
Jeff Thompson443398d2013-07-02 19:45:46 -0700181
182 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700183 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700184 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700185 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700186 Name(const std::vector<Component>& components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700187 : components_(components)
188 {
189 }
190
191 /**
Jeff Thompson443398d2013-07-02 19:45:46 -0700192 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700193 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -0700194 */
Jeff Thompson3549ef32013-09-25 14:05:17 -0700195 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -0700196 {
197 set(uri);
198 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700199
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700200 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -0700201 * Parse the uri according to the NDN URI Scheme and create the name with the components.
202 * @param uri The URI string.
203 */
204 Name(const std::string& uri)
205 {
206 set(uri.c_str());
207 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700208
Jeff Thompson3549ef32013-09-25 14:05:17 -0700209 /**
Jeff Thompson016ed642013-07-02 14:39:06 -0700210 * Set the nameStruct to point to the components in this name, without copying any memory.
211 * 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 -0700212 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -0700213 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700214 void
215 get(struct ndn_Name& nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700216
217 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700218 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700219 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700220 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700221 void
222 set(const struct ndn_Name& nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700223
224 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700225 * Parse the uri according to the NDN URI Scheme and set the name with the components.
226 * @param uri The URI string.
227 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700228 void
229 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700230
231 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700232 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700233 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700234 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700235 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700236 append(const uint8_t *value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700237 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700238 components_.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700239 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700240 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700241
242 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700243 * Append a new component, copying from value.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700244 * @return This name so that you can chain calls to append.
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700245 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700246 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700247 append(const std::vector<uint8_t>& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700248 {
249 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700250 return *this;
Jeff Thompson0f743452013-09-12 14:23:18 -0700251 }
252
Jeff Thompson26b0d792013-09-23 16:19:01 -0700253 Name&
254 append(const Blob &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700255 {
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700256 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700257 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700258 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700259
Jeff Thompson21eb7212013-09-26 09:05:40 -0700260 Name&
261 append(const Component &value)
262 {
263 components_.push_back(value);
264 return *this;
265 }
266
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700267 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700268 * Append the components of the given name to this name.
269 * @param name The Name with components to append.
270 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700271 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700272 Name&
273 append(const Name& name);
274
275 /**
276 * @deprecated Use append.
277 */
278 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700279 appendComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700280 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700281 return append(value, valueLength);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700282 }
283
284 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700285 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700286 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700287 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700288 appendComponent(const std::vector<uint8_t>& value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700289 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700290 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700291 }
292
293 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700294 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700295 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700296 Name&
297 appendComponent(const Blob &value)
298 {
299 return append(value);
300 }
301
302 /**
303 * @deprecated Use append.
304 */
305 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700306 addComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700307 {
308 return append(value, valueLength);
309 }
310
311 /**
312 * @deprecated Use append.
313 */
314 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700315 addComponent(const std::vector<uint8_t>& value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700316 {
317 return append(value);
318 }
319
320 /**
321 * @deprecated Use append.
322 */
323 Name&
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700324 addComponent(const Blob &value)
325 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700326 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700327 }
328
329 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700330 * Clear all the components.
331 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700332 void
333 clear() {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700334 components_.clear();
335 }
336
337 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700338 * @deprecated use size().
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700339 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700340 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700341 getComponentCount() const { return size(); }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700342
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700343 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700344 * @deprecated Use get(i).
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700345 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700346 const Component&
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700347 getComponent(size_t i) const { return get(i); }
Jeff Thompson443398d2013-07-02 19:45:46 -0700348
Jeff Thompsone6063512013-07-01 15:11:28 -0700349 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700350 * Get a new name, constructed as a subset of components.
351 * @param iStartComponent The index if the first component to get.
352 * @param nComponents The number of components starting at iStartComponent.
353 * @return A new name.
354 */
355 Name
356 getSubName(size_t iStartComponent, size_t nComponents) const;
357
358 /**
359 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
360 * @param iStartComponent The index if the first component to get.
361 * @return A new name.
362 */
363 Name
364 getSubName(size_t iStartComponent) const;
365
366 /**
367 * Return a new Name with the first nComponents components of this Name.
368 * @param nComponents The number of prefix components.
369 * @return A new Name.
370 */
371 Name
372 getPrefix(size_t nComponents) const
373 {
374 return getSubName(0, nComponents);
375 }
376
377 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700378 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700379 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700380 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700381 std::string
382 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700383
Jeff Thompson21844fc2013-08-08 14:52:51 -0700384 /**
385 * @deprecated Use toUri().
386 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700387 std::string
388 to_uri() const
Jeff Thompson21844fc2013-08-08 14:52:51 -0700389 {
390 return toUri();
391 }
Jeff Thompson26b0d792013-09-23 16:19:01 -0700392
Jeff Thompson8aac1992013-08-12 17:26:02 -0700393 /**
394 * Append a component with the encoded segment number.
395 * @param segment The segment number.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700396 * @return This name so that you can chain calls to append.
397 */
398 Name&
Jeff Thompsond129ac12013-10-11 14:30:12 -0700399 appendSegment(uint64_t segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700400 {
Jeff Thompsond129ac12013-10-11 14:30:12 -0700401 components_.push_back(Component::fromNumberWithMarker(segment, 0x00));
402 return *this;
403 }
404
405 /**
406 * Append a component with the encoded version number.
407 * Note that this encodes the exact value of version without converting from a time representation.
408 * @param version The version number.
409 * @return This name so that you can chain calls to append.
410 */
411 Name&
412 appendVersion(uint64_t version)
413 {
414 components_.push_back(Component::fromNumberWithMarker(version, 0xFD));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700415 return *this;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700416 }
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700417
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700418 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700419 * Check if this name has the same component count and components as the given name.
420 * @param name The Name to check.
421 * @return true if the names are equal, otherwise false.
422 */
423 bool
424 equals(const Name& name) const;
425
426 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700427 * Check if the N components of this name are the same as the first N components of the given name.
428 * @param name The Name to check.
429 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
430 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700431 bool
432 match(const Name& name) const;
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700433
434 /**
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700435 * Make a Blob value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme.
436 * If the escaped string is "", "." or ".." then return a Blob with a null pointer, which means this component value was not changed, and
437 * the component should be skipped in a URI name.
438 * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
439 * @param beginOffset The offset in escapedString of the beginning of the portion to decode.
440 * @param endOffset The offset in escapedString of the end of the portion to decode.
441 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
442 */
443 static Blob
444 fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
445
446 /**
447 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
448 * If the escaped string is "", "." or ".." then return a Blob with a null pointer, which means this component value was not changed, and
449 * the component should be skipped in a URI name.
450 * @param escapedString The null-terminated escaped string.
451 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
452 */
453 static Blob
454 fromEscapedString(const char *escapedString);
455
456 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700457 * Write the value to result, escaping characters according to the NDN URI Scheme.
458 * This also adds "..." to a value with zero or more ".".
459 * @param value the buffer with the value to escape
460 * @param result the string stream to write to.
461 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700462 static void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700463 toEscapedString(const std::vector<uint8_t>& value, std::ostringstream& result);
Jeff Thompson21844fc2013-08-08 14:52:51 -0700464
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700465 /**
466 * Convert the value by escaping characters according to the NDN URI Scheme.
467 * This also adds "..." to a value with zero or more ".".
468 * @param value the buffer with the value to escape
469 * @return The escaped string.
470 */
471 static std::string
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700472 toEscapedString(const std::vector<uint8_t>& value);
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700473
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700474 //
475 // vector equivalent interface.
476 //
477
478 /**
479 * Get the number of components.
480 * @return The number of components.
481 */
482 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700483 size() const { return components_.size(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700484
485 /**
486 * Get the component at the given index.
487 * @param i The index of the component, starting from 0.
488 * @return The name component at the index.
489 */
490 const Component&
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700491 get(size_t i) const { return components_[i]; }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700492
493
494 const Component&
495 operator [] (int i) const
496 {
497 return get(i);
498 }
499
500 /**
501 * Append the component
502 * @param component The component of type T.
503 */
504 template<class T> void
505 push_back(const T &component)
506 {
507 append(component);
508 }
509
Jeff Thompson91737f52013-10-04 11:07:24 -0700510 /**
511 * Check if this name has the same component count and components as the given name.
512 * @param name The Name to check.
513 * @return true if the names are equal, otherwise false.
514 */
515 bool
516 operator == (const Name &name) const { return equals(name); }
517
518 /**
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 not equal, otherwise false.
522 */
523 bool
524 operator != (const Name &name) const { return !equals(name); }
525
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700526 //
527 // Iterator interface to name components.
528 //
529 typedef std::vector<Component>::iterator iterator;
530 typedef std::vector<Component>::const_iterator const_iterator;
531 typedef std::vector<Component>::reverse_iterator reverse_iterator;
532 typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator;
533 typedef std::vector<Component>::reference reference;
534 typedef std::vector<Component>::const_reference const_reference;
535
536 typedef Component partial_type;
537
538 /**
539 * Begin iterator (const).
540 */
541 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700542 begin() const { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700543
544 /**
545 * Begin iterator.
546 */
547 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700548 begin() { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700549
550 /**
551 * End iterator (const).
552 */
553 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700554 end() const { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700555
556 /**
557 * End iterator.
558 */
559 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700560 end() { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700561
562 /**
563 * Reverse begin iterator (const).
564 */
565 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700566 rbegin() const { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700567
568 /**
569 * Reverse begin iterator.
570 */
571 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700572 rbegin() { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700573
574 /**
575 * Reverse end iterator (const).
576 */
577 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700578 rend() const { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700579
580 /**
581 * Reverse end iterator.
582 */
583 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700584 rend() { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700585
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700586private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700587 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700588};
589
Jeff Thompson49e321a2013-10-04 17:35:59 -0700590inline std::ostream&
591operator << (std::ostream& os, const Name& name)
592{
593 os << name.toUri();
594 return os;
595}
596
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700597}
598
599#endif
600