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 ")"); |
| 12 | static const std::regex kRE3(RE_PATH); |
| 13 | |
| 14 | namespace android { |
| 15 | |
| 16 | FQName::FQName(const std::string &s) |
| 17 | : mValid(false) { |
| 18 | setTo(s); |
| 19 | } |
| 20 | |
| 21 | bool FQName::isValid() const { |
| 22 | return mValid; |
| 23 | } |
| 24 | |
| 25 | bool FQName::setTo(const std::string &s) { |
| 26 | mPackage.clear(); |
| 27 | mVersion.clear(); |
| 28 | mName.clear(); |
| 29 | |
| 30 | mValid = true; |
| 31 | |
| 32 | std::smatch match; |
| 33 | if (std::regex_match(s, match, kRE1)) { |
| 34 | CHECK_EQ(match.size(), 6u); |
| 35 | |
| 36 | mPackage = match.str(1); |
| 37 | mVersion = match.str(3); |
| 38 | mName = match.str(4); |
| 39 | } else if (std::regex_match(s, match, kRE2)) { |
| 40 | CHECK_EQ(match.size(), 4u); |
| 41 | |
| 42 | mVersion = match.str(1); |
| 43 | mName = match.str(2); |
| 44 | } else if (std::regex_match(s, match, kRE3)) { |
| 45 | mName = match.str(0); |
| 46 | } else { |
| 47 | mValid = false; |
| 48 | } |
| 49 | |
| 50 | return isValid(); |
| 51 | } |
| 52 | |
| 53 | std::string FQName::package() const { |
| 54 | return mPackage; |
| 55 | } |
| 56 | |
| 57 | std::string FQName::version() const { |
| 58 | return mVersion; |
| 59 | } |
| 60 | |
| 61 | std::string FQName::name() const { |
| 62 | return mName; |
| 63 | } |
| 64 | |
| 65 | void FQName::applyDefaults( |
| 66 | const std::string &defaultPackage, |
| 67 | const std::string &defaultVersion) { |
| 68 | if (mPackage.empty()) { |
| 69 | mPackage = defaultPackage; |
| 70 | } |
| 71 | |
| 72 | if (mVersion.empty()) { |
| 73 | mVersion = defaultVersion; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | std::string FQName::debugString() const { |
| 78 | CHECK(mValid); |
| 79 | |
| 80 | std::string out = "FQName("; |
| 81 | out.append(mPackage); |
| 82 | out.append(mVersion); |
| 83 | if (!mPackage.empty() || !mVersion.empty()) { |
| 84 | out.append("::"); |
| 85 | } |
| 86 | out.append(mName); |
| 87 | out.append(")"); |
| 88 | |
| 89 | return out; |
| 90 | } |
| 91 | |
| 92 | } // namespace android |
| 93 | |