Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 5 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 9 | #include "common.hpp" |
| 10 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 11 | #include "name.hpp" |
| 12 | |
Jeff Thompson | b8f1b13 | 2013-08-13 11:07:43 -0700 | [diff] [blame] | 13 | #include <algorithm> |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 14 | #include <cstring> |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 15 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 16 | #include "util/time.hpp" |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 17 | #include "util/string-helper.hpp" |
| 18 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 19 | namespace ndn { |
| 20 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 21 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 22 | Name::set(const char *uri_cstr) |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 23 | { |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 24 | clear(); |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 26 | std::string uri = uri_cstr; |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 27 | trim(uri); |
| 28 | if (uri.size() == 0) |
| 29 | return; |
| 30 | |
| 31 | size_t iColon = uri.find(':'); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 32 | if (iColon != std::string::npos) { |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 33 | // Make sure the colon came before a '/'. |
| 34 | size_t iFirstSlash = uri.find('/'); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 35 | if (iFirstSlash == std::string::npos || iColon < iFirstSlash) { |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 36 | // Omit the leading protocol such as ndn: |
| 37 | uri.erase(0, iColon + 1); |
| 38 | trim(uri); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Trim the leading slash and possibly the authority. |
| 43 | if (uri[0] == '/') { |
| 44 | if (uri.size() >= 2 && uri[1] == '/') { |
| 45 | // Strip the authority following "//". |
| 46 | size_t iAfterAuthority = uri.find('/', 2); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 47 | if (iAfterAuthority == std::string::npos) |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 48 | // Unusual case: there was only an authority. |
| 49 | return; |
| 50 | else { |
| 51 | uri.erase(0, iAfterAuthority + 1); |
| 52 | trim(uri); |
| 53 | } |
| 54 | } |
| 55 | else { |
| 56 | uri.erase(0, 1); |
| 57 | trim(uri); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | size_t iComponentStart = 0; |
| 62 | |
| 63 | // Unescape the components. |
| 64 | while (iComponentStart < uri.size()) { |
| 65 | size_t iComponentEnd = uri.find("/", iComponentStart); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 66 | if (iComponentEnd == std::string::npos) |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 67 | iComponentEnd = uri.size(); |
| 68 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 69 | Component component = Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd); |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 70 | // Ignore illegal components. This also gets rid of a trailing '/'. |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 71 | if (!component.empty()) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 72 | append(Component(component)); |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 73 | |
| 74 | iComponentStart = iComponentEnd + 1; |
| 75 | } |
| 76 | } |
| 77 | |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 78 | Name& |
| 79 | Name::append(const Name& name) |
| 80 | { |
| 81 | if (&name == this) |
| 82 | // Copying from this name, so need to make a copy first. |
| 83 | return append(Name(name)); |
| 84 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 85 | for (size_t i = 0; i < name.size(); ++i) |
| 86 | append(name.at(i)); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 87 | |
| 88 | return *this; |
| 89 | } |
| 90 | |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 91 | Name |
| 92 | Name::getSubName(size_t iStartComponent, size_t nComponents) const |
| 93 | { |
| 94 | Name result; |
| 95 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 96 | size_t iEnd = iStartComponent + nComponents; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 97 | for (size_t i = iStartComponent; i < iEnd && i < size(); ++i) |
| 98 | result.append(at(i)); |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 99 | |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | Name |
| 104 | Name::getSubName(size_t iStartComponent) const |
| 105 | { |
| 106 | Name result; |
| 107 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 108 | for (size_t i = iStartComponent; i < size(); ++i) |
| 109 | result.append(at(i)); |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 110 | |
| 111 | return result; |
| 112 | } |
| 113 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 114 | bool |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 115 | Name::equals(const Name& name) const |
| 116 | { |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 117 | if (size() != name.size()) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 118 | return false; |
| 119 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 120 | for (size_t i = 0; i < size(); ++i) { |
| 121 | if (at(i) != name.at(i)) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 125 | return true; |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | bool |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 129 | Name::isPrefixOf(const Name& name) const |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 130 | { |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 131 | // This name is longer than the name we are checking it against. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 132 | if (size() > name.size()) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 133 | return false; |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 134 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 135 | // Check if at least one of given components doesn't match. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 136 | for (size_t i = 0; i < size(); ++i) { |
| 137 | if (at(i) != name.at(i)) |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 141 | return true; |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 144 | |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 145 | int |
| 146 | Name::compare(const Name& other) const |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 147 | { |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 148 | for (size_t i = 0; i < size() && i < other.size(); ++i) { |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 149 | int comparison = at(i).compare(other.at(i)); |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 150 | if (comparison == 0) |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 151 | // The components at this index are equal, so check the next components. |
| 152 | continue; |
| 153 | |
| 154 | // Otherwise, the result is based on the components at this index. |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 155 | return comparison; |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 158 | // The components up to min(this.size(), other.size()) are equal, so the shorter name is less. |
| 159 | if (size() < other.size()) |
| 160 | return -1; |
| 161 | else if (size() > other.size()) |
| 162 | return 1; |
| 163 | else |
| 164 | return 0; |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 167 | std::ostream& |
| 168 | operator << (std::ostream& os, const Name& name) |
| 169 | { |
| 170 | if (name.empty()) |
| 171 | { |
| 172 | os << "/"; |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | for (Name::const_iterator i = name.begin(); i != name.end(); i++) { |
| 177 | os << "/"; |
| 178 | i->toEscapedString(os); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return os; |
| 183 | } |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 184 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 185 | } // namespace ndn |