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
/*
* Copyright (c) 2015-2018, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/** \file
* \brief Dump code for character classes (expressed as CharReach objects).
*/
#include "config.h"
// Everything in this file is dump code
#if defined(DUMP_SUPPORT)
#include "charreach.h"
#include "dump_charclass.h"
#include <cctype>
#include <iomanip>
#include <ostream>
#include <sstream>
using std::string;
using std::ostream;
namespace ue2 {
static
void describeChar(ostream &os, char c, enum cc_output_t out_type) {
// these characters must always be escaped
static const string escaped("^-[].");
static const string dot_single_escaped("\"\'");
const string backslash((out_type == CC_OUT_DOT ? 2 : 1), '\\');
#ifdef _WIN32
if (c >= 0x21 && c < 0x7F && c != '\\') {
#else
if (isgraph(c) && c != '\\') {
#endif
if (escaped.find(c) != string::npos) {
os << backslash << c;
} else if (out_type == CC_OUT_DOT
&& dot_single_escaped.find(c) != string::npos) {
os << '\\' << c;
} else {
os << c;
}
} else if (c == 0x09) {
os << backslash << 't';
} else if (c == 0x0a) {
os << backslash << 'n';
} else if (c == 0x0d) {
os << backslash << 'r';
} else {
auto fmt(os.flags());
os << backslash << 'x' << std::hex << std::setw(2)
<< std::setfill('0') << (unsigned)(c & 0xff);
os.flags(fmt);
}
}
static
void describeRange(ostream &os, unsigned char c1, unsigned char c2, enum cc_output_t out_type) {
assert(c1 <= c2);
if (c1 == c2) {
describeChar(os, (char)c1, out_type);
}
else if (c2 - c1 < 4) {
// render as individual chars
do {
describeChar(os, (char)c1, out_type);
} while (c1++ != c2);
} else {
// range
describeChar(os, (char)c1, out_type);
os << '-';
describeChar(os, (char)c2, out_type);
}
}
static
bool extractMnemonic(ostream &os, CharReach &cr, enum cc_output_t out_type) {
const string backslash((out_type == CC_OUT_DOT ? 2 : 1), '\\');
// \w (word characters: any letter, digit, or underscore)
static const CharReach words(string("_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ"));
if (words == (cr & words)) {
cr &= ~words;
os << backslash << 'w';
return true;
}
// \d (digits)
static const CharReach digits(string("0123456789"));
if (digits == (cr & digits)) {
cr &= ~digits;
os << backslash << 'd';
return true;
}
// \s (whitespace)
static const CharReach whitespace(string("\x09\x0a\x0b\x0c\x0d\x20", 6));
if (whitespace == (cr & whitespace)) {
cr &= ~whitespace;
os << backslash << 's';
return true;
}
return false;
}
static
bool isContiguous(const CharReach &cr, size_t &first, size_t &last) {
first = cr.find_first();
size_t c = first;
while (c != CharReach::npos) {
last = c;
c = cr.find_next(c);
if (c == CharReach::npos) {
break;
}
if (c != last + 1) {
return false;
}
}
return true;
}
static
size_t describeClassInt(ostream &os, const CharReach &incr, size_t maxLength,
enum cc_output_t out_type) {
// Approx size of output
size_t i = 0;
// one we can break
CharReach cr(incr);
// If we can be rendered as a single range, do it
size_t first = 0, last = 0;
if (isContiguous(cr, first, last)) {
describeRange(os, first, last, out_type);
i = 2;
return i;
}
// Extract any mnemonics
while (extractMnemonic(os, cr, out_type)) {
if (++i == maxLength) {
os << "...]";
return maxLength;
}
}
if (cr.none()) {
// all mnemonics, all the time
return i;
}
// Render charclass as a series of ranges
size_t c_start = cr.find_first();
size_t c = c_start, c_last = 0;
while (c != CharReach::npos) {
c_last = c;
c = cr.find_next(c);
if (c != c_last + 1 || c_last == 0xff) {
describeRange(os, c_start, c_last, out_type);
c_start = c;
if (++i == maxLength && c != CharReach::npos) {
os << "...]";
return maxLength;
}
}
}
return i;
}
////
//// Functions exported outside this unit.
////
// C++ iostreams interface
void describeClass(ostream &os, const CharReach &incr, size_t maxLength,
enum cc_output_t out_type) {
if (incr.all()) {
os << "<any>";
return;
}
if (incr.none()) {
os << "<empty>";
return;
}
if (incr.count() == 1) {
describeChar(os, (char)incr.find_first(), out_type);
return;
}
if ((~incr).count() == 1) {
os << "[^";
describeChar(os, (char)(~incr).find_first(), out_type);
os << ']';
return;
}
// build up a normal string and a negated one, and see which is shorter
std::ostringstream out;
int out_count = describeClassInt(out, incr, maxLength, out_type);
std::ostringstream neg;
UNUSED int neg_count = describeClassInt(neg, ~incr, maxLength, out_type);
if (out.tellp() <= neg.tellp()) {
if (out_count > 1) {
os << '[' << out.str() << ']';
} else {
os << out.str();
}
} else {
// TODO: negated single mnemonics
os << "[^" << neg.str() << ']';
}
}
// Version that returns a string, for convenience.
string describeClass(const CharReach &cr, size_t maxLength,
enum cc_output_t out_type) {
std::ostringstream oss;
describeClass(oss, cr, maxLength, out_type);
return oss.str();
}
// C stdio wrapper
void describeClass(FILE *f, const CharReach &cr, size_t maxLength,
enum cc_output_t out_type) {
fprintf(f, "%s", describeClass(cr, maxLength, out_type).c_str());
}
} // namespace ue2
#endif // DUMP_SUPPORT