From 0592d58e5366eac92bc99ef9bdb4368e07c0ddf9 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 4 Aug 2021 20:31:30 +0300 Subject: Add extract_package_name() and extract_package_version() --- libbpkg/manifest.cxx | 37 +++++++++++++++++++++++++++++++++++++ libbpkg/manifest.hxx | 30 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/libbpkg/manifest.cxx b/libbpkg/manifest.cxx index 210aa3b..d4c3f9d 100644 --- a/libbpkg/manifest.cxx +++ b/libbpkg/manifest.cxx @@ -4573,4 +4573,41 @@ namespace bpkg s.next ("", ""); // End of manifest. } + + // extract_package_*() + // + package_name + extract_package_name (const char* s, bool allow_version) + { + if (!allow_version) + return package_name (s); + + // Calculate the package name length as a length of the prefix that + // doesn't contain spaces, slashes and the version constraint starting + // characters. Note that none of them are valid package name characters. + // + size_t n (strcspn (s, " /=<>([~^")); + return package_name (string (s, n)); + } + + version + extract_package_version (const char* s, bool fold_zero_revision) + { + using traits = string::traits_type; + + if (const char* p = traits::find (s, traits::length (s), '/')) + { + version r (p + 1, fold_zero_revision); + + if (r.release && r.release->empty ()) + throw invalid_argument ("earliest version"); + + if (r.compare (stub_version, true /* ignore_revision */) == 0) + throw invalid_argument ("stub version"); + + return r; + } + + return version (); + } } diff --git a/libbpkg/manifest.hxx b/libbpkg/manifest.hxx index b666716..bdb7a9b 100644 --- a/libbpkg/manifest.hxx +++ b/libbpkg/manifest.hxx @@ -1568,6 +1568,36 @@ namespace bpkg butl::manifest_name_value start, bool ignore_unknown); }; + + // Extract the package name component from [/] or + // . Throw invalid_argument on parsing error. + // + // Note: the version and version constraint are not verified. + // + LIBBPKG_EXPORT package_name + extract_package_name (const char*, bool allow_version = true); + + inline package_name + extract_package_name (const std::string& s, bool allow_version = true) + { + return extract_package_name (s.c_str (), allow_version); + } + + // Extract the package version component from [/]. Return + // empty version if none is specified. Throw invalid_argument on parsing + // error and for the earliest and stub versions. + // + // Note: the package name is not verified. + // + LIBBPKG_EXPORT version + extract_package_version (const char*, bool fold_zero_revision = true); + + inline version + extract_package_version (const std::string& s, + bool fold_zero_revision = true) + { + return extract_package_version (s.c_str (), fold_zero_revision); + } } #endif // LIBBPKG_MANIFEST_HXX -- cgit v1.1