-
Notifications
You must be signed in to change notification settings - Fork 0
/
translations.hh
381 lines (339 loc) · 10.6 KB
/
translations.hh
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#ifndef __OICOMPARE_TRANSLATIONS_HH__
#define __OICOMPARE_TRANSLATIONS_HH__
#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <iterator>
#include <string_view>
#include <utility>
#include <fmt/format.h>
#include "oicompare.hh"
#include "print_format.hh"
namespace oicompare::translations
{
enum class kind
{
terse,
abbreviated,
full,
};
namespace detail
{
using namespace std::string_view_literals;
constexpr bool
is_ascii_printable (char ch)
{
return ch >= 32 && ch <= 126 && ch != '"' && ch != '<' && ch != '>';
}
constexpr std::size_t
char_length (char ch)
{
if (detail::is_ascii_printable (ch)) [[likely]]
return 1;
else
return 6;
}
constexpr bool
string_length_fits (std::string_view str, std::size_t limit)
{
std::size_t result = 0;
for (char ch : str)
{
std::size_t new_result = result + char_length (ch);
if (new_result > limit)
return false;
result = new_result;
}
return true;
}
template <std::output_iterator<char> OutputIt>
OutputIt
append_char (OutputIt out, char ch)
{
if (detail::is_ascii_printable (ch)) [[likely]]
*out++ = ch;
else
{
auto u_ch = static_cast<unsigned char>(ch);
constexpr std::array hex_table{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
*out++ = '<';
*out++ = '0';
*out++ = 'x';
*out++ = hex_table[u_ch / 16];
*out++ = hex_table[u_ch % 16];
*out++ = '>';
}
return out;
}
constexpr std::size_t abbreviated_max = 100;
constexpr std::string_view ellipsis = "…"sv;
}
template <bool Abbreviated> struct represent_word;
template <> struct represent_word<false>
{
template <std::output_iterator<char> OutputIt>
static OutputIt
represent (OutputIt out, std::string_view word, std::size_t)
{
*out++ = '"';
for (char ch : word)
out = detail::append_char (std::move (out), ch);
*out++ = '"';
return out;
}
};
template <> struct represent_word<true>
{
template <std::output_iterator<char> OutputIt>
static OutputIt
represent (OutputIt out, std::string_view word, std::size_t first_difference)
{
using namespace detail;
using namespace std::string_view_literals;
assert (!word.empty ());
assert (first_difference <= word.size ());
std::size_t used_chars
= char_length ('"');
if (first_difference > 0)
{
used_chars += char_length (word[first_difference - 1]);
if (first_difference > 1)
{
used_chars += char_length (word[0]);
if (first_difference > 2)
used_chars += ellipsis.size ();
}
}
if (first_difference < word.size ())
used_chars += char_length (word[first_difference]);
if (first_difference < word.size () - 1)
{
used_chars += char_length (word[first_difference + 1]);
if (first_difference < word.size () - 2)
used_chars += ellipsis.size ();
}
used_chars += char_length ('"');
assert (used_chars <= abbreviated_max);
auto append_char_helper
= [&] (char ch) constexpr { out = append_char (std::move (out), ch); };
*out++ = '"';
if (first_difference > 0)
{
if (first_difference > 1)
{
append_char_helper (word[0]);
/*
* A simple greedy algorithm will not work, because:
*
* XY (2 bytes)
*
* is shorter than
*
* … (3 bytes)
*
* so instead of lookahead we will use a limited length check (so
* the running time is limited).
*/
auto ellipsized = word.substr (1, first_difference - 1);
if (string_length_fits (ellipsized,
abbreviated_max
- (used_chars - ellipsis.size ())))
{
for (std::size_t i = 1; i < first_difference - 1; ++i)
{
used_chars += char_length (word[i]);
append_char_helper (word[i]);
}
used_chars -= ellipsis.size ();
}
else
{
for (std::size_t i = 1; i < first_difference - 1; ++i)
{
std::size_t new_used_chars
= used_chars + char_length (word[i]);
if (new_used_chars > abbreviated_max)
break;
used_chars = new_used_chars;
append_char_helper (word[i]);
}
out = std::move (
std::ranges::copy (ellipsis, std::move (out)).out);
}
}
append_char_helper (word[first_difference - 1]);
}
if (first_difference < word.size ())
append_char_helper (word[first_difference]);
if (first_difference < word.size () - 1)
{
append_char_helper (word[first_difference + 1]);
if (first_difference < word.size () - 2)
{
auto ellipsized = word.substr (first_difference + 2);
if (string_length_fits (ellipsized,
abbreviated_max
- (used_chars - ellipsis.size ())))
{
for (std::size_t i = first_difference + 2; i < word.size ();
++i)
{
used_chars += char_length (word[i]);
append_char_helper (word[i]);
}
used_chars -= ellipsis.size ();
}
else
{
for (std::size_t i = first_difference + 2; i < word.size ();
++i)
{
std::size_t new_used_chars
= used_chars + char_length (word[i]);
if (new_used_chars > abbreviated_max)
break;
used_chars = new_used_chars;
append_char_helper (word[i]);
}
out = std::move (
std::ranges::copy (ellipsis, std::move (out)).out);
}
}
}
assert (used_chars <= abbreviated_max);
*out++ = '"';
return out;
}
};
template <kind Kind> struct english_translation
{
static auto
represent (const oicompare::token<const char *> &token,
const char *mismatch) noexcept
{
using namespace std::string_view_literals;
return print_format ([&token, mismatch] (auto &ctx) {
auto out = ctx.out ();
switch (token.type)
{
case oicompare::token_type::eof:
out = std::move (
std::ranges::copy ("end of file"sv, std::move (out)).out);
break;
case oicompare::token_type::newline:
out = std::move (
std::ranges::copy ("end of line"sv, std::move (out)).out);
break;
case oicompare::token_type::word:
represent_word<Kind == kind::abbreviated>::represent (
out, {token.first, token.last},
mismatch ? mismatch - token.first : 0);
break;
default:
#ifdef __GNUC__
__builtin_unreachable ();
#endif
out = std::move (
std::ranges::copy ("unknown token"sv, std::move (out)).out);
break;
}
return out;
});
}
static void
print (const std::optional<oicompare::mismatch<const char *, const char *>>
&mismatch)
{
if (mismatch)
switch (Kind)
{
case kind::terse:
fmt::println ("WRONG");
break;
case kind::abbreviated:
case kind::full:
fmt::println ("WRONG: line {}: expected {}, got {}",
mismatch->line_number,
represent (mismatch->first,
mismatch->first_difference.has_value ()
? mismatch->first_difference->first
: nullptr),
represent (mismatch->second,
mismatch->first_difference.has_value ()
? mismatch->first_difference->second
: nullptr));
break;
}
else
fmt::println ("OK");
}
};
template <kind Kind> struct polish_translation
{
static auto
represent (const oicompare::token<const char *> &token,
const char *mismatch) noexcept
{
using namespace std::string_view_literals;
return print_format ([&token, mismatch] (auto &ctx) {
auto out = ctx.out ();
switch (token.type)
{
case oicompare::token_type::eof:
out = std::move (
std::ranges::copy ("koniec pliku"sv, std::move (out)).out);
break;
case oicompare::token_type::newline:
out = std::move (
std::ranges::copy ("koniec wiersza"sv, std::move (out)).out);
break;
case oicompare::token_type::word:
represent_word<Kind == kind::abbreviated>::represent (
out, {token.first, token.last},
mismatch ? mismatch - token.first : 0);
break;
default:
#ifdef __GNUC__
__builtin_unreachable ();
#endif
out = std::move (
std::ranges::copy ("nieznany token"sv, std::move (out)).out);
break;
}
return out;
});
}
static void
print (const std::optional<oicompare::mismatch<const char *, const char *>>
&mismatch)
{
if (mismatch)
switch (Kind)
{
case kind::terse:
fmt::println ("ŹLE");
break;
case kind::abbreviated:
case kind::full:
fmt::println ("ŹLE: wiersz {}: oczekiwano {}, otrzymano {}",
mismatch->line_number,
represent (mismatch->first,
mismatch->first_difference.has_value ()
? mismatch->first_difference->first
: nullptr),
represent (mismatch->second,
mismatch->first_difference.has_value ()
? mismatch->first_difference->second
: nullptr));
break;
}
else
fmt::println ("OK");
}
};
using translation = void (*) (
const std::optional<oicompare::mismatch<const char *, const char *>> &);
}
#endif /* __OICOMPARE_TRANSLATIONS_HH__ */