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
|
// file : butl/utility -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#ifndef BUTL_UTILITY
#define BUTL_UTILITY
#include <string>
#include <iosfwd> // ostream
#include <cstddef> // size_t
#include <utility> // move(), forward()
#include <cstring> // strcmp(), strlen()
#include <exception> // exception, uncaught_exception(s)()
//#include <functional> // hash
#include <butl/export>
#include <butl/config>
namespace butl
{
// Convert ASCII character/string case. If there is no upper/lower case
// counterpart, leave the character unchanged. The POSIX locale (also known
// as C locale) must be the current application locale. Otherwise the
// behavior is undefined.
//
// Note that the POSIX locale specifies behaviour on data consisting
// entirely of characters from the portable character set (subset of ASCII
// including 103 non-negative characters and English alphabet letters in
// particular) and the control character set (more about them at
// http://pubs.opengroup.org/onlinepubs/009696899/basedefs/xbd_chap06.html).
//
// Also note that according to the POSIX locale definition the case
// conversion can be applied only to [A-Z] and [a-z] character ranges being
// translated to each other (more about that at
// http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html#tag_07_02)
//
char ucase (char);
std::string ucase (const char*, std::size_t = std::string::npos);
std::string ucase (const std::string&);
std::string& ucase (std::string&);
void ucase (char*, std::size_t);
char lcase (char);
std::string lcase (const char*, std::size_t = std::string::npos);
std::string lcase (const std::string&);
std::string& lcase (std::string&);
void lcase (char*, std::size_t);
// Compare ASCII characters/strings ignoring case. Behave as if characters
// had been converted to the lower case and then byte-compared. Return a
// negative, zero or positive value if the left hand side is less, equal or
// greater than the right hand side, respectivelly. The POSIX locale (also
// known as C locale) must be the current application locale. Otherwise the
// behavior is undefined.
//
// The optional size argument specifies the maximum number of characters
// to compare.
//
int casecmp (char, char);
int casecmp (const std::string&, const std::string&,
std::size_t = std::string::npos);
int casecmp (const std::string&, const char*,
std::size_t = std::string::npos);
int casecmp (const char*, const char*, std::size_t = std::string::npos);
// Case-insensitive key comparators (i.e., to be used in sets, maps, etc).
//
struct case_compare_string
{
bool operator() (const std::string& x, const std::string& y) const
{
return casecmp (x, y) < 0;
}
};
struct case_compare_c_string
{
bool operator() (const char* x, const char* y) const
{
return casecmp (x, y) < 0;
}
};
bool
alpha (char);
bool
digit (char);
bool
alnum (char);
// Key comparators (i.e., to be used in sets, maps, etc).
//
struct compare_c_string
{
bool operator() (const char* x, const char* y) const noexcept
{
return std::strcmp (x, y) < 0;
}
};
struct compare_pointer_target
{
template <typename P>
bool operator() (const P& x, const P& y) const {return *x < *y;}
};
//struct hash_pointer_target
//{
// template <typename P>
// std::size_t operator() (const P& x) const {return std::hash (*x);}
//};
// Combine one or more hash values.
//
inline std::size_t
combine_hash (std::size_t s, std::size_t h)
{
// Magic formula from boost::hash_combine().
//
return s ^ (h + 0x9e3779b9 + (s << 6) + (s >> 2));
}
template <typename... S>
inline std::size_t
combine_hash (std::size_t s, std::size_t h, S... hs)
{
return combine_hash (combine_hash (s, h), hs...);
}
// Support for reverse iteration using range-based for-loop:
//
// for (... : reverse_iterate (x)) ...
//
template <typename T>
class reverse_range
{
T x_;
public:
reverse_range (T&& x): x_ (std::forward<T> (x)) {}
auto begin () const -> decltype (this->x_.rbegin ()) {return x_.rbegin ();}
auto end () const -> decltype (this->x_.rend ()) {return x_.rend ();}
};
template <typename T>
inline reverse_range<T>
reverse_iterate (T&& x) {return reverse_range<T> (std::forward<T> (x));}
// Call a function if there is an exception.
//
template <typename F>
struct exception_guard;
template <typename F>
inline exception_guard<F>
make_exception_guard (F f)
{
return exception_guard<F> (std::move (f));
}
#ifdef BUTL_CXX17_UNCAUGHT_EXCEPTIONS
template <typename F>
struct exception_guard
{
exception_guard (F f)
: f_ (std::move (f)),
u_ (std::uncaught_exceptions ()) {}
~exception_guard ()
{
if (u_ != std::uncaught_exceptions ())
f_ ();
}
private:
F f_;
int u_;
};
#else
// Fallback implementation using a TLS flag.
//
// True means we are in the body of a destructor that is being called as
// part of the exception stack unwindining.
//
extern
#ifdef BUTL_CXX11_THREAD_LOCAL
thread_local
#else
__thread
#endif
bool exception_unwinding_dtor_;
// On Windows one cannot export a thread-local variable so we have to
// use a wrapper functions.
//
#ifdef _WIN32
LIBBUTL_EXPORT bool&
exception_unwinding_dtor ();
#else
inline bool&
exception_unwinding_dtor () {return exception_unwinding_dtor_;}
#endif
template <typename F>
struct exception_guard
{
exception_guard (F f): f_ (std::move (f)) {}
~exception_guard ()
{
if (std::uncaught_exception ())
{
exception_unwinding_dtor () = true;
f_ ();
exception_unwinding_dtor () = false;
}
}
private:
F f_;
};
#endif
}
namespace std
{
// Sanitize the exception description before printing. This includes:
//
// - stripping leading colons and spaces (see fdstream.cxx)
// - stripping trailing newlines, periods, and spaces
// - lower-case the first letter if the beginning looks like a word
//
LIBBUTL_EXPORT ostream&
operator<< (ostream&, const exception&);
}
#include <butl/utility.ixx>
#endif // BUTL_UTILITY
|