aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbpkg/package-name.cxx94
-rw-r--r--libbpkg/package-name.hxx196
2 files changed, 2 insertions, 288 deletions
diff --git a/libbpkg/package-name.cxx b/libbpkg/package-name.cxx
deleted file mode 100644
index 9a3e787..0000000
--- a/libbpkg/package-name.cxx
+++ /dev/null
@@ -1,94 +0,0 @@
-// file : libbpkg/package-name.cxx -*- C++ -*-
-// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
-// license : MIT; see accompanying LICENSE file
-
-#include <libbpkg/package-name.hxx>
-
-#include <string>
-#include <vector>
-#include <utility> // move()
-#include <iterator> // back_inserter
-#include <algorithm> // find(), transform()
-#include <stdexcept> // invalid_argument
-
-#include <libbutl/path.mxx> // path::traits
-#include <libbutl/utility.mxx> // alpha(), alnum()
-
-using namespace std;
-using namespace butl;
-
-namespace bpkg
-{
- // package_name
- //
- static const vector<string> illegal_pkg_names ({
- "build",
- "con", "prn", "aux", "nul",
- "com1", "com2", "com3", "com4", "com5", "com6", "com7", "com8", "com9",
- "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9"});
-
- static const string legal_pkg_chars ("_+-.");
-
- package_name::
- package_name (std::string&& nm)
- {
- if (nm.size () < 2)
- throw invalid_argument ("length is less than two characters");
-
- if (find (illegal_pkg_names.begin (), illegal_pkg_names.end (), nm) !=
- illegal_pkg_names.end ())
- throw invalid_argument ("illegal name");
-
- if (!alpha (nm.front ()))
- throw invalid_argument ("illegal first character (must be alphabetic)");
-
- // Here we rely on the fact that the name length >= 2.
- //
- for (auto i (nm.cbegin () + 1), e (nm.cend () - 1); i != e; ++i)
- {
- char c (*i);
-
- if (!(alnum(c) || legal_pkg_chars.find (c) != string::npos))
- throw invalid_argument ("illegal character");
- }
-
- if (!alnum (nm.back ()) && nm.back () != '+')
- throw invalid_argument (
- "illegal last character (must be alphabetic, digit, or plus)");
-
- 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 ecc9f3b..f478be3 100644
--- a/libbpkg/package-name.hxx
+++ b/libbpkg/package-name.hxx
@@ -5,206 +5,14 @@
#ifndef LIBBPKG_PACKAGE_NAME_HXX
#define LIBBPKG_PACKAGE_NAME_HXX
-#include <string>
-#include <utility> // move()
-#include <ostream>
-
-#include <libbutl/utility.mxx> // casecmp()
+#include <libbutl/project-name.mxx>
#include <libbpkg/export.hxx>
#include <libbpkg/version.hxx>
namespace bpkg
{
- class LIBBPKG_EXPORT package_name
- {
- public:
- // Create package name from string verifying that it complied with the
- // specification and throwing std::invalid_argument if that's not the
- // case. Note that in this case the passed value is guaranteed to be
- // unchanged.
- //
- explicit
- package_name (const std::string& s): package_name (std::string (s)) {}
-
- explicit
- package_name (std::string&&);
-
- // Create a special empty package name.
- //
- package_name () = default;
-
- // Create an arbitrary string that can be used in contexts that expect
- // a package name. For example, a package name pattern for use in ODB query
- // expressions.
- //
- enum raw_string_type {raw_string};
- package_name (std::string s, raw_string_type): value_ (std::move (s)) {}
-
- bool
- empty () const noexcept {return value_.empty ();}
-
- const std::string&
- string () const& noexcept {return value_;}
-
- // Moves the underlying package name string out of the package name object.
- // The object becomes empty. Usage: std::move (name).string ().
- //
- 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.
- //
- int compare (const package_name& n) const {return compare (n.value_);}
- int compare (const std::string& n) const {return compare (n.c_str ());}
- int compare (const char* n) const {return butl::casecmp (value_, n);}
-
- private:
- std::string value_;
- };
-
- inline bool
- operator< (const package_name& x, const package_name& y)
- {
- return x.compare (y) < 0;
- }
-
- inline bool
- operator> (const package_name& x, const package_name& y)
- {
- return x.compare (y) > 0;
- }
-
- inline bool
- operator== (const package_name& x, const package_name& y)
- {
- return x.compare (y) == 0;
- }
-
- inline bool
- operator<= (const package_name& x, const package_name& y)
- {
- return x.compare (y) <= 0;
- }
-
- inline bool
- operator>= (const package_name& x, const package_name& y)
- {
- return x.compare (y) >= 0;
- }
-
- inline bool
- operator!= (const package_name& x, const package_name& y)
- {
- return x.compare (y) != 0;
- }
-
- template <typename T>
- inline auto
- operator< (const package_name& x, const T& y)
- {
- return x.compare (y) < 0;
- }
-
- template <typename T>
- inline auto
- operator> (const package_name& x, const T& y)
- {
- return x.compare (y) > 0;
- }
-
- template <typename T>
- inline auto
- operator== (const package_name& x, const T& y)
- {
- return x.compare (y) == 0;
- }
-
- template <typename T>
- inline auto
- operator<= (const package_name& x, const T& y)
- {
- return x.compare (y) <= 0;
- }
-
- template <typename T>
- inline auto
- operator>= (const package_name& x, const T& y)
- {
- return x.compare (y) >= 0;
- }
-
- template <typename T>
- inline auto
- operator!= (const package_name& x, const T& y)
- {
- return x.compare (y) != 0;
- }
-
- template <typename T>
- inline auto
- operator< (const T& x, const package_name& y)
- {
- return y > x;
- }
-
- template <typename T>
- inline auto
- operator> (const T& x, const package_name& y)
- {
- return y < x;
- }
-
- template <typename T>
- inline auto
- operator== (const T& x, const package_name& y)
- {
- return y == x;
- }
-
- template <typename T>
- inline auto
- operator<= (const T& x, const package_name& y)
- {
- return y >= x;
- }
-
- template <typename T>
- inline auto
- operator>= (const T& x, const package_name& y)
- {
- return y <= x;
- }
-
- template <typename T>
- inline auto
- operator!= (const T& x, const package_name& y)
- {
- return y != x;
- }
-
- inline std::ostream&
- operator<< (std::ostream& os, const package_name& v)
- {
- return os << v.string ();
- }
+ using package_name = butl::project_name;
}
#endif // LIBBPKG_PACKAGE_NAME_HXX