File: cmCMakeString.hxx

package info (click to toggle)
cmake 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 158,704 kB
  • sloc: ansic: 406,077; cpp: 309,512; sh: 4,233; python: 3,696; yacc: 3,109; lex: 1,279; f90: 538; asm: 471; lisp: 375; java: 310; cs: 270; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 110; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22; sed: 2
file content (259 lines) | stat: -rw-r--r-- 6,361 bytes parent folder | download | duplicates (2)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */

#pragma once

#include "cmConfigure.h" // IWYU pragma: keep

#include <cstddef>
#include <string>
#include <utility>
#include <vector>

#include <cm/string_view>

#include "cmList.h"
#include "cmRange.h"
#include "cmString.hxx"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmValue.h"

class cmMakefile;

//
// Class offering various string operations which is used by
// * CMake string() command
// * $<STRING> generator expression
//

namespace cm {

class CMakeString
{
public:
  using size_type = cm::String::size_type;
  static auto const npos = cm::String::npos;

  using string_range = cmRange<std::vector<std::string>::const_iterator>;

  CMakeString(std::string const& str)
    : String_(cm::String::borrow(str))
  {
  }
  CMakeString(cm::string_view str)
    : String_(cm::String::borrow(str))
  {
  }
  CMakeString(cm::String const& str)
    : String_(str)
  {
  }
  CMakeString(cm::String&& str)
    : String_(std::move(str))
  {
  }
  CMakeString(cmValue value)
    : String_(cm::String::borrow(*value))
  {
  }
  CMakeString(string_range range,
              cm::string_view separator = cm::string_view{})
    : String_(cmJoin(range, separator))
  {
  }

  CMakeString() = default;
  CMakeString(CMakeString const&) = default;
  CMakeString(CMakeString&&) = default;

  CMakeString& operator=(CMakeString const& string)
  {
    if (this != &string) {
      this->String_ = string.String_;
    }
    return *this;
  }
  CMakeString& operator=(CMakeString&& string) noexcept
  {
    if (this != &string) {
      this->String_ = std::move(string.String_);
    }
    return *this;
  }
  CMakeString& operator=(cm::string_view string)
  {
    this->String_ = string;
    return *this;
  }

  // conversions
  string_view view() const noexcept { return this->String_.view(); }
  operator cm::string_view() noexcept { return this->String_.view(); }
  operator std::string const&() { return this->String_.str(); }

  size_type Size() const { return this->String_.size(); }
  size_type Length() const { return this->String_.size(); }

  enum class CompOperator
  {
    EQUAL,
    LESS,
    LESS_EQUAL,
    GREATER,
    GREATER_EQUAL
  };
  bool Compare(CompOperator op, cm::string_view other);

  enum class FindFrom
  {
    Begin,
    End
  };
  size_type Find(cm::string_view substring,
                 FindFrom from = FindFrom::Begin) const
  {
    return from == FindFrom::Begin ? this->String_.find(substring)
                                   : this->String_.rfind(substring);
  }

  enum class Regex
  {
    No,
    Yes
  };
  // Throw std::invalid_argument if regular expression is invalid
  //       std::runtime_error if replacement failed
  CMakeString& Replace(std::string const& matchExpression,
                       std::string const& replaceExpression,
                       Regex regex = Regex::No,
                       cmMakefile* makefile = nullptr);

  enum class MatchItems
  {
    Once,
    All
  };
  // Throw std::invalid_argument if regular expression is invalid
  cmList Match(std::string const& matchExpression,
               MatchItems matchItems = MatchItems::Once,
               cmMakefile* makefile = nullptr) const;

  CMakeString& Append(cm::string_view str)
  {
    this->String_.append(str);
    return *this;
  }
  CMakeString& Append(string_range range)
  {
    this->Append(cmJoin(range, {}));
    return *this;
  }
  CMakeString& Prepend(cm::string_view str)
  {
    this->String_.insert(0, str);
    return *this;
  }
  CMakeString& Prepend(string_range range)
  {
    this->Prepend(cmJoin(range, {}));
    return *this;
  }

  CMakeString& ToLower()
  {
    this->String_ = cmSystemTools::LowerCase(this->String_);
    return *this;
  }
  CMakeString& ToLower(cm::string_view str)
  {
    this->String_ = cmSystemTools::LowerCase(str);
    return *this;
  }
  CMakeString& ToUpper()
  {
    this->String_ = cmSystemTools::UpperCase(this->String_);
    return *this;
  }
  CMakeString& ToUpper(cm::string_view str)
  {
    this->String_ = cmSystemTools::UpperCase(str);
    return *this;
  }

  // Throw std::out_of_range if pos or count are outside of the expected range
  CMakeString Substring(long pos = 0, long count = -1) const;

  enum class StripItems
  {
    Space,
    Genex
  };
  CMakeString& Strip(StripItems stripItems = StripItems::Space);

  // Throw std::runtime_error  if quoting string failed
  enum class QuoteItems
  {
    Regex
  };
  CMakeString& Quote(QuoteItems quoteItems = QuoteItems::Regex);

  CMakeString& Repeat(size_type count);

  // Throw std::invalid_argument if the hash algorithm is invalid
  CMakeString& Hash(cm::string_view hashAlgorithm);

  // Throw std::invalid_argument if one of the codes is invalid
  CMakeString& FromASCII(string_range codes);
  CMakeString& ToHexadecimal() { return this->ToHexadecimal(this->String_); }
  CMakeString& ToHexadecimal(cm::string_view str);

  CMakeString& MakeCIdentifier()
  {
    this->String_ = cmSystemTools::MakeCidentifier(this->String_.str());
    return *this;
  }
  CMakeString& MakeCIdentifier(std::string const& str)
  {
    this->String_ = cmSystemTools::MakeCidentifier(str);
    return *this;
  }

  static cm::string_view const RandomDefaultAlphabet;
  // Throw std::invalid_argument if the alphabet is invalid
  //       std::out_of_range if length is outside valid range
  CMakeString& Random(std::size_t length = 5,
                      cm::string_view alphabet = RandomDefaultAlphabet)
  {
    return this->Random(cmSystemTools::RandomSeed(), length, alphabet);
  }
  CMakeString& Random(unsigned int seed, std::size_t length = 5,
                      cm::string_view alphabet = RandomDefaultAlphabet);

  enum class UTC
  {
    No,
    Yes
  };
  CMakeString& Timestamp(cm::string_view format, UTC utc = UTC::No);

  enum class UUIDType
  {
    MD5,
    SHA1
  };
  enum class Case
  {
    Lower,
    Upper
  };
  // Throw std::invalid_argument if namespace or the type are invalid
  //       std::runtime_error if the UUID cannot be generated
  CMakeString& UUID(cm::string_view nameSpace, cm::string_view name,
                    UUIDType type, Case uuidCase = Case::Lower);

private:
  static bool Seeded;
  cm::String String_;
};
}