Canonicalize a package name per PEP 503: lowercase, and treat runs of '-', '_', '.' as a single '-'. Used to match resolved package names against the original requirement specifiers.
| 47 | // '-', '_', '.' as a single '-'. Used to match resolved package names |
| 48 | // against the original requirement specifiers. |
| 49 | std::string CanonicalizePackageName(const std::string& name) |
| 50 | { |
| 51 | std::string result; |
| 52 | result.reserve(name.size()); |
| 53 | |
| 54 | for (char c : name) |
| 55 | { |
| 56 | if (c == '_' || c == '.' || c == '-') |
| 57 | { |
| 58 | if (!result.empty() && result.back() != '-') |
| 59 | result.push_back('-'); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | result.push_back(static_cast<char>(std::tolower(static_cast<unsigned char>(c)))); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | while (!result.empty() && result.back() == '-') |
| 68 | result.pop_back(); |
| 69 | |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | // Extract the package-name prefix of a requirement specifier. |
| 74 | // Stops at the first character that is not part of a PEP 508 distribution name. |
no test coverage detected