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
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _OUSIA_UTILS_H_
#define _OUSIA_UTILS_H_
#include <sstream>
#include <string>
#include <vector>
namespace ousia {
class Utils {
public:
/**
* Returns true if the given character is in [A-Za-z]
*/
static bool isAlphabetic(const char c)
{
return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
}
/**
* Returns true if the given character is in [0-9]
*/
static bool isNumeric(const char c) { return (c >= '0') && (c <= '9'); }
/**
* Returns true if the given character is in [0-9A-Fa-f]
*/
static bool isHexadecimal(const char c)
{
return ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F')) ||
((c >= 'a') && (c <= 'f'));
}
/**
* Returns true if the given character is in [A-Za-z0-9]
*/
static bool isAlphanumeric(const char c)
{
return isAlphabetic(c) || isNumeric(c);
}
/**
* Returns true if the given character is in [A-Za-z].
*/
static bool isIdentifierStartCharacter(const char c)
{
return isAlphabetic(c);
}
/**
* Returns true if the given character is in [A-Za-z0-9_-].
*/
static bool isIdentifierCharacter(const char c)
{
return isAlphanumeric(c) || (c == '_') || (c == '-');
}
/**
* Returns true if the given string is in
* \code{.txt}
* [A-Za-z][A-Za-z0-9_-]*
* \endCode
*
* @param name is the string that should be tested.
* @return true if the string matches the regular expression given above,
* false otherwise.
*/
static bool isIdentifier(const std::string &name);
/**
* Returns true if the given string is in
* \code{.txt}
* ([A-Za-z][A-Za-z0-9_-]*)(:[A-Za-z][A-Za-z0-9_-]*)*
* \endCode
*
* @param name is the string that should be tested.
* @return true if the string matches the regular expression given above,
* false otherwise.
*/
static bool isNamespacedIdentifier(const std::string &name);
/**
* Returns true if the given character is a linebreak character.
*/
static bool isLinebreak(const char c) { return (c == '\n') || (c == '\r'); }
/**
* Returns true if the given character is a whitespace character.
*/
static bool isWhitespace(const char c)
{
return (c == ' ') || (c == '\t') || isLinebreak(c);
}
/**
* Returns true if the given string has a non-whitespace character.
*
* @param s is the string that should be checked.
* @return true if the string contains a non-whitespace character.
*/
static bool hasNonWhitepaceChar(const std::string &s);
/**
* Removes whitespace at the beginning and the end of the given string.
*
* @param s is the string that should be trimmed.
* @return a trimmed copy of s.
*/
static std::string trim(const std::string &s);
/**
* Trims the given string or vector of chars by returning the start and end
* index.
*
* @param s is the container that should be trimmed.
* @param f is a function that returns true for values that should be
* removed.
* @return start and end index. Note that "end" points at the character
* beyond the end, thus "end" minus "start"
*/
template <class T, class Filter>
static std::pair<size_t, size_t> trim(const T &s, Filter f)
{
return trim(s, s.size(), f);
}
/**
* Trims the given string or vector of chars by returning the start and end
* index.
*
* @param s is the container that should be trimmed.
* @param len is the number of elements in the container.
* @param f is a function that returns true for values that should be
* removed.
* @return start and end index. Note that "end" points at the character
* beyond the end, thus "end" minus "start"
*/
template <class T, class Filter>
static std::pair<size_t, size_t> trim(const T &s, size_t len, Filter f)
{
size_t start = 0;
for (size_t i = 0; i < len; i++) {
if (!f(s[i])) {
start = i;
break;
}
}
size_t end = 0;
for (ssize_t i = len - 1; i >= static_cast<ssize_t>(start); i--) {
if (!f(s[i])) {
end = i + 1;
break;
}
}
if (end < start) {
start = 0;
end = 0;
}
return std::pair<size_t, size_t>{start, end};
}
/**
* Collapses the whitespaces in the given string (trims the string and
* replaces all whitespace characters by a single one).
*
* @param s is the string in which the whitespace should be collapsed.
* @return a copy of s with collapsed whitespace.
*/
static std::string collapse(const std::string &s);
/**
* Turns the elements of a collection into a string separated by the
* given delimiter.
*
* @param es is an iterable container of elements that can be appended to an
* output stream (the << operator must be implemented).
* @param delim is the delimiter that should be used to separate the items.
* @param start is a character sequence that should be prepended to the
* result.
* @param end is a character sequence that should be appended to the result.
*/
template <class T>
static std::string join(T es, const std::string &delim,
const std::string &start = "",
const std::string &end = "")
{
std::stringstream res;
bool first = true;
res << start;
for (const auto &e : es) {
if (!first) {
res << delim;
}
res << e;
first = false;
}
res << end;
return res.str();
}
/**
* Splits the given string at the delimiter and returns an array of
* substrings without the delimiter.
*
* @param s is the string that should be splitted.
* @param delim is the delimiter at which the string should be splitted.
* @return a vector of strings containing the splitted sub-strings.
*/
static std::vector<std::string> split(const std::string &s, char delim);
/**
* Converts the given string to lowercase (only works for ANSI characters).
*
* @param s is the string that should be converted to lowercase.
* @return s in lowercase.
*/
static std::string toLower(std::string s);
/**
* Reads the file extension of the given filename.
*
* @param filename is the filename from which the extension should be
* extracted.
* @return the extension, excluding any leading dot. The extension is
* defined as the substring after the last dot in the given string, if the
* dot is after a slash or backslash. The extension is converted to
* lowercase.
*/
static std::string extractFileExtension(const std::string &filename);
/**
* Checks whether the given string starts with the given prefix.
*
* @param s is the string.
* @param prefix is the string which should be checked for being a prefix of
* s.
*/
static bool startsWith(const std::string &s, const std::string &prefix);
/**
* Checks whether the given string ends with the given suffix.
*
* @param s is the string.
* @param suffix is the string which should be checked for being a suffix of
* s.
*/
static bool endsWith(const std::string &s, const std::string &suffix);
/**
* Hash functional to be used for enum classes.
* See http://stackoverflow.com/a/24847480/2188211
*/
struct EnumHash {
template <typename T>
std::size_t operator()(T t) const
{
return static_cast<std::size_t>(t);
}
};
};
}
#endif /* _OUSIA_UTILS_H_ */
|