From 190fb7a0ef8a28193a89c66d9d1fd84340ff99bd Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 25 Jul 2018 12:57:03 +0200 Subject: Add package_name::{base,extension,variable}() --- libbpkg/package-name.cxx | 37 ++++++++++++++++++++++++++++++++++++- libbpkg/package-name.hxx | 16 ++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/libbpkg/package-name.cxx b/libbpkg/package-name.cxx index 7a42ee3..9a3e787 100644 --- a/libbpkg/package-name.cxx +++ b/libbpkg/package-name.cxx @@ -7,9 +7,11 @@ #include #include #include // move() -#include // find() +#include // back_inserter +#include // find(), transform() #include // invalid_argument +#include // path::traits #include // alpha(), alnum() using namespace std; @@ -56,4 +58,37 @@ namespace bpkg value_ = move (nm); } + + string package_name:: + base () const + { + using std::string; + + size_t p (path::traits::find_extension (value_)); + return string (value_, 0, p); + } + + string package_name:: + extension () const + { + using std::string; + + size_t p (path::traits::find_extension (value_)); + return p != string::npos ? string (value_, p + 1) : string (); + } + + string package_name:: + variable () const + { + using std::string; + + auto sanitize = [] (char c) + { + return (c == '-' || c == '+' || c == '.') ? '_' : c; + }; + + string r; + transform (value_.begin (), value_.end (), back_inserter (r), sanitize); + return r; + } } diff --git a/libbpkg/package-name.hxx b/libbpkg/package-name.hxx index 6c7ec6c..ecc9f3b 100644 --- a/libbpkg/package-name.hxx +++ b/libbpkg/package-name.hxx @@ -53,6 +53,22 @@ namespace bpkg std::string string () && {std::string r; r.swap (this->value_); return r;} + // Package name base and extension (without the dot). If there is no + // extension, then the base name is the same as the full name and the + // returned extension is empty. + // + std::string + base () const; + + std::string + extension () const; + + // Package name sanitized to a canonical variable name. Specifically, + // '.', '-', and '+' are replaced with '_'. + // + std::string + variable () const; + // Compare ignoring case. Note that a string is not checked to be a valid // package name. // -- cgit v1.1