-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_predicates.hpp
180 lines (167 loc) · 3.87 KB
/
string_predicates.hpp
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
#ifndef ryk_string_predicates
#define ryk_string_predicates
#include <cctype>
#include <string>
#include "traits.hpp"
#include "iterable_algorithms.hpp"
using std::string;
namespace ryk {
// This file contains quite a few synonyms for a given definition.
// The reason for this is C made many predicates using the notation is___
// i.e. isdigit, isspace, having no underbar between is and the word
// which looks a bit odd. Further Lisp offers predicates with the word
// suffixed with -p. i.e. digitp spacep.
// We'll offer all these choices.
//
// is_uint(string) and its synonyms
//
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
is_uint(const String& s)
{
return !s.empty() && all_of(s, ::isdigit);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
isuint(const String& s)
{
return is_uint(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
uintp(const String& s)
{
return is_uint(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
is_nat(const String& s)
{
return is_uint(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
isnat(const String& s)
{
return is_uint(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
natp(const String& s)
{
return is_uint(s);
}
//
// is_int(string) and its synonyms
//
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
is_int(const String& s)
{
if (is_nat(s)) return true;
if (s.size() >= 2 && s[0] == '-') return is_nat(s.substr(1));
return false;
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
isint(const String& s)
{
return is_int(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
intp(const String& s)
{
return is_int(s);
}
//
// is_float(string) and its synonyms
//
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
is_float(const String& s)
{
// some test cases:
// '-.' '-' '.' '..' '.-.' '--.'
// '-a.' '$a.' 'a-.' 'a'
// '2-.' '2..' '.2.' '2-'
// '2' '2.' '-2.' '22.22' '-22.22'
bool dot = false, dash = false, digit = false;
for (auto rit = s.rbegin(); rit != s.rend(); ++rit) {
if (*rit == '.' && dot) return false;
else if (*rit == '.') dot = true;
else if (*rit == '-' && !digit) return false;
else if (*rit == '-') dash = true;
else if (dash && !isspace(*rit)) return false;
else if (!isdigit(*rit)) return false;
else digit = true;
}
return true;
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
is_double(const String& s)
{
return is_float(s);
}
//
// is_number(string) is synonymous with is_float
//
template<class String, enable_if_p<is_string_v<String>>...> inline
bool is_number(const String& s)
{
return is_float(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
is_numeric(const String& s)
{
return is_number(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
isnumber(const String& s)
{
return is_number(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
isnumeric(const String& s)
{
return is_number(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
numberp(const String& s)
{
return is_number(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
numericp(const String& s)
{
return is_number(s);
}
//
// has_space(string) and its synonyms
//
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
has_space(const String& s)
{
return has_if(s, ::isspace);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
hasspace(const String& s)
{
return has_space(s);
}
template<class String> inline
std::enable_if_t<is_string_v<String>, bool>
spacep(const String& s)
{
return has_space(s);
}
} // namespace ryk
#endif