aboutsummaryrefslogtreecommitdiff
path: root/libbutl/project-name.mxx
blob: 1117e2868f1f91c1295c912dabe5050dc0bba647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// file      : libbutl/project-name.mxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#pragma once
#endif

// C includes.

#ifndef __cpp_lib_modules_ts
#include <string>
#include <utility> // move()
#include <ostream>
#endif

// Other includes.

#ifdef __cpp_modules_ts
export module butl.project_name;
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.utility; // icasecmp(), sanitize_identifier()
#else
#include <libbutl/utility.mxx>
#endif

#include <libbutl/export.hxx>

LIBBUTL_MODEXPORT namespace butl
{
  // Build system project name.
  //
  // Since a build system project is often a package, it is also used as a
  // package name by the package dependency manager. And since a package is
  // often a project (in the "collection of related packages" sense), it is
  // also used as a project name by the project dependency manager.
  //
  class LIBBUTL_SYMEXPORT project_name
  {
  public:
    // Create project name from string verifying that it complies with the
    // specification and throw std::invalid_argument if that's not the
    // case. Note that in this case the passed value is guaranteed to be
    // unchanged.
    //
    explicit
    project_name (const std::string& s): project_name (std::string (s)) {}

    explicit
    project_name (std::string&&);

    // Create a special empty project name.
    //
    project_name () {} // For Clang 3.8 (const initialization).

    // Create an arbitrary string that can be used in contexts that expect a
    // project name. For example, a project name pattern for use in ODB query
    // expressions.
    //
    enum raw_string_type {raw_string};
    project_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 project name string out of the project name object.
    // The object becomes empty. Usage: std::move (name).string ().
    //
    std::string
    string () && {std::string r; r.swap (this->value_); return r;}

    // Project 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.
    //
    // If the ext argument is not NULL, then only remove the specified
    // extension. Note that the extension should not include the dot and the
    // comparison is always case-insensitive.
    //
    std::string
    base (const char* ext = nullptr) const;

    std::string
    extension () const;

    // Project name sanitized to a canonical variable name. Specifically,
    // '.', '-', and '+' are replaced with '_'.
    //
    std::string
    variable () const {return sanitize_identifier (value_);}

    // Compare ignoring case. Note that a string is not checked to be a valid
    // project name.
    //
    int compare (const project_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 icasecmp (value_, n);}

  private:
    std::string value_;
  };

  inline bool
  operator< (const project_name& x, const project_name& y)
  {
    return x.compare (y) < 0;
  }

  inline bool
  operator> (const project_name& x, const project_name& y)
  {
    return x.compare (y) > 0;
  }

  inline bool
  operator== (const project_name& x, const project_name& y)
  {
    return x.compare (y) == 0;
  }

  inline bool
  operator<= (const project_name& x, const project_name& y)
  {
    return x.compare (y) <= 0;
  }

  inline bool
  operator>= (const project_name& x, const project_name& y)
  {
    return x.compare (y) >= 0;
  }

  inline bool
  operator!= (const project_name& x, const project_name& y)
  {
    return x.compare (y) != 0;
  }

  template <typename T>
  inline auto
  operator< (const project_name& x, const T& y)
  {
    return x.compare (y) < 0;
  }

  template <typename T>
  inline auto
  operator> (const project_name& x, const T& y)
  {
    return x.compare (y) > 0;
  }

  template <typename T>
  inline auto
  operator== (const project_name& x, const T& y)
  {
    return x.compare (y) == 0;
  }

  template <typename T>
  inline auto
  operator<= (const project_name& x, const T& y)
  {
    return x.compare (y) <= 0;
  }

  template <typename T>
  inline auto
  operator>= (const project_name& x, const T& y)
  {
    return x.compare (y) >= 0;
  }

  template <typename T>
  inline auto
  operator!= (const project_name& x, const T& y)
  {
    return x.compare (y) != 0;
  }

  template <typename T>
  inline auto
  operator< (const T& x, const project_name& y)
  {
    return y > x;
  }

  template <typename T>
  inline auto
  operator> (const T& x, const project_name& y)
  {
    return y < x;
  }

  template <typename T>
  inline auto
  operator== (const T& x, const project_name& y)
  {
    return y == x;
  }

  template <typename T>
  inline auto
  operator<= (const T& x, const project_name& y)
  {
    return y >= x;
  }

  template <typename T>
  inline auto
  operator>= (const T& x, const project_name& y)
  {
    return y <= x;
  }

  template <typename T>
  inline auto
  operator!= (const T& x, const project_name& y)
  {
    return y != x;
  }

  inline std::ostream&
  operator<< (std::ostream& os, const project_name& v)
  {
    return os << v.string ();
  }
}