-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.hpp
More file actions
319 lines (258 loc) · 8.04 KB
/
Copy pathstring.hpp
File metadata and controls
319 lines (258 loc) · 8.04 KB
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
/* File: /lib/utils/string.hpp
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#ifndef CPPLIB__lib__utils__string__hpp
#define CPPLIB__lib__utils__string__hpp
#include <cctype>
#include <string>
#include <vector>
#include <cpp/lib_debug>
#include "../../lib/types.hpp"
#include "../../lib/literals.hpp"
#include "../../lib/cstring.hpp"
#include "../../lib/data/buffer.hpp"
/// @todo Support all char types (c8, c16, c32).
namespace lib {
// DECLARATION lib::str<>(), lib::str()
template< class T >
requires requires( const T & t ) { ::std::to_string( t ); }
inline ::std::string str( const T & object ) {
return ::std::to_string( object );
}
inline ::std::string str( const char * object ) {
return ::std::string {object};
}
inline ::std::string str( const cstring & str ) {
return ::std::string {str.begin(), str.end()};
}
inline constexpr cstring str( bool value ) noexcept {
return value ? "true" : "false";
}
// DECLARATION lib::split()
inline ::std::vector<cstring>
split( const cstring & str, const char * separators, const char * group = "" ) {
CPP_ASSERT( separators != nullptr );
CPP_ASSERT( group != nullptr );
::std::vector<cstring> result;
auto str_begin = 0_sz;
while ( str_begin < str.size() ) {
str_begin = str.find_first_not_of( separators, str_begin );
if ( str_begin == cstring::npos )
break;
const char * g = group;
for ( ; *g != '\0'; ++g )
if ( *g == str[ str_begin ] )
break;
usize str_end;
if ( *g != '\0' and str_begin + 1 < str.size() )
str_end = str.find_first_of( *g, ++str_begin );
else
str_end = str.find_first_of( separators, str_begin );
if ( str_end == cstring::npos )
str_end = str.size();
result.emplace_back( str.substr( str_begin, str_end - str_begin ) );
str_begin = str_end + 1;
}
return result;
}
// DECLARATION lib::to_lower(), lib::to_upper()
inline /*constexpr */char to_lower( const char c ) {
return (char)::std::tolower( c );
}
inline /*constexpr */char to_upper( const char c ) {
return (char)::std::toupper( c );
}
inline ::std::string to_lower( const cstring & str_ ) {
auto result = str( str_ );
for ( auto & c : result ) c = to_lower( c );
return result;
}
inline ::std::string to_upper( const cstring & str_ ) {
auto result = str( str_ );
for ( auto & c : result ) c = to_upper( c );
return result;
}
// DECLARATION lib::is_lower(), lib::is_upper()
inline /*constexpr */bool is_lower( const char c ) {
return ::std::islower( c ) != 0;
}
inline /*constexpr */bool is_upper( const char c ) {
return ::std::isupper( c ) != 0;
}
inline constexpr bool is_lower( const cstring & str ) {
for ( const auto c : str )
if ( not is_lower( c ) )
return false;
return true;
}
inline constexpr bool is_upper( const cstring & str ) {
for ( const auto c : str )
if ( not is_upper( c ) )
return false;
return true;
}
// DECLARATION lib::is_digit()
/// @todo <cctype> ::std::isdigit()
inline constexpr bool is_digit( const char c ) {
return '0' <= c and c <= '9';
}
inline constexpr bool is_digit( const cstring & str ) {
if ( str.empty() )
return false;
for ( const auto c : str )
if ( not is_digit( c ) )
return false;
return true;
}
inline constexpr bool is_digit( const char * str ) {
return is_digit( cstring {str} );
}
// DECLARATION lib::is_integer()
inline constexpr bool is_integer( const cstring & str ) {
/// @regex /^(\+|-|)\d+$/
if ( str.empty() )
return false;
auto str_begin = str.begin();
if ( str.front() == '+' or str.front() == '-' )
++str_begin;
return is_digit( cstring {str_begin, str.end()} );
}
inline constexpr bool is_integer( const char * str ) {
return is_integer( cstring {str} );
}
// DECLARATION lib::is_float()
inline constexpr bool is_float( const cstring & str ) {
/// @regex /^(\+|-|)\d+[Ee](\+|-|)\d+$/
/// @regex /^(\+|-|\d*)\.\d+$/
/// @regex /^(\+|-|)\d+\.\d*$/
if ( str.empty() )
return false;
const bool sign = str.front() == '+' or str.front() == '-';
const bool dot = str.front() == '.';
auto str_begin = str.begin();
if ( sign or dot )
++str_begin;
bool digits = false;
while ( str_begin != str.end() and is_digit( *str_begin ) ) {
digits = true;
++str_begin;
}
if ( str_begin == str.end() )
return dot and digits; ///< e.g. ".123"
if ( *str_begin == '.' ) {
if ( dot )
return false; ///< second "." symbol
if ( ++str_begin == str.end() )
return digits; ///< e.g. "456."
return is_digit( cstring {str_begin, str.end()} ); ///< e.g. "78.9"
}
if ( *str_begin == 'e' or *str_begin == 'E' )
return digits
and is_integer( cstring {str_begin + 1, str.end()} ); ///< exponent form
return false;
}
inline constexpr bool is_float( const char * str ) {
return is_float( cstring {str} );
}
// DECLARATION lib::is_ascii_control()
inline /*constexpr */bool is_ascii_control( const char c ) noexcept {
return ::std::iscntrl( (u8) c ) != 0;
}
inline /*constexpr */bool is_ascii_control( const cstring & str ) noexcept {
for ( const auto c : str )
if ( not is_ascii_control( c ) )
return false;
return not str.empty();
}
// DECLARATION lib::is_ascii_printable()
inline /*constexpr */bool is_ascii_printable( const char c ) noexcept {
return ::std::isprint( (u8) c ) != 0;
}
inline /*constexpr */bool is_ascii_printable( const cstring & str ) noexcept {
for ( const auto c : str )
if ( not is_ascii_printable( c ) )
return false;
return not str.empty();
}
// DECLARATION lib::is_ascii_space()
inline /*constexpr */bool is_ascii_space( const char c ) noexcept {
return ::std::isspace( (u8) c ) != 0;
}
inline /*constexpr */bool is_ascii_space( const cstring & str ) noexcept {
for ( const auto c : str )
if ( not is_ascii_space( c ) )
return false;
return not str.empty();
}
// DECLARATION lib::is_ascii_blank()
inline /*constexpr */bool is_ascii_blank( const char c ) noexcept {
return ::std::isblank( (u8) c ) != 0;
}
inline /*constexpr */bool is_ascii_blank( const cstring & str ) noexcept {
for ( const auto c : str )
if ( not is_ascii_blank( c ) )
return false;
return not str.empty();
}
// DECLARATION lib::is_ascii_graphical()
inline /*constexpr */bool is_ascii_graphical( const char c ) noexcept {
return ::std::isgraph( (u8) c ) != 0
and c != '\x20'; ///< @fix for isgraph('\x20')
}
inline /*constexpr */bool is_ascii_graphical( const cstring & str ) noexcept {
for ( const auto c : str )
if ( not is_ascii_graphical( c ) )
return false;
return not str.empty();
}
// DECLARATION lib::is_ascii()
inline /*constexpr */bool is_ascii( const char c ) noexcept {
return is_ascii_control( c ) or is_ascii_printable( c );
}
inline /*constexpr */bool is_ascii( const cstring & str ) noexcept {
for ( const auto c : str )
if ( not is_ascii( c ) )
return false;
return not str.empty();
}
// DECLARATION lib::is_AZ(), lib::is_az(), lib::is_Az(), lib::is_09()
inline constexpr bool is_AZ( const char c ) noexcept {
return 'A' <= c and c <= 'Z';
}
inline constexpr bool is_az( const char c ) noexcept {
return 'a' <= c and c <= 'z';
}
inline constexpr bool is_Az( const char c ) noexcept {
return is_AZ( c ) and is_az( c );
}
inline constexpr bool is_09( const char c ) noexcept {
return '0' <= c and c <= '9';
}
// DECLARATION lib::hex()
inline constexpr char hex( u8 value, bool hi ) noexcept {
constexpr const char * MAP = "0123456789abcdef";
if ( hi ) value >>= 4;
value &= 0x0F;
return MAP[ value ];
}
inline ::std::string hex( const data::cbuffer_t & buffer, const cstring & separator = {} ) noexcept {
if ( buffer.empty() )
return {};
const auto size = buffer.size() * 2 + separator.size() * ( buffer.size() - 1 );
::std::string result;
result.reserve( size );
result.append( 1, hex( buffer.front(), true ) );
result.append( 1, hex( buffer.front(), false ) );
for ( auto i = 1_sz; i < buffer.size(); ++i ) {
result.append( separator );
result.append( 1, hex( buffer[i], true ) );
result.append( 1, hex( buffer[i], false ) );
}
return result;
}
} // namespace lib
#endif // CPPLIB__lib__utils__string__hpp