aboutsummaryrefslogtreecommitdiff
path: root/libbutl/utility.ixx
blob: fa37a148b1a36bcdda8605fd8defb42fe8dce765 (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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// file      : libbutl/utility.ixx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_lib_modules_ts
#include <cctype>    // toupper(), tolower(), is*()
#include <cwctype>   // isw*()
#include <cstdlib>   // getenv()
#include <algorithm> // for_each()
#include <stdexcept> // invalid_argument
#endif

namespace butl
{
  inline char
  ucase (char c)
  {
    return std::toupper (c);
  }

  inline void
  ucase (char* s, std::size_t n)
  {
    for (const char* e (s + n); s != e; ++s)
      *s = ucase (*s);
  }

  inline std::string&
  ucase (std::string& s, std::size_t p, std::size_t n)
  {
    if (n == std::string::npos)
      n = s.size () - p;

    if (n != 0)
    {
      s.front () = s.front (); // Force copy in CoW.
      ucase (const_cast<char*> (s.data ()) + p, n);
    }
    return s;
  }

  inline std::string
  ucase (const char* s, std::size_t n)
  {
    std::string r (s, n == std::string::npos ? std::strlen (s) : n);
    return ucase (r);
  }

  inline std::string
  ucase (const std::string& s, std::size_t p, std::size_t n)
  {
    return ucase (s.c_str () + p, n != std::string::npos ? n : s.size () - p);
  }

  inline char
  lcase (char c)
  {
    return std::tolower (c);
  }

  inline void
  lcase (char* s, std::size_t n)
  {
    for (const char* e (s + n); s != e; ++s)
      *s = lcase (*s);
  }

  inline std::string&
  lcase (std::string& s, std::size_t p, std::size_t n)
  {
    if (n == std::string::npos)
      n = s.size () - p;

    if (n != 0)
    {
      s.front () = s.front (); // Force copy in CoW.
      lcase (const_cast<char*> (s.data ()) + p, n);
    }
    return s;
  }

  inline std::string
  lcase (const char* s, std::size_t n)
  {
    std::string r (s, n == std::string::npos ? std::strlen (s) : n);
    return lcase (r);
  }

  inline std::string
  lcase (const std::string& s, std::size_t p, std::size_t n)
  {
    return lcase (s.c_str () + p, n != std::string::npos ? n : s.size () - p);
  }

  inline int
  icasecmp (char l, char r)
  {
    l = lcase (l);
    r = lcase (r);
    return l < r ? -1 : (l > r ? 1 : 0);
  }

  inline int
  icasecmp (const char* l, const char* r, std::size_t n)
  {
#ifndef _WIN32
    return n == std::string::npos ? strcasecmp (l, r) : strncasecmp (l, r, n);
#else
    return n == std::string::npos ? _stricmp (l, r) : _strnicmp (l, r, n);
#endif
  }

  inline int
  icasecmp (const std::string& l, const std::string& r, std::size_t n)
  {
    return icasecmp (l.c_str (), r.c_str (), n);
  }

  inline int
  icasecmp (const std::string& l, const char* r, std::size_t n)
  {
    return icasecmp (l.c_str (), r, n);
  }

  inline bool
  alpha (char c)
  {
    return std::isalpha (c);
  }

  inline bool
  digit (char c)
  {
    return std::isdigit (c);
  }

  inline bool
  alnum (char c)
  {
    return std::isalnum (c);
  }

  inline bool
  xdigit (char c)
  {
    return std::isxdigit (c);
  }

  inline bool
  alpha (wchar_t c)
  {
    return std::iswalpha (c);
  }

  inline bool
  digit (wchar_t c)
  {
    return std::iswdigit (c);
  }

  inline bool
  alnum (wchar_t c)
  {
    return std::iswalnum (c);
  }

  inline bool
  xdigit (wchar_t c)
  {
    return std::iswxdigit (c);
  }

  inline std::size_t
  next_word (const std::string& s, std::size_t& b, std::size_t& e,
             char d1, char d2)
  {
    return next_word (s, s.size (), b, e, d1, d2);
  }

  inline size_t
  next_word (const std::string& s,
             std::size_t n, std::size_t& b, std::size_t& e,
             char d1, char d2)
  {
    if (b != e)
      b = e;

    // Skip leading delimiters.
    //
    for (; b != n && (s[b] == d1 || s[b] == d2); ++b) ;

    if (b == n)
    {
      e = n;
      return 0;
    }

    // Find first trailing delimiter.
    //
    for (e = b + 1; e != n && s[e] != d1 && s[e] != d2; ++e) ;

    return e - b;
  }

  inline std::string&
  sanitize_identifier (std::string& s)
  {
    std::for_each (s.begin (), s.end (), [] (char& c)
                   {
                     if (!alnum (c) && c != '_')
                       c = '_';
                   });
    return s;
  }

  inline std::string
  sanitize_identifier (std::string&& s)
  {
    sanitize_identifier (s);
    return std::move (s);
  }

  inline std::string
  sanitize_identifier (const std::string& s)
  {
    return sanitize_identifier (std::string (s));
  }

  inline void
  sanitize_strlit (const std::string& s, std::string& o)
  {
    for (size_t i (0), j;; i = j + 1)
    {
      j = s.find_first_of ("\\\"\n", i);
      o.append (s.c_str () + i, (j == std::string::npos ? s.size () : j) - i);

      if (j == std::string::npos)
        break;

      switch (s[j])
      {
      case '\\': o += "\\\\"; break;
      case '"':  o += "\\\""; break;
      case '\n': o += "\\n";  break;
      }
    }
  }

  inline std::string
  sanitize_strlit (const std::string& s)
  {
    std::string r;
    sanitize_strlit (s, r);
    return r;
  }

  inline bool
  eof (std::istream& is)
  {
    if (!is.fail ())
      return false;

    if (is.eof ())
      return true;

    throw std::istream::failure ("");
  }

  inline optional<std::size_t>
  utf8_length_impl (const std::string& s,
                    std::string* what,
                    codepoint_types ts,
                    const char32_t* wl)
  {
    using namespace std;

    // Optimize for an empty string.
    //
    if (s.empty ())
      return 0;

    size_t r (0);
    pair<bool, bool> v;
    utf8_validator val (ts, wl);

    for (char c: s)
    {
      v = val.validate (c, what);

      if (!v.first) // Invalid byte?
        return nullopt;

      if (v.second) // Last byte in the sequence?
        ++r;
    }

    // Make sure that the last UTF-8 sequence is complete.
    //
    if (!v.second)
    {
      if (what != nullptr)
        *what = "incomplete UTF-8 sequence";

      return nullopt;
    }

    return r;
  }

  inline std::size_t
  utf8_length (const std::string& s, codepoint_types ts, const char32_t* wl)
  {
    using namespace std;

    string what;
    if (optional<size_t> r = utf8_length_impl (s, &what, ts, wl))
      return *r;

    throw invalid_argument (what);
  }

  inline bool
  utf8 (const std::string& s,
        std::string& what,
        codepoint_types ts,
        const char32_t* wl)
  {
    return utf8_length_impl (s, &what, ts, wl).has_value ();
  }

  inline bool
  utf8 (const std::string& s, codepoint_types ts, const char32_t* wl)
  {
    return utf8_length_impl (s, nullptr, ts, wl).has_value ();
  }

  inline optional<std::string>
  getenv (const std::string& name)
  {
    if (const char* r = std::getenv (name.c_str ()))
      return std::string (r);

    return nullopt;
  }

  template <typename F, typename P>
  inline F
  function_cast (P* p)
  {
    union { P* p; F f; } r;
    r.p = p;
    return r.f;
  }
}