Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 17 | #include "FQName.h" |
| 18 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 19 | #include <hidl-util/StringHelper.h> |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 20 | #include <android-base/logging.h> |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 21 | #include <iostream> |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 22 | #include <regex> |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 23 | #include <sstream> |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 24 | |
| 25 | #define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*" |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 26 | #define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*" |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 27 | #define RE_VERSION "@[0-9]+[.][0-9]+" |
| 28 | |
| 29 | static const std::regex kRE1("(" RE_PATH ")(" RE_VERSION ")?::(" RE_PATH ")"); |
| 30 | static const std::regex kRE2("(" RE_VERSION ")::(" RE_PATH ")"); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 31 | static const std::regex kRE3("(" RE_PATH ")(" RE_VERSION ")"); |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 32 | static const std::regex kRE4("(" RE_COMPONENT ")([.]" RE_COMPONENT ")+"); |
| 33 | static const std::regex kRE5("(" RE_COMPONENT ")"); |
| 34 | |
| 35 | static const std::regex kRE6("(" RE_PATH ")(" RE_VERSION ")?::(" RE_PATH "):(" RE_COMPONENT ")"); |
| 36 | static const std::regex kRE7("(" RE_VERSION ")::(" RE_PATH "):(" RE_COMPONENT ")"); |
| 37 | static const std::regex kRE8("(" RE_PATH "):(" RE_COMPONENT ")"); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 41 | FQName::FQName() |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 42 | : mValid(false), |
| 43 | mIsIdentifier(false) { |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 46 | FQName::FQName(const std::string &s) |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 47 | : mValid(false), |
| 48 | mIsIdentifier(false) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 49 | setTo(s); |
| 50 | } |
| 51 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 52 | FQName::FQName( |
| 53 | const std::string &package, |
| 54 | const std::string &version, |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 55 | const std::string &name, |
| 56 | const std::string &valueName) |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 57 | : mValid(true), |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 58 | mIsIdentifier(false), |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 59 | mPackage(package), |
| 60 | mVersion(version), |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 61 | mName(name), |
| 62 | mValueName(valueName) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 65 | FQName::FQName(const FQName& other) |
| 66 | : mValid(other.mValid), |
| 67 | mIsIdentifier(other.mIsIdentifier), |
| 68 | mPackage(other.mPackage), |
| 69 | mVersion(other.mVersion), |
| 70 | mName(other.mName), |
| 71 | mValueName(other.mValueName) { |
| 72 | } |
| 73 | |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 74 | FQName::FQName(const std::vector<std::string> &names) |
| 75 | : mValid(false), |
| 76 | mIsIdentifier(false) { |
| 77 | setTo(StringHelper::JoinStrings(names, ".")); |
| 78 | } |
| 79 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 80 | bool FQName::isValid() const { |
| 81 | return mValid; |
| 82 | } |
| 83 | |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 84 | bool FQName::isIdentifier() const { |
| 85 | return mIsIdentifier; |
| 86 | } |
| 87 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 88 | bool FQName::isFullyQualified() const { |
| 89 | return !mPackage.empty() && !mVersion.empty() && !mName.empty(); |
| 90 | } |
| 91 | |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 92 | bool FQName::isValidValueName() const { |
| 93 | return mIsIdentifier |
| 94 | || (!mName.empty() && !mValueName.empty()); |
| 95 | } |
| 96 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 97 | bool FQName::setTo(const std::string &s) { |
| 98 | mPackage.clear(); |
| 99 | mVersion.clear(); |
| 100 | mName.clear(); |
| 101 | |
| 102 | mValid = true; |
| 103 | |
| 104 | std::smatch match; |
| 105 | if (std::regex_match(s, match, kRE1)) { |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 106 | CHECK_EQ(match.size(), 4u); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 107 | |
| 108 | mPackage = match.str(1); |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 109 | mVersion = match.str(2); |
| 110 | mName = match.str(3); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 111 | } else if (std::regex_match(s, match, kRE2)) { |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 112 | CHECK_EQ(match.size(), 3u); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 113 | |
| 114 | mVersion = match.str(1); |
| 115 | mName = match.str(2); |
| 116 | } else if (std::regex_match(s, match, kRE3)) { |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 117 | CHECK_EQ(match.size(), 3u); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 118 | |
| 119 | mPackage = match.str(1); |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 120 | mVersion = match.str(2); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 121 | } else if (std::regex_match(s, match, kRE4)) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 122 | mName = match.str(0); |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 123 | } else if (std::regex_match(s, match, kRE5)) { |
| 124 | mIsIdentifier = true; |
| 125 | mName = match.str(0); |
| 126 | } else if (std::regex_match(s, match, kRE6)) { |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 127 | CHECK_EQ(match.size(), 5u); |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 128 | |
| 129 | mPackage = match.str(1); |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 130 | mVersion = match.str(2); |
| 131 | mName = match.str(3); |
| 132 | mValueName = match.str(4); |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 133 | } else if (std::regex_match(s, match, kRE7)) { |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 134 | CHECK_EQ(match.size(), 4u); |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 135 | |
| 136 | mVersion = match.str(1); |
| 137 | mName = match.str(2); |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 138 | mValueName = match.str(3); |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 139 | } else if (std::regex_match(s, match, kRE8)) { |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 140 | CHECK_EQ(match.size(), 3u); |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 141 | |
| 142 | mName = match.str(1); |
Yifan Hong | 95a47bb | 2016-09-29 10:08:10 -0700 | [diff] [blame] | 143 | mValueName = match.str(2); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 144 | } else { |
| 145 | mValid = false; |
| 146 | } |
| 147 | |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 148 | // mValueName must go with mName. |
| 149 | if (!mValueName.empty()) { |
| 150 | CHECK(!mName.empty()); |
| 151 | } |
| 152 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 153 | return isValid(); |
| 154 | } |
| 155 | |
| 156 | std::string FQName::package() const { |
| 157 | return mPackage; |
| 158 | } |
| 159 | |
| 160 | std::string FQName::version() const { |
| 161 | return mVersion; |
| 162 | } |
| 163 | |
| 164 | std::string FQName::name() const { |
| 165 | return mName; |
| 166 | } |
| 167 | |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 168 | std::vector<std::string> FQName::names() const { |
| 169 | std::vector<std::string> res {}; |
| 170 | std::istringstream ss(name()); |
| 171 | std::string s; |
| 172 | while (std::getline(ss, s, '.')) { |
| 173 | res.push_back(s); |
| 174 | } |
| 175 | return res; |
| 176 | } |
| 177 | |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 178 | std::string FQName::valueName() const { |
| 179 | return mValueName; |
| 180 | } |
| 181 | |
| 182 | FQName FQName::typeName() const { |
| 183 | return FQName(mPackage, mVersion, mName); |
| 184 | } |
| 185 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 186 | void FQName::applyDefaults( |
| 187 | const std::string &defaultPackage, |
| 188 | const std::string &defaultVersion) { |
| 189 | if (mPackage.empty()) { |
| 190 | mPackage = defaultPackage; |
| 191 | } |
| 192 | |
| 193 | if (mVersion.empty()) { |
| 194 | mVersion = defaultVersion; |
| 195 | } |
| 196 | } |
| 197 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 198 | std::string FQName::string() const { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 199 | CHECK(mValid); |
| 200 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 201 | std::string out; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 202 | out.append(mPackage); |
| 203 | out.append(mVersion); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 204 | if (!mName.empty()) { |
| 205 | if (!mPackage.empty() || !mVersion.empty()) { |
| 206 | out.append("::"); |
| 207 | } |
| 208 | out.append(mName); |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 209 | |
| 210 | if (!mValueName.empty()) { |
| 211 | out.append(":"); |
| 212 | out.append(mValueName); |
| 213 | } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 214 | } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 215 | |
| 216 | return out; |
| 217 | } |
| 218 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 219 | void FQName::print() const { |
| 220 | if (!mValid) { |
| 221 | LOG(INFO) << "INVALID"; |
| 222 | return; |
| 223 | } |
| 224 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 225 | LOG(INFO) << string(); |
| 226 | } |
| 227 | |
| 228 | bool FQName::operator<(const FQName &other) const { |
| 229 | return string() < other.string(); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 232 | bool FQName::operator==(const FQName &other) const { |
| 233 | return string() == other.string(); |
| 234 | } |
| 235 | |
Steven Moreland | 197d56c | 2016-09-09 10:03:58 -0700 | [diff] [blame] | 236 | std::string FQName::getInterfaceBaseName() const { |
| 237 | CHECK(names().size() == 1) << "Must be a top level type"; |
| 238 | CHECK(!mName.empty() && mName[0] == 'I') << mName; |
| 239 | |
| 240 | // cut off the leading 'I'. |
| 241 | return mName.substr(1); |
| 242 | } |
| 243 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 244 | FQName FQName::getTypesForPackage() const { |
| 245 | return FQName(package(), version(), "types"); |
| 246 | } |
| 247 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 248 | const FQName FQName::getTopLevelType() const { |
| 249 | auto idx = mName.find('.'); |
| 250 | |
| 251 | if (idx == std::string::npos) { |
| 252 | return *this; |
| 253 | } |
| 254 | |
| 255 | return FQName(mPackage, mVersion, mName.substr(0, idx)); |
| 256 | } |
| 257 | |
| 258 | std::string FQName::tokenName() const { |
| 259 | std::vector<std::string> components; |
| 260 | getPackageAndVersionComponents(&components, true /* cpp_compatible */); |
| 261 | |
| 262 | std::vector<std::string> nameComponents; |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 263 | StringHelper::SplitString(mName, '.', &nameComponents); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 264 | |
| 265 | components.insert(components.end(), nameComponents.begin(), nameComponents.end()); |
| 266 | |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 267 | return StringHelper::JoinStrings(components, "_"); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 270 | std::string FQName::cppNamespace() const { |
| 271 | std::vector<std::string> components; |
| 272 | getPackageAndVersionComponents(&components, true /* cpp_compatible */); |
| 273 | |
| 274 | std::string out = "::"; |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 275 | out += StringHelper::JoinStrings(components, "::"); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 276 | |
| 277 | return out; |
| 278 | } |
| 279 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 280 | std::string FQName::cppLocalName() const { |
| 281 | std::vector<std::string> components; |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 282 | StringHelper::SplitString(mName, '.', &components); |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 283 | |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 284 | return StringHelper::JoinStrings(components, "::") |
| 285 | + (mValueName.empty() ? "" : ("::" + mValueName)); |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 288 | std::string FQName::cppName() const { |
| 289 | std::string out = cppNamespace(); |
| 290 | |
| 291 | std::vector<std::string> components; |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 292 | StringHelper::SplitString(name(), '.', &components); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 293 | out += "::"; |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 294 | out += StringHelper::JoinStrings(components, "::"); |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 295 | if (!mValueName.empty()) { |
| 296 | out += "::" + mValueName; |
| 297 | } |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 298 | |
| 299 | return out; |
| 300 | } |
| 301 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 302 | std::string FQName::javaPackage() const { |
| 303 | std::vector<std::string> components; |
| 304 | getPackageAndVersionComponents(&components, true /* cpp_compatible */); |
| 305 | |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 306 | return StringHelper::JoinStrings(components, "."); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | std::string FQName::javaName() const { |
Yifan Hong | b44a6c8 | 2016-09-22 15:50:18 -0700 | [diff] [blame] | 310 | return javaPackage() + "." + name() |
| 311 | + (mValueName.empty() ? "" : ("." + mValueName)); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 314 | void FQName::getPackageComponents(std::vector<std::string> *components) const { |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 315 | StringHelper::SplitString(package(), '.', components); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void FQName::getPackageAndVersionComponents( |
| 319 | std::vector<std::string> *components, |
| 320 | bool cpp_compatible) const { |
| 321 | getPackageComponents(components); |
| 322 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 323 | if (!cpp_compatible) { |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 324 | components->push_back(getPackageMajorVersion() + |
| 325 | "." + getPackageMinorVersion()); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 329 | // Form "Vmajor_minor". |
| 330 | std::string versionString = "V"; |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 331 | versionString.append(getPackageMajorVersion()); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 332 | versionString.append("_"); |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 333 | versionString.append(getPackageMinorVersion()); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 334 | |
| 335 | components->push_back(versionString); |
| 336 | } |
| 337 | |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 338 | std::string FQName::getPackageMajorVersion() const { |
| 339 | const std::string packageVersion = version(); |
| 340 | CHECK(packageVersion[0] == '@'); |
| 341 | const size_t dotPos = packageVersion.find('.'); |
| 342 | CHECK(dotPos != std::string::npos); |
| 343 | return packageVersion.substr(1, dotPos - 1); |
| 344 | } |
| 345 | |
| 346 | std::string FQName::getPackageMinorVersion() const { |
| 347 | const std::string packageVersion = version(); |
| 348 | CHECK(packageVersion[0] == '@'); |
| 349 | const size_t dotPos = packageVersion.find('.'); |
| 350 | CHECK(dotPos != std::string::npos); |
| 351 | return packageVersion.substr(dotPos + 1); |
| 352 | } |
| 353 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 354 | std::string FQName::getPackageFullVersion() const { |
| 355 | const std::string packageVersion = version(); |
| 356 | CHECK_GT(packageVersion.length(), 1u); |
| 357 | return packageVersion.substr(1); |
| 358 | } |
| 359 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 360 | bool FQName::endsWith(const FQName &other) const { |
| 361 | std::string s1 = string(); |
| 362 | std::string s2 = other.string(); |
| 363 | |
| 364 | size_t pos = s1.rfind(s2); |
| 365 | if (pos == std::string::npos || pos + s2.size() != s1.size()) { |
| 366 | return false; |
| 367 | } |
| 368 | |
Yifan Hong | d9f22f7 | 2016-10-05 15:17:33 -0700 | [diff] [blame] | 369 | // A match is only a match if it is preceded by a "boundary", i.e. |
| 370 | // we perform a component-wise match from the end. |
| 371 | // "az" is not a match for "android.hardware.foo@1.0::IFoo.bar.baz", |
| 372 | // "baz", "bar.baz", "IFoo.bar.baz", "@1.0::IFoo.bar.baz" are. |
| 373 | if (pos == 0) { |
| 374 | // matches "android.hardware.foo@1.0::IFoo.bar.baz" |
| 375 | return true; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Yifan Hong | d9f22f7 | 2016-10-05 15:17:33 -0700 | [diff] [blame] | 378 | if (s1[pos - 1] == '.') { |
| 379 | // matches "baz" and "bar.baz" |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | if (s1[pos - 1] == ':') { |
| 384 | // matches "IFoo.bar.baz" |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | if (s1[pos] == '@') { |
| 389 | // matches "@1.0::IFoo.bar.baz" |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | return false; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 396 | } // namespace android |
| 397 | |