forked from psemiletov/tea-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextproc.cpp
584 lines (500 loc) · 15.1 KB
/
textproc.cpp
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/***************************************************************************
* 2007-2015 by Peter Semiletov *
* *
* 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, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/***************************************************************************
some code is taken from Scribus::util.cpp:
-------------------
begin : Fri Sep 14 2001
copyright : (C) 2001 by Franz Schmid
email : [email protected]
***************************************************************************/
/* roman.c by Adam Rogoyski ([email protected]) Temperanc on EFNet irc
* Copyright (C) 1998 Adam Rogoyski
* Converts Decimal numbers to Roman Numerals and Roman Numberals to
* Decimals on the command line or in Interactive mode.
* Uses an expanded Roman Numeral set to handle numbers up to 999999999
*/
#include <QString>
#include <QMap>
#include "textproc.h"
#include "utils.h"
int str_fuzzy_search (const QString &s, const QString &text_to_find, int start_pos, double q)
{
int counter;
int result = -1;
bool jump = false;
int end_pos = s.length() - 1;
for (int i = start_pos; i < end_pos; i++)
{
if (jump)
break;
counter = 0;
for (int j = 0; j < text_to_find.length(); j++)
{
if (s[i + j] == text_to_find[j])
counter++;
if (get_percent ((double)text_to_find.length(), (double)counter) >= q)
{
result = i;
jump = true;
break;
}
}
}
return result;
}
QString apply_table (const QString &s, const QString &fname, bool use_regexp)
{
QHash<QString, QString> h = hash_load_keyval (fname);
QString result = s;
for (int i = 0; i < h.size(); i++)
{
QString key = h.keys()[i];
if (use_regexp)
result.replace (QRegExp (key), h.value (key));
else
result.replace (key, h.value (key));
}
return result;
}
//this code is taken from Scribus::util.cpp:
QString arabicToRoman (int i)
{
QString roman;
int arabic = i;
while (arabic - 1000000 >= 0){
roman += "m";
arabic -= 1000000;
}
while (arabic - 900000 >= 0){
roman += "cm";
arabic -= 900000;
}
while (arabic - 500000 >= 0){
roman += "d";
arabic -= 500000;
}
while (arabic - 400000 >= 0){
roman += "cd";
arabic -= 400000;
}
while (arabic - 100000 >= 0){
roman += "c";
arabic -= 100000;
}
while (arabic - 90000 >= 0){
roman += "xc";
arabic -= 90000;
}
while (arabic - 50000 >= 0){
roman += "l";
arabic -= 50000;
}
while (arabic - 40000 >= 0){
roman += "xl";
arabic -= 40000;
}
while (arabic - 10000 >= 0){
roman += "x";
arabic -= 10000;
}
while (arabic - 9000 >= 0){
roman += "Mx";
arabic -= 9000;
}
while (arabic - 5000 >= 0){
roman += "v";
arabic -= 5000;
}
while (arabic - 4000 >= 0){
roman += "Mv";
arabic -= 4000;
}
while (arabic - 1000 >= 0){
roman += "M";
arabic -= 1000;
}
while (arabic - 900 >= 0){
roman += "CM";
arabic -= 900;
}
while (arabic - 500 >= 0){
roman += "D";
arabic -= 500;
}
while (arabic - 400 >= 0){
roman += "CD";
arabic -= 400;
}
while (arabic - 100 >= 0){
roman += "C";
arabic -= 100;
}
while (arabic - 90 >= 0){
roman += "XC";
arabic -= 90;
}
while (arabic - 50 >= 0){
roman += "L";
arabic -= 50;
}
while (arabic - 40 >= 0){
roman += "XL";
arabic -= 40;
}
while (arabic - 10 >= 0){
roman += "X";
arabic -= 10;
}
while (arabic - 9 >= 0){
roman += "IX";
arabic -= 9;
}
while (arabic - 5 >= 0){
roman += "V";
arabic -= 5;
}
while (arabic - 4 >= 0){
roman += "IV";
arabic -= 4;
}
while (arabic - 1 >= 0){
roman += "I";
arabic -= 1;
}
return roman;
}
/*
from:
* roman.c by Adam Rogoyski ([email protected]) Temperanc on EFNet irc
* Copyright (C) 1998 Adam Rogoyski
* Converts Decimal numbers to Roman Numerals and Roman Numberals to
* Decimals on the command line or in Interactive mode.
* Uses an expanded Roman Numeral set to handle numbers up to 999999999
*/
#define FROM_ROMAN_I 1
#define FROM_ROMAN_V 5
#define FROM_ROMAN_X 10
#define FROM_ROMAN_L 50
#define FROM_ROMAN_C 100
#define FROM_ROMAN_D 500
#define FROM_ROMAN_M 1000
#define FROM_ROMAN_P 5000
#define FROM_ROMAN_Q 10000
#define FROM_ROMAN_R 50000
#define FROM_ROMAN_S 100000
#define FROM_ROMAN_T 500000
#define FROM_ROMAN_U 1000000
#define FROM_ROMAN_B 5000000
#define FROM_ROMAN_W 10000000
#define FROM_ROMAN_N 50000000
#define FROM_ROMAN_Y 100000000
#define FROM_ROMAN_Z 500000000
int value (char c)
{
switch (c)
{
case 'I':
return FROM_ROMAN_I;
case 'V':
return FROM_ROMAN_V;
case 'X':
return FROM_ROMAN_X;
case 'L':
return FROM_ROMAN_L;
case 'C':
return FROM_ROMAN_C;
case 'D':
return FROM_ROMAN_D;
case 'M':
return FROM_ROMAN_M;
case 'P':
return FROM_ROMAN_P;
case 'Q':
return FROM_ROMAN_Q;
case 'R':
return FROM_ROMAN_R;
case 'S':
return FROM_ROMAN_S;
case 'T':
return FROM_ROMAN_T;
case 'U':
return FROM_ROMAN_U;
case 'B':
return FROM_ROMAN_B;
case 'W':
return FROM_ROMAN_W;
case 'N':
return FROM_ROMAN_N;
case 'Y':
return FROM_ROMAN_Y;
case 'Z':
return FROM_ROMAN_Z;
default:
return 0;
}
}
int romanToDecimal (const char *roman)
{
int decimal = 0;
for (; *roman; roman++)
{
/* Check for four of a letter in a fow */
if ((*(roman + 1) && *(roman + 2) && *(roman + 3))
&& (*roman == *(roman + 1))
&& (*roman == *(roman + 2))
&& (*roman == *(roman + 3)))
return 0;
/* Check for two five type numbers */
if ( ((*roman == 'V') && (*(roman + 1) == 'V'))
|| ((*roman == 'L') && (*(roman + 1) == 'L'))
|| ((*roman == 'D') && (*(roman + 1) == 'D'))
|| ((*roman == 'P') && (*(roman + 1) == 'P'))
|| ((*roman == 'R') && (*(roman + 1) == 'R'))
|| ((*roman == 'T') && (*(roman + 1) == 'T'))
|| ((*roman == 'B') && (*(roman + 1) == 'B'))
|| ((*roman == 'N') && (*(roman + 1) == 'N'))
|| ((*roman == 'Z') && (*(roman + 1) == 'Z')))
return 0;
/* Check for two lower characters before a larger one */
if ((value(*roman) == value(*(roman + 1))) && (*(roman + 2))
&& (value(*(roman + 1)) < value(*(roman + 2))))
return 0;
/* Check for the same character on either side of a larger one */
if ((*(roman + 1) && *(roman + 2))
&& (value(*roman) == value(*(roman + 2)))
&& (value(*roman) < value(*(roman + 1))))
return 0;
/* Check for illegal nine type numbers */
if (!strncmp(roman, "LXL", 3) || !strncmp(roman, "DCD", 3)
|| !strncmp(roman, "PMP", 3) || !strncmp(roman, "RQR", 3)
|| !strncmp(roman, "TST", 3) || !strncmp(roman, "BUB", 3)
|| !strncmp(roman, "NWN", 3) || !strncmp(roman, "VIV", 3))
return 0;
if (value(*roman) < value(*(roman + 1)))
{
/* check that subtracted value is at least 10% larger,
i.e. 1990 is not MXM, but MCMXC */
if ((10 * value(*roman)) < value(*(roman + 1)))
return 0;
/* check for double subtraction, i.e. IVX */
if (value(*(roman + 1)) <= value(*(roman + 2)))
return 0;
/* check for subtracting by a number starting with a 5
ie. VX, LD LM */
if (*roman == 'V' || *roman == 'L' || *roman == 'D'
|| *roman == 'P' || *roman == 'R' || *roman == 'T'
|| *roman == 'B' || *roman == 'N')
return 0;
decimal += value (*(roman + 1)) - value (*roman);
roman++;
}
else
{
decimal += value (*roman);
}
}
return decimal;
}
QString qstringlist_process (const QString &s, const QString ¶ms, int mode)
{
QStringList sl = s.split (QChar::ParagraphSeparator);
QStringList l;
QString result;
switch (mode)
{
case QSTRL_PROC_FLT_WITH_SORTNOCASECARE:
{
QMap <QString, QString> map;
foreach (QString value, sl)
map.insert (value.toLower(), value);
foreach (QString value, map)
l.append (value);
break;
}
case QSTRL_PROC_FLT_REMOVE_EMPTY:
{
foreach (QString s, sl)
if (! s.isEmpty())
l.append (s);
break;
};
case QSTRL_PROC_FLT_REMOVE_DUPS:
{
foreach (QString s, sl)
if (! l.contains (s))
l.append (s);
break;
};
case QSTRL_PROC_REMOVE_FORMATTING:
{
foreach (QString t, sl)
l.append (t.simplified());
break;
};
case QSTRL_PROC_FLT_WITH_REGEXP:
{
l = sl.filter (QRegExp (params));
break;
}
case QSTRL_PROC_FLT_WITH_SORTCASECARE:
{
l = sl;
l.sort();
break;
}
case QSTRL_PROC_LIST_FLIP:
{
foreach (QString t, sl)
l.prepend (t);
break;
}
case QSTRL_PROC_FLT_LESS:
{
int t = params.toInt();
foreach (QString s, sl)
if (s.size() > t)
l.append (s);
break;
}
case QSTRL_PROC_FLT_GREATER:
{
int t = params.toInt();
foreach (QString s, sl)
if (s.size() < t)
l.append (s);
break;
}
}
result = l.join ("\n");
return result;
}
QString string_reverse (const QString &s)
{
QString sn;
int c = s.length() - 1;
int x = 0;
for (int i = c; i > -1; i--)
sn[x++] = s.at(i);
return sn;
}
QString int_to_binary (int n)
{
QString result;
int sz = sizeof (n) * 8 - 1;
for (int i = sz; i > -1; i--)
{
if (n & (1 << i))
result.append ("1");
else
result.append ("0");
if (i % 4 == 0)
result.append (" ");
}
return result;
}
unsigned int bin_to_decimal (const QString &s)
{
unsigned int table[31];
unsigned int c = 1;
unsigned int result = 0;
QString sn = string_reverse (s);
table[0] = 1;
for (int i = 1; i < 32; i++)
{
c *= 2;
table[i] = c;
}
for (int i = 0; i < sn.size(); i++)
if (sn[i] == '1')
result += table[i];
return result;
}
QString conv_quotes (const QString &source, const QString &c1, const QString &c2)
//QString conv_quotes (const QString &source, bool tex_mode)
{
// QString c1 = "\xAB";
// QString c2 = "\xBB";
QString x;
QString dest;
/*
if (tex_mode)
{
c1 = "``";
c2 = "\'\'";
}
*/
bool flag = true;
int c = source.size() - 1;
for (int i = 0; i <= c; i++)
{
if (source.at(i) == '\"')
{
if (flag)
x = c1;
else
x = c2;
flag = ! flag;
dest += x;
}
else
dest += source[i];
}
return dest;
}
QString strip_html (const QString &source)
{
bool do_copy = true;
QString dest;
for (int i = 0; i < source.length(); i++)
{
if (source[i] == '<')
do_copy = false;
else
if (source[i] == '>')
{
do_copy = true;
if (i < source.length() - 1)
i++;
else
break;
}
if (do_copy)
dest += source[i];
}
return dest;
}
QStringList html_get_by_patt (const QString &s, const QString &spatt)
{
QStringList result;
int c = s.size();
int i = 0;
while (i < c)
{
int start = s.indexOf (spatt, i, Qt::CaseInsensitive);
if (start == -1)
break;
int end = s.indexOf ('"', start + spatt.size());
if (end == -1)
break;
result.prepend (s.mid (start + spatt.size(), (end - start) - spatt.size()));
i = end + 1;
}
return result;
}