Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 1 | #include "FQName.h" |
| 2 | |
| 3 | #include <android-base/logging.h> |
| 4 | #include <regex> |
| 5 | |
| 6 | #define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*" |
| 7 | #define RE_PATH RE_COMPONENT "([.]" RE_COMPONENT ")*" |
| 8 | #define RE_VERSION "@[0-9]+[.][0-9]+" |
| 9 | |
| 10 | static const std::regex kRE1("(" RE_PATH ")(" RE_VERSION ")?::(" RE_PATH ")"); |
| 11 | static const std::regex kRE2("(" RE_VERSION ")::(" RE_PATH ")"); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 12 | static const std::regex kRE3("(" RE_PATH ")(" RE_VERSION ")"); |
| 13 | static const std::regex kRE4(RE_PATH); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 14 | |
| 15 | namespace android { |
| 16 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 17 | FQName::FQName() |
| 18 | : mValid(false) { |
| 19 | } |
| 20 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 21 | FQName::FQName(const std::string &s) |
| 22 | : mValid(false) { |
| 23 | setTo(s); |
| 24 | } |
| 25 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame^] | 26 | FQName::FQName( |
| 27 | const std::string &package, |
| 28 | const std::string &version, |
| 29 | const std::string &name) |
| 30 | : mValid(true), |
| 31 | mPackage(package), |
| 32 | mVersion(version), |
| 33 | mName(name) { |
| 34 | } |
| 35 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 36 | bool FQName::isValid() const { |
| 37 | return mValid; |
| 38 | } |
| 39 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame^] | 40 | bool FQName::isFullyQualified() const { |
| 41 | return !mPackage.empty() && !mVersion.empty() && !mName.empty(); |
| 42 | } |
| 43 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 44 | bool FQName::setTo(const std::string &s) { |
| 45 | mPackage.clear(); |
| 46 | mVersion.clear(); |
| 47 | mName.clear(); |
| 48 | |
| 49 | mValid = true; |
| 50 | |
| 51 | std::smatch match; |
| 52 | if (std::regex_match(s, match, kRE1)) { |
| 53 | CHECK_EQ(match.size(), 6u); |
| 54 | |
| 55 | mPackage = match.str(1); |
| 56 | mVersion = match.str(3); |
| 57 | mName = match.str(4); |
| 58 | } else if (std::regex_match(s, match, kRE2)) { |
| 59 | CHECK_EQ(match.size(), 4u); |
| 60 | |
| 61 | mVersion = match.str(1); |
| 62 | mName = match.str(2); |
| 63 | } else if (std::regex_match(s, match, kRE3)) { |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 64 | CHECK_EQ(match.size(), 4u); |
| 65 | |
| 66 | mPackage = match.str(1); |
| 67 | mVersion = match.str(3); |
| 68 | } else if (std::regex_match(s, match, kRE4)) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 69 | mName = match.str(0); |
| 70 | } else { |
| 71 | mValid = false; |
| 72 | } |
| 73 | |
| 74 | return isValid(); |
| 75 | } |
| 76 | |
| 77 | std::string FQName::package() const { |
| 78 | return mPackage; |
| 79 | } |
| 80 | |
| 81 | std::string FQName::version() const { |
| 82 | return mVersion; |
| 83 | } |
| 84 | |
| 85 | std::string FQName::name() const { |
| 86 | return mName; |
| 87 | } |
| 88 | |
| 89 | void FQName::applyDefaults( |
| 90 | const std::string &defaultPackage, |
| 91 | const std::string &defaultVersion) { |
| 92 | if (mPackage.empty()) { |
| 93 | mPackage = defaultPackage; |
| 94 | } |
| 95 | |
| 96 | if (mVersion.empty()) { |
| 97 | mVersion = defaultVersion; |
| 98 | } |
| 99 | } |
| 100 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame^] | 101 | std::string FQName::string() const { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 102 | CHECK(mValid); |
| 103 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 104 | std::string out; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 105 | out.append(mPackage); |
| 106 | out.append(mVersion); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 107 | if (!mName.empty()) { |
| 108 | if (!mPackage.empty() || !mVersion.empty()) { |
| 109 | out.append("::"); |
| 110 | } |
| 111 | out.append(mName); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 112 | } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 113 | |
| 114 | return out; |
| 115 | } |
| 116 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 117 | void FQName::print() const { |
| 118 | if (!mValid) { |
| 119 | LOG(INFO) << "INVALID"; |
| 120 | return; |
| 121 | } |
| 122 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame^] | 123 | LOG(INFO) << string(); |
| 124 | } |
| 125 | |
| 126 | bool FQName::operator<(const FQName &other) const { |
| 127 | return string() < other.string(); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 130 | } // namespace android |
| 131 | |